Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 5 | #include <cctype> |
Peter Boström | 924f803 | 2021-04-02 20:36:02 | [diff] [blame] | 6 | #include <memory> |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 7 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 8 | #include "base/base_switches.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 9 | #include "base/bind.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 10 | #include "base/command_line.h" |
Jan Wilken Dörrie | b5a41c3 | 2020-12-09 18:55:47 | [diff] [blame] | 11 | #include "base/containers/contains.h" |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 12 | #include "base/metrics/field_trial_param_associator.h" |
Gabriel Charette | b71eec89 | 2017-09-14 22:52:56 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Devlin Cronin | 626d80c | 2018-06-01 01:08:36 | [diff] [blame] | 16 | #include "base/test/metrics/histogram_tester.h" |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 17 | #include "build/build_config.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 18 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 19 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
François Beaufort | 34e299e | 2021-05-26 05:54:33 | [diff] [blame] | 20 | #include "chrome/browser/devtools/protocol/devtools_protocol_test_support.h" |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 21 | #include "chrome/browser/policy/policy_test_utils.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser.h" |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser_commands.h" |
Aaron Tagliaboschi | 25625c2 | 2021-05-11 18:13:59 | [diff] [blame] | 25 | #include "chrome/test/base/chrome_test_utils.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 26 | #include "chrome/test/base/in_process_browser_test.h" |
| 27 | #include "chrome/test/base/ui_test_utils.h" |
Carlos Caballero | b428320 | 2020-08-10 14:40:46 | [diff] [blame] | 28 | #include "components/content_settings/browser/page_specific_content_settings.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 29 | #include "components/content_settings/core/browser/cookie_settings.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 30 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 31 | #include "components/content_settings/core/common/pref_names.h" |
John Abd-El-Malek | ec1fc69e | 2021-01-28 19:14:41 | [diff] [blame] | 32 | #include "components/embedder_support/user_agent_utils.h" |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 33 | #include "components/metrics/content/subprocess_metrics_provider.h" |
Aaron Tagliaboschi | 25625c2 | 2021-05-11 18:13:59 | [diff] [blame] | 34 | #include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h" |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 35 | #include "components/policy/core/common/policy_map.h" |
| 36 | #include "components/policy/core/common/policy_pref_names.h" |
| 37 | #include "components/policy/policy_constants.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 38 | #include "components/prefs/pref_service.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 39 | #include "content/public/browser/browser_thread.h" |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 40 | #include "content/public/browser/navigation_entry.h" |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 41 | #include "content/public/browser/render_view_host.h" |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 42 | #include "content/public/common/content_features.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 43 | #include "content/public/common/content_switches.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 44 | #include "content/public/test/browser_test.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 45 | #include "content/public/test/browser_test_utils.h" |
| 46 | #include "content/public/test/test_utils.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 47 | #include "content/public/test/url_loader_interceptor.h" |
| 48 | #include "net/dns/mock_host_resolver.h" |
| 49 | #include "net/http/http_request_headers.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 50 | #include "net/nqe/effective_connection_type.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 51 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 52 | #include "net/test/embedded_test_server/http_request.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 53 | #include "net/test/embedded_test_server/http_response.h" |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 54 | #include "services/network/public/cpp/client_hints.h" |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 55 | #include "services/network/public/cpp/cors/cors.h" |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 56 | #include "services/network/public/cpp/features.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 57 | #include "services/network/public/cpp/network_switches.h" |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 58 | #include "services/network/public/mojom/web_client_hints_types.mojom-shared.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 59 | #include "third_party/blink/public/common/client_hints/client_hints.h" |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 60 | #include "third_party/blink/public/common/web_preferences/web_preferences.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 61 | |
| 62 | namespace { |
| 63 | |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 64 | const unsigned expected_client_hints_number = 13u; |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 65 | const int32_t uma_histogram_max_value = 1471228928; |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 66 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 67 | // An interceptor that records count of fetches and client hint headers for |
| 68 | // requests to https://foo.com/non-existing-image.jpg. |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 69 | class ThirdPartyURLLoaderInterceptor { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 70 | public: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 71 | explicit ThirdPartyURLLoaderInterceptor(const GURL intercepted_url) |
| 72 | : intercepted_url_(intercepted_url), |
| 73 | interceptor_(base::BindRepeating( |
| 74 | &ThirdPartyURLLoaderInterceptor::InterceptURLRequest, |
| 75 | base::Unretained(this))) {} |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 76 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 77 | ~ThirdPartyURLLoaderInterceptor() = default; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 78 | |
| 79 | size_t request_count_seen() const { return request_count_seen_; } |
| 80 | |
| 81 | size_t client_hints_count_seen() const { return client_hints_count_seen_; } |
| 82 | |
| 83 | private: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 84 | bool InterceptURLRequest( |
| 85 | content::URLLoaderInterceptor::RequestParams* params) { |
| 86 | if (params->url_request.url != intercepted_url_) |
| 87 | return false; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 88 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 89 | request_count_seen_++; |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 90 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 91 | if (params->url_request.headers.HasHeader( |
| 92 | blink::kClientHintsHeaderMapping[i])) { |
| 93 | client_hints_count_seen_++; |
| 94 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 95 | } |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 96 | return false; |
| 97 | } |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 98 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 99 | GURL intercepted_url_; |
| 100 | |
| 101 | size_t request_count_seen_ = 0u; |
| 102 | |
| 103 | size_t client_hints_count_seen_ = 0u; |
| 104 | |
| 105 | content::URLLoaderInterceptor interceptor_; |
| 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(ThirdPartyURLLoaderInterceptor); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 108 | }; |
| 109 | |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 110 | // Returns true only if |header_value| satisfies ABNF: 1*DIGIT [ "." 1*DIGIT ] |
| 111 | bool IsSimilarToDoubleABNF(const std::string& header_value) { |
| 112 | if (header_value.empty()) |
| 113 | return false; |
| 114 | char first_char = header_value.at(0); |
| 115 | if (!isdigit(first_char)) |
| 116 | return false; |
| 117 | |
| 118 | bool period_found = false; |
| 119 | bool digit_found_after_period = false; |
| 120 | for (char ch : header_value) { |
| 121 | if (isdigit(ch)) { |
| 122 | if (period_found) { |
| 123 | digit_found_after_period = true; |
| 124 | } |
| 125 | continue; |
| 126 | } |
| 127 | if (ch == '.') { |
| 128 | if (period_found) |
| 129 | return false; |
| 130 | period_found = true; |
| 131 | continue; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | if (period_found) |
| 136 | return digit_found_after_period; |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // Returns true only if |header_value| satisfies ABNF: 1*DIGIT |
| 141 | bool IsSimilarToIntABNF(const std::string& header_value) { |
| 142 | if (header_value.empty()) |
| 143 | return false; |
| 144 | |
| 145 | for (char ch : header_value) { |
| 146 | if (!isdigit(ch)) |
| 147 | return false; |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 152 | } // namespace |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 153 | |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 154 | class ClientHintsBrowserTest : public policy::PolicyTest, |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 155 | public testing::WithParamInterface<bool> { |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 156 | public: |
| 157 | ClientHintsBrowserTest() |
Mikel Astiz | 2de748d | 2019-11-16 10:39:36 | [diff] [blame] | 158 | : http_server_(net::EmbeddedTestServer::TYPE_HTTP), |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 159 | https_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 160 | https_cross_origin_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 161 | expect_client_hints_on_main_frame_(false), |
| 162 | expect_client_hints_on_subresources_(false), |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 163 | count_user_agent_hint_headers_seen_(0), |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 164 | count_ua_mobile_client_hints_headers_seen_(0), |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 165 | count_ua_platform_client_hints_headers_seen_(0), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 166 | count_client_hints_headers_seen_(0), |
| 167 | request_interceptor_(nullptr) { |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 168 | http_server_.ServeFilesFromSourceDirectory("chrome/test/data/client_hints"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 169 | https_server_.ServeFilesFromSourceDirectory( |
| 170 | "chrome/test/data/client_hints"); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 171 | https_cross_origin_server_.ServeFilesFromSourceDirectory( |
| 172 | "chrome/test/data/client_hints"); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 173 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 174 | http_server_.RegisterRequestMonitor( |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 175 | base::BindRepeating(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 176 | base::Unretained(this))); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 177 | https_server_.RegisterRequestMonitor( |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 178 | base::BindRepeating(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 179 | base::Unretained(this))); |
| 180 | https_cross_origin_server_.RegisterRequestMonitor( |
| 181 | base::BindRepeating(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 182 | base::Unretained(this))); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 183 | https_cross_origin_server_.RegisterRequestHandler( |
| 184 | base::BindRepeating(&ClientHintsBrowserTest::RequestHandlerToRedirect, |
| 185 | base::Unretained(this))); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 186 | https_server_.RegisterRequestHandler(base::BindRepeating( |
| 187 | &ClientHintsBrowserTest::RequestHandlerToFetchCrossOriginIframe, |
| 188 | base::Unretained(this))); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 189 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 190 | EXPECT_TRUE(http_server_.Start()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 191 | EXPECT_TRUE(https_server_.Start()); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 192 | EXPECT_TRUE(https_cross_origin_server_.Start()); |
| 193 | |
| 194 | EXPECT_NE(https_server_.base_url(), https_cross_origin_server_.base_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 195 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 196 | accept_ch_with_lifetime_http_local_url_ = |
| 197 | http_server_.GetURL("/accept_ch_with_lifetime.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 198 | http_equiv_accept_ch_with_lifetime_http_local_url_ = |
| 199 | http_server_.GetURL("/http_equiv_accept_ch_with_lifetime.html"); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 200 | EXPECT_TRUE(accept_ch_with_lifetime_http_local_url_.SchemeIsHTTPOrHTTPS()); |
| 201 | EXPECT_FALSE( |
| 202 | accept_ch_with_lifetime_http_local_url_.SchemeIsCryptographic()); |
| 203 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 204 | accept_ch_with_lifetime_url_ = |
| 205 | https_server_.GetURL("/accept_ch_with_lifetime.html"); |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 206 | accept_ch_with_short_lifetime_url_ = |
| 207 | https_server_.GetURL("/accept_ch_with_short_lifetime.html"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 208 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 209 | accept_ch_without_lifetime_url_ = |
| 210 | https_server_.GetURL("/accept_ch_without_lifetime.html"); |
| 211 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 212 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 213 | http_equiv_accept_ch_without_lifetime_url_ = |
| 214 | https_server_.GetURL("/http_equiv_accept_ch_without_lifetime.html"); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 215 | |
| 216 | without_accept_ch_without_lifetime_url_ = |
| 217 | https_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 218 | EXPECT_TRUE(without_accept_ch_without_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 219 | EXPECT_TRUE( |
| 220 | without_accept_ch_without_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 221 | |
| 222 | without_accept_ch_without_lifetime_local_url_ = |
| 223 | http_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 224 | EXPECT_TRUE( |
| 225 | without_accept_ch_without_lifetime_local_url_.SchemeIsHTTPOrHTTPS()); |
| 226 | EXPECT_FALSE( |
| 227 | without_accept_ch_without_lifetime_local_url_.SchemeIsCryptographic()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 228 | |
| 229 | without_accept_ch_without_lifetime_img_localhost_ = https_server_.GetURL( |
| 230 | "/without_accept_ch_without_lifetime_img_localhost.html"); |
| 231 | without_accept_ch_without_lifetime_img_foo_com_ = https_server_.GetURL( |
| 232 | "/without_accept_ch_without_lifetime_img_foo_com.html"); |
| 233 | accept_ch_without_lifetime_with_iframe_url_ = |
| 234 | https_server_.GetURL("/accept_ch_without_lifetime_with_iframe.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 235 | http_equiv_accept_ch_without_lifetime_with_iframe_url_ = |
| 236 | https_server_.GetURL( |
| 237 | "/http_equiv_accept_ch_without_lifetime_with_iframe.html"); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 238 | accept_ch_without_lifetime_with_subresource_url_ = https_server_.GetURL( |
| 239 | "/accept_ch_without_lifetime_with_subresource.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 240 | http_equiv_accept_ch_without_lifetime_with_subresource_url_ = |
| 241 | https_server_.GetURL( |
| 242 | "/http_equiv_accept_ch_without_lifetime_with_subresource.html"); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 243 | accept_ch_without_lifetime_with_subresource_iframe_url_ = |
| 244 | https_server_.GetURL( |
| 245 | "/accept_ch_without_lifetime_with_subresource_iframe.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 246 | http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url_ = |
| 247 | https_server_.GetURL( |
| 248 | "/http_equiv_accept_ch_without_lifetime_with_subresource_iframe." |
| 249 | "html"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 250 | accept_ch_without_lifetime_img_localhost_ = |
| 251 | https_server_.GetURL("/accept_ch_without_lifetime_img_localhost.html"); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 252 | http_equiv_accept_ch_without_lifetime_img_localhost_ = https_server_.GetURL( |
| 253 | "/http_equiv_accept_ch_without_lifetime_img_localhost.html"); |
| 254 | http_equiv_accept_ch_with_lifetime_ = |
| 255 | https_server_.GetURL("/http_equiv_accept_ch_with_lifetime.html"); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 256 | |
| 257 | redirect_url_ = https_cross_origin_server_.GetURL("/redirect.html"); |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 258 | |
| 259 | accept_ch_empty_ = https_server_.GetURL("/accept_ch_empty.html"); |
| 260 | http_equiv_accept_ch_merge_ = |
| 261 | https_server_.GetURL("/http_equiv_accept_ch_merge.html"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | ~ClientHintsBrowserTest() override {} |
| 265 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 266 | virtual std::unique_ptr<base::FeatureList> EnabledFeatures() { |
| 267 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 268 | feature_list->InitializeFromCommandLine( |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 269 | "UserAgentClientHint,LangClientHintHeader,CriticalClientHint," |
François Beaufort | 1b51d06 | 2021-05-18 11:11:23 | [diff] [blame] | 270 | "AcceptCHFrame,PrefersColorSchemeClientHintHeader", |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 271 | ""); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 272 | return feature_list; |
| 273 | } |
| 274 | |
| 275 | void SetUp() override { |
Maksim Sisov (GMT+3) | 830244f | 2020-08-10 12:52:24 | [diff] [blame] | 276 | scoped_feature_list_.InitWithFeatureList(EnabledFeatures()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 277 | InProcessBrowserTest::SetUp(); |
| 278 | } |
| 279 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 280 | void SetUpOnMainThread() override { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 281 | host_resolver()->AddRule("*", "127.0.0.1"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 282 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 283 | request_interceptor_ = std::make_unique<ThirdPartyURLLoaderInterceptor>( |
| 284 | GURL("https://foo.com/non-existing-image.jpg")); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 285 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 286 | } |
| 287 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 288 | void TearDownOnMainThread() override { request_interceptor_.reset(); } |
| 289 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 290 | void SetUpCommandLine(base::CommandLine* cmd) override { |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 291 | cmd->AppendSwitchASCII(network::switches::kForceEffectiveConnectionType, |
| 292 | net::kEffectiveConnectionType2G); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 293 | } |
| 294 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 295 | void SetClientHintExpectationsOnMainFrame(bool expect_client_hints) { |
| 296 | expect_client_hints_on_main_frame_ = expect_client_hints; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 297 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 298 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 299 | void SetClientHintExpectationsOnSubresources(bool expect_client_hints) { |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 300 | base::AutoLock lock(expect_client_hints_on_subresources_lock_); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 301 | expect_client_hints_on_subresources_ = expect_client_hints; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 302 | } |
| 303 | |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 304 | bool expect_client_hints_on_subresources() { |
| 305 | base::AutoLock lock(expect_client_hints_on_subresources_lock_); |
| 306 | return expect_client_hints_on_subresources_; |
| 307 | } |
| 308 | |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 309 | // Verify that the user is not notified that cookies or JavaScript were |
| 310 | // blocked on the webpage due to the checks done by client hints. |
| 311 | void VerifyContentSettingsNotNotified() const { |
Carlos Caballero | b428320 | 2020-08-10 14:40:46 | [diff] [blame] | 312 | auto* pscs = content_settings::PageSpecificContentSettings::GetForFrame( |
Carlos Caballero | d7b75941 | 2020-07-31 18:00:08 | [diff] [blame] | 313 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); |
Carlos Caballero | b428320 | 2020-08-10 14:40:46 | [diff] [blame] | 314 | EXPECT_FALSE(pscs->IsContentBlocked(ContentSettingsType::COOKIES)); |
| 315 | EXPECT_FALSE(pscs->IsContentBlocked(ContentSettingsType::JAVASCRIPT)); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 316 | } |
| 317 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 318 | void SetExpectedEffectiveConnectionType( |
| 319 | net::EffectiveConnectionType effective_connection_type) { |
| 320 | expected_ect = effective_connection_type; |
| 321 | } |
| 322 | |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 323 | void SetJsEnabledForActiveView(bool enabled) { |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 324 | content::WebContents* web_contents = |
| 325 | browser()->tab_strip_model()->GetActiveWebContents(); |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 326 | blink::web_pref::WebPreferences prefs = |
| 327 | web_contents->GetOrCreateWebPreferences(); |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 328 | prefs.javascript_enabled = enabled; |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 329 | web_contents->SetWebPreferences(prefs); |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 330 | } |
| 331 | |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 332 | void TestProfilesIndependent(Browser* browser_a, Browser* browser_b); |
| 333 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 334 | const GURL& accept_ch_with_lifetime_http_local_url() const { |
| 335 | return accept_ch_with_lifetime_http_local_url_; |
| 336 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 337 | const GURL& http_equiv_accept_ch_with_lifetime_http_local_url() const { |
| 338 | return http_equiv_accept_ch_with_lifetime_http_local_url_; |
| 339 | } |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 340 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 341 | // A URL whose response headers include Accept-CH and Accept-CH-Lifetime |
| 342 | // headers. |
| 343 | const GURL& accept_ch_with_lifetime_url() const { |
| 344 | return accept_ch_with_lifetime_url_; |
| 345 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 346 | const GURL& http_equiv_accept_ch_with_lifetime() { |
| 347 | return http_equiv_accept_ch_with_lifetime_; |
| 348 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 349 | |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 350 | // A URL whose response headers include Accept-CH and Accept-CH-Lifetime |
| 351 | // headers. The Accept-CH-Lifetime duration is set very short to 1 second. |
| 352 | const GURL& accept_ch_with_short_lifetime() const { |
| 353 | return accept_ch_with_short_lifetime_url_; |
| 354 | } |
| 355 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 356 | // A URL whose response headers include only Accept-CH header. |
| 357 | const GURL& accept_ch_without_lifetime_url() const { |
| 358 | return accept_ch_without_lifetime_url_; |
| 359 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 360 | const GURL& http_equiv_accept_ch_without_lifetime_url() const { |
| 361 | return http_equiv_accept_ch_without_lifetime_url_; |
| 362 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 363 | |
| 364 | // A URL whose response headers do not include either Accept-CH or |
| 365 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 366 | const GURL& without_accept_ch_without_lifetime_url() const { |
| 367 | return without_accept_ch_without_lifetime_url_; |
| 368 | } |
| 369 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 370 | // A URL whose response headers do not include either Accept-CH or |
| 371 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 372 | const GURL& without_accept_ch_without_lifetime_local_url() const { |
| 373 | return without_accept_ch_without_lifetime_local_url_; |
| 374 | } |
| 375 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 376 | // A URL whose response headers do not include either Accept-CH or |
| 377 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 378 | // from localhost. |
| 379 | const GURL& without_accept_ch_without_lifetime_img_localhost() const { |
| 380 | return without_accept_ch_without_lifetime_img_localhost_; |
| 381 | } |
| 382 | |
| 383 | // A URL whose response headers do not include either Accept-CH or |
| 384 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 385 | // from foo.com. |
| 386 | const GURL& without_accept_ch_without_lifetime_img_foo_com() const { |
| 387 | return without_accept_ch_without_lifetime_img_foo_com_; |
| 388 | } |
| 389 | |
| 390 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
| 391 | // headers. The response loads accept_ch_with_lifetime_url() in an iframe. |
| 392 | const GURL& accept_ch_without_lifetime_with_iframe_url() const { |
| 393 | return accept_ch_without_lifetime_with_iframe_url_; |
| 394 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 395 | const GURL& http_equiv_accept_ch_without_lifetime_with_iframe_url() const { |
| 396 | return http_equiv_accept_ch_without_lifetime_with_iframe_url_; |
| 397 | } |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 398 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 399 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
| 400 | // headers. The response loads accept_ch_with_lifetime_url() as a subresource |
| 401 | // in the main frame. |
| 402 | const GURL& accept_ch_without_lifetime_with_subresource_url() const { |
| 403 | return accept_ch_without_lifetime_with_subresource_url_; |
| 404 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 405 | const GURL& http_equiv_accept_ch_without_lifetime_with_subresource_url() |
| 406 | const { |
| 407 | return http_equiv_accept_ch_without_lifetime_with_subresource_url_; |
| 408 | } |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 409 | |
| 410 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 411 | // headers. The response loads accept_ch_with_lifetime_url() or |
| 412 | // http_equiv_accept_ch_with_lifetime_url() as a subresource in the iframe. |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 413 | const GURL& accept_ch_without_lifetime_with_subresource_iframe_url() const { |
| 414 | return accept_ch_without_lifetime_with_subresource_iframe_url_; |
| 415 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 416 | const GURL& |
| 417 | http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url() const { |
| 418 | return http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url_; |
| 419 | } |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 420 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 421 | // A URL whose response includes only Accept-CH header. Navigating to |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 422 | // this URL also fetches two images: One from the localhost, and one from |
| 423 | // foo.com. |
| 424 | const GURL& accept_ch_without_lifetime_img_localhost() const { |
| 425 | return accept_ch_without_lifetime_img_localhost_; |
| 426 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 427 | const GURL& http_equiv_accept_ch_without_lifetime_img_localhost() const { |
| 428 | return http_equiv_accept_ch_without_lifetime_img_localhost_; |
| 429 | } |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 430 | |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 431 | const GURL& redirect_url() const { return redirect_url_; } |
| 432 | |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 433 | // A URL to a page with a response containing an empty accept_ch header. |
| 434 | const GURL& accept_ch_empty() const { return accept_ch_empty_; } |
| 435 | |
| 436 | // A page where some hints are in accept-ch header, some in http-equiv. |
| 437 | const GURL& http_equiv_accept_ch_merge() const { |
| 438 | return http_equiv_accept_ch_merge_; |
| 439 | } |
| 440 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 441 | size_t count_user_agent_hint_headers_seen() const { |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 442 | base::AutoLock lock(count_headers_lock_); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 443 | return count_user_agent_hint_headers_seen_; |
| 444 | } |
| 445 | |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 446 | size_t count_ua_mobile_client_hints_headers_seen() const { |
| 447 | base::AutoLock lock(count_headers_lock_); |
| 448 | return count_ua_mobile_client_hints_headers_seen_; |
| 449 | } |
| 450 | |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 451 | size_t count_ua_platform_client_hints_headers_seen() const { |
| 452 | base::AutoLock lock(count_headers_lock_); |
| 453 | return count_ua_platform_client_hints_headers_seen_; |
| 454 | } |
| 455 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 456 | size_t count_client_hints_headers_seen() const { |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 457 | base::AutoLock lock(count_headers_lock_); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 458 | return count_client_hints_headers_seen_; |
| 459 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 460 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 461 | size_t third_party_request_count_seen() const { |
| 462 | return request_interceptor_->request_count_seen(); |
| 463 | } |
| 464 | |
| 465 | size_t third_party_client_hints_count_seen() const { |
| 466 | return request_interceptor_->client_hints_count_seen(); |
| 467 | } |
| 468 | |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 469 | const std::string& main_frame_ua_observed() const { |
| 470 | return main_frame_ua_observed_; |
| 471 | } |
| 472 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 473 | const std::string& main_frame_ua_full_version_observed() const { |
| 474 | return main_frame_ua_full_version_observed_; |
| 475 | } |
| 476 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 477 | const std::string& main_frame_ua_mobile_observed() const { |
| 478 | return main_frame_ua_mobile_observed_; |
| 479 | } |
| 480 | |
| 481 | const std::string& main_frame_ua_platform_observed() const { |
| 482 | return main_frame_ua_platform_observed_; |
| 483 | } |
| 484 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 485 | base::test::ScopedFeatureList scoped_feature_list_; |
| 486 | |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 487 | std::string intercept_iframe_resource_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 488 | bool intercept_to_http_equiv_iframe_ = false; |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 489 | mutable base::Lock count_headers_lock_; |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 490 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 491 | private: |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 492 | // Intercepts only the main frame requests that contain |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 493 | // "redirect" in the resource path. The intercepted requests |
| 494 | // are served an HTML file that fetches an iframe from a cross-origin HTTPS |
| 495 | // server. |
| 496 | std::unique_ptr<net::test_server::HttpResponse> RequestHandlerToRedirect( |
| 497 | const net::test_server::HttpRequest& request) { |
| 498 | // Check if it's a main frame request. |
| 499 | if (request.relative_url.find(".html") == std::string::npos) |
| 500 | return nullptr; |
| 501 | |
| 502 | if (request.GetURL().spec().find("redirect") == std::string::npos) |
| 503 | return nullptr; |
| 504 | |
Lei Zhang | e00db75 | 2021-04-17 00:48:46 | [diff] [blame] | 505 | auto response = std::make_unique<net::test_server::BasicHttpResponse>(); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 506 | response->set_code(net::HTTP_FOUND); |
| 507 | response->AddCustomHeader("Location", |
| 508 | without_accept_ch_without_lifetime_url().spec()); |
| 509 | return std::move(response); |
| 510 | } |
| 511 | |
| 512 | // Intercepts only the main frame requests that contain |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 513 | // |intercept_iframe_resource_| in the resource path. The intercepted requests |
| 514 | // are served an HTML file that fetches an iframe from a cross-origin HTTPS |
| 515 | // server. |
| 516 | std::unique_ptr<net::test_server::HttpResponse> |
| 517 | RequestHandlerToFetchCrossOriginIframe( |
| 518 | const net::test_server::HttpRequest& request) { |
| 519 | if (intercept_iframe_resource_.empty()) |
| 520 | return nullptr; |
| 521 | |
| 522 | // Check if it's a main frame request. |
| 523 | if (request.relative_url.find(".html") == std::string::npos) |
| 524 | return nullptr; |
| 525 | |
| 526 | if (request.relative_url.find(intercept_iframe_resource_) == |
| 527 | std::string::npos) { |
| 528 | return nullptr; |
| 529 | } |
| 530 | |
| 531 | const std::string iframe_url = |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 532 | intercept_to_http_equiv_iframe_ |
| 533 | ? https_cross_origin_server_ |
| 534 | .GetURL("/http_equiv_accept_ch_with_lifetime.html") |
| 535 | .spec() |
| 536 | : https_cross_origin_server_.GetURL("/accept_ch_with_lifetime.html") |
| 537 | .spec(); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 538 | |
| 539 | std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
| 540 | new net::test_server::BasicHttpResponse()); |
| 541 | http_response->set_code(net::HTTP_OK); |
| 542 | http_response->set_content_type("text/html"); |
| 543 | http_response->set_content( |
| 544 | "<html>" |
| 545 | "<link rel='icon' href='data:;base64,='><head></head>" |
| 546 | "Empty file which uses link-rel to disable favicon fetches. " |
| 547 | "<iframe src='" + |
| 548 | iframe_url + "'></iframe></html>"); |
| 549 | |
| 550 | return std::move(http_response); |
| 551 | } |
| 552 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 553 | static std::string UpdateHeaderObservation( |
| 554 | const net::test_server::HttpRequest& request, |
| 555 | const std::string& header) { |
| 556 | if (request.headers.find(header) != request.headers.end()) |
| 557 | return request.headers.find(header)->second; |
| 558 | else |
| 559 | return ""; |
| 560 | } |
| 561 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 562 | // Called by |https_server_|. |
| 563 | void MonitorResourceRequest(const net::test_server::HttpRequest& request) { |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 564 | bool is_main_frame_navigation = |
| 565 | request.GetURL().spec().find(".html") != std::string::npos; |
| 566 | |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 567 | if (is_main_frame_navigation && |
| 568 | request.GetURL().spec().find("redirect") != std::string::npos) { |
| 569 | return; |
| 570 | } |
| 571 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 572 | if (is_main_frame_navigation) { |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 573 | main_frame_ua_observed_ = UpdateHeaderObservation(request, "sec-ch-ua"); |
| 574 | main_frame_ua_full_version_observed_ = |
| 575 | UpdateHeaderObservation(request, "sec-ch-ua-full-version"); |
| 576 | main_frame_ua_mobile_observed_ = |
| 577 | UpdateHeaderObservation(request, "sec-ch-ua-mobile"); |
| 578 | main_frame_ua_platform_observed_ = |
| 579 | UpdateHeaderObservation(request, "sec-ch-ua-platform"); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 580 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 581 | VerifyClientHintsReceived(expect_client_hints_on_main_frame_, request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 582 | if (expect_client_hints_on_main_frame_) { |
| 583 | double value = 0.0; |
| 584 | EXPECT_TRUE(base::StringToDouble( |
| 585 | request.headers.find("device-memory")->second, &value)); |
| 586 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 587 | EXPECT_TRUE(IsSimilarToDoubleABNF( |
| 588 | request.headers.find("device-memory")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 589 | main_frame_device_memory_observed_ = value; |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 590 | |
| 591 | EXPECT_TRUE( |
| 592 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 593 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 594 | EXPECT_TRUE(IsSimilarToDoubleABNF(request.headers.find("dpr")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 595 | main_frame_dpr_observed_ = value; |
| 596 | |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 597 | EXPECT_TRUE(base::StringToDouble( |
| 598 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 599 | EXPECT_TRUE( |
| 600 | IsSimilarToIntABNF(request.headers.find("viewport-width")->second)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 601 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 602 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 603 | #else |
| 604 | EXPECT_EQ(980, value); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 605 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 606 | main_frame_viewport_width_observed_ = value; |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 607 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 608 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 609 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 610 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 611 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 612 | if (!is_main_frame_navigation) { |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 613 | VerifyClientHintsReceived(expect_client_hints_on_subresources(), request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 614 | |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 615 | if (expect_client_hints_on_subresources()) { |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 616 | double value = 0.0; |
| 617 | EXPECT_TRUE(base::StringToDouble( |
| 618 | request.headers.find("device-memory")->second, &value)); |
| 619 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 620 | EXPECT_TRUE(IsSimilarToDoubleABNF( |
| 621 | request.headers.find("device-memory")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 622 | if (main_frame_device_memory_observed_ > 0) { |
| 623 | EXPECT_EQ(main_frame_device_memory_observed_, value); |
| 624 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 625 | |
| 626 | EXPECT_TRUE( |
| 627 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 628 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 629 | EXPECT_TRUE(IsSimilarToDoubleABNF(request.headers.find("dpr")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 630 | if (main_frame_dpr_observed_ > 0) { |
| 631 | EXPECT_EQ(main_frame_dpr_observed_, value); |
| 632 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 633 | |
| 634 | EXPECT_TRUE(base::StringToDouble( |
| 635 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 636 | EXPECT_TRUE( |
| 637 | IsSimilarToIntABNF(request.headers.find("viewport-width")->second)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 638 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 639 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 640 | #else |
| 641 | EXPECT_EQ(980, value); |
| 642 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 643 | #if defined(OS_ANDROID) |
| 644 | // TODO(tbansal): https://crbug.com/825892: Viewport width on main |
| 645 | // frame requests may be incorrect when the Chrome window is not |
| 646 | // maximized. |
| 647 | if (main_frame_viewport_width_observed_ > 0) { |
| 648 | EXPECT_EQ(main_frame_viewport_width_observed_, value); |
| 649 | } |
| 650 | #endif |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 651 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 652 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 653 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 654 | |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 655 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
Jan Wilken Dörrie | a8cb5630 | 2019-06-06 18:59:36 | [diff] [blame] | 656 | if (base::Contains(request.headers, |
| 657 | blink::kClientHintsHeaderMapping[i])) { |
Callum May | 7d88ff3e | 2019-11-12 18:21:46 | [diff] [blame] | 658 | base::AutoLock lock(count_headers_lock_); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 659 | // The user agent hint is special: |
| 660 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "sec-ch-ua") { |
| 661 | count_user_agent_hint_headers_seen_++; |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 662 | } else if (std::string(blink::kClientHintsHeaderMapping[i]) == |
| 663 | "sec-ch-ua-mobile") { |
| 664 | count_ua_mobile_client_hints_headers_seen_++; |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 665 | } else if (std::string(blink::kClientHintsHeaderMapping[i]) == |
| 666 | "sec-ch-ua-platform") { |
| 667 | count_ua_platform_client_hints_headers_seen_++; |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 668 | } else { |
| 669 | count_client_hints_headers_seen_++; |
| 670 | } |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 674 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 675 | void VerifyClientHintsReceived(bool expect_client_hints, |
| 676 | const net::test_server::HttpRequest& request) { |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 677 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
| 678 | SCOPED_TRACE(testing::Message() |
| 679 | << std::string(blink::kClientHintsHeaderMapping[i])); |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 680 | // Resource width client hint is only attached on image subresources. |
| 681 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "width") { |
| 682 | continue; |
| 683 | } |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 684 | |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 685 | // `Sec-CH-UA`, `Sec-CH-UA-Mobile`, and `Sec-CH-UA-Platform` is attached |
| 686 | // on all requests. |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 687 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "sec-ch-ua" || |
| 688 | std::string(blink::kClientHintsHeaderMapping[i]) == |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 689 | "sec-ch-ua-mobile" || |
| 690 | std::string(blink::kClientHintsHeaderMapping[i]) == |
| 691 | "sec-ch-ua-platform") { |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 692 | continue; |
| 693 | } |
| 694 | |
Jan Wilken Dörrie | a8cb5630 | 2019-06-06 18:59:36 | [diff] [blame] | 695 | EXPECT_EQ( |
| 696 | expect_client_hints, |
| 697 | base::Contains(request.headers, blink::kClientHintsHeaderMapping[i])); |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 698 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 699 | } |
| 700 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 701 | void VerifyNetworkQualityClientHints( |
| 702 | const net::test_server::HttpRequest& request) const { |
| 703 | // Effective connection type is forced to 2G using command line in these |
| 704 | // tests. |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 705 | int rtt_value = 0.0; |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 706 | EXPECT_TRUE( |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 707 | base::StringToInt(request.headers.find("rtt")->second, &rtt_value)); |
| 708 | EXPECT_LE(0, rtt_value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 709 | EXPECT_TRUE(IsSimilarToIntABNF(request.headers.find("rtt")->second)); |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 710 | // Verify that RTT value is a multiple of 50 milliseconds. |
| 711 | EXPECT_EQ(0, rtt_value % 50); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 712 | EXPECT_GE(expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G ? 3000 : 500, |
| 713 | rtt_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 714 | |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 715 | double mbps_value = 0.0; |
| 716 | EXPECT_TRUE(base::StringToDouble(request.headers.find("downlink")->second, |
| 717 | &mbps_value)); |
| 718 | EXPECT_LE(0, mbps_value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 719 | EXPECT_TRUE( |
| 720 | IsSimilarToDoubleABNF(request.headers.find("downlink")->second)); |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 721 | // Verify that the mbps value is a multiple of 0.050 mbps. |
| 722 | // Allow for small amount of noise due to double to integer conversions. |
| 723 | EXPECT_NEAR(0, (static_cast<int>(mbps_value * 1000)) % 50, 1); |
| 724 | EXPECT_GE(10.0, mbps_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 725 | |
| 726 | EXPECT_FALSE(request.headers.find("ect")->second.empty()); |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 727 | |
| 728 | // TODO(tbansal): https://crbug.com/819244: When network servicification is |
Tarun Bansal | 5ac53354 | 2018-08-10 19:45:52 | [diff] [blame] | 729 | // enabled, the renderer processes do not receive notifications on |
| 730 | // change in the network quality. Hence, the network quality client hints |
| 731 | // are not set to the correct value on subresources. |
| 732 | bool is_main_frame_navigation = |
| 733 | request.GetURL().spec().find(".html") != std::string::npos; |
John Abd-El-Malek | 67facbe8 | 2019-06-06 22:37:08 | [diff] [blame] | 734 | if (is_main_frame_navigation) { |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 735 | // Effective connection type is forced to 2G using command line in these |
| 736 | // tests. RTT is expected to be 1800 msec but leave some gap to account |
| 737 | // for added noise and randomization. |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 738 | if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G) { |
| 739 | EXPECT_NEAR(1800, rtt_value, 360); |
| 740 | } else if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_3G) { |
| 741 | EXPECT_NEAR(450, rtt_value, 90); |
| 742 | } else { |
| 743 | NOTREACHED(); |
| 744 | } |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 745 | |
| 746 | // Effective connection type is forced to 2G using command line in these |
| 747 | // tests. downlink is expected to be 0.075 Mbps but leave some gap to |
| 748 | // account for added noise and randomization. |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 749 | if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G) { |
| 750 | EXPECT_NEAR(0.075, mbps_value, 0.05); |
| 751 | } else if (expected_ect == net::EFFECTIVE_CONNECTION_TYPE_3G) { |
| 752 | EXPECT_NEAR(0.4, mbps_value, 0.1); |
| 753 | } else { |
| 754 | NOTREACHED(); |
| 755 | } |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 756 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 757 | EXPECT_EQ(expected_ect == net::EFFECTIVE_CONNECTION_TYPE_2G ? "2g" : "3g", |
| 758 | request.headers.find("ect")->second); |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 759 | } |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 760 | } |
| 761 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 762 | net::EmbeddedTestServer http_server_; |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 763 | net::EmbeddedTestServer https_server_; |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 764 | net::EmbeddedTestServer https_cross_origin_server_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 765 | GURL accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 766 | GURL http_equiv_accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 767 | GURL accept_ch_with_lifetime_url_; |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 768 | GURL accept_ch_with_short_lifetime_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 769 | GURL accept_ch_without_lifetime_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 770 | GURL http_equiv_accept_ch_without_lifetime_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 771 | GURL without_accept_ch_without_lifetime_url_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 772 | GURL without_accept_ch_without_lifetime_local_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 773 | GURL accept_ch_without_lifetime_with_iframe_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 774 | GURL http_equiv_accept_ch_without_lifetime_with_iframe_url_; |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 775 | GURL accept_ch_without_lifetime_with_subresource_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 776 | GURL http_equiv_accept_ch_without_lifetime_with_subresource_url_; |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 777 | GURL accept_ch_without_lifetime_with_subresource_iframe_url_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 778 | GURL http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 779 | GURL without_accept_ch_without_lifetime_img_foo_com_; |
| 780 | GURL without_accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 781 | GURL accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 782 | GURL http_equiv_accept_ch_without_lifetime_img_localhost_; |
| 783 | GURL http_equiv_accept_ch_with_lifetime_; |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 784 | GURL redirect_url_; |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 785 | GURL accept_ch_empty_; |
| 786 | GURL http_equiv_accept_ch_merge_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 787 | |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 788 | std::string main_frame_ua_observed_; |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 789 | std::string main_frame_ua_full_version_observed_; |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 790 | std::string main_frame_ua_mobile_observed_; |
| 791 | std::string main_frame_ua_platform_observed_; |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 792 | |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 793 | double main_frame_dpr_observed_ = -1; |
| 794 | double main_frame_viewport_width_observed_ = -1; |
| 795 | double main_frame_device_memory_observed_ = -1; |
| 796 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 797 | // Expect client hints on all the main frame request. |
| 798 | bool expect_client_hints_on_main_frame_; |
| 799 | // Expect client hints on all the subresource requests. |
Callum May | 1d93974 | 2020-03-02 17:51:30 | [diff] [blame] | 800 | bool expect_client_hints_on_subresources_ |
| 801 | GUARDED_BY(expect_client_hints_on_subresources_lock_); |
| 802 | |
| 803 | base::Lock expect_client_hints_on_subresources_lock_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 804 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 805 | size_t count_user_agent_hint_headers_seen_; |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 806 | size_t count_ua_mobile_client_hints_headers_seen_; |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 807 | size_t count_ua_platform_client_hints_headers_seen_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 808 | size_t count_client_hints_headers_seen_; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 809 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 810 | std::unique_ptr<ThirdPartyURLLoaderInterceptor> request_interceptor_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 811 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 812 | // Set to 2G in SetUpCommandLine(). |
| 813 | net::EffectiveConnectionType expected_ect = net::EFFECTIVE_CONNECTION_TYPE_2G; |
| 814 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 815 | DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTest); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 816 | }; |
| 817 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 818 | // True if testing for http-equiv correctness. When set to true, the tests |
| 819 | // use webpages that may contain http-equiv Accept-CH and Accept-CH-Lifetime |
| 820 | // headers. When set to false, the tests use webpages that set the headers in |
| 821 | // the HTTP response headers. |
Ilia Samsonov | 282c3841 | 2019-12-09 08:15:10 | [diff] [blame] | 822 | INSTANTIATE_TEST_SUITE_P(All, |
Victor Costan | e5e9151 | 2019-02-13 08:24:02 | [diff] [blame] | 823 | ClientHintsBrowserTest, |
| 824 | testing::Bool()); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 825 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 826 | class ClientHintsAllowThirdPartyBrowserTest : public ClientHintsBrowserTest { |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 827 | std::unique_ptr<base::FeatureList> EnabledFeatures() override { |
| 828 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 829 | feature_list->InitializeFromCommandLine( |
François Beaufort | 1b51d06 | 2021-05-18 11:11:23 | [diff] [blame] | 830 | "AllowClientHintsToThirdParty,UserAgentClientHint," |
| 831 | "LangClientHintHeader,PrefersColorSchemeClientHintHeader", |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 832 | ""); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 833 | return feature_list; |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 834 | } |
| 835 | }; |
| 836 | |
Ilia Samsonov | 282c3841 | 2019-12-09 08:15:10 | [diff] [blame] | 837 | INSTANTIATE_TEST_SUITE_P(All, |
Aaron Tagliaboschi | a09ec44 | 2019-09-18 15:29:03 | [diff] [blame] | 838 | ClientHintsAllowThirdPartyBrowserTest, |
| 839 | testing::Bool()); |
| 840 | |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 841 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, CorsChecks) { |
Mike West | 14c1110 | 2019-02-04 16:16:47 | [diff] [blame] | 842 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; ++i) { |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 843 | // Do not test for headers that have not been enabled on the blink "stable" |
| 844 | // yet. |
| 845 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "rtt" || |
| 846 | std::string(blink::kClientHintsHeaderMapping[i]) == "downlink" || |
| 847 | std::string(blink::kClientHintsHeaderMapping[i]) == "ect") { |
| 848 | continue; |
| 849 | } |
Takashi Toyoshima | 2e01e69 | 2018-11-16 03:23:27 | [diff] [blame] | 850 | EXPECT_TRUE(network::cors::IsCorsSafelistedHeader( |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 851 | blink::kClientHintsHeaderMapping[i], "42" /* value */)); |
| 852 | } |
Takashi Toyoshima | 2e01e69 | 2018-11-16 03:23:27 | [diff] [blame] | 853 | EXPECT_FALSE(network::cors::IsCorsSafelistedHeader("not-a-client-hint-header", |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 854 | "" /* value */)); |
| 855 | EXPECT_TRUE( |
Takashi Toyoshima | 2e01e69 | 2018-11-16 03:23:27 | [diff] [blame] | 856 | network::cors::IsCorsSafelistedHeader("save-data", "on" /* value */)); |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 857 | } |
| 858 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 859 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, HttpEquivWorks) { |
| 860 | const GURL gurl = http_equiv_accept_ch_without_lifetime_img_localhost(); |
| 861 | base::HistogramTester histogram_tester; |
| 862 | |
| 863 | SetClientHintExpectationsOnMainFrame(false); |
| 864 | SetClientHintExpectationsOnSubresources(true); |
| 865 | |
| 866 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 867 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 868 | } |
| 869 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 870 | // Loads a webpage that requests persisting of client hints. Verifies that |
| 871 | // the browser receives the mojo notification from the renderer and persists the |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 872 | // client hints to the disk --- unless it's using http-equiv which shouldn't |
| 873 | // persist. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 874 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, ClientHintsHttps) { |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 875 | base::HistogramTester histogram_tester; |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 876 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 877 | : accept_ch_with_lifetime_url(); |
| 878 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 879 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 880 | if (GetParam()) |
| 881 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 882 | else |
| 883 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 884 | |
| 885 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 886 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 887 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 888 | if (GetParam()) { |
| 889 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 890 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 891 | } else { |
| 892 | // client_hints_url() sets the expected number of client hints. |
| 893 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 894 | expected_client_hints_number, 1); |
| 895 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 896 | // seconds, but a maximum value is registered instead. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 897 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 898 | uma_histogram_max_value, 1); |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 899 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 900 | } |
| 901 | |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 902 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, PRE_ClientHintsClearSession) { |
| 903 | const GURL gurl = accept_ch_with_lifetime_url(); |
| 904 | |
| 905 | base::HistogramTester histogram_tester; |
| 906 | ContentSettingsForOneType host_settings; |
| 907 | |
| 908 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 909 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 910 | &host_settings); |
| 911 | EXPECT_EQ(0u, host_settings.size()); |
| 912 | |
| 913 | // Fetching |gurl| should persist the request for client hints iff using |
| 914 | // headers and not http-equiv. |
| 915 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 916 | |
| 917 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 918 | |
| 919 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 920 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 921 | base::RunLoop().RunUntilIdle(); |
| 922 | |
| 923 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 924 | expected_client_hints_number, 1); |
| 925 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 926 | // seconds, but a maximum value is registered instead. |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 927 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 928 | uma_histogram_max_value, 1); |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 929 | |
| 930 | // Clients hints preferences for one origin should be persisted. |
| 931 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 932 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 933 | &host_settings); |
| 934 | EXPECT_EQ(1u, host_settings.size()); |
| 935 | |
| 936 | SetClientHintExpectationsOnMainFrame(true); |
| 937 | SetClientHintExpectationsOnSubresources(true); |
| 938 | ui_test_utils::NavigateToURL(browser(), |
| 939 | without_accept_ch_without_lifetime_url()); |
| 940 | |
| 941 | // The user agent hint is attached to all three requests: |
| 942 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
| 943 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 944 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 945 | |
| 946 | // Expected number of hints attached to the image request, and the same number |
| 947 | // to the main frame request. |
| 948 | EXPECT_EQ(expected_client_hints_number * 2, |
| 949 | count_client_hints_headers_seen()); |
| 950 | } |
| 951 | |
| 952 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsClearSession) { |
| 953 | const GURL gurl = accept_ch_with_lifetime_url(); |
| 954 | |
| 955 | base::HistogramTester histogram_tester; |
| 956 | ContentSettingsForOneType host_settings; |
| 957 | |
| 958 | // Clients hints preferences for one origin should be persisted. |
| 959 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 960 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 961 | &host_settings); |
| 962 | EXPECT_EQ(0u, host_settings.size()); |
| 963 | |
| 964 | SetClientHintExpectationsOnMainFrame(false); |
| 965 | SetClientHintExpectationsOnSubresources(false); |
| 966 | ui_test_utils::NavigateToURL(browser(), |
| 967 | without_accept_ch_without_lifetime_url()); |
| 968 | |
| 969 | // The user agent hint is attached to all three requests: |
| 970 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
| 971 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 972 | EXPECT_EQ(2u, count_ua_platform_client_hints_headers_seen()); |
Aaron Tagliaboschi | 8ef04eca | 2020-05-12 23:16:44 | [diff] [blame] | 973 | |
| 974 | // Expected number of hints attached to the image request, and the same number |
| 975 | // to the main frame request. |
| 976 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
| 977 | } |
| 978 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 979 | // Test that client hints are attached to subresources only if they belong |
| 980 | // to the same host as document host. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 981 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 982 | ClientHintsHttpsSubresourceDifferentOrigin) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 983 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 984 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 985 | base::HistogramTester histogram_tester; |
| 986 | |
| 987 | // Add client hints for the embedded test server. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 988 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 989 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 990 | |
| 991 | // Verify that the client hints settings for localhost have been saved. |
| 992 | ContentSettingsForOneType client_hints_settings; |
| 993 | HostContentSettingsMap* host_content_settings_map = |
| 994 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 995 | host_content_settings_map->GetSettingsForOneType( |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 996 | ContentSettingsType::CLIENT_HINTS, &client_hints_settings); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 997 | ASSERT_EQ(1U, client_hints_settings.size()); |
| 998 | |
| 999 | // Copy the client hints setting for localhost to foo.com. |
| 1000 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1001 | GURL("https://foo.com/"), GURL(), ContentSettingsType::CLIENT_HINTS, |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1002 | |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 1003 | std::make_unique<base::Value>( |
Oksana Zhuravlova | b14dc88 | 2018-04-12 17:34:57 | [diff] [blame] | 1004 | client_hints_settings.at(0).setting_value.Clone())); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1005 | |
| 1006 | // Verify that client hints for the two hosts has been saved. |
| 1007 | host_content_settings_map = |
| 1008 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 1009 | host_content_settings_map->GetSettingsForOneType( |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1010 | ContentSettingsType::CLIENT_HINTS, &client_hints_settings); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1011 | ASSERT_EQ(2U, client_hints_settings.size()); |
| 1012 | |
| 1013 | // Navigating to without_accept_ch_without_lifetime_img_localhost() should |
| 1014 | // attach client hints to the image subresouce contained in that page since |
| 1015 | // the image is located on the same server as the document origin. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1016 | SetClientHintExpectationsOnMainFrame(true); |
| 1017 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1018 | ui_test_utils::NavigateToURL( |
| 1019 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 1020 | base::RunLoop().RunUntilIdle(); |
| 1021 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1022 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1023 | |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1024 | // The user agent hint is attached to all three requests, as is UA-mobile: |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1025 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1026 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1027 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1028 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1029 | // Expected number of hints attached to the image request, and the same number |
| 1030 | // to the main frame request. |
| 1031 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1032 | count_client_hints_headers_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1033 | |
| 1034 | // Navigating to without_accept_ch_without_lifetime_img_foo_com() should not |
| 1035 | // attach client hints to the image subresouce contained in that page since |
| 1036 | // the image is located on a different server as the document origin. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1037 | ui_test_utils::NavigateToURL( |
| 1038 | browser(), without_accept_ch_without_lifetime_img_foo_com()); |
| 1039 | base::RunLoop().RunUntilIdle(); |
| 1040 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1041 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1042 | |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 1043 | // The device-memory and dprheader is attached to the main frame request. |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 1044 | #if defined(OS_ANDROID) |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1045 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 1046 | #else |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1047 | EXPECT_EQ(expected_client_hints_number * 3, |
| 1048 | count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 1049 | #endif |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1050 | |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1051 | // Requests to third party servers should have three (3) client hints attached |
| 1052 | // (`Sec-CH-UA`, `Sec-CH-UA-Mobile`, `Sec-CH-UA-Platform`). |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1053 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1054 | EXPECT_EQ(3u, third_party_client_hints_count_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1055 | } |
| 1056 | |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1057 | // Test that client hints are attached to subresources checks the right setting |
| 1058 | // for OTR profile. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1059 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1060 | ClientHintsHttpsSubresourceOffTheRecord) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1061 | const GURL gurl = accept_ch_with_lifetime_url(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1062 | |
| 1063 | base::HistogramTester histogram_tester; |
| 1064 | |
| 1065 | // Add client hints for the embedded test server. |
| 1066 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1067 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1068 | |
| 1069 | // Main profile should get hints for both page and subresources. |
| 1070 | SetClientHintExpectationsOnMainFrame(true); |
| 1071 | SetClientHintExpectationsOnSubresources(true); |
| 1072 | ui_test_utils::NavigateToURL( |
| 1073 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 1074 | base::RunLoop().RunUntilIdle(); |
| 1075 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1076 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1077 | |
| 1078 | // The user agent hint is attached to all three requests: |
| 1079 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
| 1080 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1081 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1082 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1083 | // Expected number of hints attached to the image request, and the same number |
| 1084 | // to the main frame request. |
| 1085 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1086 | count_client_hints_headers_seen()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1087 | |
| 1088 | // OTR profile should get neither. |
| 1089 | Browser* otr_browser = CreateIncognitoBrowser(browser()->profile()); |
| 1090 | SetClientHintExpectationsOnMainFrame(false); |
| 1091 | SetClientHintExpectationsOnSubresources(false); |
| 1092 | ui_test_utils::NavigateToURL( |
| 1093 | otr_browser, without_accept_ch_without_lifetime_img_localhost()); |
| 1094 | } |
| 1095 | |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1096 | // Verify that we send only major version information in the `Sec-CH-UA` header |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1097 | // by default, regardless of opt-in. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1098 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, UserAgentVersion) { |
| 1099 | const GURL gurl = accept_ch_with_lifetime_url(); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1100 | |
John Abd-El-Malek | ec1fc69e | 2021-01-28 19:14:41 | [diff] [blame] | 1101 | blink::UserAgentMetadata ua = embedder_support::GetUserAgentMetadata(); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1102 | |
| 1103 | // Navigate to a page that opts-into the header: the value should end with |
| 1104 | // the major version, and not contain the full version. |
| 1105 | SetClientHintExpectationsOnMainFrame(false); |
| 1106 | ui_test_utils::NavigateToURL(browser(), gurl); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1107 | std::string expected_ua = ua.SerializeBrandVersionList(); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1108 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1109 | EXPECT_TRUE(main_frame_ua_full_version_observed().empty()); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1110 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1111 | // Navigate again, after the opt-in: the value should stay the major |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1112 | // version. |
| 1113 | SetClientHintExpectationsOnMainFrame(true); |
| 1114 | ui_test_utils::NavigateToURL(browser(), gurl); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1115 | std::string expected_full_version = "\"" + ua.full_version + "\""; |
| 1116 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1117 | EXPECT_EQ(main_frame_ua_full_version_observed(), expected_full_version); |
Mike West | e555be86 | 2019-02-20 16:17:30 | [diff] [blame] | 1118 | } |
| 1119 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1120 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, UAHintsTabletMode) { |
| 1121 | const GURL gurl = accept_ch_with_lifetime_url(); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1122 | |
John Abd-El-Malek | ec1fc69e | 2021-01-28 19:14:41 | [diff] [blame] | 1123 | blink::UserAgentMetadata ua = embedder_support::GetUserAgentMetadata(); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1124 | |
| 1125 | // First request: only minimal hints, no tablet override. |
| 1126 | SetClientHintExpectationsOnMainFrame(false); |
| 1127 | ui_test_utils::NavigateToURL(browser(), gurl); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1128 | std::string expected_ua = ua.SerializeBrandVersionList(); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1129 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1130 | EXPECT_EQ(main_frame_ua_full_version_observed(), ""); |
| 1131 | EXPECT_EQ(main_frame_ua_mobile_observed(), "?0"); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1132 | EXPECT_EQ(main_frame_ua_platform_observed(), "\"" + ua.platform + "\""); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1133 | |
| 1134 | // Second request: table override, all hints. |
| 1135 | chrome::ToggleRequestTabletSite(browser()); |
| 1136 | SetClientHintExpectationsOnMainFrame(true); |
| 1137 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1138 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1139 | std::string expected_full_version = "\"" + ua.full_version + "\""; |
| 1140 | EXPECT_EQ(main_frame_ua_full_version_observed(), expected_full_version); |
| 1141 | EXPECT_EQ(main_frame_ua_mobile_observed(), "?1"); |
| 1142 | EXPECT_EQ(main_frame_ua_platform_observed(), "\"Android\""); |
| 1143 | } |
| 1144 | |
| 1145 | // TODO(morlovich): Move this into WebContentsImplBrowserTest once things are |
| 1146 | // refactored enough that UA client hints actually work in content/ |
| 1147 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, UserAgentOverrideClientHints) { |
| 1148 | content::WebContents* web_contents = |
| 1149 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 1150 | |
| 1151 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1152 | const std::string kHeaderPath = std::string("/echoheader?") + |
| 1153 | net::HttpRequestHeaders::kUserAgent + |
| 1154 | "&sec-ch-ua&sec-ch-ua-mobile"; |
| 1155 | const GURL kUrl(embedded_test_server()->GetURL(kHeaderPath)); |
| 1156 | |
| 1157 | web_contents->SetUserAgentOverride( |
| 1158 | blink::UserAgentOverride::UserAgentOnly("foo"), false); |
| 1159 | // Not enabled first. |
| 1160 | ui_test_utils::NavigateToURL(browser(), kUrl); |
| 1161 | std::string header_value; |
| 1162 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1163 | web_contents, |
| 1164 | "window.domAutomationController.send(document.body.textContent);", |
| 1165 | &header_value)); |
| 1166 | EXPECT_EQ(std::string::npos, header_value.find("foo")) << header_value; |
| 1167 | |
| 1168 | // Actually turn it on. |
| 1169 | web_contents->GetController() |
| 1170 | .GetLastCommittedEntry() |
| 1171 | ->SetIsOverridingUserAgent(true); |
| 1172 | |
| 1173 | ui_test_utils::NavigateToURL(browser(), kUrl); |
| 1174 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1175 | web_contents, |
| 1176 | "window.domAutomationController.send(document.body.textContent);", |
| 1177 | &header_value)); |
| 1178 | // Since no value was provided for client hints, they are not sent. |
| 1179 | EXPECT_EQ("foo\nNone\nNone", header_value); |
| 1180 | |
| 1181 | // Now actually provide values for the hints. |
| 1182 | blink::UserAgentOverride ua_override; |
| 1183 | ua_override.ua_string_override = "foobar"; |
| 1184 | ua_override.ua_metadata_override.emplace(); |
| 1185 | ua_override.ua_metadata_override->mobile = true; |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1186 | ua_override.ua_metadata_override->brand_version_list.emplace_back( |
| 1187 | "Foobarnator", "3.14"); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1188 | web_contents->SetUserAgentOverride(ua_override, false); |
| 1189 | ui_test_utils::NavigateToURL(browser(), kUrl); |
| 1190 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1191 | web_contents, |
| 1192 | "window.domAutomationController.send(document.body.textContent);", |
| 1193 | &header_value)); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1194 | EXPECT_EQ("foobar\n\"Foobarnator\";v=\"3.14\"\n?1", header_value); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1195 | } |
| 1196 | |
Maks Orlovich | 7a588d8 | 2020-05-06 15:17:24 | [diff] [blame] | 1197 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, EmptyAcceptCH) { |
| 1198 | // First navigate to a page that enables hints. No CH for it yet, since |
| 1199 | // nothing opted in. |
| 1200 | GURL gurl = accept_ch_with_lifetime_url(); |
| 1201 | SetClientHintExpectationsOnMainFrame(false); |
| 1202 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1203 | |
| 1204 | // Now go to a page with blank Accept-CH. Should get hints from previous |
| 1205 | // visit. |
| 1206 | gurl = accept_ch_empty(); |
| 1207 | SetClientHintExpectationsOnMainFrame(true); |
| 1208 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1209 | |
| 1210 | // Visiting again should not expect them since we opted out again. |
| 1211 | SetClientHintExpectationsOnMainFrame(false); |
| 1212 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1213 | } |
| 1214 | |
| 1215 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, MergeAcceptCH) { |
| 1216 | // Go to page where some hints are enabled by headers, some by |
| 1217 | // http-equiv. It shouldn't get hints itself (due to first visit), |
| 1218 | // but subresources should get all the client hints. |
| 1219 | GURL gurl = http_equiv_accept_ch_merge(); |
| 1220 | SetClientHintExpectationsOnMainFrame(false); |
| 1221 | SetClientHintExpectationsOnSubresources(true); |
| 1222 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1223 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
| 1224 | } |
| 1225 | |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1226 | void ClientHintsBrowserTest::TestProfilesIndependent(Browser* browser_a, |
| 1227 | Browser* browser_b) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1228 | const GURL gurl = accept_ch_with_lifetime_url(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1229 | |
John Abd-El-Malek | ec1fc69e | 2021-01-28 19:14:41 | [diff] [blame] | 1230 | blink::UserAgentMetadata ua = embedder_support::GetUserAgentMetadata(); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1231 | |
| 1232 | // Navigate |browser_a| to a page that opts-into the header: the value should |
| 1233 | // end with the major version, and not contain the full version. |
| 1234 | SetClientHintExpectationsOnMainFrame(false); |
| 1235 | ui_test_utils::NavigateToURL(browser_a, gurl); |
Aaron Tagliaboschi | 9f01b68 | 2020-05-05 21:03:17 | [diff] [blame] | 1236 | std::string expected_ua = ua.SerializeBrandVersionList(); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1237 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1238 | EXPECT_TRUE(main_frame_ua_full_version_observed().empty()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1239 | |
| 1240 | // Try again on |browser_a|, the header should have an effect there. |
| 1241 | SetClientHintExpectationsOnMainFrame(true); |
| 1242 | ui_test_utils::NavigateToURL(browser_a, gurl); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1243 | std::string expected_full_version = "\"" + ua.full_version + "\""; |
| 1244 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1245 | EXPECT_EQ(main_frame_ua_full_version_observed(), expected_full_version); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1246 | |
| 1247 | // Navigate on |browser_b|. That should still only have the major |
| 1248 | // version. |
| 1249 | SetClientHintExpectationsOnMainFrame(false); |
| 1250 | ui_test_utils::NavigateToURL(browser_b, gurl); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1251 | EXPECT_EQ(main_frame_ua_observed(), expected_ua); |
| 1252 | EXPECT_TRUE(main_frame_ua_full_version_observed().empty()); |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | // Check that client hints attached to navigation inside OTR profiles |
| 1256 | // use the right settings, regular -> OTR direction. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1257 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, OffTheRecordIndependent) { |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1258 | TestProfilesIndependent(browser(), |
| 1259 | CreateIncognitoBrowser(browser()->profile())); |
| 1260 | } |
| 1261 | |
| 1262 | // Check that client hints attached to navigation inside OTR profiles |
| 1263 | // use the right settings, OTR -> regular direction. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1264 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, OffTheRecordIndependent2) { |
Maks Orlovich | 7227ce0d | 2020-02-28 17:13:16 | [diff] [blame] | 1265 | TestProfilesIndependent(CreateIncognitoBrowser(browser()->profile()), |
| 1266 | browser()); |
| 1267 | } |
| 1268 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1269 | // Test that client hints are attached to third party subresources if |
| 1270 | // AllowClientHintsToThirdParty feature is enabled. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1271 | IN_PROC_BROWSER_TEST_P(ClientHintsAllowThirdPartyBrowserTest, |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1272 | ClientHintsThirdPartyAllowed) { |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1273 | GURL gurl; |
| 1274 | unsigned update_event_count = 0; |
| 1275 | if (GetParam()) { |
| 1276 | gurl = http_equiv_accept_ch_without_lifetime_img_localhost(); |
| 1277 | } else { |
| 1278 | gurl = accept_ch_without_lifetime_img_localhost(); |
| 1279 | update_event_count = 1; |
| 1280 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1281 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1282 | base::HistogramTester histogram_tester; |
| 1283 | |
| 1284 | SetClientHintExpectationsOnMainFrame(false); |
| 1285 | SetClientHintExpectationsOnSubresources(true); |
| 1286 | |
| 1287 | // Add client hints for the embedded test server. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1288 | ui_test_utils::NavigateToURL(browser(), gurl); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1289 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", |
| 1290 | update_event_count); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1291 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1292 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1293 | |
| 1294 | // Requests to third party servers should not have client hints attached. |
| 1295 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 1296 | |
Aaron Tagliaboschi | a09ec44 | 2019-09-18 15:29:03 | [diff] [blame] | 1297 | // Device memory, viewport width, DRP, and UA client hints should be sent to |
| 1298 | // the third-party when feature "AllowClientHintsToThirdParty" is enabled. |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1299 | EXPECT_EQ(6u, third_party_client_hints_count_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | // Test that client hints are not attached to third party subresources if |
| 1303 | // AllowClientHintsToThirdParty feature is not enabled. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1304 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1305 | ClientHintsThirdPartyNotAllowed) { |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1306 | GURL gurl; |
| 1307 | unsigned update_event_count = 0; |
| 1308 | if (GetParam()) { |
| 1309 | gurl = http_equiv_accept_ch_without_lifetime_img_localhost(); |
| 1310 | } else { |
| 1311 | gurl = accept_ch_without_lifetime_img_localhost(); |
| 1312 | update_event_count = 1; |
| 1313 | } |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1314 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1315 | base::HistogramTester histogram_tester; |
| 1316 | |
| 1317 | SetClientHintExpectationsOnMainFrame(false); |
| 1318 | SetClientHintExpectationsOnSubresources(true); |
| 1319 | |
| 1320 | // Add client hints for the embedded test server. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1321 | ui_test_utils::NavigateToURL(browser(), gurl); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1322 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", |
| 1323 | update_event_count); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1324 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1325 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1326 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1327 | EXPECT_EQ(2u, count_ua_platform_client_hints_headers_seen()); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1328 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1329 | |
| 1330 | // Requests to third party servers should not have client hints attached. |
| 1331 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 1332 | |
| 1333 | // Client hints should not be sent to the third-party when feature |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1334 | // "AllowClientHintsToThirdParty" is not enabled, with the exception of the |
| 1335 | // `Sec-CH-UA` hint, which is sent with every request. |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1336 | EXPECT_EQ(3u, third_party_client_hints_count_seen()); |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 1337 | } |
| 1338 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1339 | // Loads a HTTPS webpage that does not request persisting of client hints. |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1340 | // A same-origin iframe loaded by the webpage requests persistence of client |
Maks Orlovich | 46619c73 | 2020-05-07 10:40:53 | [diff] [blame] | 1341 | // hints. Since that's not a main frame, persistence should not happen. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1342 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1343 | PersistenceRequestIframe_SameOrigin) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1344 | const GURL gurl = accept_ch_without_lifetime_with_iframe_url(); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1345 | base::HistogramTester histogram_tester; |
| 1346 | ContentSettingsForOneType host_settings; |
| 1347 | |
| 1348 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1349 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1350 | &host_settings); |
| 1351 | EXPECT_EQ(0u, host_settings.size()); |
| 1352 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1353 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1354 | |
Maks Orlovich | 46619c73 | 2020-05-07 10:40:53 | [diff] [blame] | 1355 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1356 | |
| 1357 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1358 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1359 | |
| 1360 | // accept_ch_without_lifetime_with_iframe_url() loads |
| 1361 | // accept_ch_with_lifetime() in an iframe. The request to persist client |
Maks Orlovich | 46619c73 | 2020-05-07 10:40:53 | [diff] [blame] | 1362 | // hints from accept_ch_with_lifetime() should not be persisted. |
| 1363 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1364 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 1368 | // An iframe loaded by the webpage from an cross origin server requests |
| 1369 | // persistence of client hints. |
| 1370 | // Verify that the request from the cross origin iframe is not honored, and |
| 1371 | // client hints preference is not persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1372 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1373 | DisregardPersistenceRequestIframe_CrossOrigin) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1374 | const GURL gurl = |
| 1375 | GetParam() ? http_equiv_accept_ch_without_lifetime_with_iframe_url() |
| 1376 | : accept_ch_without_lifetime_with_iframe_url(); |
| 1377 | |
| 1378 | intercept_iframe_resource_ = gurl.path(); |
| 1379 | intercept_to_http_equiv_iframe_ = GetParam(); |
| 1380 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1381 | base::HistogramTester histogram_tester; |
| 1382 | ContentSettingsForOneType host_settings; |
| 1383 | |
| 1384 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1385 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1386 | &host_settings); |
| 1387 | EXPECT_EQ(0u, host_settings.size()); |
| 1388 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1389 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1390 | |
| 1391 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1392 | |
| 1393 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1394 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1395 | |
| 1396 | // accept_ch_without_lifetime_with_iframe_url() loads |
Tarun Bansal | 34541863 | 2018-06-29 11:07:04 | [diff] [blame] | 1397 | // accept_ch_with_lifetime() in a cross origin iframe. The request to persist |
| 1398 | // client hints from accept_ch_with_lifetime() should be disregarded. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 1399 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1400 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1401 | } |
| 1402 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1403 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 1404 | // A subresource loaded by the webpage requests persistence of client hints. |
| 1405 | // Verify that the request from the subresource is not honored, and client hints |
| 1406 | // preference is not persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1407 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1408 | DisregardPersistenceRequestSubresource) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1409 | const GURL gurl = |
| 1410 | GetParam() ? http_equiv_accept_ch_without_lifetime_with_subresource_url() |
| 1411 | : accept_ch_without_lifetime_with_subresource_url(); |
| 1412 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1413 | base::HistogramTester histogram_tester; |
| 1414 | ContentSettingsForOneType host_settings; |
| 1415 | |
| 1416 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1417 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1418 | &host_settings); |
| 1419 | EXPECT_EQ(0u, host_settings.size()); |
| 1420 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1421 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1422 | |
| 1423 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1424 | |
| 1425 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1426 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1427 | |
| 1428 | // accept_ch_without_lifetime_with_subresource_url() loads |
| 1429 | // accept_ch_with_lifetime() as a subresource. The request to persist client |
| 1430 | // hints from accept_ch_with_lifetime() should be disregarded. |
| 1431 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1432 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1433 | } |
| 1434 | |
| 1435 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 1436 | // A subresource loaded by the webpage in an iframe requests persistence of |
| 1437 | // client hints. Verify that the request from the subresource in the iframe |
| 1438 | // is not honored, and client hints preference is not persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1439 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1440 | DisregardPersistenceRequestSubresourceIframe) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1441 | const GURL gurl = |
| 1442 | GetParam() |
| 1443 | ? http_equiv_accept_ch_without_lifetime_with_subresource_iframe_url() |
| 1444 | : accept_ch_without_lifetime_with_subresource_iframe_url(); |
| 1445 | |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1446 | base::HistogramTester histogram_tester; |
| 1447 | ContentSettingsForOneType host_settings; |
| 1448 | |
| 1449 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1450 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1451 | &host_settings); |
| 1452 | EXPECT_EQ(0u, host_settings.size()); |
| 1453 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1454 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1455 | |
| 1456 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1457 | |
| 1458 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1459 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1460 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1461 | // |gurl| loads accept_ch_with_lifetime() or |
| 1462 | // http_equiv_accept_ch_with_lifetime() as a subresource in an iframe. The |
| 1463 | // request to persist client hints from accept_ch_with_lifetime() or |
| 1464 | // http_equiv_accept_ch_with_lifetime() should be disregarded. |
Tarun Bansal | 62cc354 | 2018-06-27 23:53:30 | [diff] [blame] | 1465 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1466 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1467 | } |
| 1468 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1469 | // Loads a HTTP local webpage (which qualifies as a secure context) that |
| 1470 | // requests persisting of client hints. Verifies that the browser receives the |
| 1471 | // mojo notification from the renderer and persists the client hints to the |
| 1472 | // disk. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1473 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1474 | ClientHintsLifetimeFollowedByNoClientHintHttpLocal) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1475 | const GURL gurl = accept_ch_with_lifetime_http_local_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1476 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1477 | base::HistogramTester histogram_tester; |
| 1478 | ContentSettingsForOneType host_settings; |
| 1479 | |
| 1480 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1481 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1482 | &host_settings); |
| 1483 | EXPECT_EQ(0u, host_settings.size()); |
| 1484 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1485 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1486 | |
| 1487 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1488 | |
| 1489 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1490 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1491 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1492 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1493 | expected_client_hints_number, 1); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1494 | // |gurl| sets client hints persist duration to 3600 seconds, but a maximum |
| 1495 | // value is registered instead. |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1496 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1497 | uma_histogram_max_value, 1); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1498 | |
| 1499 | base::RunLoop().RunUntilIdle(); |
| 1500 | |
| 1501 | // Clients hints preferences for one origin should be persisted. |
| 1502 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1503 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1504 | &host_settings); |
| 1505 | EXPECT_EQ(1u, host_settings.size()); |
| 1506 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1507 | SetClientHintExpectationsOnMainFrame(true); |
| 1508 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1509 | ui_test_utils::NavigateToURL(browser(), |
| 1510 | without_accept_ch_without_lifetime_local_url()); |
| 1511 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1512 | // The user agent hint is attached to all three requests: |
| 1513 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1514 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1515 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1516 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1517 | // Expected number of hints attached to the image request, and the same number |
| 1518 | // to the main frame request. |
| 1519 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1520 | count_client_hints_headers_seen()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 1521 | } |
| 1522 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1523 | // Loads a webpage that does not request persisting of client hints. |
| 1524 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, NoClientHintsHttps) { |
| 1525 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1526 | ui_test_utils::NavigateToURL(browser(), |
| 1527 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1528 | |
| 1529 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1530 | |
| 1531 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1532 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1533 | |
| 1534 | // no_client_hints_url() does not sets the client hints. |
| 1535 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1536 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1537 | } |
| 1538 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1539 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1540 | ClientHintsLifetimeFollowedByNoClientHint) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1541 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 1542 | : accept_ch_with_lifetime_url(); |
| 1543 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1544 | base::HistogramTester histogram_tester; |
| 1545 | ContentSettingsForOneType host_settings; |
| 1546 | |
| 1547 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1548 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1549 | &host_settings); |
| 1550 | EXPECT_EQ(0u, host_settings.size()); |
| 1551 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1552 | // Fetching |gurl| should persist the request for client hints iff using |
| 1553 | // headers and not http-equiv. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1554 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1555 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1556 | if (GetParam()) |
| 1557 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 1558 | else |
| 1559 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1560 | |
| 1561 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1562 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1563 | base::RunLoop().RunUntilIdle(); |
| 1564 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1565 | if (GetParam()) { |
| 1566 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 1567 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 1568 | } else { |
| 1569 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1570 | expected_client_hints_number, 1); |
| 1571 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1572 | // seconds, but a maximum value is registered instead. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1573 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1574 | uma_histogram_max_value, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1575 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1576 | // Clients hints preferences for one origin should be persisted. |
| 1577 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1578 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1579 | &host_settings); |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1580 | EXPECT_EQ(1u, host_settings.size()); |
| 1581 | } |
| 1582 | |
| 1583 | SetClientHintExpectationsOnMainFrame(!GetParam()); |
| 1584 | SetClientHintExpectationsOnSubresources(!GetParam()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1585 | ui_test_utils::NavigateToURL(browser(), |
| 1586 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 1587 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1588 | // The user agent hint is attached to all three requests: |
| 1589 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1590 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1591 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1592 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1593 | // Expected number of hints attached to the image request, and the same number |
| 1594 | // to the main frame request. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1595 | EXPECT_EQ(GetParam() ? 0 : expected_client_hints_number * 2, |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1596 | count_client_hints_headers_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1597 | } |
| 1598 | |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1599 | // The test first fetches a page that sets Accept-CH-Lifetime. Next, it fetches |
| 1600 | // a URL from a different origin. However, that URL response redirects to the |
| 1601 | // same origin from where the first page was fetched. The test verifies that |
| 1602 | // on receiving redirect to an origin for which the browser has persisted client |
| 1603 | // hints prefs, the browser attaches the client hints headers when fetching the |
| 1604 | // redirected URL. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1605 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1606 | ClientHintsLifetimeFollowedByRedirectToNoClientHint) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1607 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1608 | |
| 1609 | base::HistogramTester histogram_tester; |
| 1610 | ContentSettingsForOneType host_settings; |
| 1611 | |
| 1612 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1613 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1614 | &host_settings); |
| 1615 | EXPECT_EQ(0u, host_settings.size()); |
| 1616 | |
| 1617 | // Fetching |gurl| should persist the request for client hints. |
| 1618 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 1619 | |
| 1620 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1621 | |
| 1622 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1623 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1624 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1625 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1626 | expected_client_hints_number, 1); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1627 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1628 | // seconds, but a maximum value is registered instead. |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1629 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1630 | uma_histogram_max_value, 1); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1631 | base::RunLoop().RunUntilIdle(); |
| 1632 | |
| 1633 | // Clients hints preferences for one origin should be persisted. |
| 1634 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1635 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1636 | &host_settings); |
| 1637 | EXPECT_EQ(1u, host_settings.size()); |
| 1638 | |
| 1639 | SetClientHintExpectationsOnMainFrame(true); |
| 1640 | SetClientHintExpectationsOnSubresources(true); |
| 1641 | ui_test_utils::NavigateToURL(browser(), redirect_url()); |
| 1642 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1643 | // The user agent hint is attached to all three requests: |
| 1644 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1645 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1646 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1647 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1648 | // Expected number of hints attached to the image request, and the same number |
| 1649 | // to the main frame request. |
| 1650 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1651 | count_client_hints_headers_seen()); |
Tarun Bansal | 293a7b6c | 2018-12-05 17:41:12 | [diff] [blame] | 1652 | } |
| 1653 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1654 | // Ensure that even when cookies are blocked, client hint preferences are |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1655 | // persisted. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1656 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1657 | ClientHintsLifetimePersistedCookiesBlocked) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1658 | const GURL gurl_with = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1659 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1660 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 1661 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 1662 | base::HistogramTester histogram_tester; |
| 1663 | ContentSettingsForOneType host_settings; |
| 1664 | |
| 1665 | // Block cookies. |
| 1666 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1667 | ->SetContentSettingDefaultScope(gurl_with, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1668 | ContentSettingsType::COOKIES, |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1669 | CONTENT_SETTING_BLOCK); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1670 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1671 | // Fetching |gurl_with| should persist the request for client hints. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1672 | ui_test_utils::NavigateToURL(browser(), gurl_with); |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1673 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1674 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1675 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1676 | &host_settings); |
| 1677 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1678 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1679 | } |
| 1680 | |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1681 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1682 | ClientHintsLifetimeAttachedCookiesBlocked) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1683 | const GURL gurl_with = accept_ch_with_lifetime_url(); |
| 1684 | const GURL gurl_without = accept_ch_without_lifetime_url(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1685 | base::HistogramTester histogram_tester; |
| 1686 | ContentSettingsForOneType host_settings; |
| 1687 | |
| 1688 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1689 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1690 | &host_settings); |
| 1691 | EXPECT_EQ(0u, host_settings.size()); |
| 1692 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1693 | // Fetching |gurl_with| should persist the request for client hints. |
| 1694 | ui_test_utils::NavigateToURL(browser(), gurl_with); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1695 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1696 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1697 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1698 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1699 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1700 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1701 | expected_client_hints_number, 1); |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1702 | // |gurl_with| tries to set client hints persist duration to 3600 seconds, but |
| 1703 | // a maximum value is registered instead. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1704 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1705 | uma_histogram_max_value, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1706 | |
| 1707 | // Clients hints preferences for one origin should be persisted. |
| 1708 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1709 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1710 | &host_settings); |
| 1711 | EXPECT_EQ(1u, host_settings.size()); |
| 1712 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1713 | // Block the cookies: Client hints should be attached. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1714 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1715 | ->SetContentSettingDefaultScope(gurl_without, GURL(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1716 | ContentSettingsType::COOKIES, |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1717 | CONTENT_SETTING_BLOCK); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1718 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1719 | SetClientHintExpectationsOnMainFrame(true); |
| 1720 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1721 | ui_test_utils::NavigateToURL(browser(), |
| 1722 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 1723 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1724 | // The user agent hint is attached to all three requests: |
| 1725 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1726 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1727 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1728 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1729 | // Expected number of hints attached to the image request, and the same number |
| 1730 | // to the main frame request. |
| 1731 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1732 | count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1733 | |
| 1734 | // Clear settings. |
| 1735 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1736 | ->ClearSettingsForOneType(ContentSettingsType::COOKIES); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | // Ensure that when the JavaScript is blocked, client hint preferences are not |
| 1740 | // persisted. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1741 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1742 | ClientHintsLifetimeNotPersistedJavaScriptBlocked) { |
| 1743 | ContentSettingsForOneType host_settings; |
| 1744 | |
| 1745 | // Start a navigation. This navigation makes it possible to block JavaScript |
| 1746 | // later. |
| 1747 | ui_test_utils::NavigateToURL(browser(), |
| 1748 | without_accept_ch_without_lifetime_url()); |
| 1749 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1750 | const GURL gurl = |
| 1751 | GetParam() ? http_equiv_accept_ch_without_lifetime_with_iframe_url() |
| 1752 | : accept_ch_with_lifetime_url(); |
| 1753 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1754 | // Block the JavaScript: Client hint preferences should not be persisted. |
| 1755 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1756 | ->SetContentSettingDefaultScope( |
| 1757 | gurl, GURL(), ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1758 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 1759 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1760 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1761 | &host_settings); |
| 1762 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1763 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1764 | |
| 1765 | // Allow the JavaScript: Client hint preferences should be persisted. |
| 1766 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1767 | ->SetContentSettingDefaultScope( |
| 1768 | gurl, GURL(), ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_ALLOW); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1769 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 1770 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1771 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1772 | &host_settings); |
| 1773 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1774 | |
| 1775 | // Clear settings. |
| 1776 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1777 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | // Ensure that when the JavaScript is blocked, persisted client hints are not |
| 1781 | // attached to the request headers. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1782 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1783 | ClientHintsLifetimeNotAttachedJavaScriptBlocked) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 1784 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1785 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1786 | base::HistogramTester histogram_tester; |
| 1787 | ContentSettingsForOneType host_settings; |
| 1788 | |
| 1789 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1790 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1791 | &host_settings); |
| 1792 | EXPECT_EQ(0u, host_settings.size()); |
| 1793 | |
| 1794 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 1795 | // client hints. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1796 | ui_test_utils::NavigateToURL(browser(), gurl); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1797 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1798 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 1799 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1800 | EXPECT_EQ(1u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1801 | EXPECT_EQ(1u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1802 | EXPECT_EQ(1u, count_ua_platform_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1803 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1804 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 1805 | expected_client_hints_number, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1806 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1807 | // 3600 seconds, but a maximum value is registered instead. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1808 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 1809 | uma_histogram_max_value, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1810 | base::RunLoop().RunUntilIdle(); |
| 1811 | |
| 1812 | // Clients hints preferences for one origin should be persisted. |
| 1813 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1814 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1815 | &host_settings); |
| 1816 | EXPECT_EQ(1u, host_settings.size()); |
| 1817 | |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 1818 | // Block JavaScript via WebPreferences: Client hints should not be attached. |
| 1819 | SetJsEnabledForActiveView(false); |
| 1820 | |
| 1821 | SetClientHintExpectationsOnMainFrame(false); |
| 1822 | SetClientHintExpectationsOnSubresources(false); |
| 1823 | ui_test_utils::NavigateToURL(browser(), |
| 1824 | without_accept_ch_without_lifetime_url()); |
| 1825 | |
| 1826 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
| 1827 | VerifyContentSettingsNotNotified(); |
| 1828 | EXPECT_EQ(1u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1829 | EXPECT_EQ(1u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1830 | EXPECT_EQ(1u, count_ua_platform_client_hints_headers_seen()); |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 1831 | |
| 1832 | SetJsEnabledForActiveView(true); |
| 1833 | |
| 1834 | // Block JavaScript via ContentSetting: Client hints should not be attached. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1835 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1836 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1837 | GURL(), ContentSettingsType::JAVASCRIPT, |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1838 | CONTENT_SETTING_BLOCK); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1839 | ui_test_utils::NavigateToURL(browser(), |
| 1840 | without_accept_ch_without_lifetime_url()); |
| 1841 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1842 | VerifyContentSettingsNotNotified(); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1843 | EXPECT_EQ(1u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1844 | EXPECT_EQ(1u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1845 | EXPECT_EQ(1u, count_ua_platform_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1846 | |
Changwan Ryu | 434c3a3 | 2019-07-30 23:42:58 | [diff] [blame] | 1847 | // Allow JavaScript: Client hints should now be attached. |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1848 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1849 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1850 | GURL(), ContentSettingsType::JAVASCRIPT, |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1851 | CONTENT_SETTING_ALLOW); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1852 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1853 | SetClientHintExpectationsOnMainFrame(true); |
| 1854 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1855 | ui_test_utils::NavigateToURL(browser(), |
| 1856 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 1857 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1858 | // The user agent hint is attached to all three requests: |
| 1859 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1860 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1861 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1862 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 1863 | // Expected number of hints attached to the image request, and the same number |
| 1864 | // to the main frame request. |
| 1865 | EXPECT_EQ(expected_client_hints_number * 2, |
| 1866 | count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1867 | |
| 1868 | // Clear settings. |
| 1869 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1870 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 1871 | } |
| 1872 | |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1873 | // Test that if the content settings are malformed, then the browser does not |
| 1874 | // crash. |
| 1875 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
| 1876 | ClientHintsMalformedContentSettings) { |
| 1877 | ContentSettingsForOneType client_hints_settings; |
| 1878 | HostContentSettingsMap* host_content_settings_map = |
| 1879 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 1880 | |
| 1881 | // Add setting for the host. |
| 1882 | std::unique_ptr<base::ListValue> expiration_times_list = |
| 1883 | std::make_unique<base::ListValue>(); |
| 1884 | expiration_times_list->AppendInteger(42 /* client hint value */); |
| 1885 | auto expiration_times_dictionary = std::make_unique<base::DictionaryValue>(); |
| 1886 | expiration_times_dictionary->SetList("client_hints", |
| 1887 | std::move(expiration_times_list)); |
Aaron Tagliaboschi | 3c96a68 | 2019-10-29 18:10:28 | [diff] [blame] | 1888 | expiration_times_dictionary->SetDouble( |
| 1889 | "expiration_time", |
| 1890 | (base::Time::Now() + base::TimeDelta::FromDays(1)).ToDoubleT()); |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1891 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 1892 | without_accept_ch_without_lifetime_url(), GURL(), |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1893 | ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1894 | std::make_unique<base::Value>(expiration_times_dictionary->Clone())); |
| 1895 | |
| 1896 | // Reading the settings should now return one setting. |
| 1897 | host_content_settings_map->GetSettingsForOneType( |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1898 | ContentSettingsType::CLIENT_HINTS, &client_hints_settings); |
Tarun Bansal | 73502f9 | 2019-04-16 21:21:19 | [diff] [blame] | 1899 | EXPECT_EQ(1U, client_hints_settings.size()); |
| 1900 | |
| 1901 | SetClientHintExpectationsOnMainFrame(false); |
| 1902 | SetClientHintExpectationsOnSubresources(false); |
| 1903 | ui_test_utils::NavigateToURL(browser(), |
| 1904 | without_accept_ch_without_lifetime_url()); |
| 1905 | } |
| 1906 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1907 | // Ensure that when the JavaScript is blocked, client hints requested using |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1908 | // Accept-CH are not attached to the request headers for subresources. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1909 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1910 | ClientHintsNoLifetimeScriptNotAllowed) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1911 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 1912 | : accept_ch_with_lifetime_url(); |
| 1913 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1914 | base::HistogramTester histogram_tester; |
| 1915 | ContentSettingsForOneType host_settings; |
| 1916 | |
| 1917 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1918 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1919 | &host_settings); |
| 1920 | EXPECT_EQ(0u, host_settings.size()); |
| 1921 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1922 | // Block the Javascript: Client hints should not be attached. |
| 1923 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1924 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1925 | ->SetContentSettingDefaultScope( |
| 1926 | accept_ch_without_lifetime_img_localhost(), GURL(), |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1927 | ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1928 | ui_test_utils::NavigateToURL(browser(), |
| 1929 | accept_ch_without_lifetime_img_localhost()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1930 | EXPECT_EQ(0u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1931 | EXPECT_EQ(0u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1932 | EXPECT_EQ(0u, count_ua_platform_client_hints_headers_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1933 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1934 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1935 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1936 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1937 | // Allow the Javascript: Client hints should now be attached. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1938 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1939 | ->SetContentSettingDefaultScope( |
| 1940 | accept_ch_without_lifetime_img_localhost(), GURL(), |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1941 | ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_ALLOW); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1942 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1943 | SetClientHintExpectationsOnSubresources(true); |
| 1944 | ui_test_utils::NavigateToURL(browser(), |
| 1945 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1946 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1947 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1948 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1949 | EXPECT_EQ(2u, count_ua_platform_client_hints_headers_seen()); |
Aaron Tagliaboschi | 150fb904 | 2020-05-14 22:20:37 | [diff] [blame] | 1950 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1951 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1952 | EXPECT_EQ(3u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1953 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1954 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1955 | // Clear settings. |
| 1956 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1957 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1958 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1959 | // Block the Javascript again: Client hints should not be attached. |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1960 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1961 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1962 | ->SetContentSettingDefaultScope( |
| 1963 | accept_ch_without_lifetime_img_localhost(), GURL(), |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1964 | ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1965 | ui_test_utils::NavigateToURL(browser(), |
| 1966 | accept_ch_without_lifetime_img_localhost()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 1967 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 1968 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1969 | EXPECT_EQ(2u, count_ua_platform_client_hints_headers_seen()); |
Aaron Tagliaboschi | 150fb904 | 2020-05-14 22:20:37 | [diff] [blame] | 1970 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1971 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 1972 | EXPECT_EQ(3u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1973 | |
| 1974 | // Clear settings. |
| 1975 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 1976 | ->ClearSettingsForOneType(ContentSettingsType::JAVASCRIPT); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1977 | } |
| 1978 | |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1979 | // Ensure that when the cookies is blocked, client hints are attached to the |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1980 | // request headers. |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1981 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 1982 | ClientHintsLifetimeCookiesNotAllowed) { |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 1983 | const GURL gurl = GetParam() |
| 1984 | ? http_equiv_accept_ch_without_lifetime_img_localhost() |
| 1985 | : accept_ch_without_lifetime_img_localhost(); |
| 1986 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1987 | base::HistogramTester histogram_tester; |
| 1988 | ContentSettingsForOneType host_settings; |
| 1989 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 1990 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 1991 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1992 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1993 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1994 | &host_settings); |
| 1995 | EXPECT_EQ(0u, host_settings.size()); |
| 1996 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1997 | // Block cookies. |
| 1998 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 1999 | ->SetContentSettingDefaultScope( |
| 2000 | gurl, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 2001 | base::RunLoop().RunUntilIdle(); |
| 2002 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 2003 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 2004 | ui_test_utils::NavigateToURL(browser(), gurl); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2005 | EXPECT_EQ(2u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2006 | EXPECT_EQ(2u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2007 | EXPECT_EQ(2u, count_ua_platform_client_hints_headers_seen()); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2008 | EXPECT_EQ(expected_client_hints_number, count_client_hints_headers_seen()); |
Tarun Bansal | a0c1fc3 | 2018-10-03 16:14:52 | [diff] [blame] | 2009 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2010 | EXPECT_EQ(3u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 2011 | |
| 2012 | // Clear settings. |
| 2013 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
Darin Fisher | 42f5e7d | 2019-10-30 07:15:45 | [diff] [blame] | 2014 | ->ClearSettingsForOneType(ContentSettingsType::COOKIES); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 2015 | } |
| 2016 | |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2017 | // Verify that client hints are sent in the incognito profiles, and server |
| 2018 | // client hint opt-ins are honored within the incognito profile. |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 2019 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2020 | ClientHintsLifetimeFollowedByNoClientHintIncognito) { |
Maks Orlovich | f0a2eed | 2020-05-02 20:08:21 | [diff] [blame] | 2021 | const GURL gurl = accept_ch_with_lifetime_url(); |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 2022 | |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2023 | base::HistogramTester histogram_tester; |
| 2024 | Browser* incognito = CreateIncognitoBrowser(); |
| 2025 | ContentSettingsForOneType host_settings; |
| 2026 | |
| 2027 | HostContentSettingsMapFactory::GetForProfile(incognito->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 2028 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2029 | &host_settings); |
| 2030 | EXPECT_EQ(0u, host_settings.size()); |
| 2031 | |
Tarun Bansal | 9a7051f | 2018-07-10 18:30:05 | [diff] [blame] | 2032 | // Fetching |gurl| should persist the request for client hints. |
| 2033 | ui_test_utils::NavigateToURL(incognito, gurl); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2034 | |
| 2035 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 2036 | |
| 2037 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 2038 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2039 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2040 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", |
| 2041 | expected_client_hints_number, 1); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2042 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 2043 | // seconds, but a maximum value is registered instead. |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2044 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
Yoav Weiss | 76e1afb76 | 2020-05-14 19:28:12 | [diff] [blame] | 2045 | uma_histogram_max_value, 1); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2046 | base::RunLoop().RunUntilIdle(); |
| 2047 | |
| 2048 | // Clients hints preferences for one origin should be persisted. |
| 2049 | HostContentSettingsMapFactory::GetForProfile(incognito->profile()) |
Illia Klimov | 48f643c | 2020-11-05 20:06:14 | [diff] [blame] | 2050 | ->GetSettingsForOneType(ContentSettingsType::CLIENT_HINTS, |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2051 | &host_settings); |
| 2052 | EXPECT_EQ(1u, host_settings.size()); |
| 2053 | |
| 2054 | SetClientHintExpectationsOnMainFrame(true); |
| 2055 | SetClientHintExpectationsOnSubresources(true); |
| 2056 | ui_test_utils::NavigateToURL(incognito, |
| 2057 | without_accept_ch_without_lifetime_url()); |
| 2058 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2059 | // The user agent hint is attached to all three requests: |
| 2060 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2061 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2062 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2063 | |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2064 | // Expected number of hints attached to the image request, and the same number |
| 2065 | // to the main frame request. |
| 2066 | EXPECT_EQ(expected_client_hints_number * 2, |
| 2067 | count_client_hints_headers_seen()); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2068 | |
| 2069 | // Navigate using regular profile. Client hints should not be send. |
| 2070 | SetClientHintExpectationsOnMainFrame(false); |
| 2071 | SetClientHintExpectationsOnSubresources(false); |
| 2072 | ui_test_utils::NavigateToURL(browser(), |
| 2073 | without_accept_ch_without_lifetime_url()); |
| 2074 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2075 | // The user agent hint is attached to the two new requests. |
| 2076 | EXPECT_EQ(5u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2077 | EXPECT_EQ(5u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2078 | EXPECT_EQ(5u, count_ua_platform_client_hints_headers_seen()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2079 | |
| 2080 | // No additional hints are sent. |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2081 | EXPECT_EQ(expected_client_hints_number * 2, |
| 2082 | count_client_hints_headers_seen()); |
Tarun Bansal | 3ce0ca4 | 2018-06-25 22:52:22 | [diff] [blame] | 2083 | } |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2084 | |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 2085 | class ClientHintsEnterprisePolicyTest : public ClientHintsBrowserTest { |
| 2086 | void SetUpInProcessBrowserTestFixture() override { |
| 2087 | policy::PolicyTest::SetUpInProcessBrowserTestFixture(); |
| 2088 | policy::PolicyMap policies; |
| 2089 | SetPolicy(&policies, policy::key::kUserAgentClientHintsEnabled, |
Aya ElAttar | e881174 | 2020-07-20 08:24:37 | [diff] [blame] | 2090 | base::Value(false)); |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 2091 | provider_.UpdateChromePolicy(policies); |
| 2092 | } |
| 2093 | }; |
| 2094 | |
| 2095 | // Makes sure that no client hints are sent by default when the |
| 2096 | // "UserAgentClientHintsEnabled" enterprise polickly is set to |
| 2097 | // false |
| 2098 | IN_PROC_BROWSER_TEST_F(ClientHintsEnterprisePolicyTest, |
| 2099 | ClientHintsEnterprisePolicy) { |
| 2100 | const GURL gurl = accept_ch_without_lifetime_url(); |
| 2101 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 2102 | // These would normally be one each |
| 2103 | EXPECT_EQ(0u, count_user_agent_hint_headers_seen()); |
| 2104 | EXPECT_EQ(0u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2105 | EXPECT_EQ(0u, count_ua_platform_client_hints_headers_seen()); |
Aaron Tagliaboschi | 8f3a2830 | 2020-06-24 03:08:26 | [diff] [blame] | 2106 | } |
| 2107 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2108 | class ClientHintsWebHoldbackBrowserTest : public ClientHintsBrowserTest { |
| 2109 | public: |
| 2110 | ClientHintsWebHoldbackBrowserTest() : ClientHintsBrowserTest() { |
| 2111 | ConfigureHoldbackExperiment(); |
| 2112 | } |
| 2113 | |
| 2114 | net::EffectiveConnectionType web_effective_connection_type_override() const { |
| 2115 | return web_effective_connection_type_override_; |
| 2116 | } |
| 2117 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2118 | std::unique_ptr<base::FeatureList> EnabledFeatures() override { |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2119 | base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
| 2120 | const std::string kTrialName = "TrialFoo"; |
| 2121 | const std::string kGroupName = "GroupFoo"; // Value not used |
| 2122 | |
| 2123 | scoped_refptr<base::FieldTrial> trial = |
| 2124 | base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); |
| 2125 | |
| 2126 | std::map<std::string, std::string> params; |
| 2127 | |
| 2128 | params["web_effective_connection_type_override"] = |
| 2129 | net::GetNameForEffectiveConnectionType( |
| 2130 | web_effective_connection_type_override_); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2131 | EXPECT_TRUE( |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2132 | base::FieldTrialParamAssociator::GetInstance() |
| 2133 | ->AssociateFieldTrialParams(kTrialName, kGroupName, params)); |
| 2134 | |
| 2135 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
Maks Orlovich | c66745a | 2020-06-30 17:40:02 | [diff] [blame] | 2136 | feature_list->InitializeFromCommandLine( |
François Beaufort | 1b51d06 | 2021-05-18 11:11:23 | [diff] [blame] | 2137 | "UserAgentClientHint,LangClientHintHeader," |
| 2138 | "PrefersColorSchemeClientHintHeader", |
| 2139 | ""); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2140 | feature_list->RegisterFieldTrialOverride( |
| 2141 | features::kNetworkQualityEstimatorWebHoldback.name, |
| 2142 | base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get()); |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2143 | return feature_list; |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2144 | } |
| 2145 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2146 | private: |
| 2147 | void ConfigureHoldbackExperiment() {} |
| 2148 | |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2149 | const net::EffectiveConnectionType web_effective_connection_type_override_ = |
| 2150 | net::EFFECTIVE_CONNECTION_TYPE_3G; |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2151 | }; |
| 2152 | |
| 2153 | // Make sure that when NetInfo holdback experiment is enabled, the NetInfo APIs |
| 2154 | // and client hints return the overridden values. Verify that the client hints |
| 2155 | // are overridden on both main frame and subresource requests. |
| 2156 | IN_PROC_BROWSER_TEST_F(ClientHintsWebHoldbackBrowserTest, |
| 2157 | EffectiveConnectionTypeChangeNotified) { |
| 2158 | SetExpectedEffectiveConnectionType(web_effective_connection_type_override()); |
| 2159 | |
| 2160 | SetClientHintExpectationsOnMainFrame(false); |
| 2161 | SetClientHintExpectationsOnSubresources(true); |
| 2162 | |
| 2163 | base::RunLoop().RunUntilIdle(); |
| 2164 | |
| 2165 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 2166 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 2167 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
| 2168 | EXPECT_EQ(0u, third_party_request_count_seen()); |
| 2169 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
| 2170 | |
| 2171 | SetClientHintExpectationsOnMainFrame(true); |
| 2172 | SetClientHintExpectationsOnSubresources(true); |
| 2173 | ui_test_utils::NavigateToURL( |
| 2174 | browser(), accept_ch_without_lifetime_with_subresource_url()); |
| 2175 | base::RunLoop().RunUntilIdle(); |
| 2176 | content::FetchHistogramsFromChildProcesses(); |
John Abd-El-Malek | 161073c | 2020-06-12 20:40:28 | [diff] [blame] | 2177 | metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2178 | |
Mike West | 2fddeeb7 | 2019-02-15 11:29:48 | [diff] [blame] | 2179 | EXPECT_EQ(3u, count_user_agent_hint_headers_seen()); |
Maks Orlovich | 5dcc99c | 2020-02-13 19:07:46 | [diff] [blame] | 2180 | EXPECT_EQ(3u, count_ua_mobile_client_hints_headers_seen()); |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2181 | EXPECT_EQ(3u, count_ua_platform_client_hints_headers_seen()); |
Yoav Weiss | d33bacb | 2020-03-12 06:42:21 | [diff] [blame] | 2182 | EXPECT_EQ(expected_client_hints_number * 2, |
| 2183 | count_client_hints_headers_seen()); |
Tarun Bansal | bef6d65 | 2018-10-02 18:41:01 | [diff] [blame] | 2184 | EXPECT_EQ(0u, third_party_request_count_seen()); |
| 2185 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
| 2186 | } |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 2187 | |
| 2188 | class AcceptCHFrameObserverInterceptor { |
| 2189 | public: |
| 2190 | AcceptCHFrameObserverInterceptor() |
| 2191 | : interceptor_(base::BindRepeating( |
| 2192 | &AcceptCHFrameObserverInterceptor::InterceptURLRequest, |
| 2193 | base::Unretained(this))) {} |
| 2194 | |
| 2195 | void set_accept_ch_frame( |
| 2196 | std::vector<network::mojom::WebClientHintsType> frame) { |
| 2197 | accept_ch_frame_ = frame; |
| 2198 | } |
| 2199 | |
| 2200 | private: |
| 2201 | bool InterceptURLRequest( |
| 2202 | content::URLLoaderInterceptor::RequestParams* params) { |
| 2203 | if (!accept_ch_frame_ || !params->url_request.trusted_params || |
| 2204 | !params->url_request.trusted_params->accept_ch_frame_observer) { |
| 2205 | return false; |
| 2206 | } |
| 2207 | |
| 2208 | std::vector<network::mojom::WebClientHintsType> hints; |
| 2209 | for (auto hint : accept_ch_frame_.value()) { |
| 2210 | std::string header = |
| 2211 | network::kClientHintsNameMapping[static_cast<int>(hint)]; |
| 2212 | if (!params->url_request.headers.HasHeader(header)) |
| 2213 | hints.push_back(hint); |
| 2214 | } |
| 2215 | |
| 2216 | if (hints.empty()) |
| 2217 | return false; |
| 2218 | |
| 2219 | mojo::Remote<network::mojom::AcceptCHFrameObserver> remote(std::move( |
| 2220 | params->url_request.trusted_params->accept_ch_frame_observer)); |
| 2221 | remote->OnAcceptCHFrameReceived(params->url_request.url, hints, |
| 2222 | base::DoNothing::Once<int>()); |
| 2223 | // At this point it's expected that either the remote's callback will be |
| 2224 | // called or the URLLoader will be destroyed to make way for a new one. |
| 2225 | // As this is essentially unobservable, RunUntilIdle must be used. |
| 2226 | base::RunLoop().RunUntilIdle(); |
| 2227 | return false; |
| 2228 | } |
| 2229 | |
| 2230 | content::URLLoaderInterceptor interceptor_; |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 2231 | absl::optional<std::vector<network::mojom::WebClientHintsType>> |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 2232 | accept_ch_frame_; |
| 2233 | }; |
| 2234 | |
| 2235 | // Replace the request interceptor with an AcceptCHFrameObserverInterceptor. |
| 2236 | class ClientHintsAcceptCHFrameObserverBrowserTest |
| 2237 | : public ClientHintsBrowserTest { |
| 2238 | public: |
| 2239 | void SetUpOnMainThread() override { |
| 2240 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 2241 | accept_ch_frame_observer_interceptor_ = |
| 2242 | std::make_unique<AcceptCHFrameObserverInterceptor>(); |
| 2243 | } |
| 2244 | |
| 2245 | void TearDownOnMainThread() override { |
| 2246 | accept_ch_frame_observer_interceptor_.reset(); |
| 2247 | } |
| 2248 | |
| 2249 | void set_accept_ch_frame( |
| 2250 | std::vector<network::mojom::WebClientHintsType> frame) { |
| 2251 | accept_ch_frame_observer_interceptor_->set_accept_ch_frame(frame); |
| 2252 | } |
| 2253 | |
| 2254 | std::vector<network::mojom::WebClientHintsType> all_client_hints_types() { |
| 2255 | std::vector<network::mojom::WebClientHintsType> hints; |
| 2256 | for (size_t i = 0; i < blink::kClientHintsMappingsCount; i++) { |
| 2257 | hints.push_back(static_cast<network::mojom::WebClientHintsType>(i)); |
| 2258 | } |
| 2259 | |
| 2260 | return hints; |
| 2261 | } |
| 2262 | |
| 2263 | private: |
| 2264 | std::unique_ptr<AcceptCHFrameObserverInterceptor> |
| 2265 | accept_ch_frame_observer_interceptor_; |
| 2266 | }; |
| 2267 | |
Elly Fong-Jones | dd9eb01 | 2021-04-05 22:45:27 | [diff] [blame] | 2268 | #if defined(OS_CHROMEOS) |
| 2269 | // Flaky: https://crbug.com/1195790 |
| 2270 | #define MAYBE_AcceptCHFrame DISABLED_AcceptCHFrame |
| 2271 | #else |
| 2272 | #define MAYBE_AcceptCHFrame AcceptCHFrame |
| 2273 | #endif |
| 2274 | |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 2275 | // Ensure that client hints are sent when the ACCEPT_CH frame observer is |
| 2276 | // notified. |
| 2277 | IN_PROC_BROWSER_TEST_F(ClientHintsAcceptCHFrameObserverBrowserTest, |
Elly Fong-Jones | dd9eb01 | 2021-04-05 22:45:27 | [diff] [blame] | 2278 | MAYBE_AcceptCHFrame) { |
Aaron Tagliaboschi | 603540d2 | 2021-04-05 00:37:14 | [diff] [blame] | 2279 | const GURL gurl = without_accept_ch_without_lifetime_url(); |
| 2280 | set_accept_ch_frame(all_client_hints_types()); |
| 2281 | SetClientHintExpectationsOnMainFrame(true); |
| 2282 | SetClientHintExpectationsOnSubresources(false); |
| 2283 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 2284 | } |
| 2285 | |
| 2286 | // Ensure that client hints are *not* sent when the observer is notified but |
| 2287 | // client hints would normally not be sent (e.g. when JS is disabled for the |
| 2288 | // frame). |
| 2289 | IN_PROC_BROWSER_TEST_F(ClientHintsAcceptCHFrameObserverBrowserTest, |
| 2290 | AcceptCHFrameJSDisabled) { |
| 2291 | const GURL gurl = without_accept_ch_without_lifetime_url(); |
| 2292 | set_accept_ch_frame(all_client_hints_types()); |
| 2293 | SetJsEnabledForActiveView(false); |
| 2294 | SetClientHintExpectationsOnMainFrame(false); |
| 2295 | SetClientHintExpectationsOnSubresources(false); |
| 2296 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 2297 | } |
Aaron Tagliaboschi | 25625c2 | 2021-05-11 18:13:59 | [diff] [blame] | 2298 | |
| 2299 | IN_PROC_BROWSER_TEST_P(ClientHintsBrowserTest, UseCounter) { |
| 2300 | auto web_feature_waiter = |
| 2301 | std::make_unique<page_load_metrics::PageLoadMetricsTestWaiter>( |
| 2302 | chrome_test_utils::GetActiveWebContents(this)); |
| 2303 | |
| 2304 | web_feature_waiter->AddWebFeatureExpectation( |
Aaron Tagliaboschi | ec5bce6 | 2021-06-24 18:50:09 | [diff] [blame^] | 2305 | blink::mojom::WebFeature::kClientHintsUAFullVersion); |
Aaron Tagliaboschi | 25625c2 | 2021-05-11 18:13:59 | [diff] [blame] | 2306 | const GURL gurl = GetParam() ? http_equiv_accept_ch_with_lifetime() |
| 2307 | : accept_ch_with_lifetime_url(); |
| 2308 | |
| 2309 | ui_test_utils::NavigateToURL(browser(), gurl); |
| 2310 | |
| 2311 | web_feature_waiter->Wait(); |
| 2312 | } |
François Beaufort | 34e299e | 2021-05-26 05:54:33 | [diff] [blame] | 2313 | |
| 2314 | class ClientHintsBrowserTestWithEmulatedMedia |
| 2315 | : public DevToolsProtocolTestBase { |
| 2316 | public: |
| 2317 | ClientHintsBrowserTestWithEmulatedMedia() |
| 2318 | : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) { |
| 2319 | scoped_feature_list_.InitFromCommandLine( |
| 2320 | "UserAgentClientHint,AcceptCHFrame,PrefersColorSchemeClientHintHeader", |
| 2321 | ""); |
| 2322 | |
| 2323 | https_server_.ServeFilesFromSourceDirectory( |
| 2324 | "chrome/test/data/client_hints"); |
| 2325 | https_server_.RegisterRequestMonitor(base::BindRepeating( |
| 2326 | &ClientHintsBrowserTestWithEmulatedMedia::MonitorResourceRequest, |
| 2327 | base::Unretained(this))); |
| 2328 | EXPECT_TRUE(https_server_.Start()); |
| 2329 | |
| 2330 | test_url_ = https_server_.GetURL("/accept_ch_without_lifetime.html"); |
| 2331 | } |
| 2332 | |
| 2333 | ~ClientHintsBrowserTestWithEmulatedMedia() override = default; |
| 2334 | |
| 2335 | void MonitorResourceRequest(const net::test_server::HttpRequest& request) { |
| 2336 | if (request.headers.find("sec-ch-prefers-color-scheme") != |
| 2337 | request.headers.end()) { |
| 2338 | prefers_color_scheme_observed_ = |
| 2339 | request.headers.at("sec-ch-prefers-color-scheme"); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | const GURL& test_url() const { return test_url_; } |
| 2344 | |
| 2345 | const std::string& prefers_color_scheme_observed() const { |
| 2346 | return prefers_color_scheme_observed_; |
| 2347 | } |
| 2348 | |
| 2349 | void EmulatePrefersColorScheme(std::string value) { |
| 2350 | base::Value feature(base::Value::Type::DICTIONARY); |
| 2351 | feature.SetKey("name", base::Value("prefers-color-scheme")); |
| 2352 | feature.SetKey("value", base::Value(value)); |
| 2353 | base::Value features(base::Value::Type::LIST); |
| 2354 | features.Append(std::move(feature)); |
| 2355 | base::Value params(base::Value::Type::DICTIONARY); |
| 2356 | params.SetKey("features", std::move(features)); |
| 2357 | SendCommandSync("Emulation.setEmulatedMedia", std::move(params)); |
| 2358 | } |
| 2359 | |
| 2360 | private: |
| 2361 | base::test::ScopedFeatureList scoped_feature_list_; |
| 2362 | net::EmbeddedTestServer https_server_; |
| 2363 | GURL test_url_; |
| 2364 | std::string prefers_color_scheme_observed_; |
| 2365 | |
| 2366 | DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTestWithEmulatedMedia); |
| 2367 | }; |
| 2368 | |
| 2369 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTestWithEmulatedMedia, |
| 2370 | PrefersColorScheme) { |
| 2371 | ui_test_utils::NavigateToURL(browser(), test_url()); |
| 2372 | EXPECT_EQ(prefers_color_scheme_observed(), ""); |
| 2373 | Attach(); |
| 2374 | |
| 2375 | EmulatePrefersColorScheme("light"); |
| 2376 | ui_test_utils::NavigateToURL(browser(), test_url()); |
| 2377 | EXPECT_EQ(prefers_color_scheme_observed(), "light"); |
| 2378 | |
| 2379 | EmulatePrefersColorScheme("dark"); |
| 2380 | ui_test_utils::NavigateToURL(browser(), test_url()); |
| 2381 | EXPECT_EQ(prefers_color_scheme_observed(), "dark"); |
| 2382 | } |