Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / intranet_redirect_detector.cc
blob6167c61a787abba4e83014f4b66a66fe274504c8
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/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/rand_util.h"
12 #include "base/stl_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_context_getter.h"
22 #include "net/url_request/url_request_status.h"
24 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10;
26 IntranetRedirectDetector::IntranetRedirectDetector()
27 : redirect_origin_(g_browser_process->local_state()->GetString(
28 prefs::kLastKnownIntranetRedirectOrigin)),
29 weak_factory_(this),
30 in_sleep_(true) {
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 kStartFetchDelaySeconds = 7;
38 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
39 base::Bind(&IntranetRedirectDetector::FinishSleep,
40 weak_factory_.GetWeakPtr()),
41 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds));
43 net::NetworkChangeNotifier::AddIPAddressObserver(this);
46 IntranetRedirectDetector::~IntranetRedirectDetector() {
47 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
48 STLDeleteElements(&fetchers_);
51 // static
52 GURL IntranetRedirectDetector::RedirectOrigin() {
53 const IntranetRedirectDetector* const detector =
54 g_browser_process->intranet_redirect_detector();
55 return detector ? detector->redirect_origin_ : GURL();
58 // static
59 void IntranetRedirectDetector::RegisterPrefs(PrefRegistrySimple* registry) {
60 registry->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin,
61 std::string());
64 void IntranetRedirectDetector::FinishSleep() {
65 in_sleep_ = false;
67 // If another fetch operation is still running, cancel it.
68 STLDeleteElements(&fetchers_);
69 resulting_origins_.clear();
71 // The detector is not needed in Chrome Frame since we have no omnibox there.
72 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
73 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking) ||
74 cmd_line->HasSwitch(switches::kChromeFrame))
75 return;
77 DCHECK(fetchers_.empty() && resulting_origins_.empty());
79 // Start three fetchers on random hostnames.
80 for (size_t i = 0; i < 3; ++i) {
81 std::string url_string("http://");
82 for (size_t j = 0; j < kNumCharsInHostnames; ++j)
83 url_string += ('a' + base::RandInt(0, 'z' - 'a'));
84 GURL random_url(url_string + '/');
85 net::URLFetcher* fetcher = net::URLFetcher::Create(
86 random_url, net::URLFetcher::HEAD, this);
87 // We don't want these fetches to affect existing state in the profile.
88 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE |
89 net::LOAD_DO_NOT_SAVE_COOKIES |
90 net::LOAD_DO_NOT_SEND_COOKIES);
91 fetcher->SetRequestContext(g_browser_process->system_request_context());
92 fetcher->Start();
93 fetchers_.insert(fetcher);
97 void IntranetRedirectDetector::OnURLFetchComplete(
98 const net::URLFetcher* source) {
99 // Delete the fetcher on this function's exit.
100 Fetchers::iterator fetcher = fetchers_.find(
101 const_cast<net::URLFetcher*>(source));
102 DCHECK(fetcher != fetchers_.end());
103 scoped_ptr<net::URLFetcher> clean_up_fetcher(*fetcher);
104 fetchers_.erase(fetcher);
106 // If any two fetches result in the same domain/host, we set the redirect
107 // origin to that; otherwise we set it to nothing.
108 if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) {
109 if ((resulting_origins_.empty()) ||
110 ((resulting_origins_.size() == 1) &&
111 resulting_origins_.front().is_valid())) {
112 resulting_origins_.push_back(GURL());
113 return;
115 redirect_origin_ = GURL();
116 } else {
117 DCHECK(source->GetURL().is_valid());
118 GURL origin(source->GetURL().GetOrigin());
119 if (resulting_origins_.empty()) {
120 resulting_origins_.push_back(origin);
121 return;
123 if (net::registry_controlled_domains::SameDomainOrHost(
124 resulting_origins_.front(),
125 origin,
126 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) {
127 redirect_origin_ = origin;
128 if (!fetchers_.empty()) {
129 // Cancel remaining fetch, we don't need it.
130 DCHECK(fetchers_.size() == 1);
131 delete (*fetchers_.begin());
132 fetchers_.clear();
135 if (resulting_origins_.size() == 1) {
136 resulting_origins_.push_back(origin);
137 return;
139 DCHECK(resulting_origins_.size() == 2);
140 const bool same_domain_or_host =
141 net::registry_controlled_domains::SameDomainOrHost(
142 resulting_origins_.back(),
143 origin,
144 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
145 redirect_origin_ = same_domain_or_host ? origin : GURL();
148 g_browser_process->local_state()->SetString(
149 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ?
150 redirect_origin_.spec() : std::string());
153 void IntranetRedirectDetector::OnIPAddressChanged() {
154 // If a request is already scheduled, do not scheduled yet another one.
155 if (in_sleep_)
156 return;
158 // Since presumably many programs open connections after network changes,
159 // delay this a little bit.
160 in_sleep_ = true;
161 static const int kNetworkSwitchDelayMS = 1000;
162 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
163 base::Bind(&IntranetRedirectDetector::FinishSleep,
164 weak_factory_.GetWeakPtr()),
165 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS));
168 IntranetRedirectHostResolverProc::IntranetRedirectHostResolverProc(
169 net::HostResolverProc* previous)
170 : net::HostResolverProc(previous) {
173 int IntranetRedirectHostResolverProc::Resolve(
174 const std::string& host,
175 net::AddressFamily address_family,
176 net::HostResolverFlags host_resolver_flags,
177 net::AddressList* addrlist,
178 int* os_error) {
179 // We'd love to just ask the IntranetRedirectDetector, but we may not be on
180 // the same thread. So just use the heuristic that any all-lowercase a-z
181 // hostname with the right number of characters is likely from the detector
182 // (and thus should be blocked).
183 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) &&
184 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") ==
185 std::string::npos)) ?
186 net::ERR_NAME_NOT_RESOLVED :
187 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist,
188 os_error);