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