Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: messageResponder.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Left Patch Set: Properly convert Subscription objects as notification arguments Created Dec. 17, 2014, 10:15 p.m.
Right Patch Set: Using Services.vc Created Dec. 19, 2014, 5:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « firstRun.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (function(global) 18 (function(global)
19 { 19 {
20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"]; 20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"];
21 function convertSubscription(subscription) 21 function convertSubscription(subscription)
22 { 22 {
23 var result = {}; 23 var result = {};
24 for (var i = 0; i < subscriptionKeys.length; i++) 24 for (var i = 0; i < subscriptionKeys.length; i++)
25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]] 25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]]
26 return result; 26 return result;
27 } 27 }
28 28
29 var changeListeners = null; 29 var changeListeners = null;
30 var messageTypes = {
31 "app": "app.listen",
32 "filter": "filters.listen",
33 "subscription": "subscriptions.listen"
34 };
35
30 function onFilterChange(action) 36 function onFilterChange(action)
31 { 37 {
32 var parts = action.split(".", 2); 38 var parts = action.split(".", 2);
33 var type; 39 var type;
34 if (parts.length == 1) 40 if (parts.length == 1)
35 { 41 {
36 type = "app"; 42 type = "app";
37 action = parts[0]; 43 action = parts[0];
38 } 44 }
39 else 45 else
40 { 46 {
41 type = parts[0]; 47 type = parts[0];
42 action = parts[1]; 48 action = parts[1];
43 } 49 }
44 50
51 if (!messageTypes.hasOwnProperty(type))
52 return;
53
45 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 54 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
46 { 55 {
47 if (arg instanceof Subscription) 56 if (arg instanceof Subscription)
48 return convertSubscription(arg); 57 return convertSubscription(arg);
49 else 58 else
50 return arg; 59 return arg;
51 }); 60 });
52 61
53 for (var i = 0; i < changeListeners.length; i++) 62 var pages = changeListeners.keys();
63 for (var i = 0; i < pages.length; i++)
54 { 64 {
55 if (changeListeners[i].type == type && changeListeners[i].filter.indexOf(a ction) >= 0) 65 var filters = changeListeners.get(pages[i]);
66 if (filters[type] && filters[type].indexOf(action) >= 0)
56 { 67 {
57 changeListeners[i].page.sendMessage({ 68 pages[i].sendMessage({
58 type: changeListeners[i].messageType, 69 type: messageTypes[type],
59 action: action, 70 action: action,
60 args: args 71 args: args
61 }); 72 });
62 } 73 }
63 } 74 }
64 }; 75 };
65 76
66 ext.onMessage.addListener(function(message, sender, callback) 77 ext.onMessage.addListener(function(message, sender, callback)
67 { 78 {
68 switch (message.type) 79 switch (message.type)
69 { 80 {
70 case "app.doclink": 81 case "app.get":
71 callback(Utils.getDocLink(message.args[0])); 82 if (message.what == "issues")
83 {
84 var info = require("info");
85 callback({
86 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false,
87 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false,
88 legacySafariVersion: (info.platform == "safari" && (
89 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef oreload breaks websites in Safari 5
90 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext ensions are broken in 6.1 and 7.0
91 Services.vc.compare(info.platformVersion, "7.0") == 0))
92 });
93 }
94 else if (message.what == "doclink")
95 callback(Utils.getDocLink(message.link));
96 else
97 callback(null);
72 break; 98 break;
73 case "app.info": 99 case "app.open":
74 callback(require("info")); 100 if (message.what == "options")
75 break; 101 {
76 case "app.issues": 102 if (typeof UI != "undefined")
77 callback({ 103 UI.openFiltersDialog();
78 seenDataCorruption: "seenDataCorruption" in global ? global.seenDataCo rruption : false, 104 else
79 filterlistsReinitialized: "filterlistsReinitialized" in global ? globa l.filterlistsReinitialized : false 105 global.openOptions();
Sebastian Noack 2014/12/22 09:56:17 window.openOptions() is deprecated. Please use ext
Wladimir Palant 2015/01/15 13:03:22 I filed #1813 and #1814 on that.
80 }); 106 }
81 break;
82 case "app.options":
83 if (typeof UI != "undefined")
84 UI.openFiltersDialog();
85 else
86 global.openOptions();
87 break; 107 break;
88 case "subscriptions.get": 108 case "subscriptions.get":
89 callback(FilterStorage.subscriptions.map(convertSubscription)); 109 var subscriptions = FilterStorage.subscriptions.filter(function(s)
110 {
111 if (message.ignoreDisabled && s.disabled)
112 return false;
113 if (s instanceof DownloadableSubscription && message.downloadable)
114 return true;
115 if (s instanceof SpecialSubscription && message.special)
116 return true;
117 return false;
118 });
119 callback(subscriptions.map(convertSubscription));
90 break; 120 break;
91 case "filters.blocked": 121 case "filters.blocked":
92 var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty); 122 var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty);
93 callback(filter instanceof BlockingFilter); 123 callback(filter instanceof BlockingFilter);
94 break; 124 break;
95 case "subscriptions.toggle": 125 case "subscriptions.toggle":
96 var subscription = Subscription.fromURL(message.url); 126 var subscription = Subscription.fromURL(message.url);
97 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled) 127 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled)
98 FilterStorage.removeSubscription(subscription); 128 FilterStorage.removeSubscription(subscription);
99 else 129 else
100 { 130 {
101 subscription.disabled = false; 131 subscription.disabled = false;
102 subscription.title = message.title; 132 subscription.title = message.title;
103 subscription.homepage = message.homepage; 133 subscription.homepage = message.homepage;
104 FilterStorage.addSubscription(subscription); 134 FilterStorage.addSubscription(subscription);
105 if (!subscription.lastDownload) 135 if (!subscription.lastDownload)
106 Synchronizer.execute(subscription); 136 Synchronizer.execute(subscription);
107 } 137 }
108 break; 138 break;
109 case "subscriptions.listen": 139 case "subscriptions.listen":
110 if (!changeListeners) 140 if (!changeListeners)
111 { 141 {
112 changeListeners = []; 142 changeListeners = new global.ext.PageMap();
113 FilterNotifier.addListener(onFilterChange); 143 FilterNotifier.addListener(onFilterChange);
114 } 144 }
115 changeListeners.push({
116 type: "subscription",
117 filter: message.filter,
118 messageType: message.type,
119 page: sender.page
120 });
121 break;
122 case "removePage":
123 if (!changeListeners)
124 return;
125 145
126 for (var i = 0; i < changeListeners.length; i++) 146 var filters = changeListeners.get(sender.page);
127 if (changeListeners[i].page.equals(sender.page)) 147 if (!filters)
128 changeListeners.splice(i--, 1); 148 {
149 filters = Object.create(null);
150 changeListeners.set(sender.page, filters);
151 }
129 152
130 if (!changeListeners.length) 153 if (message.filter)
131 { 154 filters.subscription = message.filter;
132 changeListeners = null; 155 else
133 FilterNotifier.removeListener(onFilterChange); 156 delete filters.subscription;
134 }
135 break; 157 break;
136 } 158 }
137 }); 159 });
138 })(this); 160 })(this);
LEFTRIGHT
« firstRun.js ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld