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

Delta Between Two Patch Sets: lib/cssInjection.js

Issue 29705719: Issue 6402 - Split filter hit / request logging out into own API (Closed)
Left Patch Set: Nightmarish rebase Created April 13, 2018, 3:20 p.m.
Right Patch Set: Addressed Manish's feedback Created May 9, 2018, 5:53 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 | « lib/csp.js ('k') | lib/devtools.js » ('j') | 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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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 cssInjection */ 18 /** @module cssInjection */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses");
23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide");
24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
25 const {checkWhitelisted} = require("./whitelisting"); 25 const {checkWhitelisted} = require("./whitelisting");
26 const {extractHostFromFrame} = require("./url"); 26 const {extractHostFromFrame} = require("./url");
27 const {port} = require("./messaging"); 27 const {port} = require("./messaging");
28 const {hasListener} = require("./hitLogger"); 28 const {HitLogger} = require("./hitLogger");
29 const info = require("../buildtools/info"); 29 const info = require("../buildtools/info");
30 30
31 // Chromium's support for tabs.removeCSS is still a work in progress and the 31 // Chromium's support for tabs.removeCSS is still a work in progress and the
32 // API is likely to be different from Firefox's; for now we just don't use it 32 // API is likely to be different from Firefox's; for now we just don't use it
33 // at all, even if it's available. 33 // at all, even if it's available.
34 // See https://crbug.com/608854 34 // See https://crbug.com/608854
35 const styleSheetRemovalSupported = info.platform == "gecko"; 35 const styleSheetRemovalSupported = info.platform == "gecko";
36 36
37 const selectorGroupSize = 1024; 37 const selectorGroupSize = 1024;
38 38
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 return; 115 return;
116 116
117 browser.tabs.removeCSS(tabId, { 117 browser.tabs.removeCSS(tabId, {
118 code: styleSheet, 118 code: styleSheet,
119 cssOrigin: "user", 119 cssOrigin: "user",
120 frameId, 120 frameId,
121 matchAboutBlank: true 121 matchAboutBlank: true
122 }); 122 });
123 } 123 }
124 124
125 function updateFrameStyles(tabId, frameId, selectors, groupName) 125 function updateFrameStyles(tabId, frameId, selectors, groupName, appendOnly)
126 { 126 {
127 let styleSheet = null; 127 let styleSheet = "";
128 if (selectors.length > 0) 128 if (selectors.length > 0)
129 styleSheet = createStyleSheet(selectors); 129 styleSheet = createStyleSheet(selectors);
130 130
131 let frame = ext.getFrame(tabId, frameId); 131 let frame = ext.getFrame(tabId, frameId);
132 if (!frame) 132 if (!frame)
133 return false; 133 return false;
134 134
135 if (!frame.injectedStyleSheets) 135 if (!frame.injectedStyleSheets)
136 frame.injectedStyleSheets = new Map(); 136 frame.injectedStyleSheets = new Map();
137 137
138 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 138 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
139
140 if (appendOnly && oldStyleSheet)
141 styleSheet = oldStyleSheet + styleSheet;
139 142
140 // Ideally we would compare the old and new style sheets and skip this code 143 // Ideally we would compare the old and new style sheets and skip this code
141 // if they're the same, but the old style sheet can be a leftover from a 144 // if they're the same, but the old style sheet can be a leftover from a
142 // previous instance of the frame. We must add the new style sheet 145 // previous instance of the frame. We must add the new style sheet
143 // regardless. 146 // regardless.
144 147
145 // Add the new style sheet first to keep previously hidden elements from 148 // Add the new style sheet first to keep previously hidden elements from
146 // reappearing momentarily. 149 // reappearing momentarily.
147 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) 150 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet))
148 return false; 151 return false;
149 152
150 // Sometimes the old and new style sheets can be exactly the same. In such a 153 // Sometimes the old and new style sheets can be exactly the same. In such a
151 // case, do not remove the "old" style sheet, because it is in fact the new 154 // case, do not remove the "old" style sheet, because it is in fact the new
152 // style sheet now. 155 // style sheet now.
153 if (oldStyleSheet && oldStyleSheet != styleSheet) 156 if (oldStyleSheet && oldStyleSheet != styleSheet)
154 removeStyleSheet(tabId, frameId, oldStyleSheet); 157 removeStyleSheet(tabId, frameId, oldStyleSheet);
155 158
156 frame.injectedStyleSheets.set(groupName, styleSheet); 159 frame.injectedStyleSheets.set(groupName, styleSheet);
157 return true; 160 return true;
158 } 161 }
159 162
160 port.on("elemhide.getSelectors", (message, sender) => 163 port.on("elemhide.getSelectors", (message, sender) =>
161 { 164 {
162 let selectors = []; 165 let selectors = [];
163 let emulatedPatterns = []; 166 let emulatedPatterns = [];
164 let trace = hasListener(sender.page.id); 167 let trace = HitLogger.hasListener(sender.page.id);
165 let inline = !userStyleSheetsSupported; 168 let inline = !userStyleSheetsSupported;
166 169
167 if (!checkWhitelisted(sender.page, sender.frame, null, 170 if (!checkWhitelisted(sender.page, sender.frame, null,
168 RegExpFilter.typeMap.DOCUMENT | 171 RegExpFilter.typeMap.DOCUMENT |
169 RegExpFilter.typeMap.ELEMHIDE)) 172 RegExpFilter.typeMap.ELEMHIDE))
170 { 173 {
171 let hostname = extractHostFromFrame(sender.frame); 174 let hostname = extractHostFromFrame(sender.frame);
172 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 175 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
173 RegExpFilter.typeMap.GENERICHIDE); 176 RegExpFilter.typeMap.GENERICHIDE);
174 177
(...skipping 22 matching lines...) Expand all
197 // add styles for emulation filters inline. 200 // add styles for emulation filters inline.
198 if (!styleSheetRemovalSupported) 201 if (!styleSheetRemovalSupported)
199 response.inlineEmulated = true; 202 response.inlineEmulated = true;
200 203
201 return response; 204 return response;
202 }); 205 });
203 206
204 port.on("elemhide.injectSelectors", (message, sender) => 207 port.on("elemhide.injectSelectors", (message, sender) =>
205 { 208 {
206 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 209 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
207 message.groupName); 210 message.groupName, message.appendOnly);
208 }); 211 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld