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

Side by Side Diff: messageResponder.js

Issue 4864767881641984: Issue 1528 - Implemented backend for general tab of new options page (Closed)
Patch Set: Fixed filter management in background.js Created Jan. 27, 2015, 12:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 if (!global.ext) 20 if (!global.ext)
21 global.ext = require("ext_background"); 21 global.ext = require("ext_background");
22 22
23 var Prefs = require("prefs").Prefs;
23 var Utils = require("utils").Utils; 24 var Utils = require("utils").Utils;
24 var FilterStorage = require("filterStorage").FilterStorage; 25 var FilterStorage = require("filterStorage").FilterStorage;
25 var FilterNotifier = require("filterNotifier").FilterNotifier; 26 var FilterNotifier = require("filterNotifier").FilterNotifier;
26 var defaultMatcher = require("matcher").defaultMatcher; 27 var defaultMatcher = require("matcher").defaultMatcher;
27 var BlockingFilter = require("filterClasses").BlockingFilter; 28
29 var filterClasses = require("filterClasses");
30 var Filter = filterClasses.Filter;
31 var BlockingFilter = filterClasses.BlockingFilter;
28 var Synchronizer = require("synchronizer").Synchronizer; 32 var Synchronizer = require("synchronizer").Synchronizer;
29 33
30 var subscriptionClasses = require("subscriptionClasses"); 34 var subscriptionClasses = require("subscriptionClasses");
31 var Subscription = subscriptionClasses.Subscription; 35 var Subscription = subscriptionClasses.Subscription;
32 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; 36 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
33 var SpecialSubscription = subscriptionClasses.SpecialSubscription; 37 var SpecialSubscription = subscriptionClasses.SpecialSubscription;
34 38
35 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"]; 39 function convertObject(keys, obj)
36 function convertSubscription(subscription)
37 { 40 {
38 var result = {}; 41 var result = {};
39 for (var i = 0; i < subscriptionKeys.length; i++) 42 for (var i = 0; i < keys.length; i++)
40 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]] 43 result[keys[i]] = obj[keys[i]];
41 return result; 44 return result;
42 } 45 }
43 46
47 var convertSubscription = convertObject.bind(null, ["disabled", "downloadStatu s",
48 "homepage", "lastSuccess", "title", "url"]);
49 var convertFilter = convertObject.bind(null, ["text"]);
50
44 var changeListeners = null; 51 var changeListeners = null;
45 var messageTypes = { 52 var messageTypes = {
46 "app": "app.listen", 53 "app": "app.listen",
47 "filter": "filters.listen", 54 "filter": "filters.listen",
48 "subscription": "subscriptions.listen" 55 "subscription": "subscriptions.listen"
49 }; 56 };
50 57
51 function onFilterChange(action) 58 function onFilterChange(action)
52 { 59 {
60 if (action == "load")
61 action = "filter.loaded";
62
53 var parts = action.split(".", 2); 63 var parts = action.split(".", 2);
54 var type; 64 var type;
55 if (parts.length == 1) 65 if (parts.length == 1)
56 { 66 {
57 type = "app"; 67 type = "app";
58 action = parts[0]; 68 action = parts[0];
59 } 69 }
60 else 70 else
61 { 71 {
62 type = parts[0]; 72 type = parts[0];
63 action = parts[1]; 73 action = parts[1];
64 } 74 }
65 75
66 if (!messageTypes.hasOwnProperty(type)) 76 if (!messageTypes.hasOwnProperty(type))
67 return; 77 return;
68 78
69 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 79 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
70 { 80 {
71 if (arg instanceof Subscription) 81 if (arg instanceof Subscription)
72 return convertSubscription(arg); 82 return convertSubscription(arg);
83 else if (arg instanceof Filter)
84 return convertFilter(arg);
73 else 85 else
74 return arg; 86 return arg;
75 }); 87 });
76 88
77 var pages = changeListeners.keys(); 89 var pages = changeListeners.keys();
78 for (var i = 0; i < pages.length; i++) 90 for (var i = 0; i < pages.length; i++)
79 { 91 {
80 var filters = changeListeners.get(pages[i]); 92 var filters = changeListeners.get(pages[i]);
81 if (filters[type] && filters[type].indexOf(action) >= 0) 93 if (filters[type] && filters[type].indexOf(action) >= 0)
82 { 94 {
83 pages[i].sendMessage({ 95 pages[i].sendMessage({
84 type: messageTypes[type], 96 type: messageTypes[type],
85 action: action, 97 action: action,
86 args: args 98 args: args
87 }); 99 });
88 } 100 }
89 } 101 }
90 }; 102 };
91 103
92 global.ext.onMessage.addListener(function(message, sender, callback) 104 global.ext.onMessage.addListener(function(message, sender, callback)
93 { 105 {
106 var listenerFilters = null;
94 switch (message.type) 107 switch (message.type)
95 { 108 {
109 case "app.listen":
110 case "filters.listen":
111 case "subscriptions.listen":
112 if (!changeListeners)
113 {
114 changeListeners = new global.ext.PageMap();
115 FilterNotifier.addListener(onFilterChange);
116 }
117
118 listenerFilters = changeListeners.get(sender.page);
119 if (!listenerFilters)
120 {
121 listenerFilters = Object.create(null);
122 changeListeners.set(sender.page, listenerFilters);
123 }
124 break;
125 }
126
127 switch (message.type)
128 {
129 case "add-subscription":
130 var subscription = Subscription.fromURL(message.url);
131 subscription.title = message.title;
132 onFilterChange("addSubscription", subscription);
133 break;
96 case "app.get": 134 case "app.get":
97 if (message.what == "issues") 135 if (message.what == "issues")
98 { 136 {
99 var info = require("info"); 137 var info = require("info");
100 callback({ 138 callback({
101 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false, 139 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false,
102 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false, 140 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false,
103 legacySafariVersion: (info.platform == "safari" && ( 141 legacySafariVersion: (info.platform == "safari" && (
104 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef oreload breaks websites in Safari 5 142 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef oreload breaks websites in Safari 5
105 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext ensions are broken in 6.1 and 7.0 143 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext ensions are broken in 6.1 and 7.0
106 Services.vc.compare(info.platformVersion, "7.0") == 0)) 144 Services.vc.compare(info.platformVersion, "7.0") == 0))
107 }); 145 });
108 } 146 }
109 else if (message.what == "doclink") 147 else if (message.what == "doclink")
110 callback(Utils.getDocLink(message.link)); 148 callback(Utils.getDocLink(message.link));
111 else if (message.what == "localeInfo") 149 else if (message.what == "localeInfo")
112 { 150 {
113 callback({ 151 callback({
114 locale: Utils.appLocale, 152 locale: Utils.appLocale,
115 isRTL: Utils.chromeRegistry.isLocaleRTL("adblockplus") 153 isRTL: Utils.chromeRegistry.isLocaleRTL("adblockplus")
116 }); 154 });
117 } 155 }
118 else 156 else
119 callback(null); 157 callback(null);
120 break; 158 break;
159 case "app.listen":
160 if (message.filter)
161 listenerFilters.app = message.filter;
162 else
163 delete listenerFilters.app;
164 break;
121 case "app.open": 165 case "app.open":
122 if (message.what == "options") 166 if (message.what == "options")
123 ext.showOptions(); 167 ext.showOptions();
124 break; 168 break;
169 case "filters.add":
170 var filter = Filter.fromText(message.text);
171 FilterStorage.addFilter(filter);
172 break;
173 case "filters.blocked":
174 var filter = defaultMatcher.matchesAny(message.url, message.requestType,
175 message.docDomain, message.thirdParty);
176 callback(filter instanceof BlockingFilter);
177 break;
178 case "filters.get":
179 var subscription = Subscription.fromURL(message.subscriptionUrl);
180 if (!subscription)
181 {
182 callback([]);
183 break;
184 }
185
186 callback(subscription.filters.map(convertFilter));
187 break;
188 case "filters.listen":
189 if (message.filter)
190 listenerFilters.filter = message.filter;
191 else
192 delete listenerFilters.filter;
193 break;
194 case "filters.remove":
195 var filter = Filter.fromText(message.text);
196 var subscription = null;
197 if (message.subscriptionUrl)
198 subscription = Subscription.fromURL(message.subscriptionUrl);
199
200 if (!subscription)
201 FilterStorage.removeFilter(filter);
202 else
203 FilterStorage.removeFilter(filter, subscription, message.index);
204 break;
205 case "prefs.get":
206 callback(Prefs[message.key]);
207 break;
208 case "subscriptions.add":
209 if (message.url in FilterStorage.knownSubscriptions)
210 return;
211
212 var subscription = Subscription.fromURL(message.url);
213 if (!subscription)
214 return;
215
216 subscription.disabled = false;
217 if ("title" in message)
218 subscription.title = message.title;
219 if ("homepage" in message)
220 subscription.homepage = message.homepage;
221 FilterStorage.addSubscription(subscription);
222
223 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload)
224 Synchronizer.execute(subscription);
225 break;
125 case "subscriptions.get": 226 case "subscriptions.get":
126 var subscriptions = FilterStorage.subscriptions.filter(function(s) 227 var subscriptions = FilterStorage.subscriptions.filter(function(s)
127 { 228 {
128 if (message.ignoreDisabled && s.disabled) 229 if (message.ignoreDisabled && s.disabled)
129 return false; 230 return false;
130 if (s instanceof DownloadableSubscription && message.downloadable) 231 if (s instanceof DownloadableSubscription && message.downloadable)
131 return true; 232 return true;
132 if (s instanceof SpecialSubscription && message.special) 233 if (s instanceof SpecialSubscription && message.special)
133 return true; 234 return true;
134 return false; 235 return false;
135 }); 236 });
136 callback(subscriptions.map(convertSubscription)); 237 callback(subscriptions.map(convertSubscription));
137 break; 238 break;
138 case "filters.blocked": 239 case "subscriptions.listen":
139 var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty); 240 if (message.filter)
140 callback(filter instanceof BlockingFilter); 241 listenerFilters.subscription = message.filter;
242 else
243 delete listenerFilters.subscription;
244 break;
245 case "subscriptions.remove":
246 var subscription = Subscription.fromURL(message.url);
247 if (subscription.url in FilterStorage.knownSubscriptions)
248 FilterStorage.removeSubscription(subscription);
141 break; 249 break;
142 case "subscriptions.toggle": 250 case "subscriptions.toggle":
143 var subscription = Subscription.fromURL(message.url); 251 var subscription = Subscription.fromURL(message.url);
144 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled) 252 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled)
145 FilterStorage.removeSubscription(subscription); 253 FilterStorage.removeSubscription(subscription);
146 else 254 else
147 { 255 {
148 subscription.disabled = false; 256 subscription.disabled = false;
149 subscription.title = message.title; 257 subscription.title = message.title;
150 subscription.homepage = message.homepage; 258 subscription.homepage = message.homepage;
151 FilterStorage.addSubscription(subscription); 259 FilterStorage.addSubscription(subscription);
152 if (!subscription.lastDownload) 260 if (!subscription.lastDownload)
153 Synchronizer.execute(subscription); 261 Synchronizer.execute(subscription);
154 } 262 }
155 break; 263 break;
156 case "subscriptions.listen":
157 if (!changeListeners)
158 {
159 changeListeners = new global.ext.PageMap();
160 FilterNotifier.addListener(onFilterChange);
161 }
162
163 var filters = changeListeners.get(sender.page);
164 if (!filters)
165 {
166 filters = Object.create(null);
167 changeListeners.set(sender.page, filters);
168 }
169
170 if (message.filter)
171 filters.subscription = message.filter;
172 else
173 delete filters.subscription;
174 break;
175 } 264 }
176 }); 265 });
177 })(this); 266 })(this);
OLDNEW
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld