Convert remoting_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / chrome / browser / intranet_redirect_detector.cc
blobb38977aee8d587897f037b7d9e4c1a3acf5d4ccd
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
3 // found in the LICENSE file.
5 #include "chrome/browser/intranet_redirect_detector.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/rand_util.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "net/base/load_flags.h"
21 #include "net/base/net_errors.h"
22 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
23 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "net/url_request/url_request_status.h"
27 IntranetRedirectDetector::IntranetRedirectDetector()
28 : redirect_origin_(g_browser_process->local_state()->GetString(
29 prefs::kLastKnownIntranetRedirectOrigin)),
30 in_sleep_(true),
31 weak_ptr_factory_(this) {
32 // Because this function can be called during startup, when kicking off a URL
33 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully
34 // long enough to be after startup, but still get results back quickly.
35 // Ideally, instead of this timer, we'd do something like "check if the
36 // browser is starting up, and if so, come back later", but there is currently
37 // no function to do this.
38 static const int kStartFetchDelaySeconds = 7;
39 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
40 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep,
41 weak_ptr_factory_.GetWeakPtr()),
42 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds));
44 net::NetworkChangeNotifier::AddIPAddressObserver(this);
47 IntranetRedirectDetector::~IntranetRedirectDetector() {
48 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
49 STLDeleteElements(&fetchers_);
52 // static
53 GURL IntranetRedirectDetector::RedirectOrigin() {
54 const IntranetRedirectDetector* const detector =
55 g_browser_process->intranet_redirect_detector();
56 return detector ? detector->redirect_origin_ : GURL();
59 // static
60 void IntranetRedirectDetector::RegisterPrefs(PrefRegistrySimple* registry) {
61 registry->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin,
62 std::string());
65 void IntranetRedirectDetector::FinishSleep() {
66 in_sleep_ = false;
68 // If another fetch operation is still running, cancel it.
69 STLDeleteElements(&fetchers_);
70 resulting_origins_.clear();
72 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
73 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking))
74 return;
76 DCHECK(fetchers_.empty() && resulting_origins_.empty());
78 // Start three fetchers on random hostnames.
79 for (size_t i = 0; i < 3; ++i) {
80 std::string url_string("http://");
81 // We generate a random hostname with between 7 and 15 characters.
82 const int num_chars = base::RandInt(7, 15);
83 for (int j = 0; j < num_chars; ++j)
84 url_string += ('a' + base::RandInt(0, 'z' - 'a'));
85 GURL random_url(url_string + '/');
86 net::URLFetcher* fetcher =
87 net::URLFetcher::Create(random_url, net::URLFetcher::HEAD, this)
88 .release();
89 // We don't want these fetches to affect existing state in the profile.
90 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE |
91 net::LOAD_DO_NOT_SAVE_COOKIES |
92 net::LOAD_DO_NOT_SEND_COOKIES);
93 fetcher->SetRequestContext(g_browser_process->system_request_context());
94 fetcher->Start();
95 fetchers_.insert(fetcher);
99 void IntranetRedirectDetector::OnURLFetchComplete(
100 const net::URLFetcher* source) {
101 // Delete the fetcher on this function's exit.
102 Fetchers::iterator fetcher = fetchers_.find(
103 const_cast<net::URLFetcher*>(source));
104 DCHECK(fetcher != fetchers_.end());
105 scoped_ptr<net::URLFetcher> clean_up_fetcher(*fetcher);
106 fetchers_.erase(fetcher);
108 // If any two fetches result in the same domain/host, we set the redirect
109 // origin to that; otherwise we set it to nothing.
110 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) {
111 if ((resulting_origins_.empty()) ||
112 ((resulting_origins_.size() == 1) &&
113 resulting_origins_.front().is_valid())) {
114 resulting_origins_.push_back(GURL());
115 return;
117 redirect_origin_ = GURL();
118 } else {
119 DCHECK(source->GetURL().is_valid());
120 GURL origin(source->GetURL().GetOrigin());
121 if (resulting_origins_.empty()) {
122 resulting_origins_.push_back(origin);
123 return;
125 if (net::registry_controlled_domains::SameDomainOrHost(
126 resulting_origins_.front(),
127 origin,
128 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) {
129 redirect_origin_ = origin;
130 if (!fetchers_.empty()) {
131 // Cancel remaining fetch, we don't need it.
132 DCHECK(fetchers_.size() == 1);
133 delete (*fetchers_.begin());
134 fetchers_.clear();
137 if (resulting_origins_.size() == 1) {
138 resulting_origins_.push_back(origin);
139 return;
141 DCHECK(resulting_origins_.size() == 2);
142 const bool same_domain_or_host =
143 net::registry_controlled_domains::SameDomainOrHost(
144 resulting_origins_.back(),
145 origin,
146 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
147 redirect_origin_ = same_domain_or_host ? origin : GURL();
150 g_browser_process->local_state()->SetString(
151 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ?
152 redirect_origin_.spec() : std::string());
155 void IntranetRedirectDetector::OnIPAddressChanged() {
156 // If a request is already scheduled, do not scheduled yet another one.
157 if (in_sleep_)
158 return;
160 // Since presumably many programs open connections after network changes,
161 // delay this a little bit.
162 in_sleep_ = true;
163 static const int kNetworkSwitchDelayMS = 1000;
164 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
165 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep,
166 weak_ptr_factory_.GetWeakPtr()),
167 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS));