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

Delta Between Two Patch Sets: lib/contentPolicy.js

Issue 6201308310667264: Issue 521- Inject our stylesheet on per-site basis rather than globally (Closed)
Left Patch Set: WIP Created June 4, 2014, 5:55 p.m.
Right Patch Set: Version 1.0 Created Aug. 6, 2014, 4:30 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 | « no previous file | lib/elemHide.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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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
(...skipping 12 matching lines...) Expand all
23 Cu.import("resource://gre/modules/Services.jsm"); 23 Cu.import("resource://gre/modules/Services.jsm");
24 24
25 let {TimeLine} = require("timeline"); 25 let {TimeLine} = require("timeline");
26 let {Utils} = require("utils"); 26 let {Utils} = require("utils");
27 let {Prefs} = require("prefs"); 27 let {Prefs} = require("prefs");
28 let {FilterStorage} = require("filterStorage"); 28 let {FilterStorage} = require("filterStorage");
29 let {BlockingFilter, WhitelistFilter} = require("filterClasses"); 29 let {BlockingFilter, WhitelistFilter} = require("filterClasses");
30 let {defaultMatcher} = require("matcher"); 30 let {defaultMatcher} = require("matcher");
31 let {objectMouseEventHander} = require("objectTabs"); 31 let {objectMouseEventHander} = require("objectTabs");
32 let {RequestNotifier} = require("requestNotifier"); 32 let {RequestNotifier} = require("requestNotifier");
33 let {ElemHide} = require("elemHide");
33 34
34 /** 35 /**
35 * List of explicitly supported content types 36 * List of explicitly supported content types
36 * @type Array of String 37 * @type Array of String
37 */ 38 */
38 let contentTypes = ["OTHER", "SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "SUBDOCU MENT", "DOCUMENT", "XMLHTTPREQUEST", "OBJECT_SUBREQUEST", "FONT", "MEDIA"]; 39 let contentTypes = ["OTHER", "SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "SUBDOCU MENT", "DOCUMENT", "XMLHTTPREQUEST", "OBJECT_SUBREQUEST", "FONT", "MEDIA"];
39 40
40 /** 41 /**
41 * List of content types that aren't associated with a visual document area 42 * List of content types that aren't associated with a visual document area
42 * @type Array of String 43 * @type Array of String
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 Utils.styleService.loadAndRegisterSheet(collapseStyle, Ci.nsIStyleSheetServi ce.USER_SHEET); 136 Utils.styleService.loadAndRegisterSheet(collapseStyle, Ci.nsIStyleSheetServi ce.USER_SHEET);
136 onShutdown.add(function() 137 onShutdown.add(function()
137 { 138 {
138 Utils.styleService.unregisterSheet(collapseStyle, Ci.nsIStyleSheetService. USER_SHEET); 139 Utils.styleService.unregisterSheet(collapseStyle, Ci.nsIStyleSheetService. USER_SHEET);
139 }) 140 })
140 TimeLine.log("done registering stylesheet"); 141 TimeLine.log("done registering stylesheet");
141 142
142 TimeLine.leave("Done initializing content policy"); 143 TimeLine.leave("Done initializing content policy");
143 }, 144 },
144 145
145 processWindow: function(wnd, contentType) 146 /**
147 * Checks whether a window or its parent is whitelisted or has a valid site-ke y.
148 * @param wnd {nsIDOMWindow}
149 */
150 shouldNeverBlockWindow: function(wnd)
146 { 151 {
147 let uri = wnd.document.documentURIObject; 152 let uri = wnd.document.documentURIObject;
148 if (!uri) 153 if (!uri)
149 return true; 154 return true;
150 155
151 if (!Policy.isBlockableScheme(uri.scheme)) 156 if (!Policy.isBlockableScheme(uri.scheme))
152 return true; 157 return true;
153 158
154 let topWnd = wnd.top; 159 let topWnd = wnd.top;
155 if (!topWnd || !topWnd.location || !topWnd.location.href) 160 if (!topWnd || !topWnd.location || !topWnd.location.href)
156 return true; 161 return true;
157 162
158 let testWnd = wnd; 163 let testWnd = wnd;
159 let parentWndLocation = getWindowLocation(testWnd); 164 let parentWndLocation = getWindowLocation(testWnd);
160 while (true) 165 while (true)
161 { 166 {
162 let testWndLocation = parentWndLocation; 167 let testWndLocation = parentWndLocation;
163 parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWind owLocation(testWnd.parent)); 168 parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWind owLocation(testWnd.parent));
164 169
165 let match = Policy.isWhitelisted(testWndLocation, parentWndLocation); 170 let match = Policy.isWhitelisted(testWndLocation, parentWndLocation);
166 171
167 if (!(match instanceof WhitelistFilter)) 172 if (match == null)
tschuster 2014/06/04 18:07:40 I think we should make this match === null, becaus
168 { 173 {
169 let keydata = (testWnd.document && testWnd.document.documentElement ? te stWnd.document.documentElement.getAttribute("data-adblockkey") : null); 174 let keydata = (testWnd.document && testWnd.document.documentElement ? te stWnd.document.documentElement.getAttribute("data-adblockkey") : null);
170 if (keydata && keydata.indexOf("_") >= 0) 175 if (keydata && keydata.indexOf("_") >= 0)
171 { 176 {
172 let [key, signature] = keydata.split("_", 2); 177 let [key, signature] = keydata.split("_", 2);
173 let keyMatch = defaultMatcher.matchesByKey(testWndLocation, key.replac e(/=/g, ""), docDomain); 178 let keyMatch = defaultMatcher.matchesByKey(testWndLocation, key.replac e(/=/g, ""), docDomain);
174 if (keyMatch && Utils.crypto) 179 if (keyMatch && Utils.crypto)
175 { 180 {
176 // Website specifies a key that we know but is the signature valid? 181 // Website specifies a key that we know but is the signature valid?
177 let uri = Services.io.newURI(testWndLocation, null, null); 182 let uri = Services.io.newURI(testWndLocation, null, null);
178 let params = [ 183 let params = [
179 uri.path.replace(/#.*/, ""), // REQUEST_URI 184 uri.path.replace(/#.*/, ""), // REQUEST_URI
180 uri.asciiHost, // HTTP_HOST 185 uri.asciiHost, // HTTP_HOST
181 Utils.httpProtocol.userAgent // HTTP_USER_AGENT 186 Utils.httpProtocol.userAgent // HTTP_USER_AGENT
182 ]; 187 ];
183 if (Utils.verifySignature(key, signature, params.join("\0"))) 188 if (Utils.verifySignature(key, signature, params.join("\0")))
184 match = keyMatch; // XXX what is this? 189 match = keyMatch;
tschuster 2014/06/04 18:07:40 Okay I am not sure about this case, is that |keyMa
185 } 190 }
tschuster 2014/06/05 19:00:34 I have an other concern: This is never going to wo
186 } 191 }
187 } 192 }
188 193
189 if (match instanceof WhitelistFilter) 194 if (match instanceof WhitelistFilter)
190 { 195 {
191 FilterStorage.increaseHitCount(match, wnd); 196 FilterStorage.increaseHitCount(match, wnd);
192 RequestNotifier.addNodeData(testWnd.document, topWnd, Policy.type.DOCUME NT, getHostname(parentWndLocation), false, testWndLocation, match); 197 RequestNotifier.addNodeData(testWnd.document, topWnd, Policy.type.DOCUME NT, getHostname(parentWndLocation), false, testWndLocation, match);
193 return true; 198 return true;
194 }
195
196 if (contentType == Policy.type.ELEMHIDE)
197 {
198 let parentDocDomain = getHostname(parentWndLocation);
199 match = defaultMatcher.matchesAny(testWndLocation, "ELEMHIDE", parentDoc Domain, false);
200 if (match instanceof WhitelistFilter)
201 {
202 FilterStorage.increaseHitCount(match, wnd);
203 RequestNotifier.addNodeData(testWnd.document, topWnd, contentType, par entDocDomain, false, testWndLocation, match);
204 return true;
205 }
206 } 199 }
207 200
208 if (testWnd.parent == testWnd) 201 if (testWnd.parent == testWnd)
209 break; 202 break;
210 else 203 else
211 testWnd = testWnd.parent; 204 testWnd = testWnd.parent;
212 } 205 }
213 206
214 return false; 207 return false;
215 }, 208 },
216 209
217 /** 210 /**
218 * Checks whether a node should be blocked, hides it if necessary 211 * Checks whether a node should be blocked, hides it if necessary
219 * @param wnd {nsIDOMWindow} 212 * @param wnd {nsIDOMWindow}
220 * @param node {nsIDOMElement} 213 * @param node {nsIDOMElement}
221 * @param contentType {String} 214 * @param contentType {String}
222 * @param location {nsIURI} 215 * @param location {nsIURI}
223 * @param collapse {Boolean} true to force hiding of the node 216 * @param collapse {Boolean} true to force hiding of the node
224 * @return {Boolean} false if the node should be blocked 217 * @return {Boolean} false if the node should be blocked
225 */ 218 */
226 processNode: function(wnd, node, contentType, location, collapse) 219 processNode: function(wnd, node, contentType, location, collapse)
227 { 220 {
228 if (contentType == Policy.type.ELEMHIDE) 221 if (Policy.shouldNeverBlockWindow(wnd))
229 throw new Error("unexpected!"); 222 return true;
223
224 let topWnd = wnd.top;
225 let originWindow = Utils.getOriginWindow(wnd);
226 let wndLocation = originWindow.location.href;
227 let docDomain = getHostname(wndLocation);
230 228
231 // Data loaded by plugins should be attached to the document 229 // Data loaded by plugins should be attached to the document
232 if (contentType == Policy.type.OBJECT_SUBREQUEST && node instanceof Ci.nsIDO MElement) 230 if (contentType == Policy.type.OBJECT_SUBREQUEST && node instanceof Ci.nsIDO MElement)
233 node = node.ownerDocument; 231 node = node.ownerDocument;
234 232
235 // Fix type for objects misrepresented as frames or images 233 // Fix type for objects misrepresented as frames or images
236 if (contentType != Policy.type.OBJECT && (node instanceof Ci.nsIDOMHTMLObjec tElement || node instanceof Ci.nsIDOMHTMLEmbedElement)) 234 if (contentType != Policy.type.OBJECT && (node instanceof Ci.nsIDOMHTMLObjec tElement || node instanceof Ci.nsIDOMHTMLEmbedElement))
237 contentType = Policy.type.OBJECT; 235 contentType = Policy.type.OBJECT;
238 236
239 if (Policy.processWindow(wnd, contentType))
240 return true;
241
242 let originWindow = Utils.getOriginWindow(wnd);
243 let wndLocation = originWindow.location.href;
244 let docDomain = getHostname(wndLocation);
245 let topWnd = wnd.top;
246
247 let locationText = location.spec; 237 let locationText = location.spec;
248 let thirdParty = isThirdParty(location, docDomain); 238 let match = null;
249 239 if (contentType == Policy.type.ELEMHIDE)
250 let match = defaultMatcher.matchesAny(locationText, Policy.typeDescr[content Type] || "", docDomain, thirdParty); 240 {
251 if (match instanceof BlockingFilter && node.ownerDocument && !(contentType i n Policy.nonVisual)) 241 let testWnd = wnd;
252 { 242 let parentWndLocation = getWindowLocation(testWnd);
253 let prefCollapse = (match.collapse != null ? match.collapse : !Prefs.fastc ollapse); 243 while (true)
254 if (collapse || prefCollapse) 244 {
255 schedulePostProcess(node); 245 let testWndLocation = parentWndLocation;
256 } 246 parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWi ndowLocation(testWnd.parent));
257 247 let parentDocDomain = getHostname(parentWndLocation);
258 // Track mouse events for objects 248 match = defaultMatcher.matchesAny(testWndLocation, "ELEMHIDE", parentDoc Domain, false);
259 if (!match && contentType == Policy.type.OBJECT && node.nodeType == Ci.nsIDO MNode.ELEMENT_NODE) 249 if (match instanceof WhitelistFilter)
260 { 250 {
261 node.addEventListener("mouseover", objectMouseEventHander, true); 251 FilterStorage.increaseHitCount(match, wnd);
262 node.addEventListener("mouseout", objectMouseEventHander, true); 252 RequestNotifier.addNodeData(testWnd.document, topWnd, contentType, par entDocDomain, false, testWndLocation, match);
253 return true;
254 }
255
256 if (testWnd.parent == testWnd)
257 break;
258 else
259 testWnd = testWnd.parent;
260 }
261
262 match = location;
263 locationText = match.text.replace(/^.*?#/, '#');
264 location = locationText;
265
266 if (!match.isActiveOnDomain(docDomain))
267 return true;
268
269 let exception = ElemHide.getException(match, docDomain);
270 if (exception)
271 {
272 FilterStorage.increaseHitCount(exception, wnd);
273 RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdP arty, locationText, exception);
274 return true;
275 }
276 }
277
278 let thirdParty = (contentType == Policy.type.ELEMHIDE ? false : isThirdParty (location, docDomain));
279
280 if (!match && Prefs.enabled)
281 {
282 match = defaultMatcher.matchesAny(locationText, Policy.typeDescr[contentTy pe] || "", docDomain, thirdParty);
283 if (match instanceof BlockingFilter && node.ownerDocument && !(contentType in Policy.nonVisual))
284 {
285 let prefCollapse = (match.collapse != null ? match.collapse : !Prefs.fas tcollapse);
286 if (collapse || prefCollapse)
287 schedulePostProcess(node);
288 }
289
290 // Track mouse events for objects
291 if (!match && contentType == Policy.type.OBJECT && node.nodeType == Ci.nsI DOMNode.ELEMENT_NODE)
292 {
293 node.addEventListener("mouseover", objectMouseEventHander, true);
294 node.addEventListener("mouseout", objectMouseEventHander, true);
295 }
263 } 296 }
264 297
265 // Store node data 298 // Store node data
266 RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdParty , locationText, match); 299 RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdParty , locationText, match);
267 if (match) 300 if (match)
268 FilterStorage.increaseHitCount(match, wnd); 301 FilterStorage.increaseHitCount(match, wnd);
269 302
270 return !match || match instanceof WhitelistFilter; 303 return !match || match instanceof WhitelistFilter;
271 }, 304 },
272 305
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 if (!wnd || wnd.closed) 784 if (!wnd || wnd.closed)
752 return; 785 return;
753 786
754 if (entry.type == Policy.type.OBJECT) 787 if (entry.type == Policy.type.OBJECT)
755 { 788 {
756 node.removeEventListener("mouseover", objectMouseEventHander, true); 789 node.removeEventListener("mouseover", objectMouseEventHander, true);
757 node.removeEventListener("mouseout", objectMouseEventHander, true); 790 node.removeEventListener("mouseout", objectMouseEventHander, true);
758 } 791 }
759 Policy.processNode(wnd, node, entry.type, Utils.makeURI(entry.location), true) ; 792 Policy.processNode(wnd, node, entry.type, Utils.makeURI(entry.location), true) ;
760 } 793 }
LEFTRIGHT
« no previous file | lib/elemHide.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld