1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test setting custom user agent.
const TEST_DOCUMENT = "target_configuration_test_doc.sjs";
const TEST_URI = URL_ROOT_COM_SSL + TEST_DOCUMENT;
add_task(async function () {
const tab = await addTab(TEST_URI);
info("Create commands for the tab");
const commands = await CommandsFactory.forTab(tab);
const targetConfigurationCommand = commands.targetConfigurationCommand;
const targetCommand = commands.targetCommand;
await targetCommand.startListening();
const initialUserAgent = await getTopLevelUserAgent();
info("Update configuration to change user agent");
const CUSTOM_USER_AGENT = "<MY_BORING_CUSTOM_USER_AGENT>";
await targetConfigurationCommand.updateConfiguration({
customUserAgent: CUSTOM_USER_AGENT,
});
is(
await getTopLevelUserAgent(),
CUSTOM_USER_AGENT,
"The user agent is properly set on the top level document after updating the configuration"
);
is(
await getUserAgentForTopLevelRequest(commands),
CUSTOM_USER_AGENT,
"The custom user agent is used when retrieving resources on the top level document"
);
is(
await getIframeUserAgent(),
CUSTOM_USER_AGENT,
"The user agent is properly set on the iframe after updating the configuration"
);
is(
await getUserAgentForIframeRequest(commands),
CUSTOM_USER_AGENT,
"The custom user agent is used when retrieving resources on the iframe"
);
info("Reload the page");
await BrowserTestUtils.reloadTab(tab, /* includeSubFrames */ true);
is(
await getTopLevelDocumentUserAgentAtStartup(),
CUSTOM_USER_AGENT,
"The custom user agent was set in the content page when it loaded after reloading"
);
is(
await getTopLevelUserAgent(),
CUSTOM_USER_AGENT,
"The custom user agent is set in the content page after reloading"
);
is(
await getUserAgentForTopLevelRequest(commands),
CUSTOM_USER_AGENT,
"The custom user agent is used when retrieving resources after reloading"
);
is(
await getIframeUserAgentAtStartup(),
CUSTOM_USER_AGENT,
"The custom user agent was set in the remote iframe when it loaded after reloading"
);
is(
await getIframeUserAgent(),
CUSTOM_USER_AGENT,
"The custom user agent is set in the remote iframe after reloading"
);
is(
await getUserAgentForIframeRequest(commands),
CUSTOM_USER_AGENT,
"The custom user agent is used when retrieving resources in the remote iframe after reloading"
);
const previousBrowsingContextId = gBrowser.selectedBrowser.browsingContext.id;
info(
"Check that navigating to a page that forces the creation of a new browsing context keep the simulation enabled"
);
const onPageLoaded = BrowserTestUtils.browserLoaded(
gBrowser.selectedBrowser,
/* includeSubFrames */ true
);
BrowserTestUtils.loadURIString(
gBrowser.selectedBrowser,
URL_ROOT_ORG_SSL + TEST_DOCUMENT + "?crossOriginIsolated=true"
);
await onPageLoaded;
isnot(
gBrowser.selectedBrowser.browsingContext.id,
previousBrowsingContextId,
"A new browsing context was created"
);
is(
await getTopLevelDocumentUserAgentAtStartup(),
CUSTOM_USER_AGENT,
"The custom user agent was set in the content page when it loaded after navigating to a new browsing context"
);
is(
await getTopLevelUserAgent(),
CUSTOM_USER_AGENT,
"The custom user agent is set in the content page after navigating to a new browsing context"
);
is(
await getUserAgentForTopLevelRequest(commands),
CUSTOM_USER_AGENT,
"The custom user agent is used when retrieving resources after navigating to a new browsing context"
);
is(
await getIframeUserAgentAtStartup(),
CUSTOM_USER_AGENT,
"The custom user agent was set in the remote iframe when it loaded after navigating to a new browsing context"
);
is(
await getIframeUserAgent(),
CUSTOM_USER_AGENT,
"The custom user agent is set in the remote iframe after navigating to a new browsing context"
);
is(
await getUserAgentForTopLevelRequest(commands),
CUSTOM_USER_AGENT,
"The custom user agent is used when retrieving resources in the remote iframes after navigating to a new browsing context"
);
info(
"Create another commands instance and check that destroying it won't reset the user agent"
);
const otherCommands = await CommandsFactory.forTab(tab);
const otherTargetConfigurationCommand =
otherCommands.targetConfigurationCommand;
const otherTargetCommand = otherCommands.targetCommand;
await otherTargetCommand.startListening();
// wait for the target to be fully attached to avoid pending connection to the server
await otherTargetCommand.watchTargets({
types: [otherTargetCommand.TYPES.FRAME],
onAvailable: () => {},
});
// Let's update the configuration with this commands instance to make sure we hit the TargetConfigurationActor
await otherTargetConfigurationCommand.updateConfiguration({
colorSchemeSimulation: "dark",
});
otherTargetCommand.destroy();
await otherCommands.destroy();
is(
await getTopLevelUserAgent(),
CUSTOM_USER_AGENT,
"The custom user agent is still set on the page after destroying another commands instance"
);
info(
"Check that destroying the commands we set the user agent in will reset the user agent"
);
targetCommand.destroy();
await commands.destroy();
// XXX: This is needed at the moment since Navigator.cpp retrieve the UserAgent from the
// headers (when there's no custom user agent). And here, since we reloaded the page once
// we set the custom user agent, the header was set accordingly and still holds the custom
// user agent value. This should be fixed by Bug 1705326.
is(
await getTopLevelUserAgent(),
CUSTOM_USER_AGENT,
"The custom user agent is still set on the page after destroying the first commands instance. Bug 1705326 will fix that and make it equal to `initialUserAgent`"
);
await BrowserTestUtils.reloadTab(tab, /* includeSubFrames */ true);
is(
await getTopLevelUserAgent(),
initialUserAgent,
"The user agent was reset in the content page after destroying the commands"
);
is(
await getIframeUserAgent(),
initialUserAgent,
"The user agent was reset in the remote iframe after destroying the commands"
);
// We need commands to retrieve the headers of the network request, and
// all those we created so far were destroyed; let's create new ones.
const newCommands = await CommandsFactory.forTab(tab);
await newCommands.targetCommand.startListening();
is(
await getUserAgentForTopLevelRequest(newCommands),
initialUserAgent,
"The initial user agent is used when retrieving resources after destroying the commands"
);
is(
await getUserAgentForIframeRequest(newCommands),
initialUserAgent,
"The initial user agent is used when retrieving resources on the remote iframe after destroying the commands"
);
});
function getUserAgent(browserOrBrowsingContext) {
return SpecialPowers.spawn(browserOrBrowsingContext, [], () => {
return content.navigator.userAgent;
});
}
function getUserAgentAtStartup(browserOrBrowsingContext) {
return SpecialPowers.spawn(
browserOrBrowsingContext,
[],
() => content.wrappedJSObject.initialUserAgent
);
}
function getTopLevelUserAgent() {
return getUserAgent(gBrowser.selectedBrowser);
}
function getTopLevelDocumentUserAgentAtStartup() {
return getUserAgentAtStartup(gBrowser.selectedBrowser);
}
function getIframeBrowsingContext() {
return SpecialPowers.spawn(
gBrowser.selectedBrowser,
[],
() => content.document.querySelector("iframe").browsingContext
);
}
async function getIframeUserAgent() {
const iframeBC = await getIframeBrowsingContext();
return getUserAgent(iframeBC);
}
async function getIframeUserAgentAtStartup() {
const iframeBC = await getIframeBrowsingContext();
return getUserAgentAtStartup(iframeBC);
}
async function getRequestUserAgent(commands, browserOrBrowsingContext) {
const url = `unknown?${Date.now()}`;
// Wait for the resource and its headers to be available
const onAvailable = () => {};
let onUpdated;
const onResource = new Promise(resolve => {
onUpdated = updates => {
for (const { resource } of updates) {
if (resource.url.includes(url) && resource.requestHeadersAvailable) {
resolve(resource);
}
}
};
commands.resourceCommand.watchResources(
[commands.resourceCommand.TYPES.NETWORK_EVENT],
{
onAvailable,
onUpdated,
ignoreExistingResources: true,
}
);
});
info(`Fetch ${url}`);
SpecialPowers.spawn(browserOrBrowsingContext, [url], innerUrl => {
content.fetch(`./${innerUrl}`);
});
info("waiting for matching resource…");
const networkResource = await onResource;
info("…got resource, retrieve headers");
const packet = {
to: networkResource.actor,
type: "getRequestHeaders",
};
const { headers } = await commands.client.request(packet);
commands.resourceCommand.unwatchResources(
[commands.resourceCommand.TYPES.NETWORK_EVENT],
{
onAvailable,
onUpdated,
ignoreExistingResources: true,
}
);
return headers.find(header => header.name == "User-Agent")?.value;
}
async function getUserAgentForTopLevelRequest(commands) {
return getRequestUserAgent(commands, gBrowser.selectedBrowser);
}
async function getUserAgentForIframeRequest(commands) {
const iframeBC = await getIframeBrowsingContext();
return getRequestUserAgent(commands, iframeBC);
}
|