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