| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 41 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 42 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep, | 42 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep, |
| 43 weak_ptr_factory_.GetWeakPtr()), | 43 weak_ptr_factory_.GetWeakPtr()), |
| 44 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); | 44 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); |
| 45 | 45 |
| 46 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 46 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 47 } | 47 } |
| 48 | 48 |
| 49 IntranetRedirectDetector::~IntranetRedirectDetector() { | 49 IntranetRedirectDetector::~IntranetRedirectDetector() { |
| 50 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 50 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 51 base::STLDeleteElements(&fetchers_); | |
| 52 } | 51 } |
| 53 | 52 |
| 54 // static | 53 // static |
| 55 GURL IntranetRedirectDetector::RedirectOrigin() { | 54 GURL IntranetRedirectDetector::RedirectOrigin() { |
| 56 const IntranetRedirectDetector* const detector = | 55 const IntranetRedirectDetector* const detector = |
| 57 g_browser_process->intranet_redirect_detector(); | 56 g_browser_process->intranet_redirect_detector(); |
| 58 return detector ? detector->redirect_origin_ : GURL(); | 57 return detector ? detector->redirect_origin_ : GURL(); |
| 59 } | 58 } |
| 60 | 59 |
| 61 // static | 60 // static |
| 62 void IntranetRedirectDetector::RegisterPrefs(PrefRegistrySimple* registry) { | 61 void IntranetRedirectDetector::RegisterPrefs(PrefRegistrySimple* registry) { |
| 63 registry->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin, | 62 registry->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin, |
| 64 std::string()); | 63 std::string()); |
| 65 } | 64 } |
| 66 | 65 |
| 67 void IntranetRedirectDetector::FinishSleep() { | 66 void IntranetRedirectDetector::FinishSleep() { |
| 68 in_sleep_ = false; | 67 in_sleep_ = false; |
| 69 | 68 |
| 70 // If another fetch operation is still running, cancel it. | 69 // If another fetch operation is still running, cancel it. |
| 71 base::STLDeleteElements(&fetchers_); | 70 fetchers_.clear(); |
| 72 resulting_origins_.clear(); | 71 resulting_origins_.clear(); |
| 73 | 72 |
| 74 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 73 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 75 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking)) | 74 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking)) |
| 76 return; | 75 return; |
| 77 | 76 |
| 78 DCHECK(fetchers_.empty() && resulting_origins_.empty()); | 77 DCHECK(fetchers_.empty() && resulting_origins_.empty()); |
| 79 | 78 |
| 80 // Start three fetchers on random hostnames. | 79 // Start three fetchers on random hostnames. |
| 81 for (size_t i = 0; i < 3; ++i) { | 80 for (size_t i = 0; i < 3; ++i) { |
| 82 std::string url_string("http://"); | 81 std::string url_string("http://"); |
| 83 // We generate a random hostname with between 7 and 15 characters. | 82 // We generate a random hostname with between 7 and 15 characters. |
| 84 const int num_chars = base::RandInt(7, 15); | 83 const int num_chars = base::RandInt(7, 15); |
| 85 for (int j = 0; j < num_chars; ++j) | 84 for (int j = 0; j < num_chars; ++j) |
| 86 url_string += ('a' + base::RandInt(0, 'z' - 'a')); | 85 url_string += ('a' + base::RandInt(0, 'z' - 'a')); |
| 87 GURL random_url(url_string + '/'); | 86 GURL random_url(url_string + '/'); |
| 88 net::URLFetcher* fetcher = | 87 std::unique_ptr<net::URLFetcher> fetcher = |
| 89 net::URLFetcher::Create(random_url, net::URLFetcher::HEAD, this) | 88 net::URLFetcher::Create(random_url, net::URLFetcher::HEAD, this); |
| 90 .release(); | |
| 91 // We don't want these fetches to affect existing state in the profile. | 89 // We don't want these fetches to affect existing state in the profile. |
| 92 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 90 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| 93 net::LOAD_DO_NOT_SAVE_COOKIES | | 91 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 94 net::LOAD_DO_NOT_SEND_COOKIES | | 92 net::LOAD_DO_NOT_SEND_COOKIES | |
| 95 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 93 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 96 fetcher->SetRequestContext(g_browser_process->system_request_context()); | 94 fetcher->SetRequestContext(g_browser_process->system_request_context()); |
| 97 fetcher->Start(); | 95 fetcher->Start(); |
| 98 fetchers_.insert(fetcher); | 96 net::URLFetcher* fetcher_ptr = fetcher.get(); |
| 97 fetchers_[fetcher_ptr] = std::move(fetcher); |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 | 100 |
| 102 void IntranetRedirectDetector::OnURLFetchComplete( | 101 void IntranetRedirectDetector::OnURLFetchComplete( |
| 103 const net::URLFetcher* source) { | 102 const net::URLFetcher* source) { |
| 104 // Delete the fetcher on this function's exit. | 103 // Delete the fetcher on this function's exit. |
| 105 Fetchers::iterator fetcher = fetchers_.find( | 104 auto it = fetchers_.find(const_cast<net::URLFetcher*>(source)); |
| 106 const_cast<net::URLFetcher*>(source)); | 105 DCHECK(it != fetchers_.end()); |
| 107 DCHECK(fetcher != fetchers_.end()); | 106 std::unique_ptr<net::URLFetcher> fetcher = std::move(it->second); |
| 108 std::unique_ptr<net::URLFetcher> clean_up_fetcher(*fetcher); | 107 fetchers_.erase(it); |
| 109 fetchers_.erase(fetcher); | |
| 110 | 108 |
| 111 // If any two fetches result in the same domain/host, we set the redirect | 109 // If any two fetches result in the same domain/host, we set the redirect |
| 112 // origin to that; otherwise we set it to nothing. | 110 // origin to that; otherwise we set it to nothing. |
| 113 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) { | 111 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) { |
| 114 if ((resulting_origins_.empty()) || | 112 if ((resulting_origins_.empty()) || |
| 115 ((resulting_origins_.size() == 1) && | 113 ((resulting_origins_.size() == 1) && |
| 116 resulting_origins_.front().is_valid())) { | 114 resulting_origins_.front().is_valid())) { |
| 117 resulting_origins_.push_back(GURL()); | 115 resulting_origins_.push_back(GURL()); |
| 118 return; | 116 return; |
| 119 } | 117 } |
| 120 redirect_origin_ = GURL(); | 118 redirect_origin_ = GURL(); |
| 121 } else { | 119 } else { |
| 122 DCHECK(source->GetURL().is_valid()); | 120 DCHECK(source->GetURL().is_valid()); |
| 123 GURL origin(source->GetURL().GetOrigin()); | 121 GURL origin(source->GetURL().GetOrigin()); |
| 124 if (resulting_origins_.empty()) { | 122 if (resulting_origins_.empty()) { |
| 125 resulting_origins_.push_back(origin); | 123 resulting_origins_.push_back(origin); |
| 126 return; | 124 return; |
| 127 } | 125 } |
| 128 if (net::registry_controlled_domains::SameDomainOrHost( | 126 if (net::registry_controlled_domains::SameDomainOrHost( |
| 129 resulting_origins_.front(), | 127 resulting_origins_.front(), |
| 130 origin, | 128 origin, |
| 131 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) { | 129 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) { |
| 132 redirect_origin_ = origin; | 130 redirect_origin_ = origin; |
| 133 if (!fetchers_.empty()) { | 131 if (!fetchers_.empty()) { |
| 134 // Cancel remaining fetch, we don't need it. | 132 // Cancel remaining fetch, we don't need it. |
| 135 DCHECK(fetchers_.size() == 1); | 133 DCHECK(fetchers_.size() == 1); |
| 136 delete (*fetchers_.begin()); | |
| 137 fetchers_.clear(); | 134 fetchers_.clear(); |
| 138 } | 135 } |
| 139 } | 136 } |
| 140 if (resulting_origins_.size() == 1) { | 137 if (resulting_origins_.size() == 1) { |
| 141 resulting_origins_.push_back(origin); | 138 resulting_origins_.push_back(origin); |
| 142 return; | 139 return; |
| 143 } | 140 } |
| 144 DCHECK(resulting_origins_.size() == 2); | 141 DCHECK(resulting_origins_.size() == 2); |
| 145 const bool same_domain_or_host = | 142 const bool same_domain_or_host = |
| 146 net::registry_controlled_domains::SameDomainOrHost( | 143 net::registry_controlled_domains::SameDomainOrHost( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 162 | 159 |
| 163 // Since presumably many programs open connections after network changes, | 160 // Since presumably many programs open connections after network changes, |
| 164 // delay this a little bit. | 161 // delay this a little bit. |
| 165 in_sleep_ = true; | 162 in_sleep_ = true; |
| 166 static const int kNetworkSwitchDelayMS = 1000; | 163 static const int kNetworkSwitchDelayMS = 1000; |
| 167 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 164 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 168 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep, | 165 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep, |
| 169 weak_ptr_factory_.GetWeakPtr()), | 166 weak_ptr_factory_.GetWeakPtr()), |
| 170 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); | 167 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); |
| 171 } | 168 } |
| OLD | NEW |