| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/net/url_request_context_getter.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "content/common/notification_service.h" | 16 #include "content/common/notification_service.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_domain.h" | 19 #include "net/base/registry_controlled_domain.h" |
| 20 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 21 | 21 |
| 22 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; | 22 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; |
| 23 | 23 |
| 24 IntranetRedirectDetector::IntranetRedirectDetector() | 24 IntranetRedirectDetector::IntranetRedirectDetector() |
| 25 : redirect_origin_(g_browser_process->local_state()->GetString( | 25 : redirect_origin_(g_browser_process->local_state()->GetString( |
| 26 prefs::kLastKnownIntranetRedirectOrigin)), | 26 prefs::kLastKnownIntranetRedirectOrigin)), |
| 27 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), | 27 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), |
| 28 in_sleep_(true), | 28 in_sleep_(true) { |
| 29 request_context_available_(Profile::GetDefaultRequestContext() != NULL) { | |
| 30 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, | |
| 31 NotificationService::AllSources()); | |
| 32 | |
| 33 // Because this function can be called during startup, when kicking off a URL | 29 // Because this function can be called during startup, when kicking off a URL |
| 34 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully | 30 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully |
| 35 // long enough to be after startup, but still get results back quickly. | 31 // long enough to be after startup, but still get results back quickly. |
| 36 // Ideally, instead of this timer, we'd do something like "check if the | 32 // Ideally, instead of this timer, we'd do something like "check if the |
| 37 // browser is starting up, and if so, come back later", but there is currently | 33 // browser is starting up, and if so, come back later", but there is currently |
| 38 // no function to do this. | 34 // no function to do this. |
| 39 static const int kStartFetchDelayMS = 7000; | 35 static const int kStartFetchDelayMS = 7000; |
| 40 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 36 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 41 fetcher_factory_.NewRunnableMethod( | 37 fetcher_factory_.NewRunnableMethod( |
| 42 &IntranetRedirectDetector::FinishSleep), | 38 &IntranetRedirectDetector::FinishSleep), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 63 std::string()); | 59 std::string()); |
| 64 } | 60 } |
| 65 | 61 |
| 66 void IntranetRedirectDetector::FinishSleep() { | 62 void IntranetRedirectDetector::FinishSleep() { |
| 67 in_sleep_ = false; | 63 in_sleep_ = false; |
| 68 | 64 |
| 69 // If another fetch operation is still running, cancel it. | 65 // If another fetch operation is still running, cancel it. |
| 70 STLDeleteElements(&fetchers_); | 66 STLDeleteElements(&fetchers_); |
| 71 resulting_origins_.clear(); | 67 resulting_origins_.clear(); |
| 72 | 68 |
| 73 StartFetchesIfPossible(); | |
| 74 } | |
| 75 | |
| 76 void IntranetRedirectDetector::StartFetchesIfPossible() { | |
| 77 // Bail if a fetch isn't appropriate right now. This function will be called | |
| 78 // again each time one of the preconditions changes, so we'll fetch | |
| 79 // immediately once all of them are met. | |
| 80 if (in_sleep_ || !request_context_available_) | |
| 81 return; | |
| 82 | |
| 83 // The detector is not needed in Chrome Frame since we have no omnibox there. | 69 // The detector is not needed in Chrome Frame since we have no omnibox there. |
| 84 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 70 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 85 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking) || | 71 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking) || |
| 86 cmd_line->HasSwitch(switches::kChromeFrame)) | 72 cmd_line->HasSwitch(switches::kChromeFrame)) |
| 87 return; | 73 return; |
| 88 | 74 |
| 89 DCHECK(fetchers_.empty() && resulting_origins_.empty()); | 75 DCHECK(fetchers_.empty() && resulting_origins_.empty()); |
| 90 | 76 |
| 91 // Start three fetchers on random hostnames. | 77 // Start three fetchers on random hostnames. |
| 92 for (size_t i = 0; i < 3; ++i) { | 78 for (size_t i = 0; i < 3; ++i) { |
| 93 std::string url_string("http://"); | 79 std::string url_string("http://"); |
| 94 for (size_t j = 0; j < kNumCharsInHostnames; ++j) | 80 for (size_t j = 0; j < kNumCharsInHostnames; ++j) |
| 95 url_string += ('a' + base::RandInt(0, 'z' - 'a')); | 81 url_string += ('a' + base::RandInt(0, 'z' - 'a')); |
| 96 GURL random_url(url_string + '/'); | 82 GURL random_url(url_string + '/'); |
| 97 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); | 83 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); |
| 98 // We don't want these fetches to affect existing state in the profile. | 84 // We don't want these fetches to affect existing state in the profile. |
| 99 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | | 85 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | |
| 100 net::LOAD_DO_NOT_SAVE_COOKIES); | 86 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 101 fetcher->set_request_context(Profile::GetDefaultRequestContext()); | 87 fetcher->set_request_context(g_browser_process->system_request_context()); |
| 102 fetcher->Start(); | 88 fetcher->Start(); |
| 103 fetchers_.insert(fetcher); | 89 fetchers_.insert(fetcher); |
| 104 } | 90 } |
| 105 } | 91 } |
| 106 | 92 |
| 107 void IntranetRedirectDetector::OnURLFetchComplete( | 93 void IntranetRedirectDetector::OnURLFetchComplete( |
| 108 const URLFetcher* source, | 94 const URLFetcher* source, |
| 109 const GURL& url, | 95 const GURL& url, |
| 110 const net::URLRequestStatus& status, | 96 const net::URLRequestStatus& status, |
| 111 int response_code, | 97 int response_code, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 DCHECK(resulting_origins_.size() == 2); | 137 DCHECK(resulting_origins_.size() == 2); |
| 152 redirect_origin_ = net::RegistryControlledDomainService::SameDomainOrHost( | 138 redirect_origin_ = net::RegistryControlledDomainService::SameDomainOrHost( |
| 153 resulting_origins_.back(), origin) ? origin : GURL(); | 139 resulting_origins_.back(), origin) ? origin : GURL(); |
| 154 } | 140 } |
| 155 | 141 |
| 156 g_browser_process->local_state()->SetString( | 142 g_browser_process->local_state()->SetString( |
| 157 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ? | 143 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ? |
| 158 redirect_origin_.spec() : std::string()); | 144 redirect_origin_.spec() : std::string()); |
| 159 } | 145 } |
| 160 | 146 |
| 161 void IntranetRedirectDetector::Observe(NotificationType type, | |
| 162 const NotificationSource& source, | |
| 163 const NotificationDetails& details) { | |
| 164 DCHECK_EQ(NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, type.value); | |
| 165 request_context_available_ = true; | |
| 166 StartFetchesIfPossible(); | |
| 167 } | |
| 168 | |
| 169 void IntranetRedirectDetector::OnIPAddressChanged() { | 147 void IntranetRedirectDetector::OnIPAddressChanged() { |
| 170 // If a request is already scheduled, do not scheduled yet another one. | 148 // If a request is already scheduled, do not scheduled yet another one. |
| 171 if (in_sleep_) | 149 if (in_sleep_) |
| 172 return; | 150 return; |
| 173 | 151 |
| 174 // Since presumably many programs open connections after network changes, | 152 // Since presumably many programs open connections after network changes, |
| 175 // delay this a little bit. | 153 // delay this a little bit. |
| 176 in_sleep_ = true; | 154 in_sleep_ = true; |
| 177 static const int kNetworkSwitchDelayMS = 1000; | 155 static const int kNetworkSwitchDelayMS = 1000; |
| 178 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 156 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 196 // the same thread. So just use the heuristic that any all-lowercase a-z | 174 // the same thread. So just use the heuristic that any all-lowercase a-z |
| 197 // hostname with the right number of characters is likely from the detector | 175 // hostname with the right number of characters is likely from the detector |
| 198 // (and thus should be blocked). | 176 // (and thus should be blocked). |
| 199 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && | 177 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && |
| 200 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == | 178 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == |
| 201 std::string::npos)) ? | 179 std::string::npos)) ? |
| 202 net::ERR_NAME_NOT_RESOLVED : | 180 net::ERR_NAME_NOT_RESOLVED : |
| 203 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, | 181 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, |
| 204 os_error); | 182 os_error); |
| 205 } | 183 } |
| OLD | NEW |