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

Side by Side Diff: lib/popupBlocker.js

Issue 29336278: Issue 3651 - Fix popup blocking with dynamically created openers and invisible redirects (Closed)
Patch Set: Created Feb. 11, 2016, 5:22 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 | 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-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 /** @module popupBlocker */ 18 /** @module popupBlocker */
19 19
20 "use strict"; 20 "use strict";
21 21
22 let {defaultMatcher} = require("matcher"); 22 let {defaultMatcher} = require("matcher");
23 let {BlockingFilter} = require("filterClasses"); 23 let {BlockingFilter} = require("filterClasses");
24 let {stringifyURL, isThirdParty, extractHostFromFrame} = require("url"); 24 let {stringifyURL, isThirdParty, extractHostFromFrame} = require("url");
25 let {checkWhitelisted} = require("whitelisting"); 25 let {checkWhitelisted} = require("whitelisting");
26 let {logRequest} = require("devtools"); 26 let {logRequest} = require("devtools");
27 27
28 let tabsLoading = {}; 28 let loadingPopups = Object.create(null);
29 29
30 function checkPotentialPopup(tabId, url, sourcePage, documentHost, specificOnly) 30 function hasLoadingPopups()
31 { 31 {
32 let urlObj = new URL(url || "about:blank"); 32 return Object.keys(loadingPopups).length > 0;
Wladimir Palant 2016/02/12 15:43:43 With this being called from onCreatedNavigationTar
Sebastian Noack 2016/02/12 15:52:13 V8 is optimized for usage of Object.keys() like he
33 }
34
35 function forgetPopup(tabId)
36 {
37 delete loadingPopups[tabId];
38
39 if (!hasLoadingPopups())
40 {
41 chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequest);
42 chrome.webNavigation.onCompleted.removeListener(onCompleted);
43 chrome.tabs.onRemoved.removeListener(forgetPopup);
44 }
45 }
46
47 function getFrame(tabId, processId, frameId, callback)
48 {
49 chrome.webNavigation.getFrame(
50 {
51 tabId: tabId,
52 processId: processId,
53 frameId: frameId
54 },
55 details =>
56 {
57 let {url, parentFrameId} = details;
58 let frame = {url: new URL(url), parent: null};
59
60 if (parentFrameId != -1)
61 {
62 getFrame(
63 tabId, processId, parentFrameId,
64 parentFrame =>
65 {
66 frame.parent = parentFrame;
67 callback(frame);
68 }
69 );
70 }
71 else
72 {
73 callback(frame);
74 }
75 }
76 );
77 }
78
79 function checkPotentialPopup(tabId, popup)
80 {
81 let urlObj = new URL(popup.url || "about:blank");
33 let urlString = stringifyURL(urlObj); 82 let urlString = stringifyURL(urlObj);
83 let documentHost = extractHostFromFrame(popup.sourceFrame);
34 let thirdParty = isThirdParty(urlObj, documentHost); 84 let thirdParty = isThirdParty(urlObj, documentHost);
35 85
86 let specificOnly = !!checkWhitelisted(
87 popup.sourcePage, popup.sourceFrame,
88 RegExpFilter.typeMap.GENERICBLOCK
89 );
90
36 let filter = defaultMatcher.matchesAny( 91 let filter = defaultMatcher.matchesAny(
37 urlString, RegExpFilter.typeMap.POPUP, 92 urlString, RegExpFilter.typeMap.POPUP,
38 documentHost, thirdParty, null, specificOnly 93 documentHost, thirdParty, null, specificOnly
39 ); 94 );
40 95
41 if (filter instanceof BlockingFilter) 96 if (filter instanceof BlockingFilter)
42 chrome.tabs.remove(tabId); 97 chrome.tabs.remove(tabId);
43 98
44 logRequest( 99 logRequest(
45 sourcePage, urlString, "POPUP", documentHost, 100 popup.sourcePage, urlString, "POPUP",
46 thirdParty, null, specificOnly, filter 101 documentHost, thirdParty, null,
102 specificOnly, filter
47 ); 103 );
48 } 104 }
49 105
106 function onBeforeRequest(details)
107 {
108 let popup = loadingPopups[details.tabId];
109 if (popup)
110 {
111 popup.url = details.url;
112 if (popup.sourceFrame)
113 checkPotentialPopup(details.tabId, popup);
114 }
115 }
116
117 function onCompleted(details)
118 {
119 if (details.frameId == 0 && details.url != "about:blank")
120 forgetPopup(details.tabId);
Wladimir Palant 2016/02/12 15:43:43 Will this cause a memory leak if a website opens a
Sebastian Noack 2016/02/12 15:52:13 Yes, just as with the previous implementation. But
121 }
122
123 function onSourceFrameRetrieved(tabId, frame)
124 {
125 let popup = loadingPopups[tabId];
126 if (popup)
127 {
128 if (checkWhitelisted(popup.sourcePage, frame))
129 {
130 forgetPopup(tabId);
131 }
132 else
133 {
134 popup.sourceFrame = frame;
135 checkPotentialPopup(tabId, popup);
136 }
137 }
138 }
139
50 chrome.webNavigation.onCreatedNavigationTarget.addListener(details => 140 chrome.webNavigation.onCreatedNavigationTarget.addListener(details =>
51 { 141 {
52 let sourcePage = new ext.Page({id: details.sourceTabId}); 142 if (!hasLoadingPopups())
53 let sourceFrame = ext.getFrame(details.sourceTabId, details.sourceFrameId); 143 {
144 chrome.webRequest.onBeforeRequest.addListener(
145 onBeforeRequest,
146 {
147 urls: ["<all_urls>"],
148 types: ["main_frame"]
149 }
150 );
54 151
55 if (checkWhitelisted(sourcePage, sourceFrame)) 152 chrome.webNavigation.onCompleted.addListener(onCompleted);
56 return; 153 chrome.tabs.onRemoved.addListener(forgetPopup);
57
58 let documentHost = extractHostFromFrame(sourceFrame);
59 if (!documentHost)
60 return;
61
62 let specificOnly = !!checkWhitelisted(sourcePage, sourceFrame,
63 RegExpFilter.typeMap.GENERICBLOCK);
64
65 tabsLoading[details.tabId] = {
66 page: sourcePage,
67 documentHost: documentHost,
68 specificOnly: specificOnly
69 };
70 checkPotentialPopup(details.tabId, details.url, sourcePage,
71 documentHost, specificOnly);
72 });
73
74 chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) =>
75 {
76 if (!(tabId in tabsLoading))
77 return;
78
79 if ("url" in changeInfo)
80 {
81 let source = tabsLoading[tabId];
82 checkPotentialPopup(tabId, tab.url, source.page,
83 source.documentHost,
84 source.specificOnly);
85 } 154 }
86 155
87 if ("status" in changeInfo && changeInfo.status == "complete" && tab.url != "a bout:blank") 156 loadingPopups[details.tabId] = {
88 delete tabsLoading[tabId]; 157 url: details.url,
158 sourcePage: new ext.Page({id: details.sourceTabId}),
159 sourceFrame: null
160 };
161
162 getFrame(
163 details.sourceTabId,
164 details.sourceProcessId,
165 details.sourceFrameId,
166 onSourceFrameRetrieved.bind(null, details.tabId)
167 );
89 }); 168 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld