File: top_level_trial_service_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (773 lines) | stat: -rw-r--r-- 34,265 bytes parent folder | download | duplicates (6)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/tpcd/support/top_level_trial_service.h"

#include "base/strings/strcat.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tpcd/support/trial_test_utils.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "net/base/features.h"
#include "net/cookies/cookie_util.h"
#include "net/dns/mock_host_resolver.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/network/public/mojom/url_response_head.mojom.h"

using content::URLLoaderInterceptor;
using content::WebContents;

namespace tpcd::trial {

class TopLevelTpcdTrialBrowserTest : public InProcessBrowserTest {
 public:
  const std::string kUkmEventName =
      "ThirdPartyCookies.TopLevelDeprecationTrial";

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitchASCII("origin-trial-public-key",
                                    kTestTokenPublicKey);
    command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
  }

  void SetUp() override {
    features_.InitWithFeaturesAndParameters(
        {{net::features::kTopLevelTpcdTrialSettings, {}},
         {content_settings::features::kTrackingProtection3pcd, {}}},
        {});

    InProcessBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");

    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
    https_server_->AddDefaultHandlers(
        base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")));
    ASSERT_TRUE(https_server_->Start());

    // We use a URLLoaderInterceptor, rather than the EmbeddedTestServer, since
    // the origin trial token in the response is associated with a fixed
    // origin, whereas EmbeddedTestServer serves content on a random port.
    url_loader_interceptor_ =
        std::make_unique<URLLoaderInterceptor>(base::BindLambdaForTesting(
            [this](URLLoaderInterceptor::RequestParams* params) {
              return OnRequest(params);
            }));

    GetPrefs()->SetBoolean(prefs::kTrackingProtection3pcdEnabled, true);

    ukm_recorder_.emplace();
  }

  void TearDownOnMainThread() override {
    https_server_.reset();
    url_loader_interceptor_.reset();
    InProcessBrowserTest::TearDownOnMainThread();
  }

 protected:
  content::WebContents* GetActiveWebContents() {
    return chrome_test_utils::GetActiveWebContents(this);
  }

  Profile* GetProfile() {
    return Profile::FromBrowserContext(
        GetActiveWebContents()->GetBrowserContext());
  }

  PrefService* GetPrefs() {
    return user_prefs::UserPrefs::Get(
        GetActiveWebContents()->GetBrowserContext());
  }

  // Most other cookie-related content settings compare their primary patterns'
  // against embedded/requesting sites and their secondary patterns'
  // against top-level sites. This convenience function helps avoid confusion
  // since |TOP_LEVEL_TPCD_TRIAL| content settings only use a primary pattern
  // which is compared against top-level sites.
  ContentSettingChangeObserver CreateTopLevelTrialSettingsObserver(GURL url) {
    return ContentSettingChangeObserver(
        GetProfile(), url, GURL(), ContentSettingsType::TOP_LEVEL_TPCD_TRIAL);
  }

  bool OnRequest(content::URLLoaderInterceptor::RequestParams* params) {
    std::string host = params->url_request.url.host();
    std::string path = params->url_request.url.path().substr(1);
    std::string query = params->url_request.url.query();

    if (host != kTrialEnabledDomain && host != kTrialEnabledSubdomain &&
        host != kOtherTrialEnabledDomain) {
      return false;
    }

    bool should_use_meta_tag = path.starts_with("meta_tag");
    // Redirects are fixed from `kTrialEnabledDomain` to
    // `kOtherTrialEnabledDomain`.
    bool should_redirect =
        path.starts_with("redirect") && host == kTrialEnabledDomain;
    bool should_include_critical_header = query == "critical";

    // To simulate a followed redirect, we have to first notify the client of
    // the redirect, and then also commit the response for the redirect's
    // destination page.
    if (should_redirect) {
      NotifyClientOfRedirect(params);
    }

    // For redirects, we need to commit the response for the redirect's
    // destination page, so we should get the token for the destination page,
    // not the request page.
    std::string token_host = should_redirect ? kOtherTrialEnabledDomain : host;
    std::string token = ChooseToken(token_host, query);

    std::string headers =
        "HTTP/1.1 200 OK\n"
        "Content-type: text/html\n";
    if (!should_use_meta_tag && !token.empty()) {
      base::StrAppend(&headers, {"Origin-Trial: ", token, "\n"});
      if (should_include_critical_header) {
        base::StrAppend(&headers, {"Critical-Origin-Trial: TopLevelTpcd\n"});
      }
    }

    std::string body = (should_use_meta_tag && !token.empty())
                           ? "<html>\n"
                             "<head>\n"
                             "<meta http-equiv='origin-trial' "
                             "content='" +
                                 token +
                                 "'>\n"
                                 "</head>\n"
                                 "<body></body>\n"
                                 "</html>\n"
                           : "";

    content::URLLoaderInterceptor::WriteResponse(headers, body,
                                                 params->client.get());
    return true;
  }

  ukm::TestAutoSetUkmRecorder& ukm_recorder() { return ukm_recorder_.value(); }

  base::test::ScopedFeatureList features_;
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
  std::unique_ptr<URLLoaderInterceptor> url_loader_interceptor_;
  std::optional<ukm::TestAutoSetUkmRecorder> ukm_recorder_;
  const GURL kTrialEnabledSite{base::StrCat({"https://", kTrialEnabledDomain})};
  const GURL kTrialEnabledSiteSubdomain{
      base::StrCat({"https://", kTrialEnabledSubdomain})};
  const GURL kOtherTrialEnabledSite{
      base::StrCat({"https://", kOtherTrialEnabledDomain})};

 private:
  // Fixed redirect from `kTrialEnabledSite` to `kOtherTrialEnabledSite`.
  // Includes the token for `kTrialEnabledSite` in the response headers.
  void NotifyClientOfRedirect(
      content::URLLoaderInterceptor::RequestParams* params) {
    net::RedirectInfo redirect_info;
    redirect_info.new_url = kOtherTrialEnabledSite;
    redirect_info.new_method = "GET";
    net::HttpResponseInfo info;
    info.headers = base::MakeRefCounted<net::HttpResponseHeaders>(
        net::HttpUtil::AssembleRawHeaders(base::StringPrintf(
            "HTTP/1.1 301 Moved Permanently\n"
            "Content-Type: text/html\n"
            "Origin-Trial: %s\n"
            "Location: %s\n",
            k1pDeprecationTrialToken, kOtherTrialEnabledSite.spec().c_str())));
    auto response = network::mojom::URLResponseHead::New();
    response->headers = info.headers;
    response->headers->GetMimeType(&response->mime_type);
    response->encoded_data_length = 0;
    params->client->OnReceiveRedirect(redirect_info, std::move(response));
  }

  std::string ChooseToken(std::string host, std::string query) {
    if (query == "no_token") {
      return "";
    }

    if (host == kTrialEnabledDomain) {
      if (query == "subdomain_matching_token") {
        return k1pDeprecationTrialSubdomainMatchingToken;
      } else {
        return k1pDeprecationTrialToken;
      }
    }

    if (host == kTrialEnabledSubdomain) {
      if (query == "etld_plus_1_token") {
        return k1pDeprecationTrialSubdomainMatchingToken;
      } else if (query == "subdomain_matching_token") {
        return kSubdomain1pDeprecationTrialSubdomainMatchingToken;
      } else {
        return kSubdomain1pDeprecationTrialToken;
      }
    }

    if (host == kOtherTrialEnabledDomain) {
      return kOtherDomain1pDeprecationTrialToken;
    }

    // The host isn't one of our trial-enabled domains, so return no token.
    return "";
  }
};

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest, EnabledAfterHttpResponse) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Verify third-party cookie access isn't permitted under |kTrialEnabledSite|.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Navigate to a |kTrialEnabledSite| page that returns its origin trial token
  // in the HTTP response headers.
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(kTrialEnabledSite);
    ASSERT_TRUE(content::NavigateToURL(web_contents, kTrialEnabledSite));
    setting_observer.Wait();
  }

  // Check that third-party cookie access is now permitted under
  // |kTrialEnabledSite|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_ALLOW);

  // Verify that a subsequent load of a |kTrialEnabledSite| page
  // without the token removes the |TOP_LEVEL_TPCD_TRIAL| content setting for
  // it.
  {
    GURL enabled_site_no_token = GURL(kTrialEnabledSite.spec() + "?no_token");
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(enabled_site_no_token);
    ASSERT_TRUE(content::NavigateToURL(web_contents, enabled_site_no_token));
    setting_observer.Wait();
  }

  // Verify third-party cookie access is no longer permitted under
  // |kTrialEnabledSite|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       EnabledAfterHttpResponseWithEtldSubdomainMatchingToken) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Verify third-party cookie access isn't permitted under |kTrialEnabledSite|
  // or |kTrialEnabledSiteSubdomain|.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSiteSubdomain, {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Navigate to a |kTrialEnabledSiteSubdomain| page that returns the subdomain
  // matching origin trial token for it's eTLD+1 (|kTrialEnabledSite|) in the
  // HTTP response headers.
  {
    GURL url = GURL(kTrialEnabledSiteSubdomain.spec() + "?etld_plus_1_token");
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(url);
    ASSERT_TRUE(content::NavigateToURL(web_contents, url));
    setting_observer.Wait();
  }

  // Check that third-party cookie access is now permitted under
  // |kTrialEnabledSite| and |kTrialEnabledSiteSubdomain|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSiteSubdomain, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_ALLOW);

  // Verify that a subsequent load of a page from |kTrialEnabledSiteSubdomain|'s
  // eTLD+1 (|kTrialEnabledSite|) without the token removes the
  // |TOP_LEVEL_TPCD_TRIAL| content setting for them.
  {
    GURL enabled_site_no_token = GURL(kTrialEnabledSite.spec() + "?no_token");
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(enabled_site_no_token);
    ASSERT_TRUE(content::NavigateToURL(web_contents, enabled_site_no_token));
    setting_observer.Wait();
  }

  // Verify third-party cookie access is no longer permitted under
  // |kTrialEnabledSite| or |kTrialEnabledSiteSubdomain|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSiteSubdomain, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest, EnabledUsingMetaTag) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a |kTrialEnabledSite| page where its origin trial token is in a
  // meta tag in the head of the document.
  {
    GURL url = GURL(kTrialEnabledSite.spec() + "meta_tag");

    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(url);
    ASSERT_TRUE(content::NavigateToURL(web_contents, url));
    setting_observer.Wait();
  }

  // Check that third-party cookie access is now permitted under
  // |kTrialEnabledSite|.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSite, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       EnabledUsingMetaTagWithEtldSubdomainMatchingToken) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a |kTrialEnabledSiteSubdomain| page where the subdomain
  // matching origin trial token for it's eTLD+1 (|kTrialEnabledSite|) is in a
  // meta tag in the head of the document.
  {
    GURL url =
        GURL(kTrialEnabledSiteSubdomain.spec() + "meta_tag?etld_plus_1_token");
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(url);
    ASSERT_TRUE(content::NavigateToURL(web_contents, url));
    setting_observer.Wait();
  }

  // Verify that third-party cookie access is now permitted under
  // |kTrialEnabledSite| and |kTrialEnabledSiteSubdomain|.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSiteSubdomain, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_ALLOW);

  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSiteSubdomain, {},
                nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSite, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
}

// This test verifies (when enabled using a subdomain matching token) the trial
// is only disabled if a document from the token origin is loaded, even if one
// of its subdomains originally enabled the trial.
IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       OnlyTokenOriginCanDisableTrial) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a |kTrialEnabledSiteSubdomain| page that returns the subdomain
  // matching origin trial token for it's eTLD+1 (|kTrialEnabledSite|) in the
  // HTTP response headers.
  {
    GURL url = GURL(kTrialEnabledSiteSubdomain.spec() + "?etld_plus_1_token");
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(url);
    ASSERT_TRUE(content::NavigateToURL(web_contents, url));
    setting_observer.Wait();
  }

  // Check that Top-level 3pcd Trial grants now permit third-party cookie access
  // under |kTrialEnabledSite| and |kTrialEnabledSiteSubdomain|.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSiteSubdomain, {},
                nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSite, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);

  // Verify that subsequent loads of a pages from |kTrialEnabledSiteSubdomain|
  // and other subdomains of the token origin (|kTrialEnabledSite|) without the
  // token do not affect the |TOP_LEVEL_TPCD_TRIAL| content setting for them.
  {
    GURL enabled_site_subdomain_no_token =
        GURL(kTrialEnabledSiteSubdomain.spec() + "?no_token");
    ASSERT_TRUE(
        content::NavigateToURL(web_contents, enabled_site_subdomain_no_token));

    GURL other_subdomain_no_token = https_server_->GetURL(
        base::StrCat({"random-subdomain.", kTrialEnabledDomain}),
        "/title1.html");
    ASSERT_TRUE(content::NavigateToURL(web_contents, other_subdomain_no_token));
  }

  // Since we can't deterministically wait for the TopLevelTrialService to do
  // nothing in response to page loads that shouldn't affect trial state,
  // navigate to a page that enables the trial for a different origin and wait
  // for the associated |TOP_LEVEL_TPCD_TRIAL| settings to be created, then
  // check that the subdomain matching content setting for |kTrialEnabledSite|
  // still remains.
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(kOtherTrialEnabledSite);
    ASSERT_TRUE(content::NavigateToURL(web_contents, kOtherTrialEnabledSite));
    setting_observer.Wait();

    // Check that a Top-level 3pcd Trial grant now permits third-party cookie
    // access under |kOtherTrialEnabledSite|.
    EXPECT_EQ(
        settings->GetThirdPartyCookieAllowMechanism(
            GURL(), net::SiteForCookies(), kOtherTrialEnabledSite, {}, nullptr),
        content_settings::CookieSettingsBase::ThirdPartyCookieAllowMechanism::
            kAllowByTopLevel3PCD);
  }

  // Check that Top-level 3pcd Trial grants still permit third-party cookie
  // access under |kTrialEnabledSite| and |kTrialEnabledSiteSubdomain|.
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSiteSubdomain, {},
                nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                GURL(), net::SiteForCookies(), kTrialEnabledSite, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       NoSettingCreatedIfTrialEnabledCrossSite) {
  base::HistogramTester histograms;
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site = https_server_->GetURL("a.test", "/iframe_blank.html");

  // Verify third-party cookie access isn't permitted under |kTrialEnabledSite|.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Navigate the top-level page to `embedding_site` and update it to have an
  // `kTrialEnabledSite` iframe that returns its TopLevelTpcd origin trial token
  // in its HTTP response headers.
  {
    ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
    const std::string kIframeId = "test";  // defined in iframe_blank.html
    GURL iframe_url = GURL(kTrialEnabledSite.spec() + kTrialEnabledIframePath);
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
  }

  // We can't deterministically wait for the `TopLevelTrialService` to not
  // update content settings and then emit this UMA metric, which is why we need
  // the `RunLoop` here:
  base::RunLoop().RunUntilIdle();

  // Check the TopLevelTpcd origin trial itself is enabled for
  // `kTrialEnabledSite` embedded under `embedding_site`.
  content::OriginTrialsControllerDelegate* trial_delegate =
      web_contents->GetBrowserContext()->GetOriginTrialsControllerDelegate();

  EXPECT_TRUE(trial_delegate->IsFeaturePersistedForOrigin(
      url::Origin::Create(kTrialEnabledSite),
      url::Origin::Create(embedding_site),
      blink::mojom::OriginTrialFeature::kTopLevelTpcd, base::Time::Now()));

  // Verify that third-party cookie access is still NOT permitted under
  // `kTrialEnabledSite`.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

// Since the TopLevelTpcd origin trial itself can be enabled/disabled in a
// cross-site context despite the trial only being intended to support top-level
// sites, this test verifies that changes to the status of the trial for an
// origin when in a cross-site context doesn't affect an existing content
// setting created for it in a top-level context.
IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       CrossSiteOriginTrialStateChangesIgnored) {
  base::HistogramTester histograms;
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  // Verify third-party cookie access isn't already permitted under
  // `kTrialEnabledSite`.
  ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Enable the trial by navigating to a `kTrialEnabledSite` page that returns
  // its origin trial token in the HTTP response headers.
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(kTrialEnabledSite);
    ASSERT_TRUE(content::NavigateToURL(web_contents, kTrialEnabledSite));
    setting_observer.Wait();
    EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                         kTrialEnabledSite, {}, nullptr),
              CONTENT_SETTING_ALLOW);
  }

  GURL embedding_site = https_server_->GetURL("a.test", "/iframe_blank.html");
  const std::string kIframeId = "test";  // defined in iframe_blank.html

  // Enable the origin trial for `kTrialEnabledSite` (under `embedding_site`)
  // using a cross-site iframe embedded on `embedding_site`.
  {
    ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));

    GURL iframe_url = GURL(kTrialEnabledSite.spec() + kTrialEnabledIframePath);
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
  }

  // We can't deterministically wait for the `TopLevelTrialService` to not
  // update content settings and then emit this UMA metric, which is why we need
  // the `RunLoop` here:
  base::RunLoop().RunUntilIdle();

  // Check the TopLevelTpcd origin trial itself is enabled for
  // `kTrialEnabledSite` embedded under `embedding_site`.
  content::OriginTrialsControllerDelegate* trial_delegate =
      web_contents->GetBrowserContext()->GetOriginTrialsControllerDelegate();

  EXPECT_TRUE(trial_delegate->IsFeaturePersistedForOrigin(
      url::Origin::Create(kTrialEnabledSite),
      url::Origin::Create(embedding_site),
      blink::mojom::OriginTrialFeature::kTopLevelTpcd, base::Time::Now()));

  // Navigate the iframe to a `kTrialEnabledSite` page without the token to
  // disable the origin trial for it (under `embedding_site`).
  {
    GURL iframe_url = GURL(kTrialEnabledSite.spec() + "?no_token");
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
  }

  // We can't deterministically wait for the `TopLevelTrialService` to not
  // update content settings and then emit this UMA metric, which is why we need
  // the `RunLoop` here:
  base::RunLoop().RunUntilIdle();

  // Check the TopLevelTpcd origin trial itself is now disabled for
  // `kTrialEnabledSite` embedded under `embedding_site`.
  EXPECT_FALSE(trial_delegate->IsFeaturePersistedForOrigin(
      url::Origin::Create(kTrialEnabledSite),
      url::Origin::Create(embedding_site),
      blink::mojom::OriginTrialFeature::kTopLevelTpcd, base::Time::Now()));

  // Verify that third-party cookie access is still permitted under
  // `kTrialEnabledSite`.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       kTrialEnabledSite, {}, nullptr),
            CONTENT_SETTING_ALLOW);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       UkmEmittedWhenTrialConfiguredViaResponseHeader) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a `kTrialEnabledSite` page that returns its origin trial token
  // in the Origin-Trial HTTP response header.
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(kTrialEnabledSite);
    ASSERT_TRUE(content::NavigateToURL(web_contents, kTrialEnabledSite));
    setting_observer.Wait();
  }

  // Expect a UKM event to have been emitted for enabling the trial.
  auto ukm_entries = ukm_recorder().GetEntriesByName(kUkmEventName);
  ASSERT_EQ(ukm_entries.size(), 1u);
  auto entry = ukm_entries.at(0);
  ukm_recorder().ExpectEntrySourceHasUrl(entry, kTrialEnabledSite);
  ukm_recorder().ExpectEntryMetric(entry, "Enabled", true);
  ukm_recorder().ExpectEntryMetric(entry, "MatchSubdomains", false);

  // Load a subsequent `kTrialEnabledSite` page without the token to remove the
  // `TOP_LEVEL_TPCD_TRIAL` content setting for the site.
  GURL enabled_site_no_token = GURL(kTrialEnabledSite.spec() + "?no_token");
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(kTrialEnabledSite);
    ASSERT_TRUE(content::NavigateToURL(web_contents, enabled_site_no_token));
    setting_observer.Wait();
  }

  // Expect a UKM event to have been emitted for disabling the trial.
  ukm_entries = ukm_recorder().GetEntriesByName(kUkmEventName);
  ASSERT_EQ(ukm_entries.size(), 2u);
  entry = ukm_entries.at(1);
  ukm_recorder().ExpectEntrySourceHasUrl(entry, enabled_site_no_token);
  ukm_recorder().ExpectEntryMetric(entry, "Enabled", false);
  ukm_recorder().ExpectEntryMetric(entry, "MatchSubdomains", false);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       UkmEmittedWhenTrialEnabledViaMetaTag) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a `kTrialEnabledSite` page where its origin trial token is in a
  // meta tag in the head of the document.
  GURL url = GURL(kTrialEnabledSite.spec() + "meta_tag");
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(url);
    ASSERT_TRUE(content::NavigateToURL(web_contents, url));
    setting_observer.Wait();
  }

  // Expect a UKM event to have been emitted for enabling the trial.
  auto ukm_entries = ukm_recorder().GetEntriesByName(kUkmEventName);
  ASSERT_EQ(ukm_entries.size(), 1u);
  auto entry = ukm_entries.at(0);
  ukm_recorder().ExpectEntrySourceHasUrl(entry, url);
  ukm_recorder().ExpectEntryMetric(entry, "Enabled", true);
  ukm_recorder().ExpectEntryMetric(entry, "MatchSubdomains", false);
}

IN_PROC_BROWSER_TEST_F(
    TopLevelTpcdTrialBrowserTest,
    UkmNotDuplicatedWhenResponseToNavigationRequestHasCriticalOriginTrialHeader) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a `kTrialEnabledSite` page that returns its origin trial token
  // in the Origin-Trial HTTP response header and also returns a
  // `Critical-Origin-Trial: TopLevelTpcd` header.
  GURL url = GURL(kTrialEnabledSite.spec() + "?critical");
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(url);
    ASSERT_TRUE(content::NavigateToURL(web_contents, url));
    setting_observer.Wait();
  }

  // Expect only 1 UKM event to have been emitted for enabling the trial.
  auto ukm_entries = ukm_recorder().GetEntriesByName(kUkmEventName);
  ASSERT_EQ(ukm_entries.size(), 1u);
  auto entry = ukm_entries.at(0);
  ukm_recorder().ExpectEntrySourceHasUrl(entry, url);
  ukm_recorder().ExpectEntryMetric(entry, "Enabled", true);
  ukm_recorder().ExpectEntryMetric(entry, "MatchSubdomains", false);
}

IN_PROC_BROWSER_TEST_F(TopLevelTpcdTrialBrowserTest,
                       UkmNotEmittedWhenRedirectResponseHasTokenInHeader) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a `kTrialEnabledSite` page that returns its origin trial token
  // in the Origin-Trial HTTP response header, while redirecting to another site
  // (`kOtherTrialEnabledSite`) that also returns its own origin trial token.
  GURL url = GURL(kTrialEnabledSite.spec() + "redirect");
  {
    ContentSettingChangeObserver settings_observer =
        CreateTopLevelTrialSettingsObserver(kOtherTrialEnabledSite);
    bool nav_success = content::NavigateToURL(
        web_contents, url, /*expected_commit_url=*/kOtherTrialEnabledSite);
    ASSERT_TRUE(nav_success);
    settings_observer.Wait();
  }

  // Expect a UKM event to have been emitted for enabling the trial on
  // `kOtherTrialEnabledSite` in the response headers. Expect no UKM event to
  // have been emitted for the redirect.
  auto ukm_entries = ukm_recorder().GetEntriesByName(kUkmEventName);
  ASSERT_EQ(ukm_entries.size(), 1u);
  auto entry = ukm_entries.at(0);
  ukm_recorder().ExpectEntrySourceHasUrl(entry, kOtherTrialEnabledSite);
  ukm_recorder().ExpectEntryMetric(entry, "Enabled", true);
  ukm_recorder().ExpectEntryMetric(entry, "MatchSubdomains", false);
}

