browser(firefox): support Browser.setProxy method in juggler (#2464)

This lets us support network proxies per browser context.
This commit is contained in:
Andrey Lushnikov 2020-06-04 08:52:43 -07:00 committed by GitHub
parent d5c992e1db
commit 3c9699dc7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 1 deletions

View file

@ -1 +1 @@
1101 1102

View file

@ -19,6 +19,8 @@ const Cm = Components.manager;
const CC = Components.Constructor; const CC = Components.Constructor;
const helper = new Helper(); const helper = new Helper();
const UINT32_MAX = Math.pow(2, 32)-1;
const BinaryInputStream = CC('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream', 'setInputStream'); const BinaryInputStream = CC('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream', 'setInputStream');
const BinaryOutputStream = CC('@mozilla.org/binaryoutputstream;1', 'nsIBinaryOutputStream', 'setOutputStream'); const BinaryOutputStream = CC('@mozilla.org/binaryoutputstream;1', 'nsIBinaryOutputStream', 'setOutputStream');
const StorageStream = CC('@mozilla.org/storagestream;1', 'nsIStorageStream', 'init'); const StorageStream = CC('@mozilla.org/storagestream;1', 'nsIStorageStream', 'init');
@ -153,6 +155,31 @@ class NetworkObserver {
this._postAuthChannelIdToRequestId = new Map(); // pre-auth id => post-auth id this._postAuthChannelIdToRequestId = new Map(); // pre-auth id => post-auth id
this._bodyListeners = new Map(); // channel id => ResponseBodyListener. this._bodyListeners = new Map(); // channel id => ResponseBodyListener.
const protocolProxyService = Cc['@mozilla.org/network/protocol-proxy-service;1'].getService();
this._channelProxyFilter = {
QueryInterface: ChromeUtils.generateQI([Ci.nsIProtocolProxyChannelFilter]),
applyFilter: (channel, defaultProxyInfo, proxyFilter) => {
const originAttributes = channel.loadInfo && channel.loadInfo.originAttributes;
const browserContext = originAttributes ? this._targetRegistry.browserContextForUserContextId(originAttributes.userContextId) : null;
const proxy = browserContext ? browserContext.proxy : null;
if (!proxy) {
proxyFilter.onProxyFilterResult(defaultProxyInfo);
return;
}
proxyFilter.onProxyFilterResult(protocolProxyService.newProxyInfo(
proxy.type,
proxy.host,
proxy.port,
'', /* aProxyAuthorizationHeader */
'', /* aConnectionIsolationKey */
0, /* aFlags */
UINT32_MAX, /* aFailoverTimeout */
null, /* failover proxy */
));
},
};
protocolProxyService.registerChannelFilter(this._channelProxyFilter, 0 /* position */);
this._channelSink = { this._channelSink = {
QueryInterface: ChromeUtils.generateQI([Ci.nsIChannelEventSink]), QueryInterface: ChromeUtils.generateQI([Ci.nsIChannelEventSink]),
asyncOnChannelRedirect: (oldChannel, newChannel, flags, callback) => { asyncOnChannelRedirect: (oldChannel, newChannel, flags, callback) => {

View file

@ -252,6 +252,10 @@ class TargetRegistry {
return this._browserContextIdToBrowserContext.get(browserContextId); return this._browserContextIdToBrowserContext.get(browserContextId);
} }
browserContextForUserContextId(userContextId) {
return this._userContextIdToBrowserContext.get(userContextId);
}
async newPage({browserContextId}) { async newPage({browserContextId}) {
let window; let window;
let created = false; let created = false;
@ -458,6 +462,7 @@ class BrowserContext {
this._registry._browserContextIdToBrowserContext.set(this.browserContextId, this); this._registry._browserContextIdToBrowserContext.set(this.browserContextId, this);
this._registry._userContextIdToBrowserContext.set(this.userContextId, this); this._registry._userContextIdToBrowserContext.set(this.userContextId, this);
this.removeOnDetach = removeOnDetach; this.removeOnDetach = removeOnDetach;
this.proxy = undefined;
this.extraHTTPHeaders = undefined; this.extraHTTPHeaders = undefined;
this.httpCredentials = undefined; this.httpCredentials = undefined;
this.requestInterceptionEnabled = undefined; this.requestInterceptionEnabled = undefined;

View file

@ -151,6 +151,10 @@ class BrowserHandler {
this._targetRegistry.browserContextForId(browserContextId).httpCredentials = nullToUndefined(credentials); this._targetRegistry.browserContextForId(browserContextId).httpCredentials = nullToUndefined(credentials);
} }
async setProxy({browserContextId, type, host, port}) {
this._targetRegistry.browserContextForId(browserContextId).proxy = { type, host, port };
}
setRequestInterception({browserContextId, enabled}) { setRequestInterception({browserContextId, enabled}) {
this._targetRegistry.browserContextForId(browserContextId).requestInterceptionEnabled = enabled; this._targetRegistry.browserContextForId(browserContextId).requestInterceptionEnabled = enabled;
} }

View file

@ -259,6 +259,14 @@ const Browser = {
headers: t.Array(networkTypes.HTTPHeader), headers: t.Array(networkTypes.HTTPHeader),
}, },
}, },
'setProxy': {
params: {
browserContextId: t.Optional(t.String),
type: t.Enum(['http', 'https', 'socks', 'socks4']),
host: t.String,
port: t.Number,
},
},
'setHTTPCredentials': { 'setHTTPCredentials': {
params: { params: {
browserContextId: t.Optional(t.String), browserContextId: t.Optional(t.String),