Skip to content

Commit

Permalink
Bug 1595710 - Add support for "platform" to Emulation.setUserAgentOve…
Browse files Browse the repository at this point in the history
…rride r=remote-protocol-reviewers,whimboo

Differential Revision: https://phabricator.services.mozilla.com/D80391
  • Loading branch information
gabrielluong committed Jun 30, 2020
1 parent 3b27f08 commit 2aa7a2a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
16 changes: 13 additions & 3 deletions remote/domains/parent/Emulation.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MAX_WINDOW_SIZE = 10000000;

class Emulation extends Domain {
destructor() {
this.setUserAgentOverride({ userAgent: "" });
this.setUserAgentOverride({ userAgent: "", platform: "" });

super.destructor();
}
Expand Down Expand Up @@ -101,18 +101,22 @@ class Emulation extends Domain {
* User agent to use.
* @param {string=} options.acceptLanguage [not yet supported]
* Browser langugage to emulate.
* @param {number=} options.platform [not yet supported]
* @param {string=} options.platform
* The platform navigator.platform should return.
*/
async setUserAgentOverride(options = {}) {
const { userAgent } = options;
const { userAgent, platform } = options;

if (typeof userAgent != "string") {
throw new TypeError(
"Invalid parameters (userAgent: string value expected)"
);
}

if (!["undefined", "string"].includes(typeof platform)) {
throw new TypeError("platform: string value expected");
}

const { browsingContext } = this.session.target;

if (userAgent.length == 0) {
Expand All @@ -122,6 +126,12 @@ class Emulation extends Domain {
} else {
throw new TypeError("Invalid characters found in userAgent");
}

if (platform?.length > 0) {
browsingContext.customPlatform = platform;
} else {
browsingContext.customPlatform = null;
}
}

_isValidHTTPRequestHeaderValue(value) {
Expand Down
72 changes: 70 additions & 2 deletions remote/test/browser/emulation/browser_setUserAgentOverride.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

const DOC = toDataURL(`<script>document.write(navigator.userAgent);</script>`);

add_task(async function invalidPlatform({ client }) {
const { Emulation } = client;
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";

for (const platform of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await Emulation.setUserAgentOverride({ userAgent, platform });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/platform: string value expected/),
`Fails with invalid type: ${platform}`
);
}
});

add_task(async function setAndResetUserAgent({ client }) {
const { Emulation } = client;
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
Expand Down Expand Up @@ -50,25 +68,75 @@ add_task(async function invalidUserAgent({ Emulation }) {
ok(errorThrown, "Invalid user agent format raised error");
});

add_task(async function setAndResetPlatform({ client }) {
const { Emulation } = client;
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
const platform = "foobar";

await loadURL(DOC);
const originalUserAgent = await getNavigatorProperty("userAgent");
const originalPlatform = await getNavigatorProperty("platform");

isnot(userAgent, originalUserAgent, "Custom user agent hasn't been set");
isnot(platform, originalPlatform, "Custom platform hasn't been set");

await Emulation.setUserAgentOverride({ userAgent, platform });
await loadURL(DOC);
is(
await getNavigatorProperty("userAgent"),
userAgent,
"Custom user agent has been set"
);
is(
await getNavigatorProperty("platform"),
platform,
"Custom platform has been set"
);

await Emulation.setUserAgentOverride({ userAgent: "", platform: "" });
await loadURL(DOC);
is(
await getNavigatorProperty("userAgent"),
originalUserAgent,
"Custom user agent has been reset"
);
is(
await getNavigatorProperty("platform"),
originalPlatform,
"Custom platform has been reset"
);
});

add_task(async function notSetForNewContext({ client }) {
const { Emulation, Target } = client;
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
const platform = "foobar";

await Emulation.setUserAgentOverride({ userAgent });
await Emulation.setUserAgentOverride({ userAgent, platform });
await loadURL(DOC);
is(
await getNavigatorProperty("userAgent"),
userAgent,
"Custom user agent has been set"
);
is(
await getNavigatorProperty("platform"),
platform,
"Custom platform has been set"
);

const { targetInfo } = await openTab(Target);
await Target.activateTarget({ targetId: targetInfo.targetId });

isnot(
await getNavigatorProperty("userAgent"),
userAgent,
"Custom user agent has been set"
"Custom user agent has not been set"
);
isnot(
await getNavigatorProperty("platform"),
platform,
"Custom platform has not been set"
);
});

Expand Down
30 changes: 28 additions & 2 deletions remote/test/browser/network/browser_setUserAgentOverride.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,53 @@ const DOC = toDataURL(`<script>document.write(navigator.userAgent);</script>`);
add_task(async function forwardToEmulation({ client }) {
const { Network } = client;
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
const platform = "foobar";

await loadURL(DOC);
const originalUserAgent = await getNavigatorProperty("userAgent");
const originalPlatform = await getNavigatorProperty("platform");

isnot(originalUserAgent, userAgent, "Custom user agent hasn't been set");
isnot(originalPlatform, platform, "Custom platform hasn't been set");

await Network.setUserAgentOverride({ userAgent });
await Network.setUserAgentOverride({ userAgent, platform });
await loadURL(DOC);
is(
await getNavigatorProperty("userAgent"),
userAgent,
"Custom user agent has been set"
);
is(
await getNavigatorProperty("platform"),
platform,
"Custom platform has been set"
);

await Network.setUserAgentOverride({ userAgent: "" });
await Network.setUserAgentOverride({ userAgent: "", platform: "" });
await loadURL(DOC);
is(
await getNavigatorProperty("userAgent"),
originalUserAgent,
"Custom user agent has been reset"
);
is(
await getNavigatorProperty("platform"),
originalPlatform,
"Custom platform has been reset"
);

await Network.setUserAgentOverride({ userAgent, platform });
await loadURL(DOC);
is(
await getNavigatorProperty("userAgent"),
userAgent,
"Custom user agent has been set"
);
is(
await getNavigatorProperty("platform"),
platform,
"Custom platform has been set"
);
});

async function getNavigatorProperty(prop) {
Expand Down

0 comments on commit 2aa7a2a

Please sign in to comment.