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

Side by Side Diff: chrome/ext/background.js

Issue 5564089086509056: Issue 1801 - Use URL objects to process URLs in the background page (Closed)
Patch Set: Created Jan. 25, 2015, 1:18 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 | « background.js ('k') | lib/basedomain.js » ('j') | lib/url.js » ('J')
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 (function() 18 (function()
19 { 19 {
20 /* Pages */ 20 /* Pages */
21 21
22 var Page = ext.Page = function(tab) 22 var Page = ext.Page = function(tab)
23 { 23 {
24 this._id = tab.id; 24 this._id = tab.id;
25 this._url = tab.url;
26 25
27 this.browserAction = new BrowserAction(tab.id); 26 this.browserAction = new BrowserAction(tab.id);
28 this.contextMenus = new ContextMenus(this); 27 this.contextMenus = new ContextMenus(this);
28
29 // Usually Page objects are created from Chrome's Tab objects, which
30 // provide the url. So we can override the fallback implemented below.
31 if (tab.url)
32 Object.defineProperty(this, "url", {value: new URL(tab.url), enumerable: t rue});
Wladimir Palant 2015/02/09 12:54:29 Code that belongs together should really be togeth
Sebastian Noack 2015/02/11 10:55:51 Whatever.
29 }; 33 };
30 Page.prototype = { 34 Page.prototype = {
31 get url() 35 get url()
32 { 36 {
33 // usually our Page objects are created from Chrome's Tab objects, which 37 // Sometimes we only have the tab id when we create a Page object.
34 // provide the url. So we can return the url given in the constructor. 38 // In that case we get the url from top frame of the tab, recorded
35 if (this._url != null) 39 // by the onBeforeRequest handler.
36 return this._url;
37
38 // but sometimes we only have the tab id when we create a Page object.
39 // In that case we get the url from top frame of the tab, recorded by
40 // the onBeforeRequest handler.
41 var frames = framesOfTabs[this._id]; 40 var frames = framesOfTabs[this._id];
42 if (frames) 41 if (frames)
43 { 42 {
44 var frame = frames[0]; 43 var frame = frames[0];
45 if (frame) 44 if (frame)
46 return frame.url; 45 return frame.url;
47 } 46 }
48 }, 47 },
49 sendMessage: function(message, responseCallback) 48 sendMessage: function(message, responseCallback)
50 { 49 {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 { 257 {
259 tabs.forEach(function(tab) 258 tabs.forEach(function(tab)
260 { 259 {
261 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details) 260 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details)
262 { 261 {
263 if (details && details.length > 0) 262 if (details && details.length > 0)
264 { 263 {
265 var frames = framesOfTabs[tab.id] = {__proto__: null}; 264 var frames = framesOfTabs[tab.id] = {__proto__: null};
266 265
267 for (var i = 0; i < details.length; i++) 266 for (var i = 0; i < details.length; i++)
268 frames[details[i].frameId] = {url: details[i].url, parent: null}; 267 frames[details[i].frameId] = {url: new URL(details[i].url), parent: null};
269 268
270 for (var i = 0; i < details.length; i++) 269 for (var i = 0; i < details.length; i++)
271 { 270 {
272 var parentFrameId = details[i].parentFrameId; 271 var parentFrameId = details[i].parentFrameId;
273 272
274 if (parentFrameId != -1) 273 if (parentFrameId != -1)
275 frames[details[i].frameId].parent = frames[parentFrameId]; 274 frames[details[i].frameId].parent = frames[parentFrameId];
276 } 275 }
277 } 276 }
278 }); 277 });
(...skipping 19 matching lines...) Expand all
298 details.frameId == 0 && !(details.tabId in framesOfTabs) 297 details.frameId == 0 && !(details.tabId in framesOfTabs)
299 ); 298 );
300 299
301 var frames = null; 300 var frames = null;
302 if (!isMainFrame) 301 if (!isMainFrame)
303 frames = framesOfTabs[details.tabId]; 302 frames = framesOfTabs[details.tabId];
304 if (!frames) 303 if (!frames)
305 frames = framesOfTabs[details.tabId] = {__proto__: null}; 304 frames = framesOfTabs[details.tabId] = {__proto__: null};
306 305
307 var frame = null; 306 var frame = null;
307 var url = new URL(details.url);
308 if (!isMainFrame) 308 if (!isMainFrame)
309 { 309 {
310 // we are looking for the frame that contains the element that 310 // we are looking for the frame that contains the element that
311 // is about to load, however if a frame is loading the surrounding 311 // is about to load, however if a frame is loading the surrounding
312 // frame is indicated by parentFrameId instead of frameId 312 // frame is indicated by parentFrameId instead of frameId
313 var frameId; 313 var frameId;
314 if (requestType == "sub_frame") 314 if (requestType == "sub_frame")
315 frameId = details.parentFrameId; 315 frameId = details.parentFrameId;
316 else 316 else
317 frameId = details.frameId; 317 frameId = details.frameId;
318 318
319 frame = frames[frameId] || frames[Object.keys(frames)[0]]; 319 frame = frames[frameId] || frames[Object.keys(frames)[0]];
320 320
321 if (frame) 321 if (frame)
322 { 322 {
323 // Since Chrome 38 requests of type 'object' (e.g. requests 323 // Since Chrome 38 requests of type 'object' (e.g. requests
324 // initiated by Flash) are mistakenly reported with the type 'other'. 324 // initiated by Flash) are mistakenly reported with the type 'other'.
325 // https://code.google.com/p/chromium/issues/detail?id=410382 325 // https://code.google.com/p/chromium/issues/detail?id=410382
326 if (requestType == "other" && parseInt(navigator.userAgent.match(/\bCh rome\/(\d+)/)[1], 10) >= 38) 326 if (requestType == "other" && parseInt(navigator.userAgent.match(/\bCh rome\/(\d+)/)[1], 10) >= 38)
327 requestType = "object"; 327 requestType = "object";
328 328
329 var results = ext.webRequest.onBeforeRequest._dispatch( 329 var results = ext.webRequest.onBeforeRequest._dispatch(
330 details.url, 330 url,
331 requestType, 331 requestType,
332 new Page({id: details.tabId}), 332 new Page({id: details.tabId}),
333 frame 333 frame
334 ); 334 );
335 335
336 if (results.indexOf(false) != -1) 336 if (results.indexOf(false) != -1)
337 return {cancel: true}; 337 return {cancel: true};
338 } 338 }
339 } 339 }
340 340
341 if (isMainFrame || details.type == "sub_frame") 341 if (isMainFrame || details.type == "sub_frame")
342 frames[details.frameId] = {url: details.url, parent: frame}; 342 frames[details.frameId] = {url: url, parent: frame};
343 } 343 }
344 catch (e) 344 catch (e)
345 { 345 {
346 // recent versions of Chrome cancel the request when an error occurs in 346 // recent versions of Chrome cancel the request when an error occurs in
347 // the onBeforeRequest listener. However in our case it is preferred, to 347 // the onBeforeRequest listener. However in our case it is preferred, to
348 // let potentially some ads through, rather than blocking legit requests. 348 // let potentially some ads through, rather than blocking legit requests.
349 console.error(e); 349 console.error(e);
350 } 350 }
351 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); 351 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]);
352 352
353 353
354 /* Message passing */ 354 /* Message passing */
355 355
356 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse ) 356 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse )
357 { 357 {
358 var sender = { 358 var sender = {
359 page: new Page(rawSender.tab), 359 page: new Page(rawSender.tab),
360 frame: { 360 frame: {
361 url: rawSender.url, 361 url: new URL(rawSender.url),
362 get parent() 362 get parent()
363 { 363 {
364 var frames = framesOfTabs[rawSender.tab.id]; 364 var frames = framesOfTabs[rawSender.tab.id];
365 365
366 if (!frames) 366 if (!frames)
367 return null; 367 return null;
368 368
369 if ("frameId" in rawSender) 369 if ("frameId" in rawSender)
370 { 370 {
371 // Chrome 41+ 371 // Chrome 41+
372 var frame = frames[rawSender.frameId]; 372 var frame = frames[rawSender.frameId];
373 if (frame) 373 if (frame)
374 return frame.parent; 374 return frame.parent;
375 } 375 }
376 else 376 else
377 { 377 {
378 // Chrome 28-40 378 // Chrome 28-40
379 for (var frameId in frames) 379 for (var frameId in frames)
380 { 380 {
381 if (frames[frameId].url == rawSender.url) 381 if (frames[frameId].url.href == this.url.href)
382 return frames[frameId].parent; 382 return frames[frameId].parent;
383 } 383 }
384 } 384 }
385 385
386 return frames[0]; 386 return frames[0];
387 } 387 }
388 } 388 }
389 }; 389 };
390 390
391 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true) != -1; 391 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true) != -1;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 callback(new Page(tab)); 431 callback(new Page(tab));
432 } 432 }
433 else 433 else
434 { 434 {
435 ext.pages.open(optionsUrl, callback); 435 ext.pages.open(optionsUrl, callback);
436 } 436 }
437 }); 437 });
438 }); 438 });
439 }; 439 };
440 })(); 440 })();
OLDNEW
« no previous file with comments | « background.js ('k') | lib/basedomain.js » ('j') | lib/url.js » ('J')

Powered by Google App Engine
This is Rietveld