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: Rebased and addressed comments Created Feb. 11, 2015, 10:54 a.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; 25 this._url = tab.url && new URL(tab.url);
26 26
27 this.browserAction = new BrowserAction(tab.id); 27 this.browserAction = new BrowserAction(tab.id);
28 this.contextMenus = new ContextMenus(this); 28 this.contextMenus = new ContextMenus(this);
29 }; 29 };
30 Page.prototype = { 30 Page.prototype = {
31 get url() 31 get url()
32 { 32 {
33 // usually our Page objects are created from Chrome's Tab objects, which 33 // usually our Page objects are created from Chrome's Tab objects, which
Wladimir Palant 2015/02/11 13:45:45 Nit: A sentence is usually started by a capital le
Sebastian Noack 2015/02/11 17:07:06 Note that I just reverted those lines including th
34 // provide the url. So we can return the url given in the constructor. 34 // provide the url. So we can return the url given in the constructor.
35 if (this._url != null) 35 if (this._url)
36 return this._url; 36 return this._url;
37 37
38 // but sometimes we only have the tab id when we create a Page object. 38 // but sometimes we only have the tab id when we create a Page object.
Sebastian Noack 2015/02/11 10:55:51 All changes below are due to rebase.
39 // In that case we get the url from top frame of the tab, recorded by 39 // In that case we get the url from top frame of the tab, recorded by
40 // the onBeforeRequest handler. 40 // the onBeforeRequest handler.
41 var frames = framesOfTabs[this._id]; 41 var frames = framesOfTabs[this._id];
42 if (frames) 42 if (frames)
43 { 43 {
44 var frame = frames[0]; 44 var frame = frames[0];
45 if (frame) 45 if (frame)
46 return frame.url; 46 return frame.url;
47 } 47 }
48 }, 48 },
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 { 335 {
336 tabs.forEach(function(tab) 336 tabs.forEach(function(tab)
337 { 337 {
338 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details) 338 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details)
339 { 339 {
340 if (details && details.length > 0) 340 if (details && details.length > 0)
341 { 341 {
342 var frames = framesOfTabs[tab.id] = {__proto__: null}; 342 var frames = framesOfTabs[tab.id] = {__proto__: null};
343 343
344 for (var i = 0; i < details.length; i++) 344 for (var i = 0; i < details.length; i++)
345 frames[details[i].frameId] = {url: details[i].url, parent: null}; 345 frames[details[i].frameId] = {url: new URL(details[i].url), parent: null};
346 346
347 for (var i = 0; i < details.length; i++) 347 for (var i = 0; i < details.length; i++)
348 { 348 {
349 var parentFrameId = details[i].parentFrameId; 349 var parentFrameId = details[i].parentFrameId;
350 350
351 if (parentFrameId != -1) 351 if (parentFrameId != -1)
352 frames[details[i].frameId].parent = frames[parentFrameId]; 352 frames[details[i].frameId].parent = frames[parentFrameId];
353 } 353 }
354 } 354 }
355 }); 355 });
(...skipping 19 matching lines...) Expand all
375 details.frameId == 0 && !(details.tabId in framesOfTabs) 375 details.frameId == 0 && !(details.tabId in framesOfTabs)
376 ); 376 );
377 377
378 var frames = null; 378 var frames = null;
379 if (!isMainFrame) 379 if (!isMainFrame)
380 frames = framesOfTabs[details.tabId]; 380 frames = framesOfTabs[details.tabId];
381 if (!frames) 381 if (!frames)
382 frames = framesOfTabs[details.tabId] = {__proto__: null}; 382 frames = framesOfTabs[details.tabId] = {__proto__: null};
383 383
384 var frame = null; 384 var frame = null;
385 var url = new URL(details.url);
385 if (!isMainFrame) 386 if (!isMainFrame)
386 { 387 {
387 // we are looking for the frame that contains the element that 388 // we are looking for the frame that contains the element that
388 // is about to load, however if a frame is loading the surrounding 389 // is about to load, however if a frame is loading the surrounding
389 // frame is indicated by parentFrameId instead of frameId 390 // frame is indicated by parentFrameId instead of frameId
390 var frameId; 391 var frameId;
391 if (requestType == "sub_frame") 392 if (requestType == "sub_frame")
392 frameId = details.parentFrameId; 393 frameId = details.parentFrameId;
393 else 394 else
394 frameId = details.frameId; 395 frameId = details.frameId;
395 396
396 frame = frames[frameId] || frames[Object.keys(frames)[0]]; 397 frame = frames[frameId] || frames[Object.keys(frames)[0]];
397 398
398 if (frame) 399 if (frame)
399 { 400 {
400 // Since Chrome 38 requests of type 'object' (e.g. requests 401 // Since Chrome 38 requests of type 'object' (e.g. requests
401 // initiated by Flash) are mistakenly reported with the type 'other'. 402 // initiated by Flash) are mistakenly reported with the type 'other'.
402 // https://code.google.com/p/chromium/issues/detail?id=410382 403 // https://code.google.com/p/chromium/issues/detail?id=410382
403 if (requestType == "other" && parseInt(navigator.userAgent.match(/\bCh rome\/(\d+)/)[1], 10) >= 38) 404 if (requestType == "other" && parseInt(navigator.userAgent.match(/\bCh rome\/(\d+)/)[1], 10) >= 38)
404 requestType = "object"; 405 requestType = "object";
405 406
406 var results = ext.webRequest.onBeforeRequest._dispatch( 407 var results = ext.webRequest.onBeforeRequest._dispatch(
407 details.url, 408 url,
408 requestType, 409 requestType,
409 new Page({id: details.tabId}), 410 new Page({id: details.tabId}),
410 frame 411 frame
411 ); 412 );
412 413
413 if (results.indexOf(false) != -1) 414 if (results.indexOf(false) != -1)
414 return {cancel: true}; 415 return {cancel: true};
415 } 416 }
416 } 417 }
417 418
418 if (isMainFrame || details.type == "sub_frame") 419 if (isMainFrame || details.type == "sub_frame")
419 frames[details.frameId] = {url: details.url, parent: frame}; 420 frames[details.frameId] = {url: url, parent: frame};
420 } 421 }
421 catch (e) 422 catch (e)
422 { 423 {
423 // recent versions of Chrome cancel the request when an error occurs in 424 // recent versions of Chrome cancel the request when an error occurs in
424 // the onBeforeRequest listener. However in our case it is preferred, to 425 // the onBeforeRequest listener. However in our case it is preferred, to
425 // let potentially some ads through, rather than blocking legit requests. 426 // let potentially some ads through, rather than blocking legit requests.
426 console.error(e); 427 console.error(e);
427 } 428 }
428 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); 429 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]);
429 430
430 431
431 /* Message passing */ 432 /* Message passing */
432 433
433 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse ) 434 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse )
434 { 435 {
435 var sender = { 436 var sender = {
436 page: new Page(rawSender.tab), 437 page: new Page(rawSender.tab),
437 frame: { 438 frame: {
438 url: rawSender.url, 439 url: new URL(rawSender.url),
439 get parent() 440 get parent()
440 { 441 {
441 var frames = framesOfTabs[rawSender.tab.id]; 442 var frames = framesOfTabs[rawSender.tab.id];
442 443
443 if (!frames) 444 if (!frames)
444 return null; 445 return null;
445 446
446 if ("frameId" in rawSender) 447 if ("frameId" in rawSender)
447 { 448 {
448 // Chrome 41+ 449 // Chrome 41+
449 var frame = frames[rawSender.frameId]; 450 var frame = frames[rawSender.frameId];
450 if (frame) 451 if (frame)
451 return frame.parent; 452 return frame.parent;
452 } 453 }
453 else 454 else
454 { 455 {
455 // Chrome 28-40 456 // Chrome 28-40
456 for (var frameId in frames) 457 for (var frameId in frames)
457 { 458 {
458 if (frames[frameId].url == rawSender.url) 459 if (frames[frameId].url.href == this.url.href)
459 return frames[frameId].parent; 460 return frames[frameId].parent;
460 } 461 }
461 } 462 }
462 463
463 return frames[0]; 464 return frames[0];
464 } 465 }
465 } 466 }
466 }; 467 };
467 468
468 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true) != -1; 469 return ext.onMessage._dispatch(message, sender, sendResponse).indexOf(true) != -1;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 callback(new Page(tab)); 514 callback(new Page(tab));
514 } 515 }
515 else 516 else
516 { 517 {
517 ext.pages.open(optionsUrl, callback); 518 ext.pages.open(optionsUrl, callback);
518 } 519 }
519 }); 520 });
520 }); 521 });
521 }; 522 };
522 })(); 523 })();
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