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

Side by Side Diff: background.js

Issue 29338491: Issue 3823 - Split up message responder code (Closed)
Patch Set: Use require() inline Created March 21, 2016, 7:53 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 | « no previous file | chrome/devtools.js » ('j') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 with(require("filterClasses")) 18 with(require("filterClasses"))
19 { 19 {
20 this.BlockingFilter = BlockingFilter; 20 this.BlockingFilter = BlockingFilter;
21 this.WhitelistFilter = WhitelistFilter; 21 this.WhitelistFilter = WhitelistFilter;
22 this.RegExpFilter = RegExpFilter; 22 this.RegExpFilter = RegExpFilter;
23 } 23 }
24 with(require("whitelisting")) 24 with(require("whitelisting"))
25 { 25 {
26 this.checkWhitelisted = checkWhitelisted; 26 this.checkWhitelisted = checkWhitelisted;
27 this.processKey = processKey;
28 this.getKey = getKey; 27 this.getKey = getKey;
29 } 28 }
30 with(require("url")) 29 with(require("url"))
31 { 30 {
32 this.stringifyURL = stringifyURL; 31 this.stringifyURL = stringifyURL;
33 this.isThirdParty = isThirdParty; 32 this.isThirdParty = isThirdParty;
34 this.extractHostFromFrame = extractHostFromFrame; 33 this.extractHostFromFrame = extractHostFromFrame;
35 } 34 }
36 var FilterStorage = require("filterStorage").FilterStorage; 35 var FilterStorage = require("filterStorage").FilterStorage;
37 var FilterNotifier = require("filterNotifier").FilterNotifier; 36 var FilterNotifier = require("filterNotifier").FilterNotifier;
38 var SpecialSubscription = require("subscriptionClasses").SpecialSubscription; 37 var SpecialSubscription = require("subscriptionClasses").SpecialSubscription;
39 var ElemHide = require("elemHide").ElemHide; 38 var ElemHide = require("elemHide").ElemHide;
40 var defaultMatcher = require("matcher").defaultMatcher; 39 var defaultMatcher = require("matcher").defaultMatcher;
41 var Prefs = require("prefs").Prefs; 40 var Prefs = require("prefs").Prefs;
42 var parseFilters = require("filterValidation").parseFilters;
43 var composeFilters = require("filterComposer").composeFilters;
44 var updateIcon = require("icon").updateIcon; 41 var updateIcon = require("icon").updateIcon;
45 var showNextNotificationForUrl = require("notificationHelper").showNextNotificat ionForUrl; 42 var showNextNotificationForUrl = require("notificationHelper").showNextNotificat ionForUrl;
43 var port = require("messaging").port;
46 var devtools = require("devtools"); 44 var devtools = require("devtools");
47 45
48 // Special-case domains for which we cannot use style-based hiding rules. 46 // Special-case domains for which we cannot use style-based hiding rules.
49 // See http://crbug.com/68705. 47 // See http://crbug.com/68705.
50 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"]; 48 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"];
51 49
52 var htmlPages = new ext.PageMap(); 50 var htmlPages = new ext.PageMap();
53 51
54 var contextMenuItem = { 52 var contextMenuItem = {
55 title: ext.i18n.getMessage("block_element"), 53 title: ext.i18n.getMessage("block_element"),
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 if (filter instanceof WhitelistFilter && /^@@\|\|([^\/:]+)\^\$document$/. test(filter.text)) 111 if (filter instanceof WhitelistFilter && /^@@\|\|([^\/:]+)\^\$document$/. test(filter.text))
114 exceptions.push(RegExp.$1); 112 exceptions.push(RegExp.$1);
115 else 113 else
116 filters.push(filter.text); 114 filters.push(filter.text);
117 } 115 }
118 } 116 }
119 117
120 return {filters: filters, exceptions: exceptions}; 118 return {filters: filters, exceptions: exceptions};
121 } 119 }
122 120
123 ext.onMessage.addListener(function (msg, sender, sendResponse) 121 port.on("get-selectors", function(msg, sender)
124 { 122 {
125 switch (msg.type) 123 var selectors = [];
124 var trace = devtools && devtools.hasPanel(sender.page);
125
126 if (!checkWhitelisted(sender.page, sender.frame,
127 RegExpFilter.typeMap.DOCUMENT |
128 RegExpFilter.typeMap.ELEMHIDE))
126 { 129 {
127 case "composer.openDialog": 130 var noStyleRules = false;
128 ext.windows.create({ 131 var specificOnly = checkWhitelisted(sender.page, sender.frame,
129 url: ext.getURL("block.html"), 132 RegExpFilter.typeMap.GENERICHIDE);
130 left: 50, 133 var host = extractHostFromFrame(sender.frame);
131 top: 50,
132 width: 420,
133 height: 200,
134 focused: true,
135 type: "popup"
136 },
137 function (popupPage) {
138 var popupPageId = popupPage.id;
139 function onRemoved(removedPageId)
140 {
141 if (popupPageId == removedPageId)
142 {
143 sender.page.sendMessage({
144 type: "composer.content.dialogClosed",
145 popupId: popupPageId
146 });
147 ext.pages.onRemoved.removeListener(onRemoved);
148 }
149 }
150 ext.pages.onRemoved.addListener(onRemoved);
151 134
152 sendResponse(popupPageId); 135 for (var i = 0; i < noStyleRulesHosts.length; i++)
136 {
137 var noStyleHost = noStyleRulesHosts[i];
138 if (host == noStyleHost || (host.length > noStyleHost.length &&
139 host.substr(host.length - noStyleHost.length - 1) == "." + noStyleHost))
140 {
141 noStyleRules = true;
142 }
143 }
144 selectors = ElemHide.getSelectorsForDomain(host, specificOnly);
145 if (noStyleRules)
146 {
147 selectors = selectors.filter(function(s)
148 {
149 return !/\[style[\^\$]?=/.test(s);
153 }); 150 });
154 return true; 151 }
155 break; 152 }
156 case "get-selectors":
157 var selectors = [];
158 var trace = devtools && devtools.hasPanel(sender.page);
159 153
160 if (!checkWhitelisted(sender.page, sender.frame, 154 return {selectors: selectors, trace: trace};
161 RegExpFilter.typeMap.DOCUMENT | 155 });
162 RegExpFilter.typeMap.ELEMHIDE))
163 {
164 var noStyleRules = false;
165 var specificOnly = checkWhitelisted(sender.page, sender.frame,
166 RegExpFilter.typeMap.GENERICHIDE);
167 var host = extractHostFromFrame(sender.frame);
168 156
169 for (var i = 0; i < noStyleRulesHosts.length; i++) 157 port.on("should-collapse", function(msg, sender)
170 { 158 {
171 var noStyleHost = noStyleRulesHosts[i]; 159 if (checkWhitelisted(sender.page, sender.frame))
172 if (host == noStyleHost || (host.length > noStyleHost.length && 160 return false;
173 host.substr(host.length - noStyleHost.leng th - 1) == "." + noStyleHost))
174 {
175 noStyleRules = true;
176 }
177 }
178 selectors = ElemHide.getSelectorsForDomain(host, specificOnly);
179 if (noStyleRules)
180 {
181 selectors = selectors.filter(function(s)
182 {
183 return !/\[style[\^\$]?=/.test(s);
184 });
185 }
186 }
187 161
188 sendResponse({selectors: selectors, trace: trace}); 162 var typeMask = RegExpFilter.typeMap[msg.mediatype];
189 break; 163 var documentHost = extractHostFromFrame(sender.frame);
190 case "should-collapse": 164 var sitekey = getKey(sender.page, sender.frame);
191 if (checkWhitelisted(sender.page, sender.frame)) 165 var blocked = false;
192 {
193 sendResponse(false);
194 break;
195 }
196 166
197 var typeMask = RegExpFilter.typeMap[msg.mediatype]; 167 var specificOnly = checkWhitelisted(
198 var documentHost = extractHostFromFrame(sender.frame); 168 sender.page, sender.frame,
199 var sitekey = getKey(sender.page, sender.frame); 169 RegExpFilter.typeMap.GENERICBLOCK
200 var blocked = false; 170 );
201 171
202 var specificOnly = checkWhitelisted( 172 for (var i = 0; i < msg.urls.length; i++)
203 sender.page, sender.frame, 173 {
204 RegExpFilter.typeMap.GENERICBLOCK 174 var url = new URL(msg.urls[i], msg.baseURL);
205 ); 175 var filter = defaultMatcher.matchesAny(
176 stringifyURL(url), typeMask,
177 documentHost, isThirdParty(url, documentHost),
178 sitekey, specificOnly
179 );
206 180
207 for (var i = 0; i < msg.urls.length; i++) 181 if (filter instanceof BlockingFilter)
208 { 182 {
209 var url = new URL(msg.urls[i], msg.baseURL); 183 if (filter.collapse != null)
210 var filter = defaultMatcher.matchesAny( 184 return filter.collapse;
211 stringifyURL(url), typeMask,
212 documentHost, isThirdParty(url, documentHost),
213 sitekey, specificOnly
214 );
215 185
216 if (filter instanceof BlockingFilter) 186 blocked = true;
217 { 187 }
218 if (filter.collapse != null) 188 }
219 {
220 sendResponse(filter.collapse);
221 return;
222 }
223 189
224 blocked = true; 190 return blocked && Prefs.hidePlaceholders;
225 } 191 });
226 }
227 192
228 sendResponse(blocked && Prefs.hidePlaceholders); 193 port.on("get-domain-enabled-state", function(msg, sender)
229 break; 194 {
230 case "get-domain-enabled-state": 195 return {enabled: !checkWhitelisted(sender.page)};
231 // Returns whether this domain is in the exclusion list. 196 });
232 // The browser action popup asks us this.
233 if(sender.page)
234 {
235 sendResponse({enabled: !checkWhitelisted(sender.page)});
236 return;
237 }
238 break;
239 case "add-sitekey":
240 processKey(msg.token, sender.page, sender.frame);
241 break;
242 case "composer.ready":
243 htmlPages.set(sender.page, null);
244 refreshIconAndContextMenu(sender.page);
245 break;
246 case "composer.getFilters":
247 sendResponse(composeFilters({
248 tagName: msg.tagName,
249 id: msg.id,
250 src: msg.src,
251 style: msg.style,
252 classes: msg.classes,
253 urls: msg.urls,
254 type: msg.mediatype,
255 baseURL: msg.baseURL,
256 page: sender.page,
257 frame: sender.frame
258 }));
259 break;
260 case "trace-elemhide":
261 devtools.logHiddenElements(
262 sender.page, msg.selectors,
263 extractHostFromFrame(sender.frame)
264 );
265 break;
266 case "has-devtools-panel":
267 sendResponse(Prefs.show_devtools_panel);
268 break;
269 case "forward":
270 var targetPage;
271 if (msg.targetPageId)
272 targetPage = ext.getPage(msg.targetPageId);
273 else
274 targetPage = sender.page;
275 197
276 if (targetPage) 198 port.on("forward", function(msg, sender)
277 { 199 {
278 msg.payload.sender = sender.page.id; 200 var targetPage;
279 if (msg.expectsResponse) 201 if (msg.targetPageId)
280 { 202 targetPage = ext.getPage(msg.targetPageId);
281 targetPage.sendMessage(msg.payload, sendResponse); 203 else
282 return true; 204 targetPage = sender.page;
283 }
284 205
285 targetPage.sendMessage(msg.payload); 206 if (targetPage)
286 } 207 {
287 break; 208 msg.payload.sender = sender.page.id;
209 if (msg.expectsResponse)
210 return new Promise(targetPage.sendMessage.bind(targetPage, msg.payload));
211 targetPage.sendMessage(msg.payload);
288 } 212 }
289 }); 213 });
290 214
291 // update icon when page changes location 215 // update icon when page changes location
292 ext.pages.onLoading.addListener(function(page) 216 ext.pages.onLoading.addListener(function(page)
293 { 217 {
294 page.sendMessage({type: "composer.content.finished"}); 218 page.sendMessage({type: "composer.content.finished"});
295 refreshIconAndContextMenu(page); 219 refreshIconAndContextMenu(page);
296 showNextNotificationForUrl(page.url); 220 showNextNotificationForUrl(page.url);
297 }); 221 });
OLDNEW
« no previous file with comments | « no previous file | chrome/devtools.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld