File: trial_test_utils.h

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 (188 lines) | stat: -rw-r--r-- 7,808 bytes parent folder | download | duplicates (3)
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
// 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.

#ifndef CHROME_BROWSER_TPCD_SUPPORT_TRIAL_TEST_UTILS_H_
#define CHROME_BROWSER_TPCD_SUPPORT_TRIAL_TEST_UTILS_H_

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/network/public/mojom/cookie_access_observer.mojom.h"
#include "url/gurl.h"

namespace content {
class BrowserContext;
class WebContents;
}  // namespace content

namespace tpcd::trial {

using CookieOperation = network::mojom::CookieAccessDetails::Type;

inline constexpr char kTestTokenPublicKey[] =
    "dRCs+TocuKkocNKa0AtZ4awrt9XKH2SQCI6o4FY6BNA=,fMS4mpO6buLQ/QMd+zJmxzty/"
    "VQ6B1EUZqoCU04zoRU=";

inline constexpr char kTrialEnabledDomain[] = "example.test";
inline constexpr char kTrialEnabledSubdomain[] = "sub.example.test";
inline constexpr char kOtherTrialEnabledDomain[] = "other.test";
inline constexpr char kTrialEnabledIframePath[] = "origin-trial-iframe";
inline constexpr char kTrialEnabledTopLevelPath[] = "origin-trial-top-level";
inline constexpr char kEmbeddedScriptPagePath[] =
    "tpcd/page_with_cross_site_tpcd_support_ot.html";
inline constexpr char kSubdomainMatchingEmbeddedScriptPagePath[] =
    "tpcd/page_with_cross_site_tpcd_support_ot_with_subdomain_matching.html";
// Origin Trials token for `kTrialEnabledDomain` generated with:
// tools/origin_trials/generate_token.py  https://example.test Tpcd
// --expire-days 5000
inline constexpr char kTrialToken[] =
    "A1F5vUG256mdaDWxcpAddjWWg7LdOPuoEBswgFVy8b3j0ejT56eJ+e+"
    "IBocST6j2C8nYcnDm6gkd5O7M3FMo4AIAAABPeyJvcmlnaW4iOiAiaHR0cHM6Ly"
    "9leGFtcGxlLnRlc3Q6NDQzIiwgImZlYXR1cmUiOiAiVHBjZCIsICJleHBpcnkiO"
    "iAyMTI0MzA4MDY1fQ==";

// Origin Trials token for `kTrialEnabledDomain` (and all its subdomains)
// generated with:
// tools/origin_trials/generate_token.py https://example.test Tpcd
// --is-subdomain --expire-days 5000
inline constexpr char kTrialSubdomainMatchingToken[] =
    "AwvUTouERi5ZSbMQGkQhzRCxh3hWd4mu1/"
    "d8CPaQGC3LGmelPVjpqV8VPvKHXNB6ES337b3xvLRsQ6Z/"
    "5TAjdQAAAABkeyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLnRlc3Q6NDQzIiwgImZlYXR1cm"
    "UiOiAiVHBjZCIsICJleHBpcnkiOiAyMTMwODYwOTA1LCAiaXNTdWJkb21haW4iOiB0cnVlfQ="
    "=";

// Origin Trials token for `kTrialEnabledSiteSubdomain` (and all its subdomains)
// generated with:
// tools/origin_trials/generate_token.py https://sub.example.test Tpcd
// --is-subdomain --expire-days 5000
inline constexpr char kSubdomainTrialSubdomainMatchingToken[] =
    "A1XUCMiQfJGkSpeUIg7HmIpY9YtNoANQivDQYA8DLujoJhNovnyi0Fu2huOKeooMwHvfPecmA/"
    "8uJbrgH28T6A8AAABoeyJvcmlnaW4iOiAiaHR0cHM6Ly9zdWIuZXhhbXBsZS50ZXN0OjQ0MyIs"
    "ICJmZWF0dXJlIjogIlRwY2QiLCAiZXhwaXJ5IjogMjEzMzk2NzQwOCwgImlzU3ViZG9tYWluIj"
    "ogdHJ1ZX0=";

// Origin Trials token for `kTrialEnabledDomain` generated with:
// tools/origin_trials/generate_token.py  https://example.test TopLevelTpcd
// --expire-days 5000
inline constexpr char k1pDeprecationTrialToken[] =
    "A5sGfiy3qkhJES3yFHkBd7i0jX8rC+"
    "pCA2M0tAhfmetOLkvOVTAR2589eHxZHbdv3QgX7BtANaw3A+"
    "A3NvgAtwIAAABXeyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLnRlc3Q6NDQzIiwgImZlYXR1"
    "cmUiOiAiVG9wTGV2ZWxUcGNkIiwgImV4cGlyeSI6IDIxMzkzMjU5MjZ9";

// Origin Trials token for `kTrialEnabledDomain` (and all its subdomains)
// generated with:
// tools/origin_trials/generate_token.py https://example.test TopLevelTpcd
// --is-subdomain --expire-days 5000
inline constexpr char k1pDeprecationTrialSubdomainMatchingToken[] =
    "A5+BZIDRMyQWn2lWBHXWd3egEk2WqNdtEuzEbDZV0qXwYM8nKiqlHNYjGrfXuFgmUQ+"
    "j0wpk0EBVJC51I3K0gQkAAABseyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLnRlc3Q6NDQzI"
    "iwgImZlYXR1cmUiOiAiVG9wTGV2ZWxUcGNkIiwgImV4cGlyeSI6IDIxMzkzMzg0NjcsICJpc1N"
    "1YmRvbWFpbiI6IHRydWV9";

// Origin Trials token for `kTrialEnabledSiteSubdomain` generated with:
// tools/origin_trials/generate_token.py  https://sub.example.test TopLevelTpcd
// --expire-days 5000
inline constexpr char kSubdomain1pDeprecationTrialToken[] =
    "A7CJlPHXa8yQc2lJRvM/"
    "mq4Oi5+"
    "SJHbT4nnUmWiYKeuguuMkTd6y8DHBRAdEgvLXPajr9Qm2cMe4f5qzovm07QwAAABbeyJvcmlna"
    "W4iOiAiaHR0cHM6Ly9zdWIuZXhhbXBsZS50ZXN0OjQ0MyIsICJmZWF0dXJlIjogIlRvcExldmV"
    "sVHBjZCIsICJleHBpcnkiOiAyMTM5MzM4NTY5fQ==";

// Origin Trials token for `kTrialEnabledSiteSubdomain` (and all its subdomains)
// generated with:
// tools/origin_trials/generate_token.py https://sub.example.test TopLevelTpcd
// --is-subdomain --expire-days 5000
inline constexpr char kSubdomain1pDeprecationTrialSubdomainMatchingToken[] =
    "Ayuwtl4l9AC0MUBPlPDMZ3on5Db2hTQtFJdRM4fC1Bj03JLXWKNoe9bg4m5CslS5wFG9WQQsKu"
    "q/"
    "IbnFBxzGXwMAAABweyJvcmlnaW4iOiAiaHR0cHM6Ly9zdWIuZXhhbXBsZS50ZXN0OjQ0MyIsIC"
    "JmZWF0dXJlIjogIlRvcExldmVsVHBjZCIsICJleHBpcnkiOiAyMTM5MzM4NTIzLCAiaXNTdWJk"
    "b21haW4iOiB0cnVlfQ==";

// Origin Trials token for `kOtherTrialEnabledDomain` generated with:
// tools/origin_trials/generate_token.py  https://example.test TopLevelTpcd
// --expire-days 5000
inline constexpr char kOtherDomain1pDeprecationTrialToken[] =
    "A7Dsv5nB89HuyiPl64hqJ0V0FporBq7g33dvjUABxJ3K4z3QdjCuurIGcF9wJJE0s1piYA2y4T"
    "0B+"
    "oO2n6sbIQwAAABVeyJvcmlnaW4iOiAiaHR0cHM6Ly9vdGhlci50ZXN0OjQ0MyIsICJmZWF0dXJ"
    "lIjogIlRvcExldmVsVHBjZCIsICJleHBpcnkiOiAyMTQxNzQyODU5fQ==";

// Helper function for performing client side cookie access via JS.
void AccessCookieViaJsIn(content::WebContents* web_contents,
                         content::RenderFrameHost* frame);

class ContentSettingChangeObserver : public content_settings::Observer {
 public:
  explicit ContentSettingChangeObserver(
      content::BrowserContext* browser_context,
      const GURL request_url,
      const GURL partition_url,
      ContentSettingsType setting_type);

  ~ContentSettingChangeObserver() override;

  void Wait();

 private:
  // content_settings::Observer overrides:
  void OnContentSettingChanged(
      const ContentSettingsPattern& primary_pattern,
      const ContentSettingsPattern& secondary_pattern,
      ContentSettingsTypeSet content_type_set) override;

  raw_ptr<content::BrowserContext> browser_context_;
  base::RunLoop run_loop_;
  GURL request_url_;
  GURL partition_url_;
  ContentSettingsType setting_type_;
};

class URLCookieAccessObserver : public content::WebContentsObserver {
 public:
  URLCookieAccessObserver(content::WebContents* web_contents,
                          const GURL& url,
                          CookieOperation access_type);

  void Wait();

 private:
  // WebContentsObserver overrides
  void OnCookiesAccessed(content::RenderFrameHost* render_frame_host,
                         const content::CookieAccessDetails& details) override;
  void OnCookiesAccessed(content::NavigationHandle* navigation_handle,
                         const content::CookieAccessDetails& details) override;

  GURL url_;
  CookieOperation access_type_;
  base::RunLoop run_loop_;
};

class FrameCookieAccessObserver : public content::WebContentsObserver {
 public:
  explicit FrameCookieAccessObserver(
      content::WebContents* web_contents,
      content::RenderFrameHost* render_frame_host,
      CookieOperation access_type);

  // Wait until the frame accesses cookies.
  void Wait();

  // WebContentsObserver override
  void OnCookiesAccessed(content::RenderFrameHost* render_frame_host,
                         const content::CookieAccessDetails& details) override;

 private:
  const raw_ptr<content::RenderFrameHost> render_frame_host_;
  CookieOperation access_type_;
  base::RunLoop run_loop_;
};

}  // namespace tpcd::trial

#endif  // CHROME_BROWSER_TPCD_SUPPORT_TRIAL_TEST_UTILS_H_