| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/net/chrome_net_log.h" | 10 #include "chrome/browser/net/chrome_net_log.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 int* os_error) override { | 47 int* os_error) override { |
| 48 BrowserThread::PostTask( | 48 BrowserThread::PostTask( |
| 49 BrowserThread::UI, | 49 BrowserThread::UI, |
| 50 FROM_HERE, | 50 FROM_HERE, |
| 51 base::Bind(&HostResolutionRequestRecorder::AddToHistory, | 51 base::Bind(&HostResolutionRequestRecorder::AddToHistory, |
| 52 base::Unretained(this), | 52 base::Unretained(this), |
| 53 host)); | 53 host)); |
| 54 return net::ERR_NAME_NOT_RESOLVED; | 54 return net::ERR_NAME_NOT_RESOLVED; |
| 55 } | 55 } |
| 56 | 56 |
| 57 int RequestedHostnameCount() const { |
| 58 return requested_hostnames_.size(); |
| 59 } |
| 60 |
| 57 bool HasHostBeenRequested(const std::string& hostname) { | 61 bool HasHostBeenRequested(const std::string& hostname) { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 59 return std::find(requested_hostnames_.begin(), | 63 return std::find(requested_hostnames_.begin(), |
| 60 requested_hostnames_.end(), | 64 requested_hostnames_.end(), |
| 61 hostname) != requested_hostnames_.end(); | 65 hostname) != requested_hostnames_.end(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 void WaitUntilHostHasBeenRequested(const std::string& hostname) { | 68 void WaitUntilHostHasBeenRequested(const std::string& hostname) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 66 DCHECK(!is_waiting_for_hostname_); | 70 DCHECK(!is_waiting_for_hostname_); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 PrefService* prefs = browser()->profile()->GetPrefs(); | 211 PrefService* prefs = browser()->profile()->GetPrefs(); |
| 208 const base::ListValue* list_value = prefs->GetList(list_path); | 212 const base::ListValue* list_value = prefs->GetList(list_path); |
| 209 JSONStringValueSerializer serializer(value_as_string); | 213 JSONStringValueSerializer serializer(value_as_string); |
| 210 serializer.Serialize(*list_value); | 214 serializer.Serialize(*list_value); |
| 211 } | 215 } |
| 212 | 216 |
| 213 void WaitUntilHostHasBeenRequested(const std::string& hostname) { | 217 void WaitUntilHostHasBeenRequested(const std::string& hostname) { |
| 214 host_resolution_request_recorder_->WaitUntilHostHasBeenRequested(hostname); | 218 host_resolution_request_recorder_->WaitUntilHostHasBeenRequested(hostname); |
| 215 } | 219 } |
| 216 | 220 |
| 221 int RequestedHostnameCount() const { |
| 222 return host_resolution_request_recorder_->RequestedHostnameCount(); |
| 223 } |
| 224 |
| 217 const GURL startup_url_; | 225 const GURL startup_url_; |
| 218 const GURL referring_url_; | 226 const GURL referring_url_; |
| 219 const GURL target_url_; | 227 const GURL target_url_; |
| 220 | 228 |
| 221 private: | 229 private: |
| 222 scoped_refptr<HostResolutionRequestRecorder> | 230 scoped_refptr<HostResolutionRequestRecorder> |
| 223 host_resolution_request_recorder_; | 231 host_resolution_request_recorder_; |
| 224 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; | 232 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; |
| 225 }; | 233 }; |
| 226 | 234 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 247 | 255 |
| 248 // But also make sure this data has been first loaded into the Predictor, by | 256 // But also make sure this data has been first loaded into the Predictor, by |
| 249 // inspecting that the Predictor starts making the expected hostname requests. | 257 // inspecting that the Predictor starts making the expected hostname requests. |
| 250 PrepareFrameSubresources(referring_url_); | 258 PrepareFrameSubresources(referring_url_); |
| 251 WaitUntilHostHasBeenRequested(startup_url_.host()); | 259 WaitUntilHostHasBeenRequested(startup_url_.host()); |
| 252 WaitUntilHostHasBeenRequested(target_url_.host()); | 260 WaitUntilHostHasBeenRequested(target_url_.host()); |
| 253 } | 261 } |
| 254 | 262 |
| 255 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { | 263 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { |
| 256 ASSERT_TRUE(test_server()->Start()); | 264 ASSERT_TRUE(test_server()->Start()); |
| 265 int hostnames_requested_before_load = RequestedHostnameCount(); |
| 257 ui_test_utils::NavigateToURL( | 266 ui_test_utils::NavigateToURL( |
| 258 browser(), | 267 browser(), |
| 259 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); | 268 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); |
| 260 WaitUntilHostHasBeenRequested(kChromiumHostname); | 269 WaitUntilHostHasBeenRequested(kChromiumHostname); |
| 270 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount()); |
| 261 } | 271 } |
| 262 | 272 |
| 263 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) { | 273 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) { |
| 264 ASSERT_TRUE(test_server()->Start()); | 274 ASSERT_TRUE(test_server()->Start()); |
| 265 | 275 |
| 266 // Create a HTML preconnect reference to the local server in the form | 276 // Create a HTML preconnect reference to the local server in the form |
| 267 // <link rel="preconnect" href="http://test-server/"> | 277 // <link rel="preconnect" href="http://test-server/"> |
| 268 // and navigate to it as a data URI. | 278 // and navigate to it as a data URI. |
| 269 GURL preconnect_url = test_server()->GetURL(""); | 279 GURL preconnect_url = test_server()->GetURL(""); |
| 270 std::string preconnect_content = | 280 std::string preconnect_content = |
| 271 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; | 281 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; |
| 272 std::string encoded; | 282 std::string encoded; |
| 273 base::Base64Encode(preconnect_content, &encoded); | 283 base::Base64Encode(preconnect_content, &encoded); |
| 274 std::string data_uri = "data:text/html;base64," + encoded; | 284 std::string data_uri = "data:text/html;base64," + encoded; |
| 275 | 285 |
| 276 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url); | 286 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url); |
| 277 ConnectNetLogObserver net_log_observer(host_port_pair.ToString()); | 287 ConnectNetLogObserver net_log_observer(host_port_pair.ToString()); |
| 278 net_log_observer.Attach(); | 288 net_log_observer.Attach(); |
| 279 | 289 |
| 280 ui_test_utils::NavigateToURL(browser(), GURL(data_uri)); | 290 ui_test_utils::NavigateToURL(browser(), GURL(data_uri)); |
| 281 | 291 |
| 282 net_log_observer.WaitForConnect(); | 292 net_log_observer.WaitForConnect(); |
| 283 net_log_observer.Detach(); | 293 net_log_observer.Detach(); |
| 284 } | 294 } |
| 285 | 295 |
| 286 } // namespace chrome_browser_net | 296 } // namespace chrome_browser_net |
| 287 | 297 |
| OLD | NEW |