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

Delta Between Two Patch Sets: inject.preload.js

Issue 29721716: Issue 6473 - Remove WebSocket wrapper (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Created March 13, 2018, 4:33 p.m.
Right Patch Set: Address comments to Patch Set 1 Created March 13, 2018, 6: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 | lib/requestBlocker.js » ('j') | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 "use strict"; 18 "use strict";
19 19
20 let randomEventName = "abp-request-" + Math.random().toString(36).substr(2); 20 let randomEventName = "abp-request-" + Math.random().toString(36).substr(2);
21 21
22 // Proxy "should we block?" messages from checkRequest inside the injected 22 // Proxy "should we block?" messages from checkRequest inside the injected
23 // code to the background page and back again. 23 // code to the background page and back again.
24 document.addEventListener(randomEventName, event => 24 document.addEventListener(randomEventName, event =>
25 { 25 {
26 let {url, requestType} = event.detail; 26 let {url} = event.detail;
27 27
28 browser.runtime.sendMessage({ 28 browser.runtime.sendMessage({
29 type: "request.blockedByWrapper", 29 type: "request.blockedByRTCWrapper",
30 requestType,
31 url 30 url
32 }, block => 31 }, block =>
33 { 32 {
34 document.dispatchEvent(new CustomEvent( 33 document.dispatchEvent(new CustomEvent(
35 randomEventName + "-" + requestType + "-" + url, {detail: block} 34 randomEventName + "-" + url, {detail: block}
36 )); 35 ));
37 }); 36 });
38 }); 37 });
39 38
40 function injected(eventName, injectedIntoContentWindow) 39 function injected(eventName, injectedIntoContentWindow)
41 { 40 {
42 let checkRequest; 41 let checkRequest;
43 42
44 /* 43 /*
45 * Frame context wrapper 44 * Frame context wrapper
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 configurable: true, enumerable: true, get() 130 configurable: true, enumerable: true, get()
132 { 131 {
133 let thisShadow = shadowRoot(this); 132 let thisShadow = shadowRoot(this);
134 return thisShadow == ourShadowRoot ? null : thisShadow; 133 return thisShadow == ourShadowRoot ? null : thisShadow;
135 } 134 }
136 }); 135 });
137 } 136 }
138 } 137 }
139 138
140 /* 139 /*
141 * Shared request checking code, used by the RTCPeerConnection wrapper. 140 * RTCPeerConnection wrapper
Sebastian Noack 2018/03/13 17:43:01 Well, it's no longer shared. We probably should ju
Manish Jethani 2018/03/13 18:05:11 Done.
141 *
142 * The webRequest API in Chrome does not yet allow the blocking of
143 * WebRTC connections.
144 * See https://bugs.chromium.org/p/chromium/issues/detail?id=707683
142 */ 145 */
143 let RealCustomEvent = window.CustomEvent; 146 let RealCustomEvent = window.CustomEvent;
144 147
145 // If we've been injected into a frame via contentWindow then we can simply 148 // If we've been injected into a frame via contentWindow then we can simply
146 // grab the copy of checkRequest left for us by the parent document. Otherwise 149 // grab the copy of checkRequest left for us by the parent document. Otherwise
147 // we need to set it up now, along with the event handling functions. 150 // we need to set it up now, along with the event handling functions.
148 if (injectedIntoContentWindow) 151 if (injectedIntoContentWindow)
149 checkRequest = window[eventName]; 152 checkRequest = window[eventName];
150 else 153 else
151 { 154 {
152 let addEventListener = document.addEventListener.bind(document); 155 let addEventListener = document.addEventListener.bind(document);
153 let dispatchEvent = document.dispatchEvent.bind(document); 156 let dispatchEvent = document.dispatchEvent.bind(document);
154 let removeEventListener = document.removeEventListener.bind(document); 157 let removeEventListener = document.removeEventListener.bind(document);
155 checkRequest = (requestType, url, callback) => 158 checkRequest = (url, callback) =>
156 { 159 {
157 let incomingEventName = eventName + "-" + requestType + "-" + url; 160 let incomingEventName = eventName + "-" + url;
158 161
159 function listener(event) 162 function listener(event)
160 { 163 {
161 callback(event.detail); 164 callback(event.detail);
162 removeEventListener(incomingEventName, listener); 165 removeEventListener(incomingEventName, listener);
163 } 166 }
164 addEventListener(incomingEventName, listener); 167 addEventListener(incomingEventName, listener);
165 168
166 dispatchEvent(new RealCustomEvent(eventName, 169 dispatchEvent(new RealCustomEvent(eventName, {detail: {url}}));
167 {detail: {url, requestType}}));
168 }; 170 };
169 } 171 }
170 172
171 // Only to be called before the page's code, not hardened. 173 // Only to be called before the page's code, not hardened.
172 function copyProperties(src, dest, properties) 174 function copyProperties(src, dest, properties)
173 { 175 {
174 for (let name of properties) 176 for (let name of properties)
175 { 177 {
176 if (src.hasOwnProperty(name)) 178 if (src.hasOwnProperty(name))
177 { 179 {
178 Object.defineProperty(dest, name, 180 Object.defineProperty(dest, name,
179 Object.getOwnPropertyDescriptor(src, name)); 181 Object.getOwnPropertyDescriptor(src, name));
180 } 182 }
181 } 183 }
182 } 184 }
183 185
184 /*
185 * RTCPeerConnection wrapper
186 *
187 * The webRequest API in Chrome does not yet allow the blocking of
188 * WebRTC connections.
189 * See https://bugs.chromium.org/p/chromium/issues/detail?id=707683
190 */
191 let RealRTCPeerConnection = window.RTCPeerConnection || 186 let RealRTCPeerConnection = window.RTCPeerConnection ||
192 window.webkitRTCPeerConnection; 187 window.webkitRTCPeerConnection;
193 188
194 // Firefox has the option (media.peerconnection.enabled) to disable WebRTC 189 // Firefox has the option (media.peerconnection.enabled) to disable WebRTC
195 // in which case RealRTCPeerConnection is undefined. 190 // in which case RealRTCPeerConnection is undefined.
196 if (typeof RealRTCPeerConnection != "undefined") 191 if (typeof RealRTCPeerConnection != "undefined")
197 { 192 {
198 let closeRTCPeerConnection = Function.prototype.call.bind( 193 let closeRTCPeerConnection = Function.prototype.call.bind(
199 RealRTCPeerConnection.prototype.close 194 RealRTCPeerConnection.prototype.close
200 ); 195 );
201 let RealArray = Array; 196 let RealArray = Array;
202 let RealString = String; 197 let RealString = String;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 return createObject(configuration, { 258 return createObject(configuration, {
264 iceServers: { 259 iceServers: {
265 configurable: false, enumerable: false, writable: false, 260 configurable: false, enumerable: false, writable: false,
266 value: iceServers 261 value: iceServers
267 } 262 }
268 }); 263 });
269 }; 264 };
270 265
271 let checkUrl = (peerconnection, url) => 266 let checkUrl = (peerconnection, url) =>
272 { 267 {
273 checkRequest("webrtc", url, blocked => 268 checkRequest(url, blocked =>
274 { 269 {
275 if (blocked) 270 if (blocked)
276 { 271 {
277 // Calling .close() throws if already closed. 272 // Calling .close() throws if already closed.
278 try 273 try
279 { 274 {
280 closeRTCPeerConnection(peerconnection); 275 closeRTCPeerConnection(peerconnection);
281 } 276 }
282 catch (e) {} 277 catch (e) {}
283 } 278 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 // Firefox 58 only bypasses site CSPs when assigning to 'src'. 368 // Firefox 58 only bypasses site CSPs when assigning to 'src'.
374 let url = URL.createObjectURL(new Blob([ 369 let url = URL.createObjectURL(new Blob([
375 "(" + injected + ")('" + randomEventName + "');" 370 "(" + injected + ")('" + randomEventName + "');"
376 ])); 371 ]));
377 script.src = url; 372 script.src = url;
378 document.documentElement.appendChild(script); 373 document.documentElement.appendChild(script);
379 document.documentElement.removeChild(script); 374 document.documentElement.removeChild(script);
380 URL.revokeObjectURL(url); 375 URL.revokeObjectURL(url);
381 } 376 }
382 } 377 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld