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: Created Feb. 22, 2018, 3:23 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 {DevLogger} = require("devLogger"); 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 {
83 userStyleSheetsSupported = false; 86 // If the error is about the "cssOrigin" option, this is an older version
84 } 87 // of Chromium (65 and below) or Firefox (52 and below) that does not
85 88 // support user style sheets.
86 return userStyleSheetsSupported; 89 if (/\bcssOrigin\b/.test(error.message))
90 userStyleSheetsSupported = false;
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.
106 return false;
107 }
108
109 return true;
87 } 110 }
88 111
89 function removeStyleSheet(tabId, frameId, styleSheet) 112 function removeStyleSheet(tabId, frameId, styleSheet)
90 { 113 {
91 if (!styleSheetRemovalSupported) 114 if (!styleSheetRemovalSupported)
92 return; 115 return;
93 116
94 browser.tabs.removeCSS(tabId, { 117 browser.tabs.removeCSS(tabId, {
95 code: styleSheet, 118 code: styleSheet,
96 cssOrigin: "user", 119 cssOrigin: "user",
97 frameId, 120 frameId,
98 matchAboutBlank: true 121 matchAboutBlank: true
99 }); 122 });
100 } 123 }
101 124
102 function updateFrameStyles(tabId, frameId, selectors, groupName) 125 function updateFrameStyles(tabId, frameId, selectors, groupName, appendOnly)
103 { 126 {
104 let styleSheet = null; 127 let styleSheet = "";
105 if (selectors.length > 0) 128 if (selectors.length > 0)
106 styleSheet = createStyleSheet(selectors); 129 styleSheet = createStyleSheet(selectors);
107 130
108 let frame = ext.getFrame(tabId, frameId); 131 let frame = ext.getFrame(tabId, frameId);
109 if (!frame) 132 if (!frame)
110 return false; 133 return false;
111 134
112 if (!frame.injectedStyleSheets) 135 if (!frame.injectedStyleSheets)
113 frame.injectedStyleSheets = new Map(); 136 frame.injectedStyleSheets = new Map();
114 137
115 let oldStyleSheet = frame.injectedStyleSheets.get(groupName); 138 let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
139
140 if (appendOnly && oldStyleSheet)
141 styleSheet = oldStyleSheet + styleSheet;
116 142
117 // 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
118 // 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
119 // 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
120 // regardless. 146 // regardless.
121 147
122 // 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
123 // reappearing momentarily. 149 // reappearing momentarily.
124 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet)) 150 if (styleSheet && !addStyleSheet(tabId, frameId, styleSheet))
125 return false; 151 return false;
126 152
127 // 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
128 // 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
129 // style sheet now. 155 // style sheet now.
130 if (oldStyleSheet && oldStyleSheet != styleSheet) 156 if (oldStyleSheet && oldStyleSheet != styleSheet)
131 removeStyleSheet(tabId, frameId, oldStyleSheet); 157 removeStyleSheet(tabId, frameId, oldStyleSheet);
132 158
133 frame.injectedStyleSheets.set(groupName, styleSheet); 159 frame.injectedStyleSheets.set(groupName, styleSheet);
134 return true; 160 return true;
135 } 161 }
136 162
137 port.on("elemhide.getSelectors", (message, sender) => 163 port.on("elemhide.getSelectors", (message, sender) =>
138 { 164 {
139 let selectors = []; 165 let selectors = [];
140 let emulatedPatterns = []; 166 let emulatedPatterns = [];
141 let trace = DevLogger.listening(sender.page.id); 167 let trace = HitLogger.hasListener(sender.page.id);
142 let inline = !userStyleSheetsSupported; 168 let inline = !userStyleSheetsSupported;
143 169
144 if (!checkWhitelisted(sender.page, sender.frame, 170 if (!checkWhitelisted(sender.page, sender.frame, null,
145 RegExpFilter.typeMap.DOCUMENT | 171 RegExpFilter.typeMap.DOCUMENT |
146 RegExpFilter.typeMap.ELEMHIDE)) 172 RegExpFilter.typeMap.ELEMHIDE))
147 { 173 {
148 let hostname = extractHostFromFrame(sender.frame); 174 let hostname = extractHostFromFrame(sender.frame);
149 let specificOnly = checkWhitelisted(sender.page, sender.frame, 175 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
150 RegExpFilter.typeMap.GENERICHIDE); 176 RegExpFilter.typeMap.GENERICHIDE);
151 177
152 selectors = ElemHide.getSelectorsForDomain( 178 selectors = ElemHide.getSelectorsForDomain(
153 hostname, 179 hostname,
154 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 180 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
155 ); 181 );
156 182
157 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 183 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
158 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 184 emulatedPatterns.push({selector: filter.selector, text: filter.text});
159 } 185 }
(...skipping 14 matching lines...) Expand all
174 // add styles for emulation filters inline. 200 // add styles for emulation filters inline.
175 if (!styleSheetRemovalSupported) 201 if (!styleSheetRemovalSupported)
176 response.inlineEmulated = true; 202 response.inlineEmulated = true;
177 203
178 return response; 204 return response;
179 }); 205 });
180 206
181 port.on("elemhide.injectSelectors", (message, sender) => 207 port.on("elemhide.injectSelectors", (message, sender) =>
182 { 208 {
183 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 209 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
184 message.groupName); 210 message.groupName, message.appendOnly);
185 }); 211 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld