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

Side by Side Diff: ext/background.js

Issue 29418679: Issue 5042 - Adds handling for requests which are not associated with browser tab (Closed)
Patch Set: Created April 20, 2017, 9:43 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 | lib/devtools.js » ('j') | lib/devtools.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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 546 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 frames[details[i].frameId].parent = frames[parentFrameId]; 557 frames[details[i].frameId].parent = frames[parentFrameId];
558 } 558 }
559 } 559 }
560 }); 560 });
561 }); 561 });
562 }); 562 });
563 563
564 chrome.webRequest.onBeforeRequest.addListener(details => 564 chrome.webRequest.onBeforeRequest.addListener(details =>
565 { 565 {
566 // The high-level code isn't interested in requests that aren't 566 // The high-level code isn't interested in requests that aren't
567 // related to a tab or requests loading a top-level document, 567 // related to a tab or requests loading a top-level document,
Sebastian Noack 2017/04/21 08:39:53 This comment needs to be adapted too.
Jon Sonesen 2017/04/24 08:40:55 Acknowledged.
568 // those should never be blocked. 568 // those should never be blocked.
569 if (details.tabId == -1 || details.type == "main_frame") 569 if (details.type == "main_frame")
570 return; 570 return;
571 571
572 // Filter out requests from non web protocols. Ideally, we'd explicitly 572 // Filter out requests from non web protocols. Ideally, we'd explicitly
573 // specify the protocols we are interested in (i.e. http://, https://, 573 // specify the protocols we are interested in (i.e. http://, https://,
574 // ws:// and wss://) with the url patterns, given below, when adding this 574 // ws:// and wss://) with the url patterns, given below, when adding this
575 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket 575 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
576 // protocol and is causing an error if it is given. 576 // protocol and is causing an error if it is given.
577 let url = new URL(details.url); 577 let url = new URL(details.url);
578 if (url.protocol != "http:" && url.protocol != "https:" && 578 if (url.protocol != "http:" && url.protocol != "https:" &&
579 url.protocol != "ws:" && url.protocol != "wss:") 579 url.protocol != "ws:" && url.protocol != "wss:")
580 return; 580 return;
581 581
582 // We are looking for the frame that contains the element which 582 // We are looking for the frame that contains the element which
583 // has triggered this request. For most requests (e.g. images) we 583 // has triggered this request. For most requests (e.g. images) we
584 // can just use the request's frame ID, but for subdocument requests 584 // can just use the request's frame ID, but for subdocument requests
585 // (e.g. iframes) we must instead use the request's parent frame ID. 585 // (e.g. iframes) we must instead use the request's parent frame ID.
586 let {frameId, type} = details;
Sebastian Noack 2017/04/21 08:39:53 If you remove the variable declaration here, the t
Jon Sonesen 2017/04/24 08:40:55 Acknowledged.
587 if (type == "sub_frame") 586 if (type == "sub_frame")
588 { 587 {
589 frameId = details.parentFrameId; 588 frameId = details.parentFrameId;
590 type = "SUBDOCUMENT"; 589 type = "SUBDOCUMENT";
591 } 590 }
592 591
593 let frame = ext.getFrame(details.tabId, frameId); 592 let {frameId, type} = details;
594 if (frame) 593
594 // Sometimes requests are not associated with a browser tab and
595 // in this case we want to still be able to view the url being called.
596 // However, since tabId's are assumed to be unique elsewhere in the code
Sebastian Noack 2017/04/21 08:39:53 IMO, the second sentence is rather confusing, and
Jon Sonesen 2017/04/24 08:40:55 Acknowledged.
597 // it seems safer to initialize frame and page as null and then only
598 // initiate a page, or fetch a frame if we know it will have a unique tabId.
599 let frame = null;
600 let page = null;
601 if (details.tabId != -1)
595 { 602 {
596 let results = ext.webRequest.onBeforeRequest._dispatch( 603 frame = ext.getFrame(details.tabId, frameId);
597 url, type.toUpperCase(), new Page({id: details.tabId}), frame 604 page = new Page({id: details.tabId});
598 ); 605 }
599 606
600 if (results.indexOf(false) != -1) 607 let results = ext.webRequest.onBeforeRequest._dispatch(
601 return {cancel: true}; 608 url, type.toUpperCase(), page, frame
602 } 609 );
610
611 if (results.indexOf(false) != -1)
Sebastian Noack 2017/04/21 08:39:53 Please use results.includes(false), which is equiv
Jon Sonesen 2017/04/24 08:40:56 Acknowledged.
612 return {cancel: true};
603 }, {urls: ["<all_urls>"]}, ["blocking"]); 613 }, {urls: ["<all_urls>"]}, ["blocking"]);
604 614
605 615
606 /* Message passing */ 616 /* Message passing */
607 617
608 chrome.runtime.onMessage.addListener((message, rawSender, sendResponse) => 618 chrome.runtime.onMessage.addListener((message, rawSender, sendResponse) =>
609 { 619 {
610 let sender = {}; 620 let sender = {};
611 621
612 // Add "page" and "frame" if the message was sent by a content script. 622 // Add "page" and "frame" if the message was sent by a content script.
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 ext.windows = { 741 ext.windows = {
732 create(createData, callback) 742 create(createData, callback)
733 { 743 {
734 chrome.windows.create(createData, createdWindow => 744 chrome.windows.create(createData, createdWindow =>
735 { 745 {
736 afterTabLoaded(callback)(createdWindow.tabs[0]); 746 afterTabLoaded(callback)(createdWindow.tabs[0]);
737 }); 747 });
738 } 748 }
739 }; 749 };
740 }()); 750 }());
OLDNEW
« no previous file with comments | « no previous file | lib/devtools.js » ('j') | lib/devtools.js » ('J')

Powered by Google App Engine
This is Rietveld