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

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

Issue 5108699911684096: Issue 2569 - Leave errors in chrome.webRequest.onBeforeRequest listener unhandled (Closed)
Patch Set: Created May 21, 2015, 10:27 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 | « 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 <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
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 if (parentFrameId != -1) 395 if (parentFrameId != -1)
396 frames[details[i].frameId].parent = frames[parentFrameId]; 396 frames[details[i].frameId].parent = frames[parentFrameId];
397 } 397 }
398 } 398 }
399 }); 399 });
400 }); 400 });
401 }); 401 });
402 402
403 chrome.webRequest.onBeforeRequest.addListener(function(details) 403 chrome.webRequest.onBeforeRequest.addListener(function(details)
404 { 404 {
405 try 405 // the high-level code isn't interested in requests that aren't related
Sebastian Noack 2015/05/21 10:28:44 Yeah, only indentation changed here.
406 // to a tab and since those can only be handled in Chrome, we ignore
407 // them here instead of in the browser independent high-level code.
408 if (details.tabId == -1)
409 return;
410
411 var isMainFrame = details.type == "main_frame" || (
412
413 // assume that the first request belongs to the top frame. Chrome
414 // may give the top frame the type "object" instead of "main_frame".
415 // https://code.google.com/p/chromium/issues/detail?id=281711
416 details.frameId == 0 && !(details.tabId in framesOfTabs)
417 );
418
419 var frames = null;
420 if (!isMainFrame)
421 frames = framesOfTabs[details.tabId];
422 if (!frames)
423 frames = framesOfTabs[details.tabId] = Object.create(null);
424
425 var frame = null;
426 var url = new URL(details.url);
427 if (!isMainFrame)
406 { 428 {
407 // the high-level code isn't interested in requests that aren't related 429 // we are looking for the frame that contains the element that
408 // to a tab and since those can only be handled in Chrome, we ignore 430 // is about to load, however if a frame is loading the surrounding
409 // them here instead of in the browser independent high-level code. 431 // frame is indicated by parentFrameId instead of frameId
410 if (details.tabId == -1) 432 var frameId;
411 return; 433 var requestType;
412 434 if (details.type == "sub_frame")
413 var isMainFrame = details.type == "main_frame" || (
414
415 // assume that the first request belongs to the top frame. Chrome
416 // may give the top frame the type "object" instead of "main_frame".
417 // https://code.google.com/p/chromium/issues/detail?id=281711
418 details.frameId == 0 && !(details.tabId in framesOfTabs)
419 );
420
421 var frames = null;
422 if (!isMainFrame)
423 frames = framesOfTabs[details.tabId];
424 if (!frames)
425 frames = framesOfTabs[details.tabId] = Object.create(null);
426
427 var frame = null;
428 var url = new URL(details.url);
429 if (!isMainFrame)
430 { 435 {
431 // we are looking for the frame that contains the element that 436 frameId = details.parentFrameId;
432 // is about to load, however if a frame is loading the surrounding 437 requestType = "SUBDOCUMENT";
433 // frame is indicated by parentFrameId instead of frameId 438 }
434 var frameId; 439 else
435 var requestType; 440 {
436 if (details.type == "sub_frame") 441 frameId = details.frameId;
437 { 442 requestType = details.type.toUpperCase();
438 frameId = details.parentFrameId;
439 requestType = "SUBDOCUMENT";
440 }
441 else
442 {
443 frameId = details.frameId;
444 requestType = details.type.toUpperCase();
445 }
446
447 frame = frames[frameId] || frames[Object.keys(frames)[0]];
448
449 if (frame)
450 {
451 var results = ext.webRequest.onBeforeRequest._dispatch(
452 url,
453 requestType,
454 new Page({id: details.tabId}),
455 frame
456 );
457
458 if (results.indexOf(false) != -1)
459 return {cancel: true};
460 }
461 } 443 }
462 444
463 if (isMainFrame || details.type == "sub_frame") 445 frame = frames[frameId] || frames[Object.keys(frames)[0]];
464 frames[details.frameId] = {url: url, parent: frame}; 446
447 if (frame)
448 {
449 var results = ext.webRequest.onBeforeRequest._dispatch(
450 url,
451 requestType,
452 new Page({id: details.tabId}),
453 frame
454 );
455
456 if (results.indexOf(false) != -1)
457 return {cancel: true};
458 }
465 } 459 }
466 catch (e) 460
467 { 461 if (isMainFrame || details.type == "sub_frame")
468 // recent versions of Chrome cancel the request when an error occurs in 462 frames[details.frameId] = {url: url, parent: frame};
469 // the onBeforeRequest listener. However in our case it is preferred, to
470 // let potentially some ads through, rather than blocking legit requests.
471 console.error(e);
472 }
473 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]); 463 }, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]);
474 464
475 465
476 /* Message passing */ 466 /* Message passing */
477 467
478 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse ) 468 chrome.runtime.onMessage.addListener(function(message, rawSender, sendResponse )
479 { 469 {
480 var sender = {}; 470 var sender = {};
481 471
482 // Add "page" and "frame" if the message was sent by a content script. 472 // Add "page" and "frame" if the message was sent by a content script.
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 callback(new Page(tab)); 644 callback(new Page(tab));
655 } 645 }
656 else 646 else
657 { 647 {
658 ext.pages.open(optionsUrl, callback); 648 ext.pages.open(optionsUrl, callback);
659 } 649 }
660 }); 650 });
661 }); 651 });
662 }; 652 };
663 })(); 653 })();
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