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

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

Issue 6292017: Extended: Add "system" URLRequestContext (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased to ToT Created 9 years, 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/command_line.h" 7 #include "base/command_line.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "base/stl_util-inl.h" 9 #include "base/stl_util-inl.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/net/url_request_context_getter.h"
15 #include "chrome/common/notification_service.h"
15 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
16 #include "content/common/notification_service.h" 17 #include "content/common/notification_service.h"
17 #include "net/base/load_flags.h" 18 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
19 #include "net/base/registry_controlled_domain.h" 20 #include "net/base/registry_controlled_domain.h"
20 #include "net/url_request/url_request_status.h" 21 #include "net/url_request/url_request_status.h"
21 22
22 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10; 23 const size_t IntranetRedirectDetector::kNumCharsInHostnames = 10;
23 24
24 IntranetRedirectDetector::IntranetRedirectDetector() 25 IntranetRedirectDetector::IntranetRedirectDetector()
25 : redirect_origin_(g_browser_process->local_state()->GetString( 26 : redirect_origin_(g_browser_process->local_state()->GetString(
26 prefs::kLastKnownIntranetRedirectOrigin)), 27 prefs::kLastKnownIntranetRedirectOrigin)),
27 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), 28 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)),
28 in_sleep_(true), 29 in_sleep_(true) {
29 request_context_available_(Profile::GetDefaultRequestContext() != NULL) {
30 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
31 NotificationService::AllSources());
32
33 // Because this function can be called during startup, when kicking off a URL 30 // Because this function can be called during startup, when kicking off a URL
34 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully 31 // fetch can eat up 20 ms of time, we delay seven seconds, which is hopefully
35 // long enough to be after startup, but still get results back quickly. 32 // long enough to be after startup, but still get results back quickly.
36 // Ideally, instead of this timer, we'd do something like "check if the 33 // Ideally, instead of this timer, we'd do something like "check if the
37 // browser is starting up, and if so, come back later", but there is currently 34 // browser is starting up, and if so, come back later", but there is currently
38 // no function to do this. 35 // no function to do this.
39 static const int kStartFetchDelayMS = 7000; 36 static const int kStartFetchDelayMS = 7000;
40 MessageLoop::current()->PostDelayedTask(FROM_HERE, 37 MessageLoop::current()->PostDelayedTask(FROM_HERE,
41 fetcher_factory_.NewRunnableMethod( 38 fetcher_factory_.NewRunnableMethod(
42 &IntranetRedirectDetector::FinishSleep), 39 &IntranetRedirectDetector::FinishSleep),
(...skipping 20 matching lines...) Expand all
63 std::string()); 60 std::string());
64 } 61 }
65 62
66 void IntranetRedirectDetector::FinishSleep() { 63 void IntranetRedirectDetector::FinishSleep() {
67 in_sleep_ = false; 64 in_sleep_ = false;
68 65
69 // If another fetch operation is still running, cancel it. 66 // If another fetch operation is still running, cancel it.
70 STLDeleteElements(&fetchers_); 67 STLDeleteElements(&fetchers_);
71 resulting_origins_.clear(); 68 resulting_origins_.clear();
72 69
73 StartFetchesIfPossible();
74 }
75
76 void IntranetRedirectDetector::StartFetchesIfPossible() {
77 // Bail if a fetch isn't appropriate right now. This function will be called
78 // again each time one of the preconditions changes, so we'll fetch
79 // immediately once all of them are met.
80 if (in_sleep_ || !request_context_available_)
81 return;
82
83 // The detector is not needed in Chrome Frame since we have no omnibox there. 70 // The detector is not needed in Chrome Frame since we have no omnibox there.
84 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 71 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
85 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking) || 72 if (cmd_line->HasSwitch(switches::kDisableBackgroundNetworking) ||
86 cmd_line->HasSwitch(switches::kChromeFrame)) 73 cmd_line->HasSwitch(switches::kChromeFrame))
87 return; 74 return;
88 75
89 DCHECK(fetchers_.empty() && resulting_origins_.empty()); 76 DCHECK(fetchers_.empty() && resulting_origins_.empty());
90 77
91 // Start three fetchers on random hostnames. 78 // Start three fetchers on random hostnames.
92 for (size_t i = 0; i < 3; ++i) { 79 for (size_t i = 0; i < 3; ++i) {
93 std::string url_string("http://"); 80 std::string url_string("http://");
94 for (size_t j = 0; j < kNumCharsInHostnames; ++j) 81 for (size_t j = 0; j < kNumCharsInHostnames; ++j)
95 url_string += ('a' + base::RandInt(0, 'z' - 'a')); 82 url_string += ('a' + base::RandInt(0, 'z' - 'a'));
96 GURL random_url(url_string + '/'); 83 GURL random_url(url_string + '/');
97 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this); 84 URLFetcher* fetcher = new URLFetcher(random_url, URLFetcher::HEAD, this);
98 // We don't want these fetches to affect existing state in the profile. 85 // We don't want these fetches to affect existing state in the profile.
99 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE | 86 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE |
100 net::LOAD_DO_NOT_SAVE_COOKIES); 87 net::LOAD_DO_NOT_SAVE_COOKIES);
101 fetcher->set_request_context(Profile::GetDefaultRequestContext()); 88 fetcher->set_request_context(g_browser_process->system_request_context());
102 fetcher->Start(); 89 fetcher->Start();
103 fetchers_.insert(fetcher); 90 fetchers_.insert(fetcher);
104 } 91 }
105 } 92 }
106 93
107 void IntranetRedirectDetector::OnURLFetchComplete( 94 void IntranetRedirectDetector::OnURLFetchComplete(
108 const URLFetcher* source, 95 const URLFetcher* source,
109 const GURL& url, 96 const GURL& url,
110 const net::URLRequestStatus& status, 97 const net::URLRequestStatus& status,
111 int response_code, 98 int response_code,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 DCHECK(resulting_origins_.size() == 2); 138 DCHECK(resulting_origins_.size() == 2);
152 redirect_origin_ = net::RegistryControlledDomainService::SameDomainOrHost( 139 redirect_origin_ = net::RegistryControlledDomainService::SameDomainOrHost(
153 resulting_origins_.back(), origin) ? origin : GURL(); 140 resulting_origins_.back(), origin) ? origin : GURL();
154 } 141 }
155 142
156 g_browser_process->local_state()->SetString( 143 g_browser_process->local_state()->SetString(
157 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ? 144 prefs::kLastKnownIntranetRedirectOrigin, redirect_origin_.is_valid() ?
158 redirect_origin_.spec() : std::string()); 145 redirect_origin_.spec() : std::string());
159 } 146 }
160 147
161 void IntranetRedirectDetector::Observe(NotificationType type,
162 const NotificationSource& source,
163 const NotificationDetails& details) {
164 DCHECK_EQ(NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, type.value);
165 request_context_available_ = true;
166 StartFetchesIfPossible();
167 }
168
169 void IntranetRedirectDetector::OnIPAddressChanged() { 148 void IntranetRedirectDetector::OnIPAddressChanged() {
170 // 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.
171 if (in_sleep_) 150 if (in_sleep_)
172 return; 151 return;
173 152
174 // Since presumably many programs open connections after network changes, 153 // Since presumably many programs open connections after network changes,
175 // delay this a little bit. 154 // delay this a little bit.
176 in_sleep_ = true; 155 in_sleep_ = true;
177 static const int kNetworkSwitchDelayMS = 1000; 156 static const int kNetworkSwitchDelayMS = 1000;
178 MessageLoop::current()->PostDelayedTask(FROM_HERE, 157 MessageLoop::current()->PostDelayedTask(FROM_HERE,
(...skipping 17 matching lines...) Expand all
196 // the same thread. So just use the heuristic that any all-lowercase a-z 175 // the same thread. So just use the heuristic that any all-lowercase a-z
197 // hostname with the right number of characters is likely from the detector 176 // hostname with the right number of characters is likely from the detector
198 // (and thus should be blocked). 177 // (and thus should be blocked).
199 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) && 178 return ((host.length() == IntranetRedirectDetector::kNumCharsInHostnames) &&
200 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") == 179 (host.find_first_not_of("abcdefghijklmnopqrstuvwxyz") ==
201 std::string::npos)) ? 180 std::string::npos)) ?
202 net::ERR_NAME_NOT_RESOLVED : 181 net::ERR_NAME_NOT_RESOLVED :
203 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist, 182 ResolveUsingPrevious(host, address_family, host_resolver_flags, addrlist,
204 os_error); 183 os_error);
205 } 184 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698