| 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" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/net/url_request_context_getter.h" |
| 15 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
| 18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 19 #include "net/base/registry_controlled_domain.h" | 20 #include "net/base/registry_controlled_domain.h" |
| 20 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
| 21 | 22 |
| 22 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; | 23 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; |
| 23 | 24 |
| 24 IntranetRedirectDetector::IntranetRedirectDetector() | 25 IntranetRedirectDetector::IntranetRedirectDetector() |
| 25 : redirect_origin_(g_browser_process->local_state()->GetString( | 26 : redirect_origin_(g_browser_process->local_state()->GetString( |
| 26 prefs::kLastKnownIntranetRedirectOrigin)), | 27 prefs::kLastKnownIntranetRedirectOrigin)), |
| 27 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), | 28 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), |
| 28 in_sleep_(true), | 29 in_sleep_(true), |
| 29 request_context_available_(Profile::GetDefaultRequestContext() != NULL) { | 30 request_context_available_( |
| 31 g_browser_process->system_request_context() != NULL) { |
| 30 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, | 32 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, |
| 31 NotificationService::AllSources()); | 33 NotificationService::AllSources()); |
| 32 | 34 |
| 33 // Because this function can be called during startup, when kicking off a URL | 35 // 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 | 36 // 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. | 37 // 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 | 38 // 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 | 39 // browser is starting up, and if so, come back later", but there is currently |
| 38 // no function to do this. | 40 // no function to do this. |
| 39 static const int kStartFetchDelayMS = 7000; | 41 static const int kStartFetchDelayMS = 7000; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // Start three fetchers on random hostnames. | 93 // Start three fetchers on random hostnames. |
| 92 for (size_t i = 0; i < 3; ++i) { | 94 for (size_t i = 0; i < 3; ++i) { |
| 93 std::string url_string("http://"); | 95 std::string url_string("http://"); |
| 94 for (size_t j = 0; j < kNumCharsInHostnames; ++j) | 96 for (size_t j = 0; j < kNumCharsInHostnames; ++j) |
| 95 url_string += ('a' + base::RandInt(0, 'z' - 'a')); | 97 url_string += ('a' + base::RandInt(0, 'z' - 'a')); |
| 96 GURL random_url(url_string + '/'); | 98 GURL random_url(url_string + '/'); |
| 97 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); | 99 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); |
| 98 // We don't want these fetches to affect existing state in the profile. | 100 // We don't want these fetches to affect existing state in the profile. |
| 99 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | | 101 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | |
| 100 net::LOAD_DO_NOT_SAVE_COOKIES); | 102 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 101 fetcher->set_request_context(Profile::GetDefaultRequestContext()); | 103 fetcher->set_request_context(g_browser_process->system_request_context()); |
| 102 fetcher->Start(); | 104 fetcher->Start(); |
| 103 fetchers_.insert(fetcher); | 105 fetchers_.insert(fetcher); |
| 104 } | 106 } |
| 105 } | 107 } |
| 106 | 108 |
| 107 void IntranetRedirectDetector::OnURLFetchComplete( | 109 void IntranetRedirectDetector::OnURLFetchComplete( |
| 108 const URLFetcher* source, | 110 const URLFetcher* source, |
| 109 const GURL& url, | 111 const GURL& url, |
| 110 const net::URLRequestStatus& status, | 112 const net::URLRequestStatus& status, |
| 111 int response_code, | 113 int response_code, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 // the same thread. So just use the heuristic that any all-lowercase a-z | 198 // 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 | 199 // hostname with the right number of characters is likely from the detector |
| 198 // (and thus should be blocked). | 200 // (and thus should be blocked). |
| 199 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && | 201 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && |
| 200 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == | 202 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == |
| 201 std::string::npos)) ? | 203 std::string::npos)) ? |
| 202 net::ERR_NAME_NOT_RESOLVED : | 204 net::ERR_NAME_NOT_RESOLVED : |
| 203 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, | 205 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, |
| 204 os_error); | 206 os_error); |
| 205 } | 207 } |
| OLD | NEW |