OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/intranet_redirect_detector.h" |
| 6 |
| 7 #include "base/rand_util.h" |
| 8 #include "base/stl_util-inl.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/profile.h" |
| 12 #include "chrome/common/notification_service.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/common/pref_service.h" |
| 15 #include "net/base/load_flags.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/registry_controlled_domain.h" |
| 18 #include "net/url_request/url_request_status.h" |
| 19 |
| 20 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; |
| 21 |
| 22 IntranetRedirectDetector::IntranetRedirectDetector() |
| 23 : redirect_origin_(WideToUTF8(g_browser_process->local_state()->GetString( |
| 24 prefs::kLastKnownIntranetRedirectOrigin))), |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), |
| 26 in_startup_sleep_(true), |
| 27 request_context_available_(!!Profile::GetDefaultRequestContext()) { |
| 28 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, |
| 29 NotificationService::AllSources()); |
| 30 |
| 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 |
| 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 |
| 35 // browser is starting up, and if so, come back later", but there is currently |
| 36 // no function to do this. |
| 37 static const int kStartFetchDelayMS = 7000; |
| 38 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 39 fetcher_factory_.NewRunnableMethod( |
| 40 &IntranetRedirectDetector::FinishSleep), |
| 41 kStartFetchDelayMS); |
| 42 } |
| 43 |
| 44 IntranetRedirectDetector::~IntranetRedirectDetector() { |
| 45 STLDeleteElements(&fetchers_); |
| 46 } |
| 47 |
| 48 // static |
| 49 GURL IntranetRedirectDetector::RedirectOrigin() { |
| 50 const IntranetRedirectDetector* const detector = |
| 51 g_browser_process->intranet_redirect_detector(); |
| 52 return detector ? detector->redirect_origin_ : GURL(); |
| 53 } |
| 54 |
| 55 // static |
| 56 void IntranetRedirectDetector::RegisterPrefs(PrefService* prefs) { |
| 57 prefs->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin, |
| 58 std::wstring()); |
| 59 } |
| 60 |
| 61 void IntranetRedirectDetector::FinishSleep() { |
| 62 in_startup_sleep_ = false; |
| 63 StartFetchesIfPossible(); |
| 64 } |
| 65 |
| 66 void IntranetRedirectDetector::StartFetchesIfPossible() { |
| 67 // Bail if a fetch isn't appropriate right now. This function will be called |
| 68 // again each time one of the preconditions changes, so we'll fetch |
| 69 // immediately once all of them are met. |
| 70 if (in_startup_sleep_ || !request_context_available_) |
| 71 return; |
| 72 |
| 73 // We shouldn't somehow run twice. |
| 74 DCHECK(fetchers_.empty() && resulting_origins_.empty()); |
| 75 |
| 76 // Start three fetchers on random hostnames. |
| 77 for (size_t i = 0; i < 3; ++i) { |
| 78 std::string url_string("http://"); |
| 79 for (size_t j = 0; j < kNumCharsInHostnames; ++j) |
| 80 url_string += ('a' + base::RandInt(0, 'z' - 'a')); |
| 81 GURL random_url(url_string + '/'); |
| 82 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); |
| 83 // We don't want these fetches to affect existing state in the profile. |
| 84 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | |
| 85 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 86 fetcher->set_request_context(Profile::GetDefaultRequestContext()); |
| 87 fetcher->Start(); |
| 88 fetchers_.insert(fetcher); |
| 89 } |
| 90 } |
| 91 |
| 92 void IntranetRedirectDetector::OnURLFetchComplete( |
| 93 const URLFetcher* source, |
| 94 const GURL& url, |
| 95 const URLRequestStatus& status, |
| 96 int response_code, |
| 97 const ResponseCookies& cookies, |
| 98 const std::string& data) { |
| 99 // Delete the fetcher on this function's exit. |
| 100 Fetchers::iterator fetcher = fetchers_.find(const_cast<URLFetcher*>(source)); |
| 101 DCHECK(fetcher != fetchers_.end()); |
| 102 scoped_ptr<URLFetcher> clean_up_fetcher(*fetcher); |
| 103 fetchers_.erase(fetcher); |
| 104 |
| 105 // If any two fetches result in the same domain/host, we set the redirect |
| 106 // origin to that; otherwise we set it to nothing. |
| 107 if (!status.is_success() || (response_code != 200)) { |
| 108 if ((resulting_origins_.empty()) || |
| 109 ((resulting_origins_.size() == 1) && |
| 110 resulting_origins_.front().is_valid())) { |
| 111 resulting_origins_.push_back(GURL()); |
| 112 return; |
| 113 } |
| 114 redirect_origin_ = GURL(); |
| 115 } else { |
| 116 DCHECK(url.is_valid()); |
| 117 GURL origin(url.GetOrigin()); |
| 118 if (resulting_origins_.empty()) { |
| 119 resulting_origins_.push_back(origin); |
| 120 return; |
| 121 } |
| 122 if (net::RegistryControlledDomainService::SameDomainOrHost( |
| 123 resulting_origins_.front(), origin)) { |
| 124 redirect_origin_ = origin; |
| 125 if (!fetchers_.empty()) { |
| 126 // Cancel remaining fetch, we don't need it. |
| 127 DCHECK(fetchers_.size() == 1); |
| 128 delete (*fetchers_.begin()); |
| 129 fetchers_.clear(); |
| 130 } |
| 131 } |
| 132 if (resulting_origins_.size() == 1) { |
| 133 resulting_origins_.push_back(origin); |
| 134 return; |
| 135 } |
| 136 DCHECK(resulting_origins_.size() == 2); |
| 137 redirect_origin_ = net::RegistryControlledDomainService::SameDomainOrHost( |
| 138 resulting_origins_.back(), origin) ? origin : GURL(); |
| 139 } |
| 140 |
| 141 g_browser_process->local_state()->SetString( |
| 142 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ? |
| 143 UTF8ToWide(redirect_origin_.spec()) : std::wstring()); |
| 144 } |
| 145 |
| 146 void IntranetRedirectDetector::Observe(NotificationType type, |
| 147 const NotificationSource& source, |
| 148 const NotificationDetails& details) { |
| 149 DCHECK_EQ(NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, type.value); |
| 150 request_context_available_ = true; |
| 151 StartFetchesIfPossible(); |
| 152 } |
| 153 |
| 154 IntranetRedirectHostResolverProc::IntranetRedirectHostResolverProc( |
| 155 net::HostResolverProc* previous) |
| 156 : net::HostResolverProc(previous) { |
| 157 } |
| 158 |
| 159 int IntranetRedirectHostResolverProc::Resolve(const std::string& host, |
| 160 net::AddressFamily address_family, |
| 161 net::AddressList* addrlist) { |
| 162 // We'd love to just ask the IntranetRedirectDetector, but we may not be on |
| 163 // the same thread. So just use the heuristic that any all-lowercase a-z |
| 164 // hostname with the right number of characters is likely from the detector |
| 165 // (and thus should be blocked). |
| 166 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && |
| 167 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == |
| 168 std::string::npos)) ? |
| 169 net::ERR_NAME_NOT_RESOLVED : |
| 170 ResolveUsingPrevious(host, address_family, addrlist); |
| 171 } |
OLD | NEW |