Skip to content

Commit

Permalink
browser(firefox): move user agent emulation to browser side (#4016)
Browse files Browse the repository at this point in the history
References #3995
  • Loading branch information
aslushnikov authored Sep 30, 2020
1 parent ab2714e commit 9743584
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions browser_patches/firefox/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1176
Changed: lushnikov@chromium.org Wed Sep 30 03:06:09 MDT 2020
1177
Changed: lushnikov@chromium.org Wed Sep 30 03:11:29 MDT 2020
12 changes: 12 additions & 0 deletions browser_patches/firefox/juggler/TargetRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class TargetRegistry {
if (!browserContext)
throw new Error(`Internal error: cannot find context for userContextId=${userContextId}`);
const target = new PageTarget(this, window, tab, browserContext, openerTarget);
target.updateUserAgent();
if (!hasExplicitSize)
target.updateViewportSize();
};
Expand Down Expand Up @@ -393,6 +394,10 @@ class PageTarget {
return this._browserContext;
}

updateUserAgent() {
this._linkedBrowser.browsingContext.customUserAgent = this._browserContext.defaultUserAgent;
}

async updateViewportSize() {
// Viewport size is defined by three arguments:
// 1. default size. Could be explicit if set as part of `window.open` call, e.g.
Expand Down Expand Up @@ -513,6 +518,7 @@ class BrowserContext {
this.ignoreHTTPSErrors = undefined;
this.downloadOptions = undefined;
this.defaultViewportSize = undefined;
this.defaultUserAgent = null;
this.screencastOptions = undefined;
this.scriptsToEvaluateOnNewDocument = [];
this.bindings = [];
Expand Down Expand Up @@ -559,6 +565,12 @@ class BrowserContext {
}
}

async setDefaultUserAgent(userAgent) {
this.defaultUserAgent = userAgent;
for (const page of this.pages)
page.updateUserAgent();
}

async setDefaultViewport(viewport) {
this.defaultViewportSize = viewport ? viewport.viewportSize : undefined;
const promises = Array.from(this.pages).map(page => page.updateViewportSize());
Expand Down
4 changes: 0 additions & 4 deletions browser_patches/firefox/juggler/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ const applySetting = {
Ci.nsIDocShell.ONLINE_OVERRIDE_ONLINE : Ci.nsIDocShell.ONLINE_OVERRIDE_OFFLINE;
},

userAgent: (userAgent) => {
docShell.browsingContext.customUserAgent = userAgent;
},

bypassCSP: (bypassCSP) => {
docShell.bypassCSPEnabled = bypassCSP;
},
Expand Down
2 changes: 1 addition & 1 deletion browser_patches/firefox/juggler/protocol/BrowserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class BrowserHandler {
}

async setUserAgentOverride({browserContextId, userAgent}) {
await this._targetRegistry.browserContextForId(browserContextId).applySetting('userAgent', nullToUndefined(userAgent));
await this._targetRegistry.browserContextForId(browserContextId).setDefaultUserAgent(userAgent);
}

async setBypassCSP({browserContextId, bypassCSP}) {
Expand Down

0 comments on commit 9743584

Please sign in to comment.