IN_PROC_BROWSER_TEST_F(
    TopLevelTpcdTrialBrowserTest,
    UkmNotEmittedWhenRedirectResponseHasCriticalHeaderAndTokenInHeader) {
  content::WebContents* web_contents = GetActiveWebContents();

  // Navigate to a `kTrialEnabledSite` page that returns its origin trial token
  // in the Origin-Trial HTTP response header, as well as the
  // `Critical-Origin-Trials: TopLevelTpcd` header, while redirecting to another
  // site (`kOtherTrialEnabledSite`) that also returns its own origin trial
  // token.
  GURL url = GURL(kTrialEnabledSite.spec() + "redirect?critical");
  {
    ContentSettingChangeObserver settings_observer =
        CreateTopLevelTrialSettingsObserver(kOtherTrialEnabledSite);
    bool nav_success = content::NavigateToURL(
        web_contents, url, /*expected_commit_url=*/kOtherTrialEnabledSite);
    ASSERT_TRUE(nav_success);
    settings_observer.Wait();
  }

  // Expect a UKM event to have been emitted for enabling the trial on
  // `kOtherTrialEnabledSite` in the response headers. Expect no UKM event to
  // have been emitted for the redirect.
  auto ukm_entries = ukm_recorder().GetEntriesByName(kUkmEventName);
  ASSERT_EQ(ukm_entries.size(), 1u);
  auto entry = ukm_entries.at(0);
  ukm_recorder().ExpectEntrySourceHasUrl(entry, kOtherTrialEnabledSite);
  ukm_recorder().ExpectEntryMetric(entry, "Enabled", true);
  ukm_recorder().ExpectEntryMetric(entry, "MatchSubdomains", false);
}

}  // namespace tpcd::trial