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

Unified Diff: inject.preload.js

Issue 29607555: Issue 6030 - Ensure RTCPeerConnection exists before wrapping it (Closed)
Patch Set: Wrap WebRTC wrapping code in the if block Created Nov. 14, 2017, 11:32 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: inject.preload.js
diff --git a/inject.preload.js b/inject.preload.js
index 109f31ab1f88b79f5e3ef5d5d34423e8ed7e53aa..8c1e312cdc4cd17fe800810dd2157f2e3e472457 100644
--- a/inject.preload.js
+++ b/inject.preload.js
@@ -225,167 +225,174 @@ function injected(eventName, injectedIntoContentWindow)
*/
let RealRTCPeerConnection = window.RTCPeerConnection ||
window.webkitRTCPeerConnection;
- let closeRTCPeerConnection = Function.prototype.call.bind(
- RealRTCPeerConnection.prototype.close
- );
- let RealArray = Array;
- let RealString = String;
- let {create: createObject, defineProperty} = Object;
-
- function normalizeUrl(url)
- {
- if (typeof url != "undefined")
- return RealString(url);
- }
- function safeCopyArray(originalArray, transform)
+ // Firefox has the option (media.peerconnection.enabled) to disable WebRTC
+ // in which case RealRTCPeerConnection is undefined.
+ if (typeof RealRTCPeerConnection != "undefined")
{
- if (originalArray == null || typeof originalArray != "object")
- return originalArray;
+ let closeRTCPeerConnection = Function.prototype.call.bind(
+ RealRTCPeerConnection.prototype.close
+ );
+ let RealArray = Array;
+ let RealString = String;
+ let {create: createObject, defineProperty} = Object;
- let safeArray = RealArray(originalArray.length);
- for (let i = 0; i < safeArray.length; i++)
+ let normalizeUrl = url =>
kzar 2017/11/14 11:36:35 This change - and several like it below - are nece
{
- defineProperty(safeArray, i, {
- configurable: false, enumerable: false, writable: false,
- value: transform(originalArray[i])
- });
- }
- defineProperty(safeArray, "length", {
- configurable: false, enumerable: false, writable: false,
- value: safeArray.length
- });
- return safeArray;
- }
+ if (typeof url != "undefined")
+ return RealString(url);
+ };
- // It would be much easier to use the .getConfiguration method to obtain
- // the normalized and safe configuration from the RTCPeerConnection
- // instance. Unfortunately its not implemented as of Chrome unstable 59.
- // See https://www.chromestatus.com/feature/5271355306016768
- function protectConfiguration(configuration)
- {
- if (configuration == null || typeof configuration != "object")
- return configuration;
+ let safeCopyArray = (originalArray, transform) =>
+ {
+ if (originalArray == null || typeof originalArray != "object")
+ return originalArray;
- let iceServers = safeCopyArray(
- configuration.iceServers,
- iceServer =>
+ let safeArray = RealArray(originalArray.length);
+ for (let i = 0; i < safeArray.length; i++)
{
- let {url, urls} = iceServer;
-
- // RTCPeerConnection doesn't iterate through pseudo Arrays of urls.
- if (typeof urls != "undefined" && !(urls instanceof RealArray))
- urls = [urls];
-
- return createObject(iceServer, {
- url: {
- configurable: false, enumerable: false, writable: false,
- value: normalizeUrl(url)
- },
- urls: {
- configurable: false, enumerable: false, writable: false,
- value: safeCopyArray(urls, normalizeUrl)
- }
+ defineProperty(safeArray, i, {
+ configurable: false, enumerable: false, writable: false,
+ value: transform(originalArray[i])
});
}
- );
-
- return createObject(configuration, {
- iceServers: {
+ defineProperty(safeArray, "length", {
configurable: false, enumerable: false, writable: false,
- value: iceServers
- }
- });
- }
+ value: safeArray.length
+ });
+ return safeArray;
+ };
- function checkUrl(peerconnection, url)
- {
- checkRequest("webrtc", url, blocked =>
+ // It would be much easier to use the .getConfiguration method to obtain
+ // the normalized and safe configuration from the RTCPeerConnection
+ // instance. Unfortunately its not implemented as of Chrome unstable 59.
+ // See https://www.chromestatus.com/feature/5271355306016768
+ let protectConfiguration = configuration =>
{
- if (blocked)
- {
- // Calling .close() throws if already closed.
- try
+ if (configuration == null || typeof configuration != "object")
+ return configuration;
+
+ let iceServers = safeCopyArray(
+ configuration.iceServers,
+ iceServer =>
{
- closeRTCPeerConnection(peerconnection);
+ let {url, urls} = iceServer;
+
+ // RTCPeerConnection doesn't iterate through pseudo Arrays of urls.
+ if (typeof urls != "undefined" && !(urls instanceof RealArray))
+ urls = [urls];
+
+ return createObject(iceServer, {
+ url: {
+ configurable: false, enumerable: false, writable: false,
+ value: normalizeUrl(url)
+ },
+ urls: {
+ configurable: false, enumerable: false, writable: false,
+ value: safeCopyArray(urls, normalizeUrl)
+ }
+ });
}
- catch (e) {}
- }
- });
- }
+ );
- function checkConfiguration(peerconnection, configuration)
- {
- if (configuration && configuration.iceServers)
+ return createObject(configuration, {
+ iceServers: {
+ configurable: false, enumerable: false, writable: false,
+ value: iceServers
+ }
+ });
+ };
+
+ let checkUrl = (peerconnection, url) =>
{
- for (let i = 0; i < configuration.iceServers.length; i++)
+ checkRequest("webrtc", url, blocked =>
{
- let iceServer = configuration.iceServers[i];
- if (iceServer)
+ if (blocked)
{
- if (iceServer.url)
- checkUrl(peerconnection, iceServer.url);
-
- if (iceServer.urls)
+ // Calling .close() throws if already closed.
+ try
{
- for (let j = 0; j < iceServer.urls.length; j++)
- checkUrl(peerconnection, iceServer.urls[j]);
+ closeRTCPeerConnection(peerconnection);
}
+ catch (e) {}
}
- }
- }
- }
-
- // Chrome unstable (tested with 59) has already implemented
- // setConfiguration, so we need to wrap that if it exists too.
- // https://www.chromestatus.com/feature/5596193748942848
- if (RealRTCPeerConnection.prototype.setConfiguration)
- {
- let realSetConfiguration = Function.prototype.call.bind(
- RealRTCPeerConnection.prototype.setConfiguration
- );
+ });
+ };
- RealRTCPeerConnection.prototype.setConfiguration = function(configuration)
+ let checkConfiguration = (peerconnection, configuration) =>
{
- configuration = protectConfiguration(configuration);
-
- // Call the real method first, so that validates the configuration for
- // us. Also we might as well since checkRequest is asynchronous anyway.
- realSetConfiguration(this, configuration);
- checkConfiguration(this, configuration);
+ if (configuration && configuration.iceServers)
+ {
+ for (let i = 0; i < configuration.iceServers.length; i++)
+ {
+ let iceServer = configuration.iceServers[i];
+ if (iceServer)
+ {
+ if (iceServer.url)
+ checkUrl(peerconnection, iceServer.url);
+
+ if (iceServer.urls)
+ {
+ for (let j = 0; j < iceServer.urls.length; j++)
+ checkUrl(peerconnection, iceServer.urls[j]);
+ }
+ }
+ }
+ }
};
- }
- function WrappedRTCPeerConnection(...args)
- {
- if (!(this instanceof WrappedRTCPeerConnection))
- return RealRTCPeerConnection();
+ // Chrome unstable (tested with 59) has already implemented
+ // setConfiguration, so we need to wrap that if it exists too.
+ // https://www.chromestatus.com/feature/5596193748942848
+ if (RealRTCPeerConnection.prototype.setConfiguration)
+ {
+ let realSetConfiguration = Function.prototype.call.bind(
+ RealRTCPeerConnection.prototype.setConfiguration
+ );
- let configuration = protectConfiguration(args[0]);
+ RealRTCPeerConnection.prototype.setConfiguration = function(configuration)
+ {
+ configuration = protectConfiguration(configuration);
- // Since the old webkitRTCPeerConnection constructor takes an optional
- // second argument we need to take care to pass that through. Necessary
- // for older versions of Chrome such as 49.
- let constraints = undefined;
- if (args.length > 1)
- constraints = args[1];
+ // Call the real method first, so that validates the configuration for
+ // us. Also we might as well since checkRequest is asynchronous anyway.
+ realSetConfiguration(this, configuration);
+ checkConfiguration(this, configuration);
+ };
+ }
- let peerconnection = new RealRTCPeerConnection(configuration, constraints);
- checkConfiguration(peerconnection, configuration);
- return peerconnection;
- }
+ let WrappedRTCPeerConnection = function(...args)
+ {
+ if (!(this instanceof WrappedRTCPeerConnection))
+ return RealRTCPeerConnection();
+
+ let configuration = protectConfiguration(args[0]);
+
+ // Since the old webkitRTCPeerConnection constructor takes an optional
+ // second argument we need to take care to pass that through. Necessary
+ // for older versions of Chrome such as 49.
+ let constraints = undefined;
+ if (args.length > 1)
+ constraints = args[1];
+
+ let peerconnection = new RealRTCPeerConnection(configuration,
+ constraints);
+ checkConfiguration(peerconnection, configuration);
+ return peerconnection;
+ };
- WrappedRTCPeerConnection.prototype = RealRTCPeerConnection.prototype;
+ WrappedRTCPeerConnection.prototype = RealRTCPeerConnection.prototype;
- let boundWrappedRTCPeerConnection = WrappedRTCPeerConnection.bind();
- copyProperties(RealRTCPeerConnection, boundWrappedRTCPeerConnection,
- ["generateCertificate", "name", "prototype"]);
- RealRTCPeerConnection.prototype.constructor = boundWrappedRTCPeerConnection;
+ let boundWrappedRTCPeerConnection = WrappedRTCPeerConnection.bind();
+ copyProperties(RealRTCPeerConnection, boundWrappedRTCPeerConnection,
+ ["generateCertificate", "name", "prototype"]);
+ RealRTCPeerConnection.prototype.constructor = boundWrappedRTCPeerConnection;
- if ("RTCPeerConnection" in window)
- window.RTCPeerConnection = boundWrappedRTCPeerConnection;
- if ("webkitRTCPeerConnection" in window)
- window.webkitRTCPeerConnection = boundWrappedRTCPeerConnection;
+ if ("RTCPeerConnection" in window)
+ window.RTCPeerConnection = boundWrappedRTCPeerConnection;
+ if ("webkitRTCPeerConnection" in window)
+ window.webkitRTCPeerConnection = boundWrappedRTCPeerConnection;
+ }
}
if (document instanceof HTMLDocument)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld