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/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 IntranetRedirectDetector::IntranetRedirectDetector()
25 : redirect_origin_(g_browser_process
->local_state()->GetString(
26 prefs::kLastKnownIntranetRedirectOrigin
)),
28 weak_ptr_factory_(this) {
29 // Because this function can be called during startup, when kicking off a URL
30 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully
31 // long enough to be after startup, but still get results back quickly.
32 // Ideally, instead of this timer, we'd do something like "check if the
33 // browser is starting up, and if so, come back later", but there is currently
34 // no function to do this.
35 static const int kStartFetchDelaySeconds
= 7;
36 base::MessageLoop::current()->PostDelayedTask(FROM_HERE
,
37 base::Bind(&IntranetRedirectDetector::FinishSleep
,
38 weak_ptr_factory_
.GetWeakPtr()),
39 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds
));
41 net::NetworkChangeNotifier::AddIPAddressObserver(this);
44 IntranetRedirectDetector::~IntranetRedirectDetector() {
45 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
46 STLDeleteElements(&fetchers_
);
50 GURL
IntranetRedirectDetector::RedirectOrigin() {
51 const IntranetRedirectDetector
* const detector
=
52 g_browser_process
->intranet_redirect_detector();
53 return detector
? detector
->redirect_origin_
: GURL();
57 void IntranetRedirectDetector::RegisterPrefs(PrefRegistrySimple
* registry
) {
58 registry
->RegisterStringPref(prefs::kLastKnownIntranetRedirectOrigin
,
62 void IntranetRedirectDetector::FinishSleep() {
65 // If another fetch operation is still running, cancel it.
66 STLDeleteElements(&fetchers_
);
67 resulting_origins_
.clear();
69 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
70 if (cmd_line
->HasSwitch(switches::kDisableBackgroundNetworking
))
73 DCHECK(fetchers_
.empty() && resulting_origins_
.empty());
75 // Start three fetchers on random hostnames.
76 for (size_t i
= 0; i
< 3; ++i
) {
77 std::string
url_string("http://");
78 // We generate a random hostname with between 7 and 15 characters.
79 const int num_chars
= base::RandInt(7, 15);
80 for (int j
= 0; j
< num_chars
; ++j
)
81 url_string
+= ('a' + base::RandInt(0, 'z' - 'a'));
82 GURL
random_url(url_string
+ '/');
83 net::URLFetcher
* fetcher
=
84 net::URLFetcher::Create(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 // Delete the fetcher on this function's exit.
99 Fetchers::iterator fetcher
= fetchers_
.find(
100 const_cast<net::URLFetcher
*>(source
));
101 DCHECK(fetcher
!= fetchers_
.end());
102 scoped_ptr
<net::URLFetcher
> clean_up_fetcher(*fetcher
);
103 fetchers_
.erase(fetcher
);
105 // If any two fetches result in the same domain/host, we set the redirect
106 // origin to that; otherwise we set it to nothing.
107 if (!source
->GetStatus().is_success() || (source
->GetResponseCode() != 200)) {
108 if ((resulting_origins_
.empty()) ||
109 ((resulting_origins_
.size() == 1) &&
110 resulting_origins_
.front().is_valid())) {
111 resulting_origins_
.push_back(GURL());
114 redirect_origin_
= GURL();
116 DCHECK(source
->GetURL().is_valid());
117 GURL
origin(source
->GetURL().GetOrigin());
118 if (resulting_origins_
.empty()) {
119 resulting_origins_
.push_back(origin
);
122 if (net::registry_controlled_domains::SameDomainOrHost(
123 resulting_origins_
.front(),
125 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
)) {
126 redirect_origin_
= origin
;
127 if (!fetchers_
.empty()) {
128 // Cancel remaining fetch, we don't need it.
129 DCHECK(fetchers_
.size() == 1);
130 delete (*fetchers_
.begin());
134 if (resulting_origins_
.size() == 1) {
135 resulting_origins_
.push_back(origin
);
138 DCHECK(resulting_origins_
.size() == 2);
139 const bool same_domain_or_host
=
140 net::registry_controlled_domains::SameDomainOrHost(
141 resulting_origins_
.back(),
143 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
144 redirect_origin_
= same_domain_or_host
? origin
: GURL();
147 g_browser_process
->local_state()->SetString(
148 prefs::kLastKnownIntranetRedirectOrigin
, redirect_origin_
.is_valid() ?
149 redirect_origin_
.spec() : std::string());
152 void IntranetRedirectDetector::OnIPAddressChanged() {
153 // If a request is already scheduled, do not scheduled yet another one.
157 // Since presumably many programs open connections after network changes,
158 // delay this a little bit.
160 static const int kNetworkSwitchDelayMS
= 1000;
161 base::MessageLoop::current()->PostDelayedTask(FROM_HERE
,
162 base::Bind(&IntranetRedirectDetector::FinishSleep
,
163 weak_ptr_factory_
.GetWeakPtr()),
164 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS
));