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: Addressed Sebastian's feedback Created March 21, 2018, 3:14 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("filterClasses"); 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses");
23 const {ElemHide} = require("elemHide"); 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide");
24 const {ElemHideEmulation} = require("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("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
39 let userStyleSheetsSupported = true; 39 let userStyleSheetsSupported = true;
(...skipping 23 matching lines...) Expand all
63 63
64 function createStyleSheet(selectors) 64 function createStyleSheet(selectors)
65 { 65 {
66 return Array.from(createRules(selectors)).join("\n"); 66 return Array.from(createRules(selectors)).join("\n");
67 } 67 }
68 68
69 function addStyleSheet(tabId, frameId, styleSheet) 69 function addStyleSheet(tabId, frameId, styleSheet)
70 { 70 {
71 try 71 try
72 { 72 {
73 browser.tabs.insertCSS(tabId, { 73 let promise = browser.tabs.insertCSS(tabId, {
74 code: styleSheet, 74 code: styleSheet,
75 cssOrigin: "user", 75 cssOrigin: "user",
76 frameId, 76 frameId,
77 matchAboutBlank: true, 77 matchAboutBlank: true,
78 runAt: "document_start" 78 runAt: "document_start"
79 }); 79 });
80
81 // See error handling notes in the catch block.
82 promise.catch(() => {});
80 } 83 }
81 catch (error) 84 catch (error)
82 { 85 {
86 // If the error is about the "cssOrigin" option, this is an older version
87 // of Chromium (65 and below) or Firefox (52 and below) that does not
88 // support user style sheets.
83 if (/\bcssOrigin\b/.test(error.message)) 89 if (/\bcssOrigin\b/.test(error.message))
84 userStyleSheetsSupported = false; 90 userStyleSheetsSupported = false;
85 91
92 // For other errors, we simply return false to indicate failure.
93 //
94 // One common error that occurs frequently is when a frame is not found
95 // (e.g. "Error: No frame with id 574 in tab 266"), which can happen when
96 // the code in the parent document has removed the frame before the
97 // background page has had a chance to respond to the content script's
98 // "elemhide.getSelectors" message. We simply ignore such errors, because
99 // otherwise they show up in the log too often and make debugging
100 // difficult.
101 //
102 // Also note that the missing frame error is thrown synchronously on
103 // Firefox, while on Chromium it is an asychronous promise rejection. In
104 // the latter case, we cannot indicate failure to the caller, but we still
105 // explicitly ignore the error.
86 return false; 106 return false;
87 } 107 }
88 108
89 return true; 109 return true;
90 } 110 }
91 111
92 function removeStyleSheet(tabId, frameId, styleSheet) 112 function removeStyleSheet(tabId, frameId, styleSheet)
93 { 113 {
94 if (!styleSheetRemovalSupported) 114 if (!styleSheetRemovalSupported)
95 return; 115 return;
96 116
97 browser.tabs.removeCSS(tabId, { 117 browser.tabs.removeCSS(tabId, {
98 code: styleSheet, 118 code: styleSheet,
99 cssOrigin: "user", 119 cssOrigin: "user",
100 frameId, 120 frameId,
101 matchAboutBlank: true 121 matchAboutBlank: true
102 }); 122 });
103 } 123 }
104 124
105 function updateFrameStyles(tabId, frameId, selectors, groupName) 125 function updateFrameStyles(tabId, frameId, selectors, groupName, appendOnly)
106 { 126 {
107 let styleSheet = null; 127 let styleSheet = "";
108 if (selectors.length > 0) 128 if (selectors.length > 0)
109 styleSheet = createStyleSheet(selectors); 129 styleSheet = createStyleSheet(selectors);
110 130
111 let frame = ext.getFrame(tabId, frameId); 131 let frame = ext.getFrame(tabId, frameId);
112 if (!frame) 132 if (!frame)
113 return false; 133 return false;
114 134
115 if (!frame.injectedStyleSheets) 135 if (!frame.injectedStyleSheets)
116 frame.injectedStyleSheets = new Map(); 136 frame.injectedStyleSheets = new Map();
117 137
118 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 138 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
139
140 if (appendOnly && oldStyleSheet)
141 styleSheet = oldStyleSheet + styleSheet;
119 142
120 // 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
121 // 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
122 // 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
123 // regardless. 146 // regardless.
124 147
125 // 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
126 // reappearing momentarily. 149 // reappearing momentarily.
127 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) 150 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet))
128 return false; 151 return false;
129 152
130 // 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
131 // 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
132 // style sheet now. 155 // style sheet now.
133 if (oldStyleSheet && oldStyleSheet != styleSheet) 156 if (oldStyleSheet && oldStyleSheet != styleSheet)
134 removeStyleSheet(tabId, frameId, oldStyleSheet); 157 removeStyleSheet(tabId, frameId, oldStyleSheet);
135 158
136 frame.injectedStyleSheets.set(groupName, styleSheet); 159 frame.injectedStyleSheets.set(groupName, styleSheet);
137 return true; 160 return true;
138 } 161 }
139 162
140 port.on("elemhide.getSelectors", (message, sender) => 163 port.on("elemhide.getSelectors", (message, sender) =>
141 { 164 {
142 let selectors = []; 165 let selectors = [];
143 let emulatedPatterns = []; 166 let emulatedPatterns = [];
144 let trace = hasListener(sender.page.id); 167 let trace = HitLogger.hasListener(sender.page.id);
145 let inline = !userStyleSheetsSupported; 168 let inline = !userStyleSheetsSupported;
146 169
147 if (!checkWhitelisted(sender.page, sender.frame, 170 if (!checkWhitelisted(sender.page, sender.frame, null,
148 RegExpFilter.typeMap.DOCUMENT | 171 RegExpFilter.typeMap.DOCUMENT |
149 RegExpFilter.typeMap.ELEMHIDE)) 172 RegExpFilter.typeMap.ELEMHIDE))
150 { 173 {
151 let hostname = extractHostFromFrame(sender.frame); 174 let hostname = extractHostFromFrame(sender.frame);
152 let specificOnly = checkWhitelisted(sender.page, sender.frame, 175 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
153 RegExpFilter.typeMap.GENERICHIDE); 176 RegExpFilter.typeMap.GENERICHIDE);
154 177
155 selectors = ElemHide.getSelectorsForDomain( 178 selectors = ElemHide.getSelectorsForDomain(
156 hostname, 179 hostname,
157 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 180 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
158 ); 181 );
159 182
160 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 183 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
161 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 184 emulatedPatterns.push({selector: filter.selector, text: filter.text});
162 } 185 }
(...skipping 14 matching lines...) Expand all
177 // add styles for emulation filters inline. 200 // add styles for emulation filters inline.
178 if (!styleSheetRemovalSupported) 201 if (!styleSheetRemovalSupported)
179 response.inlineEmulated = true; 202 response.inlineEmulated = true;
180 203
181 return response; 204 return response;
182 }); 205 });
183 206
184 port.on("elemhide.injectSelectors", (message, sender) => 207 port.on("elemhide.injectSelectors", (message, sender) =>
185 { 208 {
186 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 209 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
187 message.groupName); 210 message.groupName, message.appendOnly);
188 }); 211 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld