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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
content / browser / browsing_topics / browsing_topics_browsertest.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/browsing_topics/test_util.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_content_browser_client.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/browsing_topics/browsing_topics.mojom.h"
#include "url/gurl.h"
namespace content {
namespace {
class FixedTopicsContentBrowserClient
: public ContentBrowserTestContentBrowserClient {
public:
bool HandleTopicsWebApi(
const url::Origin& context_origin,
content::RenderFrameHost* main_frame,
browsing_topics::ApiCallerSource caller_source,
bool get_topics,
bool observe,
std::vector<blink::mojom::EpochTopicPtr>& topics) override {
blink::mojom::EpochTopicPtr result_topic = blink::mojom::EpochTopic::New();
result_topic->topic = 1;
result_topic->config_version = "chrome.1";
result_topic->taxonomy_version = "1";
result_topic->model_version = "2";
result_topic->version = "chrome.1:1:2";
topics.push_back(std::move(result_topic));
return true;
}
int NumVersionsInTopicsEpochs(
content::RenderFrameHost* main_frame) const override {
return 1;
}
StoragePartitionConfig GetStoragePartitionConfigForSite(
BrowserContext* browser_context,
const GURL& site) override {
// Use a different StoragePartition for URLs from b.test (to test the fix
// for crbug.com/40855090). Note that the port should be removed from site
// to simplify the comparison, because non-default ports are included in
// site URLs when kOriginKeyedProcessesByDefault is enabled.
GURL::Replacements replacements;
replacements.ClearPort();
if (site.ReplaceComponents(replacements) == GURL("https://b.test/")) {
return StoragePartitionConfig::Create(browser_context,
/*partition_domain=*/"b.test",
/*partition_name=*/"test_partition",
/*in_memory=*/false);
}
return StoragePartitionConfig::CreateDefault(browser_context);
}
};
} // namespace
class BrowsingTopicsBrowserTest : public ContentBrowserTest {
public:
BrowsingTopicsBrowserTest() {
feature_list_.InitWithFeatures({features::kPrivacySandboxAdsAPIsOverride,
blink::features::kBrowsingTopics},
/*disabled_features=*/{});
}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
net::test_server::RegisterDefaultHandlers(&https_server_);
https_server_.ServeFilesFromSourceDirectory("content/test/data");
content::SetupCrossSiteRedirector(&https_server_);
ASSERT_TRUE(https_server_.Start());
browser_client_ = std::make_unique<FixedTopicsContentBrowserClient>();
url_loader_monitor_ =
std::make_unique<URLLoaderInterceptor>(base::BindLambdaForTesting(
[&](URLLoaderInterceptor::RequestParams* params) -> bool {
last_request_is_topics_request_ =
params->url_request.browsing_topics;
last_topics_header_ =
params->url_request.headers.GetHeader("Sec-Browsing-Topics");
return false;
}));
}
void TearDownOnMainThread() override {
browser_client_.reset();
url_loader_monitor_.reset();
}
WebContents* web_contents() { return shell()->web_contents(); }
bool last_request_is_topics_request() const {
return last_request_is_topics_request_;
}
const std::optional<std::string>& last_topics_header() const {
return last_topics_header_;
}
std::string InvokeTopicsAPI(const ToRenderFrameHost& adapter) {
return EvalJs(adapter, R"(
if (!(document.browsingTopics instanceof Function)) {
'not a function';
} else {
document.browsingTopics()
.then(topics => {
let result = "[";
for (const topic of topics) {
result += JSON.stringify(topic, Object.keys(topic).sort()) + ";"
}
result += "]";
return result;
})
.catch(error => error.message);
}
)")
.ExtractString();
;
}
protected:
base::test::ScopedFeatureList feature_list_;
net::EmbeddedTestServer https_server_{
net::test_server::EmbeddedTestServer::TYPE_HTTPS};
std::unique_ptr<FixedTopicsContentBrowserClient> browser_client_;
bool last_request_is_topics_request_ = false;
std::unique_ptr<base::RunLoop> resource_request_url_waiter_;
std::optional<std::string> last_topics_header_;
std::unique_ptr<URLLoaderInterceptor> url_loader_monitor_;
};
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest, TopicsAPI) {
// a.test will end up on the default storage partition.
GURL main_frame_url = https_server_.GetURL("a.test", "/hello.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
EXPECT_EQ(
"[{\"configVersion\":\"chrome.1\",\"modelVersion\":\"2\","
"\"taxonomyVersion\":\"1\",\"topic\":1,\"version\":\"chrome.1:1:2\"};]",
InvokeTopicsAPI(web_contents()));
}
IN_PROC_BROWSER_TEST_F(
BrowsingTopicsBrowserTest,
TopicsAPI_InvokedFromFrameWithNonDefaultStoragePartition) {
// b.test will end up on a non-default storage partition.
GURL main_frame_url = https_server_.GetURL("b.test", "/hello.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
EXPECT_EQ("[]", InvokeTopicsAPI(web_contents()));
}
// TODO(crbug.com/40245082): migrate to WPT.
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
Fetch_TopicsHeaderNotVisibleInServiceWorker) {
GURL main_frame_url = https_server_.GetURL(
"a.test", "/browsing_topics/service_worker_factory.html");
GURL worker_script_url = https_server_.GetURL(
"a.test", "/browsing_topics/topics_service_worker.js");
GURL fetch_url = https_server_.GetURL("a.test", "/empty.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
EXPECT_EQ("ok",
EvalJs(shell()->web_contents(),
JsReplace("setupServiceWorker($1)", worker_script_url)));
// Reload the page to let it be controlled by the service worker.
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
// Initiate a topics fetch() request from the Window context. Verify that the
// topics header is not visible in the service worker during the interception.
EXPECT_EQ("null", EvalJs(shell()->web_contents(), content::JsReplace(
R"(
new Promise((resolve, reject) => {
navigator.serviceWorker.addEventListener('message', e => {
if (e.data.url == $1) {
resolve(e.data.topicsHeader);
}
});
fetch($1, {browsingTopics: true});
});
)",
fetch_url)));
}
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest, TopicsHeaderForWindowFetch) {
GURL main_frame_url = https_server_.GetURL(
"a.test", "/browsing_topics/service_worker_factory.html");
GURL fetch_url = https_server_.GetURL("a.test", "/empty.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
EXPECT_TRUE(ExecJs(
shell()->web_contents(),
content::JsReplace("fetch($1, {browsingTopics: true})", fetch_url)));
EXPECT_TRUE(last_request_is_topics_request());
EXPECT_TRUE(last_topics_header());
EXPECT_EQ(last_topics_header().value(),
"(1);v=chrome.1:1:2, ();p=P00000000000");
}
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
TopicsNotAllowedForServiceWorkerFetch) {
GURL main_frame_url = https_server_.GetURL(
"a.test", "/browsing_topics/service_worker_factory.html");
GURL worker_script_url = https_server_.GetURL(
"a.test", "/browsing_topics/topics_service_worker.js");
GURL fetch_url = https_server_.GetURL("a.test", "/empty.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
EXPECT_EQ("ok",
EvalJs(shell()->web_contents(),
JsReplace("setupServiceWorker($1)", worker_script_url)));
// Reload the page to let it be controlled by the service worker.
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
// Initiate a topics fetch request from the service worker. Verify that it
// doesn't contain the topics header.
EXPECT_TRUE(ExecJs(shell()->web_contents(), content::JsReplace(
R"(
new Promise((resolve, reject) => {
navigator.serviceWorker.addEventListener('message', e => {
if (e.data.finishedFetch) {
resolve();
}
});
navigator.serviceWorker.controller.postMessage({
fetchUrl: $1
});
});
)",
fetch_url)));
EXPECT_FALSE(last_request_is_topics_request());
EXPECT_FALSE(last_topics_header());
}
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
EmbedderOptInStatus_StaticIframe_NoBrowsingTopicsAttr) {
base::StringPairs topics_attribute_replacement;
topics_attribute_replacement.emplace_back(
"{{MAYBE_BROWSING_TOPICS_ATTRIBUTE}}", "");
GURL iframe_url = https_server_.GetURL("b.test", "/title1.html");
topics_attribute_replacement.emplace_back("{{SRC_URL}}", iframe_url.spec());
GURL main_frame_url = https_server_.GetURL(
"a.test", net::test_server::GetFilePathWithReplacements(
"/browsing_topics/page_with_iframe.html",
topics_attribute_replacement));
// Wait for the main page and the iframe navigation.
IframeBrowsingTopicsAttributeWatcher navigation_observer(
shell()->web_contents(),
/*expected_number_of_navigations=*/2);
NavigationController::LoadURLParams params(main_frame_url);
shell()->web_contents()->GetController().LoadURLWithParams(params);
navigation_observer.WaitForNavigationFinished();
EXPECT_EQ(navigation_observer.last_navigation_url(), iframe_url);
EXPECT_FALSE(navigation_observer
.last_navigation_has_iframe_browsing_topics_attribute());
}
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
EmbedderOptInStatus_StaticIframe_HasBrowsingTopicsAttr) {
base::StringPairs topics_attribute_replacement;
topics_attribute_replacement.emplace_back(
"{{MAYBE_BROWSING_TOPICS_ATTRIBUTE}}", "browsingtopics");
GURL iframe_url = https_server_.GetURL("b.test", "/title1.html");
topics_attribute_replacement.emplace_back("{{SRC_URL}}", iframe_url.spec());
GURL main_frame_url = https_server_.GetURL(
"a.test", net::test_server::GetFilePathWithReplacements(
"/browsing_topics/page_with_iframe.html",
topics_attribute_replacement));
// Wait for the main page and the iframe navigation.
IframeBrowsingTopicsAttributeWatcher navigation_observer(
shell()->web_contents(),
/*expected_number_of_navigations=*/2);
NavigationController::LoadURLParams params(main_frame_url);
shell()->web_contents()->GetController().LoadURLWithParams(params);
navigation_observer.WaitForNavigationFinished();
EXPECT_EQ(navigation_observer.last_navigation_url(), iframe_url);
EXPECT_TRUE(navigation_observer
.last_navigation_has_iframe_browsing_topics_attribute());
}
IN_PROC_BROWSER_TEST_F(
BrowsingTopicsBrowserTest,
EmbedderOptInStatus_AppendIframeElement_BrowsingTopicsAttrIsFalse) {
GURL main_frame_url = https_server_.GetURL("a.test", "/title1.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
GURL iframe_url = https_server_.GetURL("b.test", "/title1.html");
// Wait for the iframe navigation.
IframeBrowsingTopicsAttributeWatcher navigation_observer(
shell()->web_contents());
ExecuteScriptAsync(shell()->web_contents(), content::JsReplace(R"(
const iframe = document.createElement("iframe");
iframe.browsingTopics = false;
iframe.src = $1;
document.body.appendChild(iframe);
)",
iframe_url));
navigation_observer.WaitForNavigationFinished();
EXPECT_EQ(navigation_observer.last_navigation_url(), iframe_url);
EXPECT_FALSE(navigation_observer
.last_navigation_has_iframe_browsing_topics_attribute());
}
IN_PROC_BROWSER_TEST_F(
BrowsingTopicsBrowserTest,
EmbedderOptInStatus_AppendIframeElement_BrowsingTopicsAttrIsTrue) {
GURL main_frame_url = https_server_.GetURL("a.test", "/title1.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
GURL iframe_url = https_server_.GetURL("b.test", "/title1.html");
// Wait for the iframe navigation.
IframeBrowsingTopicsAttributeWatcher navigation_observer(
shell()->web_contents());
ExecuteScriptAsync(shell()->web_contents(), content::JsReplace(R"(
const iframe = document.createElement("iframe");
iframe.browsingTopics = true;
iframe.src = $1;
document.body.appendChild(iframe);
)",
iframe_url));
navigation_observer.WaitForNavigationFinished();
EXPECT_EQ(navigation_observer.last_navigation_url(), iframe_url);
EXPECT_TRUE(navigation_observer
.last_navigation_has_iframe_browsing_topics_attribute());
}
IN_PROC_BROWSER_TEST_F(BrowsingTopicsBrowserTest,
EmbedderOptInStatus_BrowsingTopicsAttrUpdated) {
GURL main_frame_url = https_server_.GetURL("a.test", "/title1.html");
EXPECT_TRUE(NavigateToURL(shell(), main_frame_url));
// Append a frame without a `browsingTopics` attribute. Expect that the
// navigation doesn't see the flag.
{
GURL iframe_url = https_server_.GetURL("b.test", "/title1.html");
// Wait for the iframe navigation.
IframeBrowsingTopicsAttributeWatcher navigation_observer(
shell()->web_contents());
ExecuteScriptAsync(shell()->web_contents(), content::JsReplace(R"(
const iframe0 = document.createElement("iframe");
iframe0.src = $1;
document.body.appendChild(iframe0);
)",
iframe_url));
navigation_observer.WaitForNavigationFinished();
EXPECT_EQ(navigation_observer.last_navigation_url(), iframe_url);
EXPECT_FALSE(navigation_observer
.last_navigation_has_iframe_browsing_topics_attribute());
}
// Set the `browsingTopics` attribute to true. Expect that the next navigation
// sees the flag.
{
GURL iframe_url = https_server_.GetURL("c.test", "/title1.html");
// Wait for the iframe navigation.
IframeBrowsingTopicsAttributeWatcher navigation_observer(
shell()->web_contents());
ExecuteScriptAsync(shell()->web_contents(), content::JsReplace(R"(
iframe0.browsingTopics = true;
iframe0.src = $1;
)",
iframe_url));
navigation_observer.WaitForNavigationFinished();
EXPECT_EQ(navigation_observer.last_navigation_url(), iframe_url);
EXPECT_TRUE(navigation_observer
.last_navigation_has_iframe_browsing_topics_attribute());
}
}
} // namespace content