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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/location.h" |
9 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/single_thread_task_runner.h" |
12 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/thread_task_runner_handle.h" |
14 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
15 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
17 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
18 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 22 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
20 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
21 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
22 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
23 | 26 |
24 IntranetRedirectDetector::IntranetRedirectDetector() | 27 IntranetRedirectDetector::IntranetRedirectDetector() |
25 : redirect_origin_(g_browser_process->local_state()->GetString( | 28 : redirect_origin_(g_browser_process->local_state()->GetString( |
26 prefs::kLastKnownIntranetRedirectOrigin)), | 29 prefs::kLastKnownIntranetRedirectOrigin)), |
27 in_sleep_(true), | 30 in_sleep_(true), |
28 weak_ptr_factory_(this) { | 31 weak_ptr_factory_(this) { |
29 // Because this function can be called during startup, when kicking off a URL | 32 // 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 | 33 // 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. | 34 // 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 | 35 // 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 | 36 // browser is starting up, and if so, come back later", but there is currently |
34 // no function to do this. | 37 // no function to do this. |
35 static const int kStartFetchDelaySeconds = 7; | 38 static const int kStartFetchDelaySeconds = 7; |
36 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 39 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
37 base::Bind(&IntranetRedirectDetector::FinishSleep, | 40 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep, |
38 weak_ptr_factory_.GetWeakPtr()), | 41 weak_ptr_factory_.GetWeakPtr()), |
39 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); | 42 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); |
40 | 43 |
41 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 44 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
42 } | 45 } |
43 | 46 |
44 IntranetRedirectDetector::~IntranetRedirectDetector() { | 47 IntranetRedirectDetector::~IntranetRedirectDetector() { |
45 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 48 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
46 STLDeleteElements(&fetchers_); | 49 STLDeleteElements(&fetchers_); |
47 } | 50 } |
48 | 51 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 154 |
152 void IntranetRedirectDetector::OnIPAddressChanged() { | 155 void IntranetRedirectDetector::OnIPAddressChanged() { |
153 // If a request is already scheduled, do not scheduled yet another one. | 156 // If a request is already scheduled, do not scheduled yet another one. |
154 if (in_sleep_) | 157 if (in_sleep_) |
155 return; | 158 return; |
156 | 159 |
157 // Since presumably many programs open connections after network changes, | 160 // Since presumably many programs open connections after network changes, |
158 // delay this a little bit. | 161 // delay this a little bit. |
159 in_sleep_ = true; | 162 in_sleep_ = true; |
160 static const int kNetworkSwitchDelayMS = 1000; | 163 static const int kNetworkSwitchDelayMS = 1000; |
161 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 164 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
162 base::Bind(&IntranetRedirectDetector::FinishSleep, | 165 FROM_HERE, base::Bind(&IntranetRedirectDetector::FinishSleep, |
163 weak_ptr_factory_.GetWeakPtr()), | 166 weak_ptr_factory_.GetWeakPtr()), |
164 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); | 167 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); |
165 } | 168 } |
OLD | NEW |