| OLD | NEW |
| 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/google/google_url_tracker.h" | 5 #include "chrome/browser/google/google_url_tracker.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/browser/search_engines/template_url.h" | 15 #include "chrome/browser/search_engines/template_url.h" |
| 17 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/net/url_request_context_getter.h" |
| 18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "content/browser/tab_contents/navigation_controller.h" | 19 #include "content/browser/tab_contents/navigation_controller.h" |
| 20 #include "content/browser/tab_contents/tab_contents.h" | 20 #include "content/browser/tab_contents/tab_contents.h" |
| 21 #include "content/common/notification_service.h" | 21 #include "content/common/notification_service.h" |
| 22 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 GoogleURLTracker::GoogleURLTracker() | 91 GoogleURLTracker::GoogleURLTracker() |
| 92 : infobar_creator_(&CreateInfobar), | 92 : infobar_creator_(&CreateInfobar), |
| 93 google_url_(g_browser_process->local_state()->GetString( | 93 google_url_(g_browser_process->local_state()->GetString( |
| 94 prefs::kLastKnownGoogleURL)), | 94 prefs::kLastKnownGoogleURL)), |
| 95 ALLOW_THIS_IN_INITIALIZER_LIST(runnable_method_factory_(this)), | 95 ALLOW_THIS_IN_INITIALIZER_LIST(runnable_method_factory_(this)), |
| 96 fetcher_id_(0), | 96 fetcher_id_(0), |
| 97 queue_wakeup_task_(true), | 97 queue_wakeup_task_(true), |
| 98 in_startup_sleep_(true), | 98 in_startup_sleep_(true), |
| 99 already_fetched_(false), | 99 already_fetched_(false), |
| 100 need_to_fetch_(false), | 100 need_to_fetch_(false), |
| 101 request_context_available_(!!Profile::GetDefaultRequestContext()), | |
| 102 need_to_prompt_(false), | 101 need_to_prompt_(false), |
| 103 controller_(NULL), | 102 controller_(NULL), |
| 104 infobar_(NULL) { | 103 infobar_(NULL) { |
| 105 registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, | |
| 106 NotificationService::AllSources()); | |
| 107 | |
| 108 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 104 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 109 | 105 |
| 110 MessageLoop::current()->PostTask(FROM_HERE, | 106 MessageLoop::current()->PostTask(FROM_HERE, |
| 111 runnable_method_factory_.NewRunnableMethod( | 107 runnable_method_factory_.NewRunnableMethod( |
| 112 &GoogleURLTracker::QueueWakeupTask)); | 108 &GoogleURLTracker::QueueWakeupTask)); |
| 113 } | 109 } |
| 114 | 110 |
| 115 GoogleURLTracker::~GoogleURLTracker() { | 111 GoogleURLTracker::~GoogleURLTracker() { |
| 116 runnable_method_factory_.RevokeAll(); | 112 runnable_method_factory_.RevokeAll(); |
| 117 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 113 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 StartFetchIfDesirable(); | 170 StartFetchIfDesirable(); |
| 175 } | 171 } |
| 176 | 172 |
| 177 void GoogleURLTracker::StartFetchIfDesirable() { | 173 void GoogleURLTracker::StartFetchIfDesirable() { |
| 178 // Bail if a fetch isn't appropriate right now. This function will be called | 174 // Bail if a fetch isn't appropriate right now. This function will be called |
| 179 // again each time one of the preconditions changes, so we'll fetch | 175 // again each time one of the preconditions changes, so we'll fetch |
| 180 // immediately once all of them are met. | 176 // immediately once all of them are met. |
| 181 // | 177 // |
| 182 // See comments in header on the class, on RequestServerCheck(), and on the | 178 // See comments in header on the class, on RequestServerCheck(), and on the |
| 183 // various members here for more detail on exactly what the conditions are. | 179 // various members here for more detail on exactly what the conditions are. |
| 184 if (in_startup_sleep_ || already_fetched_ || !need_to_fetch_ || | 180 if (in_startup_sleep_ || already_fetched_ || !need_to_fetch_) |
| 185 !request_context_available_) | |
| 186 return; | 181 return; |
| 187 | 182 |
| 188 if (CommandLine::ForCurrentProcess()->HasSwitch( | 183 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 189 switches::kDisableBackgroundNetworking)) | 184 switches::kDisableBackgroundNetworking)) |
| 190 return; | 185 return; |
| 191 | 186 |
| 192 already_fetched_ = true; | 187 already_fetched_ = true; |
| 193 fetcher_.reset(URLFetcher::Create(fetcher_id_, GURL(kSearchDomainCheckURL), | 188 fetcher_.reset(URLFetcher::Create(fetcher_id_, GURL(kSearchDomainCheckURL), |
| 194 URLFetcher::GET, this)); | 189 URLFetcher::GET, this)); |
| 195 ++fetcher_id_; | 190 ++fetcher_id_; |
| 196 // We don't want this fetch to affect existing state in the profile. For | 191 // We don't want this fetch to affect existing state in local_state. For |
| 197 // example, if a user has no Google cookies, this automatic check should not | 192 // example, if a user has no Google cookies, this automatic check should not |
| 198 // cause one to be set, lest we alarm the user. | 193 // cause one to be set, lest we alarm the user. |
| 199 fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE | | 194 fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE | |
| 200 net::LOAD_DO_NOT_SAVE_COOKIES); | 195 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 201 fetcher_->set_request_context(Profile::GetDefaultRequestContext()); | 196 fetcher_->set_request_context(g_browser_process->system_request_context()); |
| 202 | 197 |
| 203 // Configure to max_retries at most kMaxRetries times for 5xx errors. | 198 // Configure to max_retries at most kMaxRetries times for 5xx errors. |
| 204 static const int kMaxRetries = 5; | 199 static const int kMaxRetries = 5; |
| 205 fetcher_->set_max_retries(kMaxRetries); | 200 fetcher_->set_max_retries(kMaxRetries); |
| 206 | 201 |
| 207 fetcher_->Start(); | 202 fetcher_->Start(); |
| 208 } | 203 } |
| 209 | 204 |
| 210 void GoogleURLTracker::OnURLFetchComplete(const URLFetcher* source, | 205 void GoogleURLTracker::OnURLFetchComplete(const URLFetcher* source, |
| 211 const GURL& url, | 206 const GURL& url, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 GURL new_search_url(search_url_.ReplaceComponents(replacements)); | 286 GURL new_search_url(search_url_.ReplaceComponents(replacements)); |
| 292 if (new_search_url.is_valid()) | 287 if (new_search_url.is_valid()) |
| 293 controller_->tab_contents()->OpenURL(new_search_url, GURL(), CURRENT_TAB, | 288 controller_->tab_contents()->OpenURL(new_search_url, GURL(), CURRENT_TAB, |
| 294 PageTransition::GENERATED); | 289 PageTransition::GENERATED); |
| 295 } | 290 } |
| 296 | 291 |
| 297 void GoogleURLTracker::Observe(NotificationType type, | 292 void GoogleURLTracker::Observe(NotificationType type, |
| 298 const NotificationSource& source, | 293 const NotificationSource& source, |
| 299 const NotificationDetails& details) { | 294 const NotificationDetails& details) { |
| 300 switch (type.value) { | 295 switch (type.value) { |
| 301 case NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE: | |
| 302 registrar_.Remove(this, | |
| 303 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, | |
| 304 NotificationService::AllSources()); | |
| 305 request_context_available_ = true; | |
| 306 StartFetchIfDesirable(); | |
| 307 break; | |
| 308 | |
| 309 case NotificationType::NAV_ENTRY_PENDING: { | 296 case NotificationType::NAV_ENTRY_PENDING: { |
| 310 NavigationController* controller = | 297 NavigationController* controller = |
| 311 Source<NavigationController>(source).ptr(); | 298 Source<NavigationController>(source).ptr(); |
| 312 OnNavigationPending(source, controller->pending_entry()->url()); | 299 OnNavigationPending(source, controller->pending_entry()->url()); |
| 313 break; | 300 break; |
| 314 } | 301 } |
| 315 | 302 |
| 316 case NotificationType::NAV_ENTRY_COMMITTED: | 303 case NotificationType::NAV_ENTRY_COMMITTED: |
| 317 case NotificationType::TAB_CLOSED: | 304 case NotificationType::TAB_CLOSED: |
| 318 OnNavigationCommittedOrTabClosed( | 305 OnNavigationCommittedOrTabClosed( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 } | 354 } |
| 368 | 355 |
| 369 void GoogleURLTracker::ShowGoogleURLInfoBarIfNecessary( | 356 void GoogleURLTracker::ShowGoogleURLInfoBarIfNecessary( |
| 370 TabContents* tab_contents) { | 357 TabContents* tab_contents) { |
| 371 if (!need_to_prompt_) | 358 if (!need_to_prompt_) |
| 372 return; | 359 return; |
| 373 DCHECK(!fetched_google_url_.is_empty()); | 360 DCHECK(!fetched_google_url_.is_empty()); |
| 374 | 361 |
| 375 infobar_ = (*infobar_creator_)(tab_contents, this, fetched_google_url_); | 362 infobar_ = (*infobar_creator_)(tab_contents, this, fetched_google_url_); |
| 376 } | 363 } |
| OLD | NEW |