| 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.h" | 9 #include "base/stl_util.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/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "content/common/net/url_fetcher.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/base/registry_controlled_domain.h" | 18 #include "net/base/registry_controlled_domain.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 20 | 21 |
| 21 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; | 22 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; |
| 22 | 23 |
| 23 IntranetRedirectDetector::IntranetRedirectDetector() | 24 IntranetRedirectDetector::IntranetRedirectDetector() |
| 24 : redirect_origin_(g_browser_process->local_state()->GetString( | 25 : redirect_origin_(g_browser_process->local_state()->GetString( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); | 83 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); |
| 83 // 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. |
| 84 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | | 85 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | |
| 85 net::LOAD_DO_NOT_SAVE_COOKIES); | 86 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 86 fetcher->set_request_context(g_browser_process->system_request_context()); | 87 fetcher->set_request_context(g_browser_process->system_request_context()); |
| 87 fetcher->Start(); | 88 fetcher->Start(); |
| 88 fetchers_.insert(fetcher); | 89 fetchers_.insert(fetcher); |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 92 void IntranetRedirectDetector::OnURLFetchComplete( | 93 void IntranetRedirectDetector::OnURLFetchComplete(const URLFetcher* source) { |
| 93 const URLFetcher* source, | |
| 94 const GURL& url, | |
| 95 const net::URLRequestStatus& status, | |
| 96 int response_code, | |
| 97 const net::ResponseCookies& cookies, | |
| 98 const std::string& data) { | |
| 99 // Delete the fetcher on this function's exit. | 94 // Delete the fetcher on this function's exit. |
| 100 Fetchers::iterator fetcher = fetchers_.find(const_cast<URLFetcher*>(source)); | 95 Fetchers::iterator fetcher = fetchers_.find(const_cast<URLFetcher*>(source)); |
| 101 DCHECK(fetcher != fetchers_.end()); | 96 DCHECK(fetcher != fetchers_.end()); |
| 102 scoped_ptr<URLFetcher> clean_up_fetcher(*fetcher); | 97 scoped_ptr<URLFetcher> clean_up_fetcher(*fetcher); |
| 103 fetchers_.erase(fetcher); | 98 fetchers_.erase(fetcher); |
| 104 | 99 |
| 105 // If any two fetches result in the same domain/host, we set the redirect | 100 // If any two fetches result in the same domain/host, we set the redirect |
| 106 // origin to that; otherwise we set it to nothing. | 101 // origin to that; otherwise we set it to nothing. |
| 107 if (!status.is_success() || (response_code != 200)) { | 102 if (!source->status().is_success() || (source->response_code() != 200)) { |
| 108 if ((resulting_origins_.empty()) || | 103 if ((resulting_origins_.empty()) || |
| 109 ((resulting_origins_.size() == 1) && | 104 ((resulting_origins_.size() == 1) && |
| 110 resulting_origins_.front().is_valid())) { | 105 resulting_origins_.front().is_valid())) { |
| 111 resulting_origins_.push_back(GURL()); | 106 resulting_origins_.push_back(GURL()); |
| 112 return; | 107 return; |
| 113 } | 108 } |
| 114 redirect_origin_ = GURL(); | 109 redirect_origin_ = GURL(); |
| 115 } else { | 110 } else { |
| 116 DCHECK(url.is_valid()); | 111 DCHECK(source->url().is_valid()); |
| 117 GURL origin(url.GetOrigin()); | 112 GURL origin(source->url().GetOrigin()); |
| 118 if (resulting_origins_.empty()) { | 113 if (resulting_origins_.empty()) { |
| 119 resulting_origins_.push_back(origin); | 114 resulting_origins_.push_back(origin); |
| 120 return; | 115 return; |
| 121 } | 116 } |
| 122 if (net::RegistryControlledDomainService::SameDomainOrHost( | 117 if (net::RegistryControlledDomainService::SameDomainOrHost( |
| 123 resulting_origins_.front(), origin)) { | 118 resulting_origins_.front(), origin)) { |
| 124 redirect_origin_ = origin; | 119 redirect_origin_ = origin; |
| 125 if (!fetchers_.empty()) { | 120 if (!fetchers_.empty()) { |
| 126 // Cancel remaining fetch, we don't need it. | 121 // Cancel remaining fetch, we don't need it. |
| 127 DCHECK(fetchers_.size() == 1); | 122 DCHECK(fetchers_.size() == 1); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // the same thread. So just use the heuristic that any all-lowercase a-z | 168 // the same thread. So just use the heuristic that any all-lowercase a-z |
| 174 // hostname with the right number of characters is likely from the detector | 169 // hostname with the right number of characters is likely from the detector |
| 175 // (and thus should be blocked). | 170 // (and thus should be blocked). |
| 176 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && | 171 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && |
| 177 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == | 172 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == |
| 178 std::string::npos)) ? | 173 std::string::npos)) ? |
| 179 net::ERR_NAME_NOT_RESOLVED : | 174 net::ERR_NAME_NOT_RESOLVED : |
| 180 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, | 175 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, |
| 181 os_error); | 176 os_error); |
| 182 } | 177 } |
| OLD | NEW |