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

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

Issue 5328346325975040: Catch errors in the onBeforeRequest handler (Closed)
Patch Set: Created March 20, 2014, 3: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 | « no previous file | no next file » | no next file with comments »
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 <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 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 return new Frame({id: frame.parent, tab: this._tab}); 424 return new Frame({id: frame.parent, tab: this._tab});
425 } 425 }
426 } 426 }
427 }; 427 };
428 428
429 429
430 /* Web request blocking */ 430 /* Web request blocking */
431 431
432 chrome.webRequest.onBeforeRequest.addListener(function(details) 432 chrome.webRequest.onBeforeRequest.addListener(function(details)
433 { 433 {
434 // the high-level code isn't interested in requests that aren't related 434 try
435 // to a tab and since those can only be handled in Chrome, we ignore
436 // them here instead of in the browser independent high-level code.
437 if (details.tabId == -1)
438 return;
439
440 var tab = new Tab({id: details.tabId});
441 var frames = framesOfTabs.get(tab);
442
443 if (!frames)
444 { 435 {
445 frames = []; 436 // the high-level code isn't interested in requests that aren't related
446 framesOfTabs.set(tab, frames); 437 // to a tab and since those can only be handled in Chrome, we ignore
447 438 // them here instead of in the browser independent high-level code.
448 // assume that the first request belongs to the top frame. Chrome 439 if (details.tabId == -1)
449 // may give the top frame the type "object" instead of "main_frame".
450 // https://code.google.com/p/chromium/issues/detail?id=281711
451 if (frameId == 0)
452 details.type = "main_frame";
453 }
454
455 var frameId;
456 if (details.type == "main_frame" || details.type == "sub_frame")
457 {
458 frameId = details.parentFrameId;
459 frames[details.frameId] = {url: details.url, parent: frameId};
460
461 // the high-level code isn't interested in top frame requests and
462 // since those can only be handled in Chrome, we ignore them here
463 // instead of in the browser independent high-level code.
464 if (details.type == "main_frame")
465 return;
466 }
467 else
468 frameId = details.frameId;
469
470 if (!(frameId in frames))
471 {
472 // the high-level code relies on the frame. So ignore the request if we
473 // don't even know the top-level frame. That can happen for example when
474 // the extension was just (re)loaded.
475 if (!(0 in frames))
476 return; 440 return;
477 441
478 // however when the src of the frame is a javascript: or data: URL, we 442 var tab = new Tab({id: details.tabId});
479 // don't know the frame either. But since we know the top-level frame we 443 var frames = framesOfTabs.get(tab);
480 // can just pretend that we are in the top-level frame, in order to have 444
481 // at least most domain-based filter rules working. 445 if (!frames)
482 frameId = 0; 446 {
483 if (details.type == "sub_frame") 447 frames = [];
484 frames[details.frameId].parent = frameId; 448 framesOfTabs.set(tab, frames);
449
450 // assume that the first request belongs to the top frame. Chrome
451 // may give the top frame the type "object" instead of "main_frame".
452 // https://code.google.com/p/chromium/issues/detail?id=281711
453 if (frameId == 0)
454 details.type = "main_frame";
455 }
456
457 var frameId;
458 if (details.type == "main_frame" || details.type == "sub_frame")
459 {
460 frameId = details.parentFrameId;
461 frames[details.frameId] = {url: details.url, parent: frameId};
462
463 // the high-level code isn't interested in top frame requests and
464 // since those can only be handled in Chrome, we ignore them here
465 // instead of in the browser independent high-level code.
466 if (details.type == "main_frame")
467 return;
468 }
469 else
470 frameId = details.frameId;
471
472 if (!(frameId in frames))
473 {
474 // the high-level code relies on the frame. So ignore the request if we
475 // don't even know the top-level frame. That can happen for example when
476 // the extension was just (re)loaded.
477 if (!(0 in frames))
478 return;
479
480 // however when the src of the frame is a javascript: or data: URL, we
481 // don't know the frame either. But since we know the top-level frame we
482 // can just pretend that we are in the top-level frame, in order to have
483 // at least most domain-based filter rules working.
484 frameId = 0;
485 if (details.type == "sub_frame")
486 frames[details.frameId].parent = frameId;
487 }
488
489 var frame = new Frame({id: frameId, tab: tab});
490
491 for (var i = 0; i < ext.webRequest.onBeforeRequest._listeners.length; i++)
492 {
493 if (ext.webRequest.onBeforeRequest._listeners[i](details.url, details.ty pe, tab, frame) === false)
494 return {cancel: true};
495 }
485 } 496 }
486 497 catch (e)
487 var frame = new Frame({id: frameId, tab: tab});
488
489 for (var i = 0; i < ext.webRequest.onBeforeRequest._listeners.length; i++)
490 { 498 {
491 if (ext.webRequest.onBeforeRequest._listeners[i](details.url, details.type , tab, frame) === false) 499 // recent versions of Chrome cancel the request when an error occurs
492 return {cancel: true}; 500 // in the onBeforeRequest listener. However in our case it is more likely
501 // and worse for the user, to block legit requests, than potentially
502 // letting some ads through, if an error occured.
503 console.error(e);
493 } 504 }
494 }, {urls: ["<all_urls>"]}, ["blocking"]); 505 }, {urls: ["<all_urls>"]}, ["blocking"]);
495 506
496 507
497 /* API */ 508 /* API */
498 509
499 ext.windows = { 510 ext.windows = {
500 getAll: function(callback) 511 getAll: function(callback)
501 { 512 {
502 chrome.windows.getAll(function(windows) 513 chrome.windows.getAll(function(windows)
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 if (isContextMenuHidden) 590 if (isContextMenuHidden)
580 return; 591 return;
581 592
582 chrome.contextMenus.removeAll(); 593 chrome.contextMenus.removeAll();
583 isContextMenuHidden = true; 594 isContextMenuHidden = true;
584 } 595 }
585 }; 596 };
586 597
587 ext.onMessage = new BackgroundMessageEventTarget(); 598 ext.onMessage = new BackgroundMessageEventTarget();
588 })(); 599 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld