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" |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 36 | #include "third_party/WebKit/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. |
| 349 | double value = 0.0; |
| 350 | EXPECT_TRUE( |
| 351 | base::StringToDouble(request.headers.find("rtt")->second, &value)); |
| 352 | EXPECT_LE(0, value); |
| 353 | |
| 354 | EXPECT_TRUE( |
| 355 | base::StringToDouble(request.headers.find("downlink")->second, &value)); |
| 356 | EXPECT_LE(0, value); |
| 357 | |
| 358 | EXPECT_FALSE(request.headers.find("ect")->second.empty()); |
| 359 | } |
| 360 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 361 | net::EmbeddedTestServer http_server_; |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 362 | net::EmbeddedTestServer https_server_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 363 | GURL accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 364 | GURL accept_ch_with_lifetime_url_; |
| 365 | GURL accept_ch_without_lifetime_url_; |
| 366 | GURL without_accept_ch_without_lifetime_url_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 367 | GURL without_accept_ch_without_lifetime_local_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 368 | GURL accept_ch_without_lifetime_with_iframe_url_; |
| 369 | GURL without_accept_ch_without_lifetime_img_foo_com_; |
| 370 | GURL without_accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 371 | GURL accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 372 | |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 373 | double main_frame_dpr_observed_ = -1; |
| 374 | double main_frame_viewport_width_observed_ = -1; |
| 375 | double main_frame_device_memory_observed_ = -1; |
| 376 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 377 | // Expect client hints on all the main frame request. |
| 378 | bool expect_client_hints_on_main_frame_; |
| 379 | // Expect client hints on all the subresource requests. |
| 380 | bool expect_client_hints_on_subresources_; |
| 381 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 382 | size_t count_client_hints_headers_seen_; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 383 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 384 | std::unique_ptr<ThirdPartyURLLoaderInterceptor> request_interceptor_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 385 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 386 | DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTest); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | // Loads a webpage that requests persisting of client hints. Verifies that |
| 390 | // the browser receives the mojo notification from the renderer and persists the |
| 391 | // client hints to the disk. |
| 392 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsHttps) { |
| 393 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 394 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 395 | |
| 396 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 397 | |
| 398 | content::FetchHistogramsFromChildProcesses(); |
| 399 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 400 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 401 | // client_hints_url() sets six client hints. |
| 402 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 403 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
| 404 | // seconds. |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 405 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 406 | 3600 * 1000, 1); |
| 407 | } |
| 408 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 409 | // Test that client hints are attached to subresources only if they belong |
| 410 | // to the same host as document host. |
| 411 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 412 | ClientHintsHttpsSubresourceDifferentOrigin) { |
| 413 | base::HistogramTester histogram_tester; |
| 414 | |
| 415 | // Add client hints for the embedded test server. |
| 416 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 417 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 418 | |
| 419 | // Verify that the client hints settings for localhost have been saved. |
| 420 | ContentSettingsForOneType client_hints_settings; |
| 421 | HostContentSettingsMap* host_content_settings_map = |
| 422 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 423 | host_content_settings_map->GetSettingsForOneType( |
| 424 | CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 425 | &client_hints_settings); |
| 426 | ASSERT_EQ(1U, client_hints_settings.size()); |
| 427 | |
| 428 | // Copy the client hints setting for localhost to foo.com. |
| 429 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 430 | GURL("https://foo.com/"), GURL(), CONTENT_SETTINGS_TYPE_CLIENT_HINTS, |
| 431 | std::string(), |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 432 | std::make_unique<base::Value>( |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 433 | client_hints_settings.at(0).setting_value->Clone())); |
| 434 | |
| 435 | // Verify that client hints for the two hosts has been saved. |
| 436 | host_content_settings_map = |
| 437 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 438 | host_content_settings_map->GetSettingsForOneType( |
| 439 | CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 440 | &client_hints_settings); |
| 441 | ASSERT_EQ(2U, client_hints_settings.size()); |
| 442 | |
| 443 | // Navigating to without_accept_ch_without_lifetime_img_localhost() should |
| 444 | // attach client hints to the image subresouce contained in that page since |
| 445 | // the image is located on the same server as the document origin. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 446 | SetClientHintExpectationsOnMainFrame(true); |
| 447 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 448 | ui_test_utils::NavigateToURL( |
| 449 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 450 | base::RunLoop().RunUntilIdle(); |
| 451 | content::FetchHistogramsFromChildProcesses(); |
| 452 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 453 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 454 | // 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] | 455 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 456 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 457 | |
| 458 | // Navigating to without_accept_ch_without_lifetime_img_foo_com() should not |
| 459 | // attach client hints to the image subresouce contained in that page since |
| 460 | // the image is located on a different server as the document origin. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 461 | ui_test_utils::NavigateToURL( |
| 462 | browser(), without_accept_ch_without_lifetime_img_foo_com()); |
| 463 | base::RunLoop().RunUntilIdle(); |
| 464 | content::FetchHistogramsFromChildProcesses(); |
| 465 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 466 | |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 467 | // The device-memory and dprheader is attached to the main frame request. |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 468 | #if defined(OS_ANDROID) |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 469 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 470 | #else |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 471 | EXPECT_EQ(18u, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 472 | #endif |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 473 | // Requests to third party servers should not have client hints attached. |
| 474 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 475 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 479 | // An iframe loaded by the webpage requests persistence of client hints. |
| 480 | // Verify that the request from the iframe is not honored, and client hints |
| 481 | // preference is not persisted. |
| 482 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 483 | DisregardPersistenceRequestIframe) { |
| 484 | base::HistogramTester histogram_tester; |
| 485 | ContentSettingsForOneType host_settings; |
| 486 | |
| 487 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 488 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 489 | &host_settings); |
| 490 | EXPECT_EQ(0u, host_settings.size()); |
| 491 | |
| 492 | ui_test_utils::NavigateToURL(browser(), |
| 493 | accept_ch_without_lifetime_with_iframe_url()); |
| 494 | |
| 495 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 496 | |
| 497 | content::FetchHistogramsFromChildProcesses(); |
| 498 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 499 | |
| 500 | // accept_ch_without_lifetime_with_iframe_url() loads |
| 501 | // accept_ch_with_lifetime() in an iframe. The request to persist client |
| 502 | // hints from accept_ch_with_lifetime() should be disregarded. |
| 503 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 504 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 505 | } |
| 506 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 507 | // Loads a HTTP local webpage (which qualifies as a secure context) that |
| 508 | // requests persisting of client hints. Verifies that the browser receives the |
| 509 | // mojo notification from the renderer and persists the client hints to the |
| 510 | // disk. |
| 511 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 512 | ClientHintsLifetimeFollowedByNoClientHintHttpLocal) { |
| 513 | base::HistogramTester histogram_tester; |
| 514 | ContentSettingsForOneType host_settings; |
| 515 | |
| 516 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 517 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 518 | &host_settings); |
| 519 | EXPECT_EQ(0u, host_settings.size()); |
| 520 | |
| 521 | ui_test_utils::NavigateToURL(browser(), |
| 522 | accept_ch_with_lifetime_http_local_url()); |
| 523 | |
| 524 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 525 | |
| 526 | content::FetchHistogramsFromChildProcesses(); |
| 527 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 528 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 529 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 530 | // accept_ch_with_lifetime_http_local_url() sets client hints persist duration |
| 531 | // to 3600 seconds. |
| 532 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 533 | 3600 * 1000, 1); |
| 534 | |
| 535 | base::RunLoop().RunUntilIdle(); |
| 536 | |
| 537 | // Clients hints preferences for one origin should be persisted. |
| 538 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 539 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 540 | &host_settings); |
| 541 | EXPECT_EQ(1u, host_settings.size()); |
| 542 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 543 | SetClientHintExpectationsOnMainFrame(true); |
| 544 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 545 | ui_test_utils::NavigateToURL(browser(), |
| 546 | without_accept_ch_without_lifetime_local_url()); |
| 547 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 548 | // 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] | 549 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 550 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 551 | } |
| 552 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 553 | // Loads a webpage that does not request persisting of client hints. |
| 554 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, NoClientHintsHttps) { |
| 555 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 556 | ui_test_utils::NavigateToURL(browser(), |
| 557 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 558 | |
| 559 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 560 | |
| 561 | content::FetchHistogramsFromChildProcesses(); |
| 562 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 563 | |
| 564 | // no_client_hints_url() does not sets the client hints. |
| 565 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 566 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 567 | } |
| 568 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 569 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 570 | ClientHintsLifetimeFollowedByNoClientHint) { |
| 571 | base::HistogramTester histogram_tester; |
| 572 | ContentSettingsForOneType host_settings; |
| 573 | |
| 574 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 575 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 576 | &host_settings); |
| 577 | EXPECT_EQ(0u, host_settings.size()); |
| 578 | |
| 579 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 580 | // client hints. |
| 581 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 582 | |
| 583 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 584 | |
| 585 | content::FetchHistogramsFromChildProcesses(); |
| 586 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 587 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 588 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 589 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
| 590 | // seconds. |
| 591 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 592 | 3600 * 1000, 1); |
| 593 | base::RunLoop().RunUntilIdle(); |
| 594 | |
| 595 | // Clients hints preferences for one origin should be persisted. |
| 596 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 597 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 598 | &host_settings); |
| 599 | EXPECT_EQ(1u, host_settings.size()); |
| 600 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 601 | SetClientHintExpectationsOnMainFrame(true); |
| 602 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 603 | ui_test_utils::NavigateToURL(browser(), |
| 604 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 605 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 606 | // 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] | 607 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 608 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 609 | } |
| 610 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 611 | // Ensure that when cookies are blocked, client hint preferences are not |
| 612 | // persisted. |
| 613 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 614 | ClientHintsLifetimeNotPersistedCookiesBlocked) { |
| 615 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 616 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 617 | base::HistogramTester histogram_tester; |
| 618 | ContentSettingsForOneType host_settings; |
| 619 | |
| 620 | // Block cookies. |
| 621 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 622 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 623 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 624 | std::string(), CONTENT_SETTING_BLOCK); |
| 625 | |
| 626 | // Fetching accept_ch_with_lifetime_url() should not persist the request for |
| 627 | // client hints since cookies are blocked. |
| 628 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 629 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 630 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 631 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 632 | &host_settings); |
| 633 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 634 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 635 | |
| 636 | // Allow cookies. |
| 637 | cookie_settings_->SetCookieSetting(accept_ch_without_lifetime_url(), |
| 638 | CONTENT_SETTING_ALLOW); |
| 639 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 640 | // client hints since cookies are allowed. |
| 641 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 642 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 1); |
| 643 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 644 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 645 | &host_settings); |
| 646 | EXPECT_EQ(1u, host_settings.size()); |
| 647 | } |
| 648 | |
| 649 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 650 | ClientHintsLifetimeNotAttachedCookiesBlocked) { |
| 651 | base::HistogramTester histogram_tester; |
| 652 | ContentSettingsForOneType host_settings; |
| 653 | |
| 654 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 655 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 656 | &host_settings); |
| 657 | EXPECT_EQ(0u, host_settings.size()); |
| 658 | |
| 659 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 660 | // client hints. |
| 661 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 662 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 663 | content::FetchHistogramsFromChildProcesses(); |
| 664 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 665 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 666 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 667 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
| 668 | // 3600 seconds. |
| 669 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 670 | 3600 * 1000, 1); |
| 671 | base::RunLoop().RunUntilIdle(); |
| 672 | |
| 673 | // Clients hints preferences for one origin should be persisted. |
| 674 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 675 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 676 | &host_settings); |
| 677 | EXPECT_EQ(1u, host_settings.size()); |
| 678 | |
| 679 | // Block the cookies: Client hints should not be attached. |
| 680 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 681 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 682 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 683 | std::string(), CONTENT_SETTING_BLOCK); |
| 684 | |
| 685 | ui_test_utils::NavigateToURL(browser(), |
| 686 | without_accept_ch_without_lifetime_url()); |
| 687 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 688 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 689 | |
| 690 | // Allow the cookies: Client hints should now be attached. |
| 691 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 692 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 693 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 694 | std::string(), CONTENT_SETTING_ALLOW); |
| 695 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 696 | SetClientHintExpectationsOnMainFrame(true); |
| 697 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 698 | ui_test_utils::NavigateToURL(browser(), |
| 699 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 700 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 701 | // 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] | 702 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 703 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 704 | |
| 705 | // Clear settings. |
| 706 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 707 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 708 | } |
| 709 | |
| 710 | // Ensure that when the JavaScript is blocked, client hint preferences are not |
| 711 | // persisted. |
| 712 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 713 | ClientHintsLifetimeNotPersistedJavaScriptBlocked) { |
| 714 | ContentSettingsForOneType host_settings; |
| 715 | |
| 716 | // Start a navigation. This navigation makes it possible to block JavaScript |
| 717 | // later. |
| 718 | ui_test_utils::NavigateToURL(browser(), |
| 719 | without_accept_ch_without_lifetime_url()); |
| 720 | |
| 721 | // Block the JavaScript: Client hint preferences should not be persisted. |
| 722 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 723 | ->SetContentSettingDefaultScope(accept_ch_with_lifetime_url(), GURL(), |
| 724 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 725 | std::string(), CONTENT_SETTING_BLOCK); |
| 726 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 727 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 728 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 729 | &host_settings); |
| 730 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 731 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 732 | |
| 733 | // Allow the JavaScript: Client hint preferences should be persisted. |
| 734 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 735 | ->SetContentSettingDefaultScope(accept_ch_with_lifetime_url(), GURL(), |
| 736 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 737 | std::string(), CONTENT_SETTING_ALLOW); |
| 738 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 739 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 740 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 741 | &host_settings); |
| 742 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 743 | |
| 744 | // Clear settings. |
| 745 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 746 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // Ensure that when the JavaScript is blocked, persisted client hints are not |
| 750 | // attached to the request headers. |
| 751 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 752 | ClientHintsLifetimeNotAttachedJavaScriptBlocked) { |
| 753 | base::HistogramTester histogram_tester; |
| 754 | ContentSettingsForOneType host_settings; |
| 755 | |
| 756 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 757 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 758 | &host_settings); |
| 759 | EXPECT_EQ(0u, host_settings.size()); |
| 760 | |
| 761 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 762 | // client hints. |
| 763 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 764 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 765 | content::FetchHistogramsFromChildProcesses(); |
| 766 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 767 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 768 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 769 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
| 770 | // 3600 seconds. |
| 771 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 772 | 3600 * 1000, 1); |
| 773 | base::RunLoop().RunUntilIdle(); |
| 774 | |
| 775 | // Clients hints preferences for one origin should be persisted. |
| 776 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 777 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 778 | &host_settings); |
| 779 | EXPECT_EQ(1u, host_settings.size()); |
| 780 | |
| 781 | // Block the Javascript: Client hints should not be attached. |
| 782 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 783 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
| 784 | GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 785 | std::string(), CONTENT_SETTING_BLOCK); |
| 786 | ui_test_utils::NavigateToURL(browser(), |
| 787 | without_accept_ch_without_lifetime_url()); |
| 788 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 789 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 790 | |
| 791 | // Allow the Javascript: Client hints should now be attached. |
| 792 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 793 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
| 794 | GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 795 | std::string(), CONTENT_SETTING_ALLOW); |
| 796 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 797 | SetClientHintExpectationsOnMainFrame(true); |
| 798 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 799 | ui_test_utils::NavigateToURL(browser(), |
| 800 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 801 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 802 | // 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] | 803 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 804 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 805 | |
| 806 | // Clear settings. |
| 807 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 808 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
| 809 | } |
| 810 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 811 | // Ensure that when the JavaScript is blocked, client hints requested using |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 812 | // Accept-CH are not attached to the request headers for subresources. |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 813 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 814 | ClientHintsNoLifetimeScriptNotAllowed) { |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 815 | base::HistogramTester histogram_tester; |
| 816 | ContentSettingsForOneType host_settings; |
| 817 | |
| 818 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 819 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 820 | &host_settings); |
| 821 | EXPECT_EQ(0u, host_settings.size()); |
| 822 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 823 | // Block the Javascript: Client hints should not be attached. |
| 824 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 825 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 826 | ->SetContentSettingDefaultScope( |
| 827 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 828 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 829 | CONTENT_SETTING_BLOCK); |
| 830 | ui_test_utils::NavigateToURL(browser(), |
| 831 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 832 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 833 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 834 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 835 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 836 | // Allow the Javascript: Client hints should now be attached. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 837 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 838 | ->SetContentSettingDefaultScope( |
| 839 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 840 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 841 | CONTENT_SETTING_ALLOW); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 842 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 843 | SetClientHintExpectationsOnSubresources(true); |
| 844 | ui_test_utils::NavigateToURL(browser(), |
| 845 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 846 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 847 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 848 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 849 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 850 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 851 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 852 | // Clear settings. |
| 853 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 854 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 855 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 856 | // Block the Javascript again: Client hints should not be attached. |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 857 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 858 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 859 | ->SetContentSettingDefaultScope( |
| 860 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 861 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 862 | CONTENT_SETTING_BLOCK); |
| 863 | ui_test_utils::NavigateToURL(browser(), |
| 864 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 865 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 866 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 867 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 868 | |
| 869 | // Clear settings. |
| 870 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 871 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 872 | } |
| 873 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 874 | // Ensure that when the cookies is blocked, client hints are not attached to the |
| 875 | // request headers. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 876 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 877 | ClientHintsNoLifetimeCookiesNotAllowed) { |
| 878 | base::HistogramTester histogram_tester; |
| 879 | ContentSettingsForOneType host_settings; |
| 880 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 881 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 882 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 883 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 884 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 885 | &host_settings); |
| 886 | EXPECT_EQ(0u, host_settings.size()); |
| 887 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 888 | // Block cookies. |
| 889 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 890 | ->SetContentSettingDefaultScope( |
| 891 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 892 | CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 893 | base::RunLoop().RunUntilIdle(); |
| 894 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 895 | ui_test_utils::NavigateToURL(browser(), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 896 | accept_ch_without_lifetime_img_localhost()); |
| 897 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 898 | // Client hints are not attached to third party subresources even though |
| 899 | // cookies are allowed only for the first party origin. |
| 900 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 901 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 902 | |
| 903 | // Allow cookies. |
| 904 | cookie_settings_->SetCookieSetting(accept_ch_without_lifetime_img_localhost(), |
| 905 | CONTENT_SETTING_ALLOW); |
| 906 | base::RunLoop().RunUntilIdle(); |
| 907 | |
| 908 | SetClientHintExpectationsOnSubresources(true); |
| 909 | ui_test_utils::NavigateToURL(browser(), |
| 910 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 911 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 912 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 913 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 914 | |
| 915 | // Block cookies again. |
| 916 | SetClientHintExpectationsOnSubresources(false); |
| 917 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 918 | ->SetContentSettingDefaultScope( |
| 919 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 920 | CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 921 | base::RunLoop().RunUntilIdle(); |
| 922 | |
| 923 | ui_test_utils::NavigateToURL(browser(), |
| 924 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 925 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 926 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 927 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 928 | |
| 929 | // Clear settings. |
| 930 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 931 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_COOKIES); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 932 | } |
| 933 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 934 | // Check the client hints for the given URL in an incognito window. |
| 935 | // Start incognito browser twice to ensure that client hints prefs are |
| 936 | // not carried over. |
| 937 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsHttpsIncognito) { |
| 938 | for (size_t i = 0; i < 2; ++i) { |
| 939 | base::HistogramTester histogram_tester; |
| 940 | |
| 941 | Browser* incognito = CreateIncognitoBrowser(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 942 | ui_test_utils::NavigateToURL(incognito, accept_ch_with_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 943 | |
| 944 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 945 | |
| 946 | content::FetchHistogramsFromChildProcesses(); |
| 947 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 948 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame^] | 949 | // accept_ch_with_lifetime_url() sets six client hints. |
| 950 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 951 | |
| 952 | // At least one renderer must have been created. All the renderers created |
| 953 | // must have read 0 client hints. |
| 954 | EXPECT_LE(1u, |
| 955 | histogram_tester.GetAllSamples("ClientHints.CountRulesReceived") |
| 956 | .size()); |
| 957 | for (const auto& bucket : |
| 958 | histogram_tester.GetAllSamples("ClientHints.CountRulesReceived")) { |
| 959 | EXPECT_EQ(0, bucket.min); |
| 960 | } |
| 961 | // |url| sets client hints persist duration to 3600 seconds. |
| 962 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 963 | 3600 * 1000, 1); |
| 964 | |
| 965 | CloseBrowserSynchronously(incognito); |
| 966 | } |
| 967 | } |