| 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 12 matching lines...) Expand all Loading... |
| 23 #include "net/dns/mock_host_resolver.h" | 23 #include "net/dns/mock_host_resolver.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 | 25 |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 using testing::HasSubstr; | 27 using testing::HasSubstr; |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 const char kBlinkPreconnectFeature[] = "LinkPreconnect"; | 31 const char kBlinkPreconnectFeature[] = "LinkPreconnect"; |
| 32 const char kChromiumHostname[] = "chromium.org"; | 32 const char kChromiumHostname[] = "chromium.org"; |
| 33 const char kInvalidLongHostname[] = "illegally-long-hostname-over-255-" |
| 34 "characters-should-not-send-an-ipc-message-to-the-browser-" |
| 35 "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 36 "0000000000000000000000000000000000000000000000000000000000000000000000000" |
| 37 "000000000000000000000000000000000000000000000000000000.org"; |
| 33 | 38 |
| 34 // Records a history of all hostnames for which resolving has been requested, | 39 // Records a history of all hostnames for which resolving has been requested, |
| 35 // and immediately fails the resolution requests themselves. | 40 // and immediately fails the resolution requests themselves. |
| 36 class HostResolutionRequestRecorder : public net::HostResolverProc { | 41 class HostResolutionRequestRecorder : public net::HostResolverProc { |
| 37 public: | 42 public: |
| 38 HostResolutionRequestRecorder() | 43 HostResolutionRequestRecorder() |
| 39 : HostResolverProc(NULL), | 44 : HostResolverProc(NULL), |
| 40 is_waiting_for_hostname_(false) { | 45 is_waiting_for_hostname_(false) { |
| 41 } | 46 } |
| 42 | 47 |
| 43 int Resolve(const std::string& host, | 48 int Resolve(const std::string& host, |
| 44 net::AddressFamily address_family, | 49 net::AddressFamily address_family, |
| 45 net::HostResolverFlags host_resolver_flags, | 50 net::HostResolverFlags host_resolver_flags, |
| 46 net::AddressList* addrlist, | 51 net::AddressList* addrlist, |
| 47 int* os_error) override { | 52 int* os_error) override { |
| 48 BrowserThread::PostTask( | 53 BrowserThread::PostTask( |
| 49 BrowserThread::UI, | 54 BrowserThread::UI, |
| 50 FROM_HERE, | 55 FROM_HERE, |
| 51 base::Bind(&HostResolutionRequestRecorder::AddToHistory, | 56 base::Bind(&HostResolutionRequestRecorder::AddToHistory, |
| 52 base::Unretained(this), | 57 base::Unretained(this), |
| 53 host)); | 58 host)); |
| 54 return net::ERR_NAME_NOT_RESOLVED; | 59 return net::ERR_NAME_NOT_RESOLVED; |
| 55 } | 60 } |
| 56 | 61 |
| 57 bool HasHostBeenRequested(const std::string& hostname) { | 62 int RequestedHostnameCount() const { |
| 63 return requested_hostnames_.size(); |
| 64 } |
| 65 |
| 66 bool HasHostBeenRequested(const std::string& hostname) const { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 59 return std::find(requested_hostnames_.begin(), | 68 return std::find(requested_hostnames_.begin(), |
| 60 requested_hostnames_.end(), | 69 requested_hostnames_.end(), |
| 61 hostname) != requested_hostnames_.end(); | 70 hostname) != requested_hostnames_.end(); |
| 62 } | 71 } |
| 63 | 72 |
| 64 void WaitUntilHostHasBeenRequested(const std::string& hostname) { | 73 void WaitUntilHostHasBeenRequested(const std::string& hostname) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 66 DCHECK(!is_waiting_for_hostname_); | 75 DCHECK(!is_waiting_for_hostname_); |
| 67 if (HasHostBeenRequested(hostname)) | 76 if (HasHostBeenRequested(hostname)) |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 } | 212 } |
| 204 | 213 |
| 205 void GetListFromPrefsAsString(const char* list_path, | 214 void GetListFromPrefsAsString(const char* list_path, |
| 206 std::string* value_as_string) const { | 215 std::string* value_as_string) const { |
| 207 PrefService* prefs = browser()->profile()->GetPrefs(); | 216 PrefService* prefs = browser()->profile()->GetPrefs(); |
| 208 const base::ListValue* list_value = prefs->GetList(list_path); | 217 const base::ListValue* list_value = prefs->GetList(list_path); |
| 209 JSONStringValueSerializer serializer(value_as_string); | 218 JSONStringValueSerializer serializer(value_as_string); |
| 210 serializer.Serialize(*list_value); | 219 serializer.Serialize(*list_value); |
| 211 } | 220 } |
| 212 | 221 |
| 222 bool HasHostBeenRequested(const std::string& hostname) const { |
| 223 return host_resolution_request_recorder_->HasHostBeenRequested(hostname); |
| 224 } |
| 225 |
| 213 void WaitUntilHostHasBeenRequested(const std::string& hostname) { | 226 void WaitUntilHostHasBeenRequested(const std::string& hostname) { |
| 214 host_resolution_request_recorder_->WaitUntilHostHasBeenRequested(hostname); | 227 host_resolution_request_recorder_->WaitUntilHostHasBeenRequested(hostname); |
| 215 } | 228 } |
| 216 | 229 |
| 230 int RequestedHostnameCount() const { |
| 231 return host_resolution_request_recorder_->RequestedHostnameCount(); |
| 232 } |
| 233 |
| 217 const GURL startup_url_; | 234 const GURL startup_url_; |
| 218 const GURL referring_url_; | 235 const GURL referring_url_; |
| 219 const GURL target_url_; | 236 const GURL target_url_; |
| 220 | 237 |
| 221 private: | 238 private: |
| 222 scoped_refptr<HostResolutionRequestRecorder> | 239 scoped_refptr<HostResolutionRequestRecorder> |
| 223 host_resolution_request_recorder_; | 240 host_resolution_request_recorder_; |
| 224 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; | 241 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; |
| 225 }; | 242 }; |
| 226 | 243 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 247 | 264 |
| 248 // But also make sure this data has been first loaded into the Predictor, by | 265 // 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. | 266 // inspecting that the Predictor starts making the expected hostname requests. |
| 250 PrepareFrameSubresources(referring_url_); | 267 PrepareFrameSubresources(referring_url_); |
| 251 WaitUntilHostHasBeenRequested(startup_url_.host()); | 268 WaitUntilHostHasBeenRequested(startup_url_.host()); |
| 252 WaitUntilHostHasBeenRequested(target_url_.host()); | 269 WaitUntilHostHasBeenRequested(target_url_.host()); |
| 253 } | 270 } |
| 254 | 271 |
| 255 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { | 272 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { |
| 256 ASSERT_TRUE(test_server()->Start()); | 273 ASSERT_TRUE(test_server()->Start()); |
| 274 int hostnames_requested_before_load = RequestedHostnameCount(); |
| 257 ui_test_utils::NavigateToURL( | 275 ui_test_utils::NavigateToURL( |
| 258 browser(), | 276 browser(), |
| 259 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); | 277 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); |
| 260 WaitUntilHostHasBeenRequested(kChromiumHostname); | 278 WaitUntilHostHasBeenRequested(kChromiumHostname); |
| 279 ASSERT_FALSE(HasHostBeenRequested(kInvalidLongHostname)); |
| 280 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount()); |
| 261 } | 281 } |
| 262 | 282 |
| 263 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) { | 283 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) { |
| 264 ASSERT_TRUE(test_server()->Start()); | 284 ASSERT_TRUE(test_server()->Start()); |
| 265 | 285 |
| 266 // Create a HTML preconnect reference to the local server in the form | 286 // Create a HTML preconnect reference to the local server in the form |
| 267 // <link rel="preconnect" href="http://test-server/"> | 287 // <link rel="preconnect" href="http://test-server/"> |
| 268 // and navigate to it as a data URI. | 288 // and navigate to it as a data URI. |
| 269 GURL preconnect_url = test_server()->GetURL(""); | 289 GURL preconnect_url = test_server()->GetURL(""); |
| 270 std::string preconnect_content = | 290 std::string preconnect_content = |
| 271 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; | 291 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; |
| 272 std::string encoded; | 292 std::string encoded; |
| 273 base::Base64Encode(preconnect_content, &encoded); | 293 base::Base64Encode(preconnect_content, &encoded); |
| 274 std::string data_uri = "data:text/html;base64," + encoded; | 294 std::string data_uri = "data:text/html;base64," + encoded; |
| 275 | 295 |
| 276 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url); | 296 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url); |
| 277 ConnectNetLogObserver net_log_observer(host_port_pair.ToString()); | 297 ConnectNetLogObserver net_log_observer(host_port_pair.ToString()); |
| 278 net_log_observer.Attach(); | 298 net_log_observer.Attach(); |
| 279 | 299 |
| 280 ui_test_utils::NavigateToURL(browser(), GURL(data_uri)); | 300 ui_test_utils::NavigateToURL(browser(), GURL(data_uri)); |
| 281 | 301 |
| 282 net_log_observer.WaitForConnect(); | 302 net_log_observer.WaitForConnect(); |
| 283 net_log_observer.Detach(); | 303 net_log_observer.Detach(); |
| 284 } | 304 } |
| 285 | 305 |
| 286 } // namespace chrome_browser_net | 306 } // namespace chrome_browser_net |
| 287 | 307 |
| OLD | NEW |