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"
8 #include "base/command_line.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/profiler/scoped_profile.h"
12 #include "base/rand_util.h"
13 #include "base/stl_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "net/base/load_flags.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
21 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "net/url_request/url_request_status.h"
25 IntranetRedirectDetector::IntranetRedirectDetector()
26 : redirect_origin_(g_browser_process
->local_state()->GetString(
27 prefs::kLastKnownIntranetRedirectOrigin
)),
29 weak_ptr_factory_(this) {
30 // Because this function can be called during startup, when kicking off a URL
31 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully
32 // long enough to be after startup, but still get results back quickly.
33 // Ideally, instead of this timer, we'd do something like "check if the
34 // browser is starting up, and if so, come back later", but there is currently
35 // no function to do this.
36 static const int kStartFetchDelaySeconds
= 7;
37 base::MessageLoop::current()->PostDelayedTask(FROM_HERE
,
38 base::Bind(&IntranetRedirectDetector::FinishSleep
,
39 weak_ptr_factory_
.GetWeakPtr()),
40 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds
));
42 net::NetworkChangeNotifier::AddIPAddressObserver(this);
45 IntranetRedirectDetector::~IntranetRedirectDetector() {
46 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
47 STLDeleteElements(&fetchers_
);
51 GURL
IntranetRedirectDetector::RedirectOrigin() {
52 const IntranetRedirectDetector
* const detector
=
53 g_browser_process
->intranet_redirect_detector();
54 return detector
? detector
->redirect_origin_
: GURL();
58 void IntranetRedirectDetector::RegisterPrefs(PrefRegistrySimple
* registry
) {
59 registry
->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin
,
63 void IntranetRedirectDetector::FinishSleep() {
66 // If another fetch operation is still running, cancel it.
67 STLDeleteElements(&fetchers_
);
68 resulting_origins_
.clear();
70 const CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
71 if (cmd_line
->HasSwitch(switches::kDisableBackgroundNetworking
))
74 DCHECK(fetchers_
.empty() && resulting_origins_
.empty());
76 // Start three fetchers on random hostnames.
77 for (size_t i
= 0; i
< 3; ++i
) {
78 std::string
url_string("http://");
79 // We generate a random hostname with between 7 and 15 characters.
80 const int num_chars
= base::RandInt(7, 15);
81 for (int j
= 0; j
< num_chars
; ++j
)
82 url_string
+= ('a' + base::RandInt(0, 'z' - 'a'));
83 GURL
random_url(url_string
+ '/');
84 net::URLFetcher
* fetcher
= net::URLFetcher::Create(
85 random_url
, net::URLFetcher::HEAD
, this);
86 // We don't want these fetches to affect existing state in the profile.
87 fetcher
->SetLoadFlags(net::LOAD_DISABLE_CACHE
|
88 net::LOAD_DO_NOT_SAVE_COOKIES
|
89 net::LOAD_DO_NOT_SEND_COOKIES
);
90 fetcher
->SetRequestContext(g_browser_process
->system_request_context());
92 fetchers_
.insert(fetcher
);
96 void IntranetRedirectDetector::OnURLFetchComplete(
97 const net::URLFetcher
* source
) {
98 // TODO(vadimt): Remove ScopedProfile below once crbug.com/422577 is fixed.
99 tracked_objects::ScopedProfile
tracking_profile(
100 FROM_HERE_WITH_EXPLICIT_FUNCTION(
101 "422577 IntranetRedirectDetector::OnURLFetchComplete"));
103 // Delete the fetcher on this function's exit.
104 Fetchers::iterator fetcher
= fetchers_
.find(
105 const_cast<net::URLFetcher
*>(source
));
106 DCHECK(fetcher
!= fetchers_
.end());
107 scoped_ptr
<net::URLFetcher
> clean_up_fetcher(*fetcher
);
108 fetchers_
.erase(fetcher
);
110 // If any two fetches result in the same domain/host, we set the redirect
111 // origin to that; otherwise we set it to nothing.
112 if (!source
->GetStatus().is_success() || (source
->GetResponseCode() != 200)) {
113 if ((resulting_origins_
.empty()) ||
114 ((resulting_origins_
.size() == 1) &&
115 resulting_origins_
.front().is_valid())) {
116 resulting_origins_
.push_back(GURL());
119 redirect_origin_
= GURL();
121 DCHECK(source
->GetURL().is_valid());
122 GURL
origin(source
->GetURL().GetOrigin());
123 if (resulting_origins_
.empty()) {
124 resulting_origins_
.push_back(origin
);
127 if (net::registry_controlled_domains::SameDomainOrHost(
128 resulting_origins_
.front(),
130 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
)) {
131 redirect_origin_
= origin
;
132 if (!fetchers_
.empty()) {
133 // Cancel remaining fetch, we don't need it.
134 DCHECK(fetchers_
.size() == 1);
135 delete (*fetchers_
.begin());
139 if (resulting_origins_
.size() == 1) {
140 resulting_origins_
.push_back(origin
);
143 DCHECK(resulting_origins_
.size() == 2);
144 const bool same_domain_or_host
=
145 net::registry_controlled_domains::SameDomainOrHost(
146 resulting_origins_
.back(),
148 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
149 redirect_origin_
= same_domain_or_host
? origin
: GURL();
152 g_browser_process
->local_state()->SetString(
153 prefs::kLastKnownIntranetRedirectOrigin
, redirect_origin_
.is_valid() ?
154 redirect_origin_
.spec() : std::string());
157 void IntranetRedirectDetector::OnIPAddressChanged() {
158 // If a request is already scheduled, do not scheduled yet another one.
162 // Since presumably many programs open connections after network changes,
163 // delay this a little bit.
165 static const int kNetworkSwitchDelayMS
= 1000;
166 base::MessageLoop::current()->PostDelayedTask(FROM_HERE
,
167 base::Bind(&IntranetRedirectDetector::FinishSleep
,
168 weak_ptr_factory_
.GetWeakPtr()),
169 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS
));