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 | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 5 | #include "base/bind.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 6 | #include "base/command_line.h" |
Gabriel Charette | b71eec89 | 2017-09-14 22:52:56 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 8 | #include "base/stl_util.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 9 | #include "base/test/histogram_tester.h" |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 10 | #include "build/build_config.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 11 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 12 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 13 | #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 14 | #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
| 15 | #include "chrome/browser/net/url_request_mock_util.h" |
| 16 | #include "chrome/browser/profiles/profile.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 18 | #include "chrome/test/base/in_process_browser_test.h" |
| 19 | #include "chrome/test/base/ui_test_utils.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 20 | #include "components/content_settings/core/browser/cookie_settings.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 21 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 22 | #include "components/content_settings/core/common/pref_names.h" |
| 23 | #include "components/prefs/pref_service.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
| 25 | #include "content/public/common/content_switches.h" |
| 26 | #include "content/public/test/browser_test_utils.h" |
| 27 | #include "content/public/test/test_utils.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 28 | #include "content/public/test/url_loader_interceptor.h" |
| 29 | #include "net/dns/mock_host_resolver.h" |
| 30 | #include "net/http/http_request_headers.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 31 | #include "net/nqe/effective_connection_type.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 32 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 33 | #include "net/test/embedded_test_server/http_request.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 34 | #include "net/test/embedded_test_server/http_response.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 35 | #include "services/network/public/cpp/network_switches.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 36 | #include "third_party/blink/public/common/client_hints/client_hints.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | |
| 40 | // An interceptor that records count of fetches and client hint headers for |
| 41 | // requests to https://foo.com/non-existing-image.jpg. |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 42 | class ThirdPartyURLLoaderInterceptor { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 43 | public: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 44 | explicit ThirdPartyURLLoaderInterceptor(const GURL intercepted_url) |
| 45 | : intercepted_url_(intercepted_url), |
| 46 | interceptor_(base::BindRepeating( |
| 47 | &ThirdPartyURLLoaderInterceptor::InterceptURLRequest, |
| 48 | base::Unretained(this))) {} |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 49 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 50 | ~ThirdPartyURLLoaderInterceptor() = default; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 51 | |
| 52 | size_t request_count_seen() const { return request_count_seen_; } |
| 53 | |
| 54 | size_t client_hints_count_seen() const { return client_hints_count_seen_; } |
| 55 | |
| 56 | private: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 57 | bool InterceptURLRequest( |
| 58 | content::URLLoaderInterceptor::RequestParams* params) { |
| 59 | if (params->url_request.url != intercepted_url_) |
| 60 | return false; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 61 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 62 | request_count_seen_++; |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 63 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 64 | if (params->url_request.headers.HasHeader( |
| 65 | blink::kClientHintsHeaderMapping[i])) { |
| 66 | client_hints_count_seen_++; |
| 67 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 68 | } |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 69 | return false; |
| 70 | } |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 71 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 72 | GURL intercepted_url_; |
| 73 | |
| 74 | size_t request_count_seen_ = 0u; |
| 75 | |
| 76 | size_t client_hints_count_seen_ = 0u; |
| 77 | |
| 78 | content::URLLoaderInterceptor interceptor_; |
| 79 | |
| 80 | DISALLOW_COPY_AND_ASSIGN(ThirdPartyURLLoaderInterceptor); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 84 | |
| 85 | class ClientHintsBrowserTest : public InProcessBrowserTest { |
| 86 | public: |
| 87 | ClientHintsBrowserTest() |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 88 | : http_server_(net::EmbeddedTestServer::TYPE_HTTP), |
| 89 | https_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 90 | expect_client_hints_on_main_frame_(false), |
| 91 | expect_client_hints_on_subresources_(false), |
| 92 | count_client_hints_headers_seen_(0), |
| 93 | request_interceptor_(nullptr) { |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 94 | http_server_.ServeFilesFromSourceDirectory("chrome/test/data/client_hints"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 95 | https_server_.ServeFilesFromSourceDirectory( |
| 96 | "chrome/test/data/client_hints"); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 97 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 98 | http_server_.RegisterRequestMonitor( |
| 99 | base::Bind(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 100 | base::Unretained(this))); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 101 | https_server_.RegisterRequestMonitor( |
| 102 | base::Bind(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 103 | base::Unretained(this))); |
| 104 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 105 | EXPECT_TRUE(http_server_.Start()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 106 | EXPECT_TRUE(https_server_.Start()); |
| 107 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 108 | accept_ch_with_lifetime_http_local_url_ = |
| 109 | http_server_.GetURL("/accept_ch_with_lifetime.html"); |
| 110 | EXPECT_TRUE(accept_ch_with_lifetime_http_local_url_.SchemeIsHTTPOrHTTPS()); |
| 111 | EXPECT_FALSE( |
| 112 | accept_ch_with_lifetime_http_local_url_.SchemeIsCryptographic()); |
| 113 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 114 | accept_ch_with_lifetime_url_ = |
| 115 | https_server_.GetURL("/accept_ch_with_lifetime.html"); |
| 116 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 117 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 118 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 119 | accept_ch_without_lifetime_url_ = |
| 120 | https_server_.GetURL("/accept_ch_without_lifetime.html"); |
| 121 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 122 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsCryptographic()); |
| 123 | |
| 124 | without_accept_ch_without_lifetime_url_ = |
| 125 | https_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 126 | EXPECT_TRUE(without_accept_ch_without_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 127 | EXPECT_TRUE( |
| 128 | without_accept_ch_without_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 129 | |
| 130 | without_accept_ch_without_lifetime_local_url_ = |
| 131 | http_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 132 | EXPECT_TRUE( |
| 133 | without_accept_ch_without_lifetime_local_url_.SchemeIsHTTPOrHTTPS()); |
| 134 | EXPECT_FALSE( |
| 135 | without_accept_ch_without_lifetime_local_url_.SchemeIsCryptographic()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 136 | |
| 137 | without_accept_ch_without_lifetime_img_localhost_ = https_server_.GetURL( |
| 138 | "/without_accept_ch_without_lifetime_img_localhost.html"); |
| 139 | without_accept_ch_without_lifetime_img_foo_com_ = https_server_.GetURL( |
| 140 | "/without_accept_ch_without_lifetime_img_foo_com.html"); |
| 141 | accept_ch_without_lifetime_with_iframe_url_ = |
| 142 | https_server_.GetURL("/accept_ch_without_lifetime_with_iframe.html"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 143 | accept_ch_without_lifetime_img_localhost_ = |
| 144 | https_server_.GetURL("/accept_ch_without_lifetime_img_localhost.html"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | ~ClientHintsBrowserTest() override {} |
| 148 | |
| 149 | void SetUpOnMainThread() override { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 150 | host_resolver()->AddRule("*", "127.0.0.1"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 151 | content::BrowserThread::PostTask( |
| 152 | content::BrowserThread::IO, FROM_HERE, |
| 153 | base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 154 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 155 | request_interceptor_ = std::make_unique<ThirdPartyURLLoaderInterceptor>( |
| 156 | GURL("https://foo.com/non-existing-image.jpg")); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 157 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 158 | } |
| 159 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 160 | void TearDownOnMainThread() override { request_interceptor_.reset(); } |
| 161 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 162 | void SetUpCommandLine(base::CommandLine* cmd) override { |
| 163 | cmd->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 164 | cmd->AppendSwitchASCII(network::switches::kForceEffectiveConnectionType, |
| 165 | net::kEffectiveConnectionType2G); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 166 | } |
| 167 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 168 | void SetClientHintExpectationsOnMainFrame(bool expect_client_hints) { |
| 169 | expect_client_hints_on_main_frame_ = expect_client_hints; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 170 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 171 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 172 | void SetClientHintExpectationsOnSubresources(bool expect_client_hints) { |
| 173 | expect_client_hints_on_subresources_ = expect_client_hints; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 174 | } |
| 175 | |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 176 | // Verify that the user is not notified that cookies or JavaScript were |
| 177 | // blocked on the webpage due to the checks done by client hints. |
| 178 | void VerifyContentSettingsNotNotified() const { |
| 179 | content::WebContents* web_contents = |
| 180 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 181 | EXPECT_FALSE(TabSpecificContentSettings::FromWebContents(web_contents) |
| 182 | ->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 183 | |
| 184 | EXPECT_FALSE(TabSpecificContentSettings::FromWebContents(web_contents) |
| 185 | ->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 186 | } |
| 187 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 188 | const GURL& accept_ch_with_lifetime_http_local_url() const { |
| 189 | return accept_ch_with_lifetime_http_local_url_; |
| 190 | } |
| 191 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 192 | // A URL whose response headers include Accept-CH and Accept-CH-Lifetime |
| 193 | // headers. |
| 194 | const GURL& accept_ch_with_lifetime_url() const { |
| 195 | return accept_ch_with_lifetime_url_; |
| 196 | } |
| 197 | |
| 198 | // A URL whose response headers include only Accept-CH header. |
| 199 | const GURL& accept_ch_without_lifetime_url() const { |
| 200 | return accept_ch_without_lifetime_url_; |
| 201 | } |
| 202 | |
| 203 | // A URL whose response headers do not include either Accept-CH or |
| 204 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 205 | const GURL& without_accept_ch_without_lifetime_url() const { |
| 206 | return without_accept_ch_without_lifetime_url_; |
| 207 | } |
| 208 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 209 | // A URL whose response headers do not include either Accept-CH or |
| 210 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 211 | const GURL& without_accept_ch_without_lifetime_local_url() const { |
| 212 | return without_accept_ch_without_lifetime_local_url_; |
| 213 | } |
| 214 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 215 | // A URL whose response headers do not include either Accept-CH or |
| 216 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 217 | // from localhost. |
| 218 | const GURL& without_accept_ch_without_lifetime_img_localhost() const { |
| 219 | return without_accept_ch_without_lifetime_img_localhost_; |
| 220 | } |
| 221 | |
| 222 | // A URL whose response headers do not include either Accept-CH or |
| 223 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 224 | // from foo.com. |
| 225 | const GURL& without_accept_ch_without_lifetime_img_foo_com() const { |
| 226 | return without_accept_ch_without_lifetime_img_foo_com_; |
| 227 | } |
| 228 | |
| 229 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
| 230 | // headers. The response loads accept_ch_with_lifetime_url() in an iframe. |
| 231 | const GURL& accept_ch_without_lifetime_with_iframe_url() const { |
| 232 | return accept_ch_without_lifetime_with_iframe_url_; |
| 233 | } |
| 234 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 235 | // A URL whose response headers includes only Accept-CH header. Navigating to |
| 236 | // this URL also fetches two images: One from the localhost, and one from |
| 237 | // foo.com. |
| 238 | const GURL& accept_ch_without_lifetime_img_localhost() const { |
| 239 | return accept_ch_without_lifetime_img_localhost_; |
| 240 | } |
| 241 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 242 | size_t count_client_hints_headers_seen() const { |
| 243 | return count_client_hints_headers_seen_; |
| 244 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 245 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 246 | size_t third_party_request_count_seen() const { |
| 247 | return request_interceptor_->request_count_seen(); |
| 248 | } |
| 249 | |
| 250 | size_t third_party_client_hints_count_seen() const { |
| 251 | return request_interceptor_->client_hints_count_seen(); |
| 252 | } |
| 253 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 254 | private: |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 255 | // Called by |https_server_|. |
| 256 | void MonitorResourceRequest(const net::test_server::HttpRequest& request) { |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 257 | bool is_main_frame_navigation = |
| 258 | request.GetURL().spec().find(".html") != std::string::npos; |
| 259 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 260 | if (is_main_frame_navigation) { |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 261 | VerifyClientHintsReceived(expect_client_hints_on_main_frame_, request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 262 | if (expect_client_hints_on_main_frame_) { |
| 263 | double value = 0.0; |
| 264 | EXPECT_TRUE(base::StringToDouble( |
| 265 | request.headers.find("device-memory")->second, &value)); |
| 266 | EXPECT_LT(0.0, value); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 267 | main_frame_device_memory_observed_ = value; |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 268 | |
| 269 | EXPECT_TRUE( |
| 270 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 271 | EXPECT_LT(0.0, value); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 272 | main_frame_dpr_observed_ = value; |
| 273 | |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 274 | EXPECT_TRUE(base::StringToDouble( |
| 275 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 276 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 277 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 278 | #else |
| 279 | EXPECT_EQ(980, value); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 280 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 281 | main_frame_viewport_width_observed_ = value; |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 282 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 283 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 284 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 285 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 286 | if (!is_main_frame_navigation) { |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 287 | VerifyClientHintsReceived(expect_client_hints_on_subresources_, request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 288 | |
| 289 | if (expect_client_hints_on_subresources_) { |
| 290 | double value = 0.0; |
| 291 | EXPECT_TRUE(base::StringToDouble( |
| 292 | request.headers.find("device-memory")->second, &value)); |
| 293 | EXPECT_LT(0.0, value); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 294 | if (main_frame_device_memory_observed_ > 0) { |
| 295 | EXPECT_EQ(main_frame_device_memory_observed_, value); |
| 296 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 297 | |
| 298 | EXPECT_TRUE( |
| 299 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 300 | EXPECT_LT(0.0, value); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 301 | if (main_frame_dpr_observed_ > 0) { |
| 302 | EXPECT_EQ(main_frame_dpr_observed_, value); |
| 303 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 304 | |
| 305 | EXPECT_TRUE(base::StringToDouble( |
| 306 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 307 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 308 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 309 | #else |
| 310 | EXPECT_EQ(980, value); |
| 311 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 312 | #if defined(OS_ANDROID) |
| 313 | // TODO(tbansal): https://crbug.com/825892: Viewport width on main |
| 314 | // frame requests may be incorrect when the Chrome window is not |
| 315 | // maximized. |
| 316 | if (main_frame_viewport_width_observed_ > 0) { |
| 317 | EXPECT_EQ(main_frame_viewport_width_observed_, value); |
| 318 | } |
| 319 | #endif |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 320 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 321 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 322 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 323 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 324 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 325 | if (base::ContainsKey(request.headers, |
| 326 | blink::kClientHintsHeaderMapping[i])) { |
| 327 | count_client_hints_headers_seen_++; |
| 328 | } |
| 329 | } |
| 330 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 331 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 332 | void VerifyClientHintsReceived(bool expect_client_hints, |
| 333 | const net::test_server::HttpRequest& request) { |
| 334 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 335 | // Resource width client hint is only attached on image subresources. |
| 336 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "width") { |
| 337 | continue; |
| 338 | } |
| 339 | EXPECT_EQ(expect_client_hints, |
| 340 | base::ContainsKey(request.headers, |
| 341 | blink::kClientHintsHeaderMapping[i])); |
| 342 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 343 | } |
| 344 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 345 | void VerifyNetworkQualityClientHints( |
| 346 | const net::test_server::HttpRequest& request) const { |
| 347 | // Effective connection type is forced to 2G using command line in these |
| 348 | // tests. |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 349 | int rtt_value = 0.0; |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 350 | EXPECT_TRUE( |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 351 | base::StringToInt(request.headers.find("rtt")->second, &rtt_value)); |
| 352 | EXPECT_LE(0, rtt_value); |
| 353 | // Verify that RTT value is a multiple of 50 milliseconds. |
| 354 | EXPECT_EQ(0, rtt_value % 50); |
| 355 | EXPECT_GE(3000, rtt_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 356 | |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 357 | double mbps_value = 0.0; |
| 358 | EXPECT_TRUE(base::StringToDouble(request.headers.find("downlink")->second, |
| 359 | &mbps_value)); |
| 360 | EXPECT_LE(0, mbps_value); |
| 361 | // Verify that the mbps value is a multiple of 0.050 mbps. |
| 362 | // Allow for small amount of noise due to double to integer conversions. |
| 363 | EXPECT_NEAR(0, (static_cast<int>(mbps_value * 1000)) % 50, 1); |
| 364 | EXPECT_GE(10.0, mbps_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 365 | |
| 366 | EXPECT_FALSE(request.headers.find("ect")->second.empty()); |
| 367 | } |
| 368 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 369 | net::EmbeddedTestServer http_server_; |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 370 | net::EmbeddedTestServer https_server_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 371 | GURL accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 372 | GURL accept_ch_with_lifetime_url_; |
| 373 | GURL accept_ch_without_lifetime_url_; |
| 374 | GURL without_accept_ch_without_lifetime_url_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 375 | GURL without_accept_ch_without_lifetime_local_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 376 | GURL accept_ch_without_lifetime_with_iframe_url_; |
| 377 | GURL without_accept_ch_without_lifetime_img_foo_com_; |
| 378 | GURL without_accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 379 | GURL accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 380 | |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 381 | double main_frame_dpr_observed_ = -1; |
| 382 | double main_frame_viewport_width_observed_ = -1; |
| 383 | double main_frame_device_memory_observed_ = -1; |
| 384 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 385 | // Expect client hints on all the main frame request. |
| 386 | bool expect_client_hints_on_main_frame_; |
| 387 | // Expect client hints on all the subresource requests. |
| 388 | bool expect_client_hints_on_subresources_; |
| 389 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 390 | size_t count_client_hints_headers_seen_; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 391 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 392 | std::unique_ptr<ThirdPartyURLLoaderInterceptor> request_interceptor_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 393 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 394 | DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTest); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | // Loads a webpage that requests persisting of client hints. Verifies that |
| 398 | // the browser receives the mojo notification from the renderer and persists the |
| 399 | // client hints to the disk. |
| 400 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsHttps) { |
| 401 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 402 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 403 | |
| 404 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 405 | |
| 406 | content::FetchHistogramsFromChildProcesses(); |
| 407 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 408 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 409 | // client_hints_url() sets six client hints. |
| 410 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 411 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
| 412 | // seconds. |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 413 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 414 | 3600 * 1000, 1); |
| 415 | } |
| 416 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 417 | // Test that client hints are attached to subresources only if they belong |
| 418 | // to the same host as document host. |
| 419 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 420 | ClientHintsHttpsSubresourceDifferentOrigin) { |
| 421 | base::HistogramTester histogram_tester; |
| 422 | |
| 423 | // Add client hints for the embedded test server. |
| 424 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 425 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 426 | |
| 427 | // Verify that the client hints settings for localhost have been saved. |
| 428 | ContentSettingsForOneType client_hints_settings; |
| 429 | HostContentSettingsMap* host_content_settings_map = |
| 430 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 431 | host_content_settings_map->GetSettingsForOneType( |
| 432 | CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 433 | &client_hints_settings); |
| 434 | ASSERT_EQ(1U, client_hints_settings.size()); |
| 435 | |
| 436 | // Copy the client hints setting for localhost to foo.com. |
| 437 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 438 | GURL("https://foo.com/"), GURL(), CONTENT_SETTINGS_TYPE_CLIENT_HINTS, |
| 439 | std::string(), |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 440 | std::make_unique<base::Value>( |
Oksana Zhuravlova | b14dc88 | 2018-04-12 17:34:57 | [diff] [blame^] | 441 | client_hints_settings.at(0).setting_value.Clone())); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 442 | |
| 443 | // Verify that client hints for the two hosts has been saved. |
| 444 | host_content_settings_map = |
| 445 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 446 | host_content_settings_map->GetSettingsForOneType( |
| 447 | CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 448 | &client_hints_settings); |
| 449 | ASSERT_EQ(2U, client_hints_settings.size()); |
| 450 | |
| 451 | // Navigating to without_accept_ch_without_lifetime_img_localhost() should |
| 452 | // attach client hints to the image subresouce contained in that page since |
| 453 | // the image is located on the same server as the document origin. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 454 | SetClientHintExpectationsOnMainFrame(true); |
| 455 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 456 | ui_test_utils::NavigateToURL( |
| 457 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 458 | base::RunLoop().RunUntilIdle(); |
| 459 | content::FetchHistogramsFromChildProcesses(); |
| 460 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 461 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 462 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 463 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 464 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 465 | |
| 466 | // Navigating to without_accept_ch_without_lifetime_img_foo_com() should not |
| 467 | // attach client hints to the image subresouce contained in that page since |
| 468 | // the image is located on a different server as the document origin. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 469 | ui_test_utils::NavigateToURL( |
| 470 | browser(), without_accept_ch_without_lifetime_img_foo_com()); |
| 471 | base::RunLoop().RunUntilIdle(); |
| 472 | content::FetchHistogramsFromChildProcesses(); |
| 473 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 474 | |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 475 | // The device-memory and dprheader is attached to the main frame request. |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 476 | #if defined(OS_ANDROID) |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 477 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 478 | #else |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 479 | EXPECT_EQ(18u, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 480 | #endif |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 481 | // Requests to third party servers should not have client hints attached. |
| 482 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 483 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 487 | // An iframe loaded by the webpage requests persistence of client hints. |
| 488 | // Verify that the request from the iframe is not honored, and client hints |
| 489 | // preference is not persisted. |
| 490 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 491 | DisregardPersistenceRequestIframe) { |
| 492 | base::HistogramTester histogram_tester; |
| 493 | ContentSettingsForOneType host_settings; |
| 494 | |
| 495 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 496 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 497 | &host_settings); |
| 498 | EXPECT_EQ(0u, host_settings.size()); |
| 499 | |
| 500 | ui_test_utils::NavigateToURL(browser(), |
| 501 | accept_ch_without_lifetime_with_iframe_url()); |
| 502 | |
| 503 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 504 | |
| 505 | content::FetchHistogramsFromChildProcesses(); |
| 506 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 507 | |
| 508 | // accept_ch_without_lifetime_with_iframe_url() loads |
| 509 | // accept_ch_with_lifetime() in an iframe. The request to persist client |
| 510 | // hints from accept_ch_with_lifetime() should be disregarded. |
| 511 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 512 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 513 | } |
| 514 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 515 | // Loads a HTTP local webpage (which qualifies as a secure context) that |
| 516 | // requests persisting of client hints. Verifies that the browser receives the |
| 517 | // mojo notification from the renderer and persists the client hints to the |
| 518 | // disk. |
| 519 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 520 | ClientHintsLifetimeFollowedByNoClientHintHttpLocal) { |
| 521 | base::HistogramTester histogram_tester; |
| 522 | ContentSettingsForOneType host_settings; |
| 523 | |
| 524 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 525 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 526 | &host_settings); |
| 527 | EXPECT_EQ(0u, host_settings.size()); |
| 528 | |
| 529 | ui_test_utils::NavigateToURL(browser(), |
| 530 | accept_ch_with_lifetime_http_local_url()); |
| 531 | |
| 532 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 533 | |
| 534 | content::FetchHistogramsFromChildProcesses(); |
| 535 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 536 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 537 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 538 | // accept_ch_with_lifetime_http_local_url() sets client hints persist duration |
| 539 | // to 3600 seconds. |
| 540 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 541 | 3600 * 1000, 1); |
| 542 | |
| 543 | base::RunLoop().RunUntilIdle(); |
| 544 | |
| 545 | // Clients hints preferences for one origin should be persisted. |
| 546 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 547 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 548 | &host_settings); |
| 549 | EXPECT_EQ(1u, host_settings.size()); |
| 550 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 551 | SetClientHintExpectationsOnMainFrame(true); |
| 552 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 553 | ui_test_utils::NavigateToURL(browser(), |
| 554 | without_accept_ch_without_lifetime_local_url()); |
| 555 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 556 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 557 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 558 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 559 | } |
| 560 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 561 | // Loads a webpage that does not request persisting of client hints. |
| 562 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, NoClientHintsHttps) { |
| 563 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 564 | ui_test_utils::NavigateToURL(browser(), |
| 565 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 566 | |
| 567 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 568 | |
| 569 | content::FetchHistogramsFromChildProcesses(); |
| 570 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 571 | |
| 572 | // no_client_hints_url() does not sets the client hints. |
| 573 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 574 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 575 | } |
| 576 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 577 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 578 | ClientHintsLifetimeFollowedByNoClientHint) { |
| 579 | base::HistogramTester histogram_tester; |
| 580 | ContentSettingsForOneType host_settings; |
| 581 | |
| 582 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 583 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 584 | &host_settings); |
| 585 | EXPECT_EQ(0u, host_settings.size()); |
| 586 | |
| 587 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 588 | // client hints. |
| 589 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 590 | |
| 591 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 592 | |
| 593 | content::FetchHistogramsFromChildProcesses(); |
| 594 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 595 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 596 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 597 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
| 598 | // seconds. |
| 599 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 600 | 3600 * 1000, 1); |
| 601 | base::RunLoop().RunUntilIdle(); |
| 602 | |
| 603 | // Clients hints preferences for one origin should be persisted. |
| 604 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 605 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 606 | &host_settings); |
| 607 | EXPECT_EQ(1u, host_settings.size()); |
| 608 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 609 | SetClientHintExpectationsOnMainFrame(true); |
| 610 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 611 | ui_test_utils::NavigateToURL(browser(), |
| 612 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 613 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 614 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 615 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 616 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 617 | } |
| 618 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 619 | // Ensure that when cookies are blocked, client hint preferences are not |
| 620 | // persisted. |
| 621 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 622 | ClientHintsLifetimeNotPersistedCookiesBlocked) { |
| 623 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 624 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 625 | base::HistogramTester histogram_tester; |
| 626 | ContentSettingsForOneType host_settings; |
| 627 | |
| 628 | // Block cookies. |
| 629 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 630 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 631 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 632 | std::string(), CONTENT_SETTING_BLOCK); |
| 633 | |
| 634 | // Fetching accept_ch_with_lifetime_url() should not persist the request for |
| 635 | // client hints since cookies are blocked. |
| 636 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 637 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 638 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 639 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 640 | &host_settings); |
| 641 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 642 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 643 | |
| 644 | // Allow cookies. |
| 645 | cookie_settings_->SetCookieSetting(accept_ch_without_lifetime_url(), |
| 646 | CONTENT_SETTING_ALLOW); |
| 647 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 648 | // client hints since cookies are allowed. |
| 649 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 650 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 1); |
| 651 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 652 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 653 | &host_settings); |
| 654 | EXPECT_EQ(1u, host_settings.size()); |
| 655 | } |
| 656 | |
| 657 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 658 | ClientHintsLifetimeNotAttachedCookiesBlocked) { |
| 659 | base::HistogramTester histogram_tester; |
| 660 | ContentSettingsForOneType host_settings; |
| 661 | |
| 662 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 663 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 664 | &host_settings); |
| 665 | EXPECT_EQ(0u, host_settings.size()); |
| 666 | |
| 667 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 668 | // client hints. |
| 669 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 670 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 671 | content::FetchHistogramsFromChildProcesses(); |
| 672 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 673 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 674 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 675 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
| 676 | // 3600 seconds. |
| 677 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 678 | 3600 * 1000, 1); |
| 679 | base::RunLoop().RunUntilIdle(); |
| 680 | |
| 681 | // Clients hints preferences for one origin should be persisted. |
| 682 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 683 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 684 | &host_settings); |
| 685 | EXPECT_EQ(1u, host_settings.size()); |
| 686 | |
| 687 | // Block the cookies: Client hints should not be attached. |
| 688 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 689 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 690 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 691 | std::string(), CONTENT_SETTING_BLOCK); |
| 692 | |
| 693 | ui_test_utils::NavigateToURL(browser(), |
| 694 | without_accept_ch_without_lifetime_url()); |
| 695 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 696 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 697 | |
| 698 | // Allow the cookies: Client hints should now be attached. |
| 699 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 700 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 701 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 702 | std::string(), CONTENT_SETTING_ALLOW); |
| 703 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 704 | SetClientHintExpectationsOnMainFrame(true); |
| 705 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 706 | ui_test_utils::NavigateToURL(browser(), |
| 707 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 708 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 709 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 710 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 711 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 712 | |
| 713 | // Clear settings. |
| 714 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 715 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 716 | } |
| 717 | |
| 718 | // Ensure that when the JavaScript is blocked, client hint preferences are not |
| 719 | // persisted. |
| 720 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 721 | ClientHintsLifetimeNotPersistedJavaScriptBlocked) { |
| 722 | ContentSettingsForOneType host_settings; |
| 723 | |
| 724 | // Start a navigation. This navigation makes it possible to block JavaScript |
| 725 | // later. |
| 726 | ui_test_utils::NavigateToURL(browser(), |
| 727 | without_accept_ch_without_lifetime_url()); |
| 728 | |
| 729 | // Block the JavaScript: Client hint preferences should not be persisted. |
| 730 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 731 | ->SetContentSettingDefaultScope(accept_ch_with_lifetime_url(), GURL(), |
| 732 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 733 | std::string(), CONTENT_SETTING_BLOCK); |
| 734 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 735 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 736 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 737 | &host_settings); |
| 738 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 739 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 740 | |
| 741 | // Allow the JavaScript: Client hint preferences should be persisted. |
| 742 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 743 | ->SetContentSettingDefaultScope(accept_ch_with_lifetime_url(), GURL(), |
| 744 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 745 | std::string(), CONTENT_SETTING_ALLOW); |
| 746 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 747 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 748 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 749 | &host_settings); |
| 750 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 751 | |
| 752 | // Clear settings. |
| 753 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 754 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // Ensure that when the JavaScript is blocked, persisted client hints are not |
| 758 | // attached to the request headers. |
| 759 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 760 | ClientHintsLifetimeNotAttachedJavaScriptBlocked) { |
| 761 | base::HistogramTester histogram_tester; |
| 762 | ContentSettingsForOneType host_settings; |
| 763 | |
| 764 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 765 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 766 | &host_settings); |
| 767 | EXPECT_EQ(0u, host_settings.size()); |
| 768 | |
| 769 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 770 | // client hints. |
| 771 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 772 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 773 | content::FetchHistogramsFromChildProcesses(); |
| 774 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 775 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 776 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 777 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
| 778 | // 3600 seconds. |
| 779 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 780 | 3600 * 1000, 1); |
| 781 | base::RunLoop().RunUntilIdle(); |
| 782 | |
| 783 | // Clients hints preferences for one origin should be persisted. |
| 784 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 785 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 786 | &host_settings); |
| 787 | EXPECT_EQ(1u, host_settings.size()); |
| 788 | |
| 789 | // Block the Javascript: Client hints should not be attached. |
| 790 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 791 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
| 792 | GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 793 | std::string(), CONTENT_SETTING_BLOCK); |
| 794 | ui_test_utils::NavigateToURL(browser(), |
| 795 | without_accept_ch_without_lifetime_url()); |
| 796 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 797 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 798 | |
| 799 | // Allow the Javascript: Client hints should now be attached. |
| 800 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 801 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
| 802 | GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 803 | std::string(), CONTENT_SETTING_ALLOW); |
| 804 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 805 | SetClientHintExpectationsOnMainFrame(true); |
| 806 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 807 | ui_test_utils::NavigateToURL(browser(), |
| 808 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 809 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 810 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 811 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 812 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 813 | |
| 814 | // Clear settings. |
| 815 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 816 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
| 817 | } |
| 818 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 819 | // Ensure that when the JavaScript is blocked, client hints requested using |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 820 | // Accept-CH are not attached to the request headers for subresources. |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 821 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 822 | ClientHintsNoLifetimeScriptNotAllowed) { |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 823 | base::HistogramTester histogram_tester; |
| 824 | ContentSettingsForOneType host_settings; |
| 825 | |
| 826 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 827 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 828 | &host_settings); |
| 829 | EXPECT_EQ(0u, host_settings.size()); |
| 830 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 831 | // Block the Javascript: Client hints should not be attached. |
| 832 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 833 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 834 | ->SetContentSettingDefaultScope( |
| 835 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 836 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 837 | CONTENT_SETTING_BLOCK); |
| 838 | ui_test_utils::NavigateToURL(browser(), |
| 839 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 840 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 841 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 842 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 843 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 844 | // Allow the Javascript: Client hints should now be attached. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 845 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 846 | ->SetContentSettingDefaultScope( |
| 847 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 848 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 849 | CONTENT_SETTING_ALLOW); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 850 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 851 | SetClientHintExpectationsOnSubresources(true); |
| 852 | ui_test_utils::NavigateToURL(browser(), |
| 853 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 854 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 855 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 856 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 857 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 858 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 859 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 860 | // Clear settings. |
| 861 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 862 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 863 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 864 | // Block the Javascript again: Client hints should not be attached. |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 865 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 866 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 867 | ->SetContentSettingDefaultScope( |
| 868 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 869 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 870 | CONTENT_SETTING_BLOCK); |
| 871 | ui_test_utils::NavigateToURL(browser(), |
| 872 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 873 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 874 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 875 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 876 | |
| 877 | // Clear settings. |
| 878 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 879 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 880 | } |
| 881 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 882 | // Ensure that when the cookies is blocked, client hints are not attached to the |
| 883 | // request headers. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 884 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 885 | ClientHintsNoLifetimeCookiesNotAllowed) { |
| 886 | base::HistogramTester histogram_tester; |
| 887 | ContentSettingsForOneType host_settings; |
| 888 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 889 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 890 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 891 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 892 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 893 | &host_settings); |
| 894 | EXPECT_EQ(0u, host_settings.size()); |
| 895 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 896 | // Block cookies. |
| 897 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 898 | ->SetContentSettingDefaultScope( |
| 899 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 900 | CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 901 | base::RunLoop().RunUntilIdle(); |
| 902 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 903 | ui_test_utils::NavigateToURL(browser(), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 904 | accept_ch_without_lifetime_img_localhost()); |
| 905 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 906 | // Client hints are not attached to third party subresources even though |
| 907 | // cookies are allowed only for the first party origin. |
| 908 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 909 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 910 | |
| 911 | // Allow cookies. |
| 912 | cookie_settings_->SetCookieSetting(accept_ch_without_lifetime_img_localhost(), |
| 913 | CONTENT_SETTING_ALLOW); |
| 914 | base::RunLoop().RunUntilIdle(); |
| 915 | |
| 916 | SetClientHintExpectationsOnSubresources(true); |
| 917 | ui_test_utils::NavigateToURL(browser(), |
| 918 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 919 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 920 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 921 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 922 | |
| 923 | // Block cookies again. |
| 924 | SetClientHintExpectationsOnSubresources(false); |
| 925 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 926 | ->SetContentSettingDefaultScope( |
| 927 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 928 | CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 929 | base::RunLoop().RunUntilIdle(); |
| 930 | |
| 931 | ui_test_utils::NavigateToURL(browser(), |
| 932 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 933 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 934 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 935 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 936 | |
| 937 | // Clear settings. |
| 938 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 939 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_COOKIES); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 940 | } |
| 941 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 942 | // Check the client hints for the given URL in an incognito window. |
| 943 | // Start incognito browser twice to ensure that client hints prefs are |
| 944 | // not carried over. |
| 945 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsHttpsIncognito) { |
| 946 | for (size_t i = 0; i < 2; ++i) { |
| 947 | base::HistogramTester histogram_tester; |
| 948 | |
| 949 | Browser* incognito = CreateIncognitoBrowser(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 950 | ui_test_utils::NavigateToURL(incognito, accept_ch_with_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 951 | |
| 952 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 953 | |
| 954 | content::FetchHistogramsFromChildProcesses(); |
| 955 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 956 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 957 | // accept_ch_with_lifetime_url() sets six client hints. |
| 958 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 959 | |
| 960 | // At least one renderer must have been created. All the renderers created |
| 961 | // must have read 0 client hints. |
| 962 | EXPECT_LE(1u, |
| 963 | histogram_tester.GetAllSamples("ClientHints.CountRulesReceived") |
| 964 | .size()); |
| 965 | for (const auto& bucket : |
| 966 | histogram_tester.GetAllSamples("ClientHints.CountRulesReceived")) { |
| 967 | EXPECT_EQ(0, bucket.min); |
| 968 | } |
| 969 | // |url| sets client hints persist duration to 3600 seconds. |
| 970 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 971 | 3600 * 1000, 1); |
| 972 | |
| 973 | CloseBrowserSynchronously(incognito); |
| 974 | } |
| 975 | } |