| 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/json/json_string_value_serializer.h" | 5 #include "base/json/json_string_value_serializer.h" |
| 6 #include "base/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
| 7 #include "chrome/browser/net/predictor.h" | 7 #include "chrome/browser/net/predictor.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" | 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" | 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/dns/host_resolver_proc.h" | 15 #include "net/dns/host_resolver_proc.h" |
| 16 #include "net/dns/mock_host_resolver.h" | 16 #include "net/dns/mock_host_resolver.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| 20 using testing::HasSubstr; | 20 using testing::HasSubstr; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kChromiumHostname[] = "chromium.org"; | 24 const char kChromiumHostname[] = "chromium.org"; |
| 25 const char kInvalidLongHostname[] = "illegally-long-hostname-over-255-" | |
| 26 "characters-should-not-send-an-ipc-message-to-the-browser-" | |
| 27 "0000000000000000000000000000000000000000000000000000000000000000000000000" | |
| 28 "0000000000000000000000000000000000000000000000000000000000000000000000000" | |
| 29 "000000000000000000000000000000000000000000000000000000.org"; | |
| 30 | 25 |
| 31 // Records a history of all hostnames for which resolving has been requested, | 26 // Records a history of all hostnames for which resolving has been requested, |
| 32 // and immediately fails the resolution requests themselves. | 27 // and immediately fails the resolution requests themselves. |
| 33 class HostResolutionRequestRecorder : public net::HostResolverProc { | 28 class HostResolutionRequestRecorder : public net::HostResolverProc { |
| 34 public: | 29 public: |
| 35 HostResolutionRequestRecorder() | 30 HostResolutionRequestRecorder() |
| 36 : HostResolverProc(NULL), | 31 : HostResolverProc(NULL), |
| 37 is_waiting_for_hostname_(false) { | 32 is_waiting_for_hostname_(false) { |
| 38 } | 33 } |
| 39 | 34 |
| 40 int Resolve(const std::string& host, | 35 int Resolve(const std::string& host, |
| 41 net::AddressFamily address_family, | 36 net::AddressFamily address_family, |
| 42 net::HostResolverFlags host_resolver_flags, | 37 net::HostResolverFlags host_resolver_flags, |
| 43 net::AddressList* addrlist, | 38 net::AddressList* addrlist, |
| 44 int* os_error) override { | 39 int* os_error) override { |
| 45 BrowserThread::PostTask( | 40 BrowserThread::PostTask( |
| 46 BrowserThread::UI, | 41 BrowserThread::UI, |
| 47 FROM_HERE, | 42 FROM_HERE, |
| 48 base::Bind(&HostResolutionRequestRecorder::AddToHistory, | 43 base::Bind(&HostResolutionRequestRecorder::AddToHistory, |
| 49 base::Unretained(this), | 44 base::Unretained(this), |
| 50 host)); | 45 host)); |
| 51 return net::ERR_NAME_NOT_RESOLVED; | 46 return net::ERR_NAME_NOT_RESOLVED; |
| 52 } | 47 } |
| 53 | 48 |
| 54 int RequestedHostnameCount() const { | 49 bool HasHostBeenRequested(const std::string& hostname) { |
| 55 return requested_hostnames_.size(); | |
| 56 } | |
| 57 | |
| 58 bool HasHostBeenRequested(const std::string& hostname) const { | |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 return std::find(requested_hostnames_.begin(), | 51 return std::find(requested_hostnames_.begin(), |
| 61 requested_hostnames_.end(), | 52 requested_hostnames_.end(), |
| 62 hostname) != requested_hostnames_.end(); | 53 hostname) != requested_hostnames_.end(); |
| 63 } | 54 } |
| 64 | 55 |
| 65 void WaitUntilHostHasBeenRequested(const std::string& hostname) { | 56 void WaitUntilHostHasBeenRequested(const std::string& hostname) { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 67 DCHECK(!is_waiting_for_hostname_); | 58 DCHECK(!is_waiting_for_hostname_); |
| 68 if (HasHostBeenRequested(hostname)) | 59 if (HasHostBeenRequested(hostname)) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 143 } |
| 153 | 144 |
| 154 void GetListFromPrefsAsString(const char* list_path, | 145 void GetListFromPrefsAsString(const char* list_path, |
| 155 std::string* value_as_string) const { | 146 std::string* value_as_string) const { |
| 156 PrefService* prefs = browser()->profile()->GetPrefs(); | 147 PrefService* prefs = browser()->profile()->GetPrefs(); |
| 157 const base::ListValue* list_value = prefs->GetList(list_path); | 148 const base::ListValue* list_value = prefs->GetList(list_path); |
| 158 JSONStringValueSerializer serializer(value_as_string); | 149 JSONStringValueSerializer serializer(value_as_string); |
| 159 serializer.Serialize(*list_value); | 150 serializer.Serialize(*list_value); |
| 160 } | 151 } |
| 161 | 152 |
| 162 bool HasHostBeenRequested(const std::string& hostname) const { | |
| 163 return host_resolution_request_recorder_->HasHostBeenRequested(hostname); | |
| 164 } | |
| 165 | |
| 166 void WaitUntilHostHasBeenRequested(const std::string& hostname) { | 153 void WaitUntilHostHasBeenRequested(const std::string& hostname) { |
| 167 host_resolution_request_recorder_->WaitUntilHostHasBeenRequested(hostname); | 154 host_resolution_request_recorder_->WaitUntilHostHasBeenRequested(hostname); |
| 168 } | 155 } |
| 169 | 156 |
| 170 int RequestedHostnameCount() const { | |
| 171 return host_resolution_request_recorder_->RequestedHostnameCount(); | |
| 172 } | |
| 173 | |
| 174 const GURL startup_url_; | 157 const GURL startup_url_; |
| 175 const GURL referring_url_; | 158 const GURL referring_url_; |
| 176 const GURL target_url_; | 159 const GURL target_url_; |
| 177 | 160 |
| 178 private: | 161 private: |
| 179 scoped_refptr<HostResolutionRequestRecorder> | 162 scoped_refptr<HostResolutionRequestRecorder> |
| 180 host_resolution_request_recorder_; | 163 host_resolution_request_recorder_; |
| 181 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; | 164 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; |
| 182 }; | 165 }; |
| 183 | 166 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 204 | 187 |
| 205 // But also make sure this data has been first loaded into the Predictor, by | 188 // But also make sure this data has been first loaded into the Predictor, by |
| 206 // inspecting that the Predictor starts making the expected hostname requests. | 189 // inspecting that the Predictor starts making the expected hostname requests. |
| 207 PrepareFrameSubresources(referring_url_); | 190 PrepareFrameSubresources(referring_url_); |
| 208 WaitUntilHostHasBeenRequested(startup_url_.host()); | 191 WaitUntilHostHasBeenRequested(startup_url_.host()); |
| 209 WaitUntilHostHasBeenRequested(target_url_.host()); | 192 WaitUntilHostHasBeenRequested(target_url_.host()); |
| 210 } | 193 } |
| 211 | 194 |
| 212 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { | 195 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { |
| 213 ASSERT_TRUE(test_server()->Start()); | 196 ASSERT_TRUE(test_server()->Start()); |
| 214 int hostnames_requested_before_load = RequestedHostnameCount(); | |
| 215 ui_test_utils::NavigateToURL( | 197 ui_test_utils::NavigateToURL( |
| 216 browser(), | 198 browser(), |
| 217 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); | 199 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); |
| 218 WaitUntilHostHasBeenRequested(kChromiumHostname); | 200 WaitUntilHostHasBeenRequested(kChromiumHostname); |
| 219 ASSERT_FALSE(HasHostBeenRequested(kInvalidLongHostname)); | |
| 220 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount()); | |
| 221 } | 201 } |
| 222 | 202 |
| 223 } // namespace chrome_browser_net | 203 } // namespace chrome_browser_net |
| 224 | 204 |
| OLD | NEW |