Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: chrome/browser/intranet_redirect_detector.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/prefs/pref_registry_simple.h" 9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 17 matching lines...) Expand all
28 prefs::kLastKnownIntranetRedirectOrigin)), 28 prefs::kLastKnownIntranetRedirectOrigin)),
29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
30 in_sleep_(true) { 30 in_sleep_(true) {
31 // Because this function can be called during startup, when kicking off a URL 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 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. 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 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 35 // browser is starting up, and if so, come back later", but there is currently
36 // no function to do this. 36 // no function to do this.
37 static const int kStartFetchDelaySeconds = 7; 37 static const int kStartFetchDelaySeconds = 7;
38 MessageLoop::current()->PostDelayedTask(FROM_HERE, 38 base::MessageLoop::current()->PostDelayedTask(
39 FROM_HERE,
39 base::Bind(&IntranetRedirectDetector::FinishSleep, 40 base::Bind(&IntranetRedirectDetector::FinishSleep,
40 weak_factory_.GetWeakPtr()), 41 weak_factory_.GetWeakPtr()),
41 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds)); 42 base::TimeDelta::FromSeconds(kStartFetchDelaySeconds));
42 43
43 net::NetworkChangeNotifier::AddIPAddressObserver(this); 44 net::NetworkChangeNotifier::AddIPAddressObserver(this);
44 } 45 }
45 46
46 IntranetRedirectDetector::~IntranetRedirectDetector() { 47 IntranetRedirectDetector::~IntranetRedirectDetector() {
47 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); 48 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
48 STLDeleteElements(&fetchers_); 49 STLDeleteElements(&fetchers_);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 147
147 void IntranetRedirectDetector::OnIPAddressChanged() { 148 void IntranetRedirectDetector::OnIPAddressChanged() {
148 // If a request is already scheduled, do not scheduled yet another one. 149 // If a request is already scheduled, do not scheduled yet another one.
149 if (in_sleep_) 150 if (in_sleep_)
150 return; 151 return;
151 152
152 // Since presumably many programs open connections after network changes, 153 // Since presumably many programs open connections after network changes,
153 // delay this a little bit. 154 // delay this a little bit.
154 in_sleep_ = true; 155 in_sleep_ = true;
155 static const int kNetworkSwitchDelayMS = 1000; 156 static const int kNetworkSwitchDelayMS = 1000;
156 MessageLoop::current()->PostDelayedTask(FROM_HERE, 157 base::MessageLoop::current()->PostDelayedTask(
158 FROM_HERE,
157 base::Bind(&IntranetRedirectDetector::FinishSleep, 159 base::Bind(&IntranetRedirectDetector::FinishSleep,
158 weak_factory_.GetWeakPtr()), 160 weak_factory_.GetWeakPtr()),
159 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS)); 161 base::TimeDelta::FromMilliseconds(kNetworkSwitchDelayMS));
160 } 162 }
161 163
162 IntranetRedirectHostResolverProc::IntranetRedirectHostResolverProc( 164 IntranetRedirectHostResolverProc::IntranetRedirectHostResolverProc(
163 net::HostResolverProc* previous) 165 net::HostResolverProc* previous)
164 : net::HostResolverProc(previous) { 166 : net::HostResolverProc(previous) {
165 } 167 }
166 168
167 int IntranetRedirectHostResolverProc::Resolve( 169 int IntranetRedirectHostResolverProc::Resolve(
168 const std::string& host, 170 const std::string& host,
169 net::AddressFamily address_family, 171 net::AddressFamily address_family,
170 net::HostResolverFlags host_resolver_flags, 172 net::HostResolverFlags host_resolver_flags,
171 net::AddressList* addrlist, 173 net::AddressList* addrlist,
172 int* os_error) { 174 int* os_error) {
173 // We'd love to just ask the IntranetRedirectDetector, but we may not be on 175 // We'd love to just ask the IntranetRedirectDetector, but we may not be on
174 // the same thread. So just use the heuristic that any all-lowercase a-z 176 // the same thread. So just use the heuristic that any all-lowercase a-z
175 // hostname with the right number of characters is likely from the detector 177 // hostname with the right number of characters is likely from the detector
176 // (and thus should be blocked). 178 // (and thus should be blocked).
177 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && 179 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) &&
178 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == 180 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") ==
179 std::string::npos)) ? 181 std::string::npos)) ?
180 net::ERR_NAME_NOT_RESOLVED : 182 net::ERR_NAME_NOT_RESOLVED :
181 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, 183 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist,
182 os_error); 184 os_error);
183 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698