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

Delta Between Two Patch Sets: ext/background.js

Issue 29418659: Issue 5130 - Changes listener to register <all_urls> and filter non ws http requests (Closed)
Left Patch Set: Created April 20, 2017, 7:18 p.m.
Right Patch Set: pick nit, move changes to below high level bail out Created April 20, 2017, 8:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 if (parentFrameId != -1) 556 if (parentFrameId != -1)
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 // Filter out requests from non web protocols since Chrome version
567 // 58 added support for webSocket connections to the webRequests API
Jon Sonesen 2017/04/20 19:19:58 this comment should likely be improved. perhaps we
Sebastian Noack 2017/04/20 19:34:15 "Filter out requests from non web protocols. Ideal
Jon Sonesen 2017/04/20 19:53:06 Acknowledged. Thank you, I think that is good.
568 let blockURLs = new RegExp("https?|wss?");
569 let url = new URL(details.url);
Sebastian Noack 2017/04/20 19:34:15 We are now creating two URL objects, one to valida
Jon Sonesen 2017/04/20 19:53:06 Ah, sorry about that.
570 if (!blockURLs.exec(url.protocol))
Sebastian Noack 2017/04/20 19:34:15 Since the regular expression doesn't make the code
Sebastian Noack 2017/04/20 19:36:22 I accidentally inverted the logic, should be of co
Jon Sonesen 2017/04/20 19:53:06 Yeah, happy to use this instead.
571 {
Sebastian Noack 2017/04/20 19:34:15 We generally omit the braces if there is only one
Jon Sonesen 2017/04/20 19:53:06 Done.
572 return {cancel: false};
Sebastian Noack 2017/04/20 19:34:15 For consistency with the other bailout condition b
Jon Sonesen 2017/04/20 19:53:06 Done.
573 }
574 // 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
575 // related to a tab or requests loading a top-level document, 567 // related to a tab or requests loading a top-level document,
576 // those should never be blocked. 568 // those should never be blocked.
577 if (details.tabId == -1 || details.type == "main_frame") 569 if (details.tabId == -1 || details.type == "main_frame")
570 return;
571
572 // Filter out requests from non web protocols. Ideally, we'd explicitly
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
575 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket
576 // protocol and is causing an error if it is given.
577 let url = new URL(details.url);
578 if (url.protocol != "http:" && url.protocol != "https:" &&
579 url.protocol != "ws:" && url.protocol != "wss:")
578 return; 580 return;
579 581
580 // We are looking for the frame that contains the element which 582 // We are looking for the frame that contains the element which
581 // has triggered this request. For most requests (e.g. images) we 583 // has triggered this request. For most requests (e.g. images) we
582 // 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
583 // (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.
584 let {frameId, type} = details; 586 let {frameId, type} = details;
585 if (type == "sub_frame") 587 if (type == "sub_frame")
586 { 588 {
587 frameId = details.parentFrameId; 589 frameId = details.parentFrameId;
588 type = "SUBDOCUMENT"; 590 type = "SUBDOCUMENT";
589 } 591 }
590 592
591 let frame = ext.getFrame(details.tabId, frameId); 593 let frame = ext.getFrame(details.tabId, frameId);
592 if (frame) 594 if (frame)
593 { 595 {
594 let results = ext.webRequest.onBeforeRequest._dispatch( 596 let results = ext.webRequest.onBeforeRequest._dispatch(
595 new URL(details.url), 597 url, type.toUpperCase(), new Page({id: details.tabId}), frame
596 type.toUpperCase(),
597 new Page({id: details.tabId}),
598 frame
599 ); 598 );
600 599
601 if (results.indexOf(false) != -1) 600 if (results.indexOf(false) != -1)
602 return {cancel: true}; 601 return {cancel: true};
603 } 602 }
604 }, {urls: ["<all_urls>"]}, ["blocking"]); 603 }, {urls: ["<all_urls>"]}, ["blocking"]);
605 604
606 605
607 /* Message passing */ 606 /* Message passing */
608 607
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 ext.windows = { 731 ext.windows = {
733 create(createData, callback) 732 create(createData, callback)
734 { 733 {
735 chrome.windows.create(createData, createdWindow => 734 chrome.windows.create(createData, createdWindow =>
736 { 735 {
737 afterTabLoaded(callback)(createdWindow.tabs[0]); 736 afterTabLoaded(callback)(createdWindow.tabs[0]);
738 }); 737 });
739 } 738 }
740 }; 739 };
741 }()); 740 }());
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld