| 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 "chrome/browser/intranet_redirect_detector.h" | 5 #include "chrome/browser/intranet_redirect_detector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 20 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
| 23 | 23 |
| 24 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; | 24 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; |
| 25 | 25 |
| 26 IntranetRedirectDetector::IntranetRedirectDetector() | 26 IntranetRedirectDetector::IntranetRedirectDetector() |
| 27 : redirect_origin_(g_browser_process->local_state()->GetString( | 27 : redirect_origin_(g_browser_process->local_state()->GetString( |
| 28 prefs::kLastKnownIntranetRedirectOrigin)), | 28 prefs::kLastKnownIntranetRedirectOrigin)), |
| 29 weak_factory_(this), | 29 in_sleep_(true), |
| 30 in_sleep_(true) { | 30 weak_ptr_factory_(this) { |
| 31 // Because this function can be called during startup, when kicking off a URL | 31 // Because this function can be called during startup, when kicking off a URL |
| 32 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully | 32 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully |
| 33 // long enough to be after startup, but still get results back quickly. | 33 // long enough to be after startup, but still get results back quickly. |
| 34 // Ideally, instead of this timer, we'd do something like "check if the | 34 // Ideally, instead of this timer, we'd do something like "check if the |
| 35 // browser is starting up, and if so, come back later", but there is currently | 35 // browser is starting up, and if so, come back later", but there is currently |
| 36 // no function to do this. | 36 // no function to do this. |
| 37 static const int kStartFetchDelaySeconds = 7; | 37 static const int kStartFetchDelaySeconds = 7; |
| 38 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 38 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 39 base::Bind(&IntranetRedirectDetector::FinishSleep, | 39 base::Bind(&IntranetRedirectDetector::FinishSleep, |
| 40 weak_factory_.GetWeakPtr()), | 40 weak_ptr_factory_.GetWeakPtr()), |
| 41 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); | 41 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); |
| 42 | 42 |
| 43 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 43 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 44 } | 44 } |
| 45 | 45 |
| 46 IntranetRedirectDetector::~IntranetRedirectDetector() { | 46 IntranetRedirectDetector::~IntranetRedirectDetector() { |
| 47 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 47 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 48 STLDeleteElements(&fetchers_); | 48 STLDeleteElements(&fetchers_); |
| 49 } | 49 } |
| 50 | 50 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 // If a request is already scheduled, do not scheduled yet another one. | 154 // If a request is already scheduled, do not scheduled yet another one. |
| 155 if (in_sleep_) | 155 if (in_sleep_) |
| 156 return; | 156 return; |
| 157 | 157 |
| 158 // Since presumably many programs open connections after network changes, | 158 // Since presumably many programs open connections after network changes, |
| 159 // delay this a little bit. | 159 // delay this a little bit. |
| 160 in_sleep_ = true; | 160 in_sleep_ = true; |
| 161 static const int kNetworkSwitchDelayMS = 1000; | 161 static const int kNetworkSwitchDelayMS = 1000; |
| 162 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 162 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 163 base::Bind(&IntranetRedirectDetector::FinishSleep, | 163 base::Bind(&IntranetRedirectDetector::FinishSleep, |
| 164 weak_factory_.GetWeakPtr()), | 164 weak_ptr_factory_.GetWeakPtr()), |
| 165 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); | 165 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 IntranetRedirectHostResolverProc::IntranetRedirectHostResolverProc( | 168 IntranetRedirectHostResolverProc::IntranetRedirectHostResolverProc( |
| 169 net::HostResolverProc* previous) | 169 net::HostResolverProc* previous) |
| 170 : net::HostResolverProc(previous) { | 170 : net::HostResolverProc(previous) { |
| 171 } | 171 } |
| 172 | 172 |
| 173 int IntranetRedirectHostResolverProc::Resolve( | 173 int IntranetRedirectHostResolverProc::Resolve( |
| 174 const std::string& host, | 174 const std::string& host, |
| 175 net::AddressFamily address_family, | 175 net::AddressFamily address_family, |
| 176 net::HostResolverFlags host_resolver_flags, | 176 net::HostResolverFlags host_resolver_flags, |
| 177 net::AddressList* addrlist, | 177 net::AddressList* addrlist, |
| 178 int* os_error) { | 178 int* os_error) { |
| 179 // We'd love to just ask the IntranetRedirectDetector, but we may not be on | 179 // We'd love to just ask the IntranetRedirectDetector, but we may not be on |
| 180 // the same thread. So just use the heuristic that any all-lowercase a-z | 180 // the same thread. So just use the heuristic that any all-lowercase a-z |
| 181 // hostname with the right number of characters is likely from the detector | 181 // hostname with the right number of characters is likely from the detector |
| 182 // (and thus should be blocked). | 182 // (and thus should be blocked). |
| 183 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && | 183 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && |
| 184 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == | 184 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == |
| 185 std::string::npos)) ? | 185 std::string::npos)) ? |
| 186 net::ERR_NAME_NOT_RESOLVED : | 186 net::ERR_NAME_NOT_RESOLVED : |
| 187 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, | 187 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, |
| 188 os_error); | 188 os_error); |
| 189 } | 189 } |
| OLD | NEW |