From cd7c9d17c3d471e00136542092f167fb60e4244f Mon Sep 17 00:00:00 2001 From: pkasting Date: Wed, 7 Jan 2015 14:40:16 -0800 Subject: [PATCH] Remove the GoogleURLTracker infobar functionality entirely. Now instead of prompting the user which search domain they would like when they perform a search after Chrome has detected a new search domain, we just accept what searchdomaincheck tells us immediately. If the domain changes with existing search pages open, we don't reload those pages on the new domain, as we did with the infobar if you clicked the button to change domains; doing so could interrupt what the user is doing and will certainly be surprising. We just allow future searches to use the new domain. BUG=421174 TEST=A machine moved among different countries should automatically change search domains to the local domains (on network switch or Chrome restart) without prompting the user Review URL: https://codereview.chromium.org/808253006 Cr-Commit-Position: refs/heads/master@{#310395} --- build/ios/grit_whitelist.txt | 4 - chrome/browser/android/logo_service.cc | 1 - chrome/browser/android/tab_android.cc | 17 - ...stomization_wallpaper_downloader_browsertest.cc | 1 - chrome/browser/google/DEPS | 8 - .../google/chrome_google_url_tracker_client.cc | 110 +- .../google/chrome_google_url_tracker_client.h | 14 +- .../browser/google/google_url_tracker_factory.cc | 13 +- .../google_url_tracker_navigation_helper_impl.cc | 113 -- .../google_url_tracker_navigation_helper_impl.h | 46 - chrome/browser/prefs/browser_prefs.cc | 27 - .../browser/profile_resetter/profile_resetter.cc | 2 - .../profile_resetter/profile_resetter_unittest.cc | 12 - chrome/browser/search/instant_unittest_base.cc | 5 - chrome/browser/ui/browser.cc | 1 - chrome/browser/ui/browser_navigator.cc | 1 - .../ui/navigation_correction_tab_observer.cc | 4 + chrome/browser/ui/omnibox/omnibox_edit_model.cc | 13 - .../search/instant_extended_interactive_uitest.cc | 1 - chrome/chrome_browser.gypi | 2 - components/components_strings.grd | 1 - components/google.gypi | 10 - components/google/DEPS | 2 - components/google/core/browser/BUILD.gn | 14 - components/google/core/browser/DEPS | 2 - .../google/core/browser/google_pref_names.cc | 6 - components/google/core/browser/google_pref_names.h | 1 - .../google/core/browser/google_url_tracker.cc | 589 +++----- .../google/core/browser/google_url_tracker.h | 78 +- .../core/browser/google_url_tracker_client.h | 8 - .../browser/google_url_tracker_infobar_delegate.cc | 119 -- .../browser/google_url_tracker_infobar_delegate.h | 82 -- .../core/browser/google_url_tracker_map_entry.cc | 66 - .../core/browser/google_url_tracker_map_entry.h | 62 - .../google_url_tracker_navigation_helper.cc | 13 - .../browser/google_url_tracker_navigation_helper.h | 54 - .../core/browser/google_url_tracker_unittest.cc | 1408 +++++--------------- components/google/core/browser/google_util.cc | 2 +- components/google_strings.grdp | 17 - 39 files changed, 568 insertions(+), 2361 deletions(-) delete mode 100644 chrome/browser/google/DEPS rewrite chrome/browser/google/chrome_google_url_tracker_client.cc (68%) delete mode 100644 chrome/browser/google/google_url_tracker_navigation_helper_impl.cc delete mode 100644 chrome/browser/google/google_url_tracker_navigation_helper_impl.h rewrite components/google/core/browser/google_url_tracker.cc (63%) delete mode 100644 components/google/core/browser/google_url_tracker_infobar_delegate.cc delete mode 100644 components/google/core/browser/google_url_tracker_infobar_delegate.h delete mode 100644 components/google/core/browser/google_url_tracker_map_entry.cc delete mode 100644 components/google/core/browser/google_url_tracker_map_entry.h delete mode 100644 components/google/core/browser/google_url_tracker_navigation_helper.cc delete mode 100644 components/google/core/browser/google_url_tracker_navigation_helper.h rewrite components/google/core/browser/google_url_tracker_unittest.cc (75%) delete mode 100644 components/google_strings.grdp diff --git a/build/ios/grit_whitelist.txt b/build/ios/grit_whitelist.txt index 6576f0f88c3d..aa585d815a11 100644 --- a/build/ios/grit_whitelist.txt +++ b/build/ios/grit_whitelist.txt @@ -616,10 +616,6 @@ IDS_GENERIC_EXPERIMENT_CHOICE_AUTOMATIC IDS_GENERIC_EXPERIMENT_CHOICE_DEFAULT IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED -IDS_GOOGLE_URL_TRACKER_INFOBAR_DONT_SWITCH -IDS_GOOGLE_URL_TRACKER_INFOBAR_LEARN_MORE -IDS_GOOGLE_URL_TRACKER_INFOBAR_MESSAGE -IDS_GOOGLE_URL_TRACKER_INFOBAR_SWITCH IDS_GROUP_BY_DOMAIN_LABEL IDS_GUEST_PROFILE_NAME IDS_HARMFUL_V3_EXPLANATION_PARAGRAPH diff --git a/chrome/browser/android/logo_service.cc b/chrome/browser/android/logo_service.cc index 141b09ff4bf3..f9d32524a15a 100644 --- a/chrome/browser/android/logo_service.cc +++ b/chrome/browser/android/logo_service.cc @@ -9,7 +9,6 @@ #include "chrome/browser/image_decoder.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url_service_factory.h" -#include "components/google/core/browser/google_url_tracker.h" #include "components/google/core/browser/google_util.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/search_engines/template_url_service.h" diff --git a/chrome/browser/android/tab_android.cc b/chrome/browser/android/tab_android.cc index 9e2627a5e240..7354f24aac17 100644 --- a/chrome/browser/android/tab_android.cc +++ b/chrome/browser/android/tab_android.cc @@ -13,7 +13,6 @@ #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/favicon/favicon_tab_helper.h" -#include "chrome/browser/google/google_url_tracker_factory.h" #include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/prerender/prerender_contents.h" #include "chrome/browser/prerender/prerender_manager.h" @@ -43,8 +42,6 @@ #include "chrome/common/instant_types.h" #include "chrome/common/render_messages.h" #include "chrome/common/url_constants.h" -#include "components/google/core/browser/google_url_tracker.h" -#include "components/google/core/browser/google_util.h" #include "components/infobars/core/infobar_container.h" #include "components/navigation_interception/intercept_navigation_delegate.h" #include "components/navigation_interception/navigation_params.h" @@ -544,20 +541,6 @@ TabAndroid::TabLoadStatus TabAndroid::LoadUrl(JNIEnv* env, return PAGE_LOAD_FAILED; if (!HandleNonNavigationAboutURL(fixed_url)) { - // Notify the GoogleURLTracker of searches, it might want to change the - // actual Google site used (for instance when in the UK, google.co.uk, when - // in the US google.com). - // Note that this needs to happen before we initiate the navigation as the - // GoogleURLTracker uses the navigation pending notification to trigger the - // infobar. - if (google_util::IsGoogleSearchUrl(fixed_url) && - (page_transition & ui::PAGE_TRANSITION_GENERATED)) { - GoogleURLTracker* tracker = - GoogleURLTrackerFactory::GetForProfile(GetProfile()); - if (tracker) - tracker->SearchCommitted(); - } - // Record UMA "ShowHistory" here. That way it'll pick up both user // typing chrome://history as well as selecting from the drop down menu. if (fixed_url.spec() == chrome::kChromeUIHistoryURL) { diff --git a/chrome/browser/chromeos/customization/customization_wallpaper_downloader_browsertest.cc b/chrome/browser/chromeos/customization/customization_wallpaper_downloader_browsertest.cc index a9cc4a7205df..35d0adfe994c 100644 --- a/chrome/browser/chromeos/customization/customization_wallpaper_downloader_browsertest.cc +++ b/chrome/browser/chromeos/customization/customization_wallpaper_downloader_browsertest.cc @@ -18,7 +18,6 @@ #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/testing_browser_process.h" #include "chromeos/chromeos_switches.h" -#include "components/google/core/browser/google_url_tracker.h" #include "net/http/http_response_headers.h" #include "net/http/http_status_code.h" #include "net/url_request/test_url_fetcher_factory.h" diff --git a/chrome/browser/google/DEPS b/chrome/browser/google/DEPS deleted file mode 100644 index f40a6cae47d9..000000000000 --- a/chrome/browser/google/DEPS +++ /dev/null @@ -1,8 +0,0 @@ -specific_include_rules = { - # GoogleURLTracker can't use WebContents, since it is used by iOS. - # If WebContents functionality is needed, add it to - # GoogleURLTrackerNavigationHelperImpl. - 'google_url_tracker\.': [ - "-content/public/browser/web_contents.h", - ], -} diff --git a/chrome/browser/google/chrome_google_url_tracker_client.cc b/chrome/browser/google/chrome_google_url_tracker_client.cc dissimilarity index 68% index 4abbb7e609e6..60b1e7b1b4ed 100644 --- a/chrome/browser/google/chrome_google_url_tracker_client.cc +++ b/chrome/browser/google/chrome_google_url_tracker_client.cc @@ -1,80 +1,30 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/google/chrome_google_url_tracker_client.h" - -#include "base/command_line.h" -#include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/google/google_url_tracker_navigation_helper_impl.h" -#include "chrome/browser/infobars/infobar_service.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/common/chrome_switches.h" -#include "components/google/core/browser/google_url_tracker.h" -#include "content/public/browser/navigation_controller.h" -#include "content/public/browser/navigation_entry.h" -#include "content/public/browser/notification_service.h" -#include "content/public/browser/web_contents.h" - -ChromeGoogleURLTrackerClient::ChromeGoogleURLTrackerClient(Profile* profile) - : profile_(profile) { -} - -ChromeGoogleURLTrackerClient::~ChromeGoogleURLTrackerClient() { -} - -void ChromeGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) { - if (listen) { - registrar_.Add( - this, - content::NOTIFICATION_NAV_ENTRY_PENDING, - content::NotificationService::AllBrowserContextsAndSources()); - } else { - registrar_.Remove( - this, - content::NOTIFICATION_NAV_ENTRY_PENDING, - content::NotificationService::AllBrowserContextsAndSources()); - } -} - -bool ChromeGoogleURLTrackerClient::IsListeningForNavigationStart() { - return registrar_.IsRegistered( - this, - content::NOTIFICATION_NAV_ENTRY_PENDING, - content::NotificationService::AllBrowserContextsAndSources()); -} - -bool ChromeGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { - return !base::CommandLine::ForCurrentProcess()->HasSwitch( - switches::kDisableBackgroundNetworking); -} - -PrefService* ChromeGoogleURLTrackerClient::GetPrefs() { - return profile_->GetPrefs(); -} - -net::URLRequestContextGetter* -ChromeGoogleURLTrackerClient::GetRequestContext() { - return profile_->GetRequestContext(); -} - -void ChromeGoogleURLTrackerClient::Observe( - int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING, type); - content::NavigationController* controller = - content::Source(source).ptr(); - InfoBarService* infobar_service = - InfoBarService::FromWebContents(controller->GetWebContents()); - // Because we're listening to all sources, there may be no InfoBarService for - // some notifications, e.g. navigations in bubbles/balloons etc. - if (infobar_service) { - google_url_tracker()->OnNavigationPending( - scoped_ptr( - new GoogleURLTrackerNavigationHelperImpl( - controller->GetWebContents(), google_url_tracker())), - infobar_service, - controller->GetPendingEntry()->GetUniqueID()); - } -} +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/google/chrome_google_url_tracker_client.h" + +#include "base/command_line.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/common/chrome_switches.h" + +ChromeGoogleURLTrackerClient::ChromeGoogleURLTrackerClient(Profile* profile) + : profile_(profile) { +} + +ChromeGoogleURLTrackerClient::~ChromeGoogleURLTrackerClient() { +} + +bool ChromeGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { + return !base::CommandLine::ForCurrentProcess()->HasSwitch( + switches::kDisableBackgroundNetworking); +} + +PrefService* ChromeGoogleURLTrackerClient::GetPrefs() { + return profile_->GetPrefs(); +} + +net::URLRequestContextGetter* +ChromeGoogleURLTrackerClient::GetRequestContext() { + return profile_->GetRequestContext(); +} diff --git a/chrome/browser/google/chrome_google_url_tracker_client.h b/chrome/browser/google/chrome_google_url_tracker_client.h index 26878096ac02..2e0d567be333 100644 --- a/chrome/browser/google/chrome_google_url_tracker_client.h +++ b/chrome/browser/google/chrome_google_url_tracker_client.h @@ -6,34 +6,22 @@ #define CHROME_BROWSER_GOOGLE_CHROME_GOOGLE_URL_TRACKER_CLIENT_H_ #include "components/google/core/browser/google_url_tracker_client.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" class Profile; -class ChromeGoogleURLTrackerClient : public GoogleURLTrackerClient, - public content::NotificationObserver { +class ChromeGoogleURLTrackerClient : public GoogleURLTrackerClient { public: explicit ChromeGoogleURLTrackerClient(Profile* profile); ~ChromeGoogleURLTrackerClient() override; // GoogleURLTrackerClient: - void SetListeningForNavigationStart(bool listen) override; - bool IsListeningForNavigationStart() override; bool IsBackgroundNetworkingEnabled() override; PrefService* GetPrefs() override; net::URLRequestContextGetter* GetRequestContext() override; private: - // content::NotificationObserver: - void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) override; - Profile* profile_; - content::NotificationRegistrar registrar_; - DISALLOW_COPY_AND_ASSIGN(ChromeGoogleURLTrackerClient); }; diff --git a/chrome/browser/google/google_url_tracker_factory.cc b/chrome/browser/google/google_url_tracker_factory.cc index bc6eb060b9ce..1ba3e69666d7 100644 --- a/chrome/browser/google/google_url_tracker_factory.cc +++ b/chrome/browser/google/google_url_tracker_factory.cc @@ -13,6 +13,12 @@ #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/pref_registry/pref_registry_syncable.h" +namespace { + +const char kLastPromptedGoogleURL[] = "browser.last_prompted_google_url"; + +} // namespace + // static GoogleURLTracker* GoogleURLTrackerFactory::GetForProfile(Profile* profile) { @@ -36,6 +42,11 @@ GoogleURLTrackerFactory::~GoogleURLTrackerFactory() { KeyedService* GoogleURLTrackerFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { + // Delete this now-unused pref. + // At some point in the future, this code can be removed entirely. + static_cast(context)->GetOriginalProfile()->GetPrefs()->ClearPref( + kLastPromptedGoogleURL); + scoped_ptr client( new ChromeGoogleURLTrackerClient(Profile::FromBrowserContext(context))); return new GoogleURLTracker(client.Pass(), GoogleURLTracker::NORMAL_MODE); @@ -48,7 +59,7 @@ void GoogleURLTrackerFactory::RegisterProfilePrefs( GoogleURLTracker::kDefaultGoogleHomepage, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); user_prefs->RegisterStringPref( - prefs::kLastPromptedGoogleURL, + kLastPromptedGoogleURL, std::string(), user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); } diff --git a/chrome/browser/google/google_url_tracker_navigation_helper_impl.cc b/chrome/browser/google/google_url_tracker_navigation_helper_impl.cc deleted file mode 100644 index 080f61113608..000000000000 --- a/chrome/browser/google/google_url_tracker_navigation_helper_impl.cc +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/google/google_url_tracker_navigation_helper_impl.h" - -#include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/infobars/infobar_service.h" -#include "components/google/core/browser/google_url_tracker.h" -#include "content/public/browser/navigation_controller.h" -#include "content/public/browser/navigation_entry.h" -#include "content/public/browser/notification_service.h" -#include "content/public/browser/web_contents.h" - -GoogleURLTrackerNavigationHelperImpl::GoogleURLTrackerNavigationHelperImpl( - content::WebContents* web_contents, - GoogleURLTracker* tracker) - : GoogleURLTrackerNavigationHelper(tracker), - web_contents_(web_contents) { -} - -GoogleURLTrackerNavigationHelperImpl::~GoogleURLTrackerNavigationHelperImpl() { - web_contents_ = NULL; -} - -void GoogleURLTrackerNavigationHelperImpl::SetListeningForNavigationCommit( - bool listen) { - content::NotificationSource navigation_controller_source = - content::Source( - &web_contents_->GetController()); - if (listen) { - registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, - navigation_controller_source); - } else { - registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, - navigation_controller_source); - } -} - -bool GoogleURLTrackerNavigationHelperImpl::IsListeningForNavigationCommit() { - content::NotificationSource navigation_controller_source = - content::Source( - &web_contents_->GetController()); - return registrar_.IsRegistered( - this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, - navigation_controller_source); -} - -void GoogleURLTrackerNavigationHelperImpl::SetListeningForTabDestruction( - bool listen) { - content::NotificationSource web_contents_source = - content::Source(web_contents_); - if (listen) { - registrar_.Add(this, - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, - web_contents_source); - } else { - registrar_.Remove(this, - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, - web_contents_source); - } -} - -bool GoogleURLTrackerNavigationHelperImpl::IsListeningForTabDestruction() { - return registrar_.IsRegistered( - this, - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, - content::Source(web_contents_)); -} - -void GoogleURLTrackerNavigationHelperImpl::OpenURL( - GURL url, - WindowOpenDisposition disposition, - bool user_clicked_on_link) { - ui::PageTransition transition_type = user_clicked_on_link ? - ui::PAGE_TRANSITION_LINK : ui::PAGE_TRANSITION_GENERATED; - web_contents_->OpenURL(content::OpenURLParams( - url, content::Referrer(), disposition, transition_type, false)); -} - -void GoogleURLTrackerNavigationHelperImpl::Observe( - int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - switch (type) { - case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { - content::NavigationController* controller = - content::Source(source).ptr(); - DCHECK_EQ(web_contents_, controller->GetWebContents()); - - // Here we're only listening to notifications where we already know - // there's an associated InfoBarService. - InfoBarService* infobar_service = - InfoBarService::FromWebContents(web_contents_); - DCHECK(infobar_service); - const GURL& search_url = controller->GetActiveEntry()->GetURL(); - if (!search_url.is_valid()) // Not clear if this can happen. - google_url_tracker()->OnTabClosed(this); - google_url_tracker()->OnNavigationCommitted(infobar_service, search_url); - break; - } - - case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { - DCHECK_EQ(web_contents_, - content::Source(source).ptr()); - google_url_tracker()->OnTabClosed(this); - break; - } - - default: - NOTREACHED() << "Unknown notification received:" << type; - } -} diff --git a/chrome/browser/google/google_url_tracker_navigation_helper_impl.h b/chrome/browser/google/google_url_tracker_navigation_helper_impl.h deleted file mode 100644 index a0a33598368e..000000000000 --- a/chrome/browser/google/google_url_tracker_navigation_helper_impl.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_NAVIGATION_HELPER_IMPL_H_ -#define CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_NAVIGATION_HELPER_IMPL_H_ - -#include "components/google/core/browser/google_url_tracker_navigation_helper.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" -#include "url/gurl.h" - -namespace content { -class WebContents; -} - -class GoogleURLTrackerNavigationHelperImpl - : public GoogleURLTrackerNavigationHelper, - public content::NotificationObserver { - public: - GoogleURLTrackerNavigationHelperImpl(content::WebContents* web_contents, - GoogleURLTracker* tracker); - ~GoogleURLTrackerNavigationHelperImpl() override; - - // GoogleURLTrackerNavigationHelper: - void SetListeningForNavigationCommit(bool listen) override; - bool IsListeningForNavigationCommit() override; - void SetListeningForTabDestruction(bool listen) override; - bool IsListeningForTabDestruction() override; - void OpenURL(GURL url, - WindowOpenDisposition disposition, - bool user_clicked_on_link) override; - - private: - // content::NotificationObserver: - void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) override; - - content::WebContents* web_contents_; - content::NotificationRegistrar registrar_; - - DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerNavigationHelperImpl); -}; - -#endif // CHROME_BROWSER_GOOGLE_GOOGLE_URL_TRACKER_NAVIGATION_HELPER_IMPL_H_ diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc index 9893e188c338..cf905b781c87 100644 --- a/chrome/browser/prefs/browser_prefs.cc +++ b/chrome/browser/prefs/browser_prefs.cc @@ -24,7 +24,6 @@ #include "chrome/browser/external_protocol/external_protocol_handler.h" #include "chrome/browser/first_run/first_run.h" #include "chrome/browser/geolocation/geolocation_prefs.h" -#include "chrome/browser/google/google_url_tracker_factory.h" #include "chrome/browser/gpu/gl_string_manager.h" #include "chrome/browser/gpu/gpu_mode_manager.h" #include "chrome/browser/intranet_redirect_detector.h" @@ -86,8 +85,6 @@ #include "components/dom_distiller/core/distilled_page_prefs.h" #include "components/enhanced_bookmarks/bookmark_server_cluster_service.h" #include "components/gcm_driver/gcm_channel_status_syncer.h" -#include "components/google/core/browser/google_pref_names.h" -#include "components/google/core/browser/google_url_tracker.h" #include "components/network_time/network_time_tracker.h" #include "components/password_manager/core/browser/password_manager.h" #include "components/password_manager/core/browser/password_manager_url_collection_experiment.h" @@ -224,7 +221,6 @@ enum MigratedPreferences { NO_PREFS = 0, DNS_PREFS = 1 << 0, WINDOWS_PREFS = 1 << 1, - GOOGLE_URL_TRACKER_PREFS = 1 << 2, }; // A previous feature (see @@ -623,29 +619,6 @@ void MigrateBrowserPrefs(Profile* profile, PrefService* local_state) { current_version); } - if (!(current_version & GOOGLE_URL_TRACKER_PREFS)) { - registry->RegisterStringPref(prefs::kLastKnownGoogleURL, - GoogleURLTracker::kDefaultGoogleHomepage); - if (local_state->HasPrefPath(prefs::kLastKnownGoogleURL)) { - user_prefs->SetString(prefs::kLastKnownGoogleURL, - local_state->GetString(prefs::kLastKnownGoogleURL)); - } - local_state->ClearPref(prefs::kLastKnownGoogleURL); - - registry->RegisterStringPref(prefs::kLastPromptedGoogleURL, - std::string()); - if (local_state->HasPrefPath(prefs::kLastPromptedGoogleURL)) { - user_prefs->SetString( - prefs::kLastPromptedGoogleURL, - local_state->GetString(prefs::kLastPromptedGoogleURL)); - } - local_state->ClearPref(prefs::kLastPromptedGoogleURL); - - current_version |= GOOGLE_URL_TRACKER_PREFS; - local_state->SetInteger(prefs::kMultipleProfilePrefMigration, - current_version); - } - #if !defined(OS_ANDROID) local_state->ClearPref(kLegacyProfileResetPromptMemento); #endif diff --git a/chrome/browser/profile_resetter/profile_resetter.cc b/chrome/browser/profile_resetter/profile_resetter.cc index 5a887d1b4501..9a5ba3fd96bc 100644 --- a/chrome/browser/profile_resetter/profile_resetter.cc +++ b/chrome/browser/profile_resetter/profile_resetter.cc @@ -21,7 +21,6 @@ #include "chrome/common/pref_names.h" #include "chrome/installer/util/browser_distribution.h" #include "components/content_settings/core/browser/host_content_settings_map.h" -#include "components/google/core/browser/google_pref_names.h" #include "components/google/core/browser/google_url_tracker.h" #include "components/search_engines/search_engines_pref_names.h" #include "components/search_engines/template_url_prepopulate_data.h" @@ -171,7 +170,6 @@ void ProfileResetter::ResetDefaultSearchEngine() { template_url_service_->RepairPrepopulatedSearchEngines(); // Reset Google search URL. - prefs->ClearPref(prefs::kLastPromptedGoogleURL); const TemplateURL* default_search_provider = template_url_service_->GetDefaultSearchProvider(); if (default_search_provider && diff --git a/chrome/browser/profile_resetter/profile_resetter_unittest.cc b/chrome/browser/profile_resetter/profile_resetter_unittest.cc index e60f96b2aa3f..b3af5aa0a6e1 100644 --- a/chrome/browser/profile_resetter/profile_resetter_unittest.cc +++ b/chrome/browser/profile_resetter/profile_resetter_unittest.cc @@ -24,7 +24,6 @@ #include "chrome/common/pref_names.h" #include "chrome/test/base/browser_with_test_window_test.h" #include "components/content_settings/core/browser/host_content_settings_map.h" -#include "components/google/core/browser/google_pref_names.h" #include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service_client.h" #include "content/public/browser/web_contents.h" @@ -424,10 +423,6 @@ TEST_F(ProfileResetterTest, ResetNothing) { } TEST_F(ProfileResetterTest, ResetDefaultSearchEngineNonOrganic) { - PrefService* prefs = profile()->GetPrefs(); - DCHECK(prefs); - prefs->SetString(prefs::kLastPromptedGoogleURL, "http://www.foo.com/"); - ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE, kDistributionConfig); TemplateURLService* model = @@ -437,17 +432,11 @@ TEST_F(ProfileResetterTest, ResetDefaultSearchEngineNonOrganic) { EXPECT_EQ(base::ASCIIToUTF16("first"), default_engine->short_name()); EXPECT_EQ(base::ASCIIToUTF16("firstkey"), default_engine->keyword()); EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", default_engine->url()); - - EXPECT_EQ("", prefs->GetString(prefs::kLastPromptedGoogleURL)); } TEST_F(ProfileResetterTest, ResetDefaultSearchEnginePartially) { // Search engine's logic is tested by // TemplateURLServiceTest.RepairPrepopulatedSearchEngines. - PrefService* prefs = profile()->GetPrefs(); - DCHECK(prefs); - prefs->SetString(prefs::kLastPromptedGoogleURL, "http://www.foo.com/"); - // Make sure TemplateURLService has loaded. ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE); @@ -459,7 +448,6 @@ TEST_F(ProfileResetterTest, ResetDefaultSearchEnginePartially) { ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE); EXPECT_EQ(urls, model->GetTemplateURLs()); - EXPECT_EQ(std::string(), prefs->GetString(prefs::kLastPromptedGoogleURL)); } TEST_F(ProfileResetterTest, ResetHomepageNonOrganic) { diff --git a/chrome/browser/search/instant_unittest_base.cc b/chrome/browser/search/instant_unittest_base.cc index 38aedb4d0c09..bedbaef80943 100644 --- a/chrome/browser/search/instant_unittest_base.cc +++ b/chrome/browser/search/instant_unittest_base.cc @@ -14,9 +14,7 @@ #include "chrome/browser/search/search.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" -#include "chrome/common/pref_names.h" #include "chrome/test/base/browser_with_test_window_test.h" -#include "chrome/test/base/testing_pref_service_syncable.h" #include "chrome/test/base/ui_test_utils.h" #include "components/google/core/browser/google_pref_names.h" #include "components/google/core/browser/google_url_tracker.h" @@ -96,9 +94,6 @@ void InstantUnitTestBase::SetUpHelper() { ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_); UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/"); - TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); - pref_service->SetUserPref(prefs::kLastPromptedGoogleURL, - new base::StringValue("https://www.google.com/")); SetUserSelectedDefaultSearchProvider("{google:baseURL}"); instant_service_ = InstantServiceFactory::GetForProfile(profile()); } diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index 28c11cd62fff..6c926b9ecb86 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -152,7 +152,6 @@ #include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_utils.h" #include "components/content_settings/core/browser/host_content_settings_map.h" -#include "components/google/core/browser/google_url_tracker.h" #include "components/infobars/core/simple_alert_infobar_delegate.h" #include "components/search/search.h" #include "components/sessions/session_types.h" diff --git a/chrome/browser/ui/browser_navigator.cc b/chrome/browser/ui/browser_navigator.cc index cddd46662971..7f14579eb654 100644 --- a/chrome/browser/ui/browser_navigator.cc +++ b/chrome/browser/ui/browser_navigator.cc @@ -30,7 +30,6 @@ #include "chrome/browser/ui/tab_helpers.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/common/url_constants.h" -#include "components/google/core/browser/google_url_tracker.h" #include "content/public/browser/browser_url_handler.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/notification_service.h" diff --git a/chrome/browser/ui/navigation_correction_tab_observer.cc b/chrome/browser/ui/navigation_correction_tab_observer.cc index 53c4326eba69..d59f3e9eff04 100644 --- a/chrome/browser/ui/navigation_correction_tab_observer.cc +++ b/chrome/browser/ui/navigation_correction_tab_observer.cc @@ -41,6 +41,10 @@ NavigationCorrectionTabObserver::NavigationCorrectionTabObserver( GoogleURLTracker* google_url_tracker = GoogleURLTrackerFactory::GetForProfile(profile_); if (google_url_tracker) { + if (google_util::IsGoogleDomainUrl(GetNavigationCorrectionURL(), + google_util::ALLOW_SUBDOMAIN, + google_util::ALLOW_NON_STANDARD_PORTS)) + google_url_tracker->RequestServerCheck(false); google_url_updated_subscription_ = google_url_tracker->RegisterCallback( base::Bind(&NavigationCorrectionTabObserver::OnGoogleURLUpdated, base::Unretained(this))); diff --git a/chrome/browser/ui/omnibox/omnibox_edit_model.cc b/chrome/browser/ui/omnibox/omnibox_edit_model.cc index f4133a714526..237acc183dca 100644 --- a/chrome/browser/ui/omnibox/omnibox_edit_model.cc +++ b/chrome/browser/ui/omnibox/omnibox_edit_model.cc @@ -26,7 +26,6 @@ #include "chrome/browser/command_updater.h" #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" #include "chrome/browser/favicon/favicon_tab_helper.h" -#include "chrome/browser/google/google_url_tracker_factory.h" #include "chrome/browser/net/predictor.h" #include "chrome/browser/omnibox/omnibox_log.h" #include "chrome/browser/predictors/autocomplete_action_predictor.h" @@ -53,7 +52,6 @@ #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "components/bookmarks/browser/bookmark_model.h" -#include "components/google/core/browser/google_url_tracker.h" #include "components/metrics/proto/omnibox_event.pb.h" #include "components/omnibox/autocomplete_provider.h" #include "components/omnibox/keyword_provider.h" @@ -688,17 +686,6 @@ void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition, match.transition = ui::PAGE_TRANSITION_LINK; } - TemplateURLService* service = - TemplateURLServiceFactory::GetForProfile(profile_); - const TemplateURL* template_url = match.GetTemplateURL(service, false); - if (template_url && template_url->url_ref().HasGoogleBaseURLs( - UIThreadSearchTermsData(profile_))) { - GoogleURLTracker* tracker = - GoogleURLTrackerFactory::GetForProfile(profile_); - if (tracker) - tracker->SearchCommitted(); - } - DCHECK(popup_model()); view_->OpenMatch(match, disposition, alternate_nav_url, base::string16(), popup_model()->selected_line()); diff --git a/chrome/browser/ui/search/instant_extended_interactive_uitest.cc b/chrome/browser/ui/search/instant_extended_interactive_uitest.cc index 89d2c15031b7..dd398dc57f4f 100644 --- a/chrome/browser/ui/search/instant_extended_interactive_uitest.cc +++ b/chrome/browser/ui/search/instant_extended_interactive_uitest.cc @@ -49,7 +49,6 @@ #include "chrome/test/base/ui_test_utils.h" #include "components/bookmarks/browser/bookmark_utils.h" #include "components/bookmarks/test/bookmark_test_helpers.h" -#include "components/google/core/browser/google_url_tracker.h" #include "components/history/core/browser/history_db_task.h" #include "components/history/core/browser/history_types.h" #include "components/history/core/common/thumbnail_score.h" diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index ec1743752902..3e5cb74f8415 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1476,8 +1476,6 @@ 'browser/google/google_update_win.h', 'browser/google/google_url_tracker_factory.cc', 'browser/google/google_url_tracker_factory.h', - 'browser/google/google_url_tracker_navigation_helper_impl.cc', - 'browser/google/google_url_tracker_navigation_helper_impl.h', ], 'chrome_browser_history_sources': [ 'browser/history/android/android_history_provider_service.cc', diff --git a/components/components_strings.grd b/components/components_strings.grd index 277d25c8827d..fbf6e6f2d54c 100644 --- a/components/components_strings.grd +++ b/components/components_strings.grd @@ -172,7 +172,6 @@ - diff --git a/components/google.gypi b/components/google.gypi index 93cc4e4e6604..5fb0dd0e314c 100644 --- a/components/google.gypi +++ b/components/google.gypi @@ -11,11 +11,9 @@ 'dependencies': [ '../base/base.gyp:base', '../net/net.gyp:net', - '../ui/base/ui_base.gyp:ui_base', '../url/url.gyp:url_lib', 'components_strings.gyp:components_strings', 'keyed_service_core', - 'infobars_core', 'url_fixer', ], 'include_dirs': [ @@ -33,17 +31,9 @@ 'google/core/browser/google_url_tracker.h', 'google/core/browser/google_url_tracker_client.cc', 'google/core/browser/google_url_tracker_client.h', - 'google/core/browser/google_url_tracker_infobar_delegate.cc', - 'google/core/browser/google_url_tracker_infobar_delegate.h', - 'google/core/browser/google_url_tracker_map_entry.cc', - 'google/core/browser/google_url_tracker_map_entry.h', - 'google/core/browser/google_url_tracker_navigation_helper.cc', - 'google/core/browser/google_url_tracker_navigation_helper.h', 'google/core/browser/google_util.cc', 'google/core/browser/google_util.h', ], - # TODO(jschuh): crbug.com/167187 fix size_t to int truncations. - 'msvs_disabled_warnings': [4267, ], }, ], } diff --git a/components/google/DEPS b/components/google/DEPS index 0d4bff12ee4d..c39802b4152d 100644 --- a/components/google/DEPS +++ b/components/google/DEPS @@ -1,6 +1,4 @@ include_rules = [ "+components/url_fixer", - "+grit", "+net", - "+ui", ] diff --git a/components/google/core/browser/BUILD.gn b/components/google/core/browser/BUILD.gn index 3935fdbfff23..9af9ff664b9b 100644 --- a/components/google/core/browser/BUILD.gn +++ b/components/google/core/browser/BUILD.gn @@ -14,30 +14,17 @@ static_library("browser") { "google_url_tracker.h", "google_url_tracker_client.cc", "google_url_tracker_client.h", - "google_url_tracker_infobar_delegate.cc", - "google_url_tracker_infobar_delegate.h", - "google_url_tracker_map_entry.cc", - "google_url_tracker_map_entry.h", - "google_url_tracker_navigation_helper.cc", - "google_url_tracker_navigation_helper.h", "google_util.cc", "google_util.h", ] - if (is_win) { - # TODO(jschuh): crbug.com/167187 fix size_t to int truncations. - cflags = [ "/wd4267" ] - } - deps = [ "//base", "//base:prefs", "//components/keyed_service/core", - "//components/infobars/core", "//components/strings", "//components/url_fixer", "//net", - "//ui/base", "//url", ] } @@ -55,7 +42,6 @@ source_set("unit_tests") { "//base", "//base:prefs", "//base:prefs_test_support", - "//components/infobars/core", "//net:test_support", "//testing/gtest", ] diff --git a/components/google/core/browser/DEPS b/components/google/core/browser/DEPS index 4059989e2bae..f0bf3d9693ec 100644 --- a/components/google/core/browser/DEPS +++ b/components/google/core/browser/DEPS @@ -1,5 +1,3 @@ include_rules = [ - "+components/infobars/core", - "+components/infobars/test", "+components/keyed_service/core", ] diff --git a/components/google/core/browser/google_pref_names.cc b/components/google/core/browser/google_pref_names.cc index 81e6749d4812..eeb8de180524 100644 --- a/components/google/core/browser/google_pref_names.cc +++ b/components/google/core/browser/google_pref_names.cc @@ -11,10 +11,4 @@ namespace prefs { // correct Google domain/country code for whatever location the user is in. const char kLastKnownGoogleURL[] = "browser.last_known_google_url"; -// String containing the last prompted Google URL to the user. -// If the user is using .x TLD for Google URL and gets prompted about .y TLD -// for Google URL, and says "no", we should leave the search engine set to .x -// but not prompt again until the domain changes away from .y. -const char kLastPromptedGoogleURL[] = "browser.last_prompted_google_url"; - } // namespace prefs diff --git a/components/google/core/browser/google_pref_names.h b/components/google/core/browser/google_pref_names.h index 2c58dd291874..4307c9271f38 100644 --- a/components/google/core/browser/google_pref_names.h +++ b/components/google/core/browser/google_pref_names.h @@ -10,7 +10,6 @@ namespace prefs { // Alphabetical list of preference names specific to the Google // component. Keep alphabetized, and document each in the .cc file. extern const char kLastKnownGoogleURL[]; -extern const char kLastPromptedGoogleURL[]; } // namespace prefs diff --git a/components/google/core/browser/google_url_tracker.cc b/components/google/core/browser/google_url_tracker.cc dissimilarity index 63% index 8896629b6bd2..444883410ccc 100644 --- a/components/google/core/browser/google_url_tracker.cc +++ b/components/google/core/browser/google_url_tracker.cc @@ -1,416 +1,173 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/google/core/browser/google_url_tracker.h" - -#include "base/bind.h" -#include "base/command_line.h" -#include "base/prefs/pref_service.h" -#include "base/strings/string_util.h" -#include "components/google/core/browser/google_pref_names.h" -#include "components/google/core/browser/google_switches.h" -#include "components/google/core/browser/google_url_tracker_infobar_delegate.h" -#include "components/google/core/browser/google_url_tracker_navigation_helper.h" -#include "components/google/core/browser/google_util.h" -#include "components/infobars/core/infobar.h" -#include "components/infobars/core/infobar_manager.h" -#include "net/base/load_flags.h" -#include "net/base/net_util.h" -#include "net/url_request/url_fetcher.h" -#include "net/url_request/url_request_status.h" - - -const char GoogleURLTracker::kDefaultGoogleHomepage[] = - "http://www.google.com/"; -const char GoogleURLTracker::kSearchDomainCheckURL[] = - "https://www.google.com/searchdomaincheck?format=url&type=chrome"; - -GoogleURLTracker::GoogleURLTracker(scoped_ptr client, - Mode mode) - : client_(client.Pass()), - google_url_(mode == UNIT_TEST_MODE ? - kDefaultGoogleHomepage : - client_->GetPrefs()->GetString(prefs::kLastKnownGoogleURL)), - fetcher_id_(0), - in_startup_sleep_(true), - already_fetched_(false), - need_to_fetch_(false), - need_to_prompt_(false), - search_committed_(false), - weak_ptr_factory_(this) { - net::NetworkChangeNotifier::AddNetworkChangeObserver(this); - client_->set_google_url_tracker(this); - - // Because this function can be called during startup, when kicking off a URL - // fetch can eat up 20 ms of time, we delay five seconds, which is hopefully - // long enough to be after startup, but still get results back quickly. - // Ideally, instead of this timer, we'd do something like "check if the - // browser is starting up, and if so, come back later", but there is currently - // no function to do this. - // - // In UNIT_TEST_MODE, where we want to explicitly control when the tracker - // "wakes up", we do nothing at all. - if (mode == NORMAL_MODE) { - static const int kStartFetchDelayMS = 5000; - base::MessageLoop::current()->PostDelayedTask(FROM_HERE, - base::Bind(&GoogleURLTracker::FinishSleep, - weak_ptr_factory_.GetWeakPtr()), - base::TimeDelta::FromMilliseconds(kStartFetchDelayMS)); - } -} - -GoogleURLTracker::~GoogleURLTracker() { - // We should only reach here after any tabs and their infobars have been torn - // down. - DCHECK(entry_map_.empty()); -} - -void GoogleURLTracker::RequestServerCheck(bool force) { - // If this instance already has a fetcher, SetNeedToFetch() is unnecessary, - // and changing |already_fetched_| is wrong. - if (!fetcher_) { - if (force) - already_fetched_ = false; - SetNeedToFetch(); - } -} - -void GoogleURLTracker::SearchCommitted() { - if (need_to_prompt_) { - search_committed_ = true; - // These notifications will fire a bit later in the same call chain we're - // currently in. - if (!client_->IsListeningForNavigationStart()) - client_->SetListeningForNavigationStart(true); - } -} - -void GoogleURLTracker::AcceptGoogleURL(bool redo_searches) { - GURL old_google_url = google_url_; - google_url_ = fetched_google_url_; - PrefService* prefs = client_->GetPrefs(); - prefs->SetString(prefs::kLastKnownGoogleURL, google_url_.spec()); - prefs->SetString(prefs::kLastPromptedGoogleURL, google_url_.spec()); - NotifyGoogleURLUpdated(); - - need_to_prompt_ = false; - CloseAllEntries(redo_searches); -} - -void GoogleURLTracker::CancelGoogleURL() { - client_->GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, - fetched_google_url_.spec()); - need_to_prompt_ = false; - CloseAllEntries(false); -} - -void GoogleURLTracker::OnURLFetchComplete(const net::URLFetcher* source) { - // Delete the fetcher on this function's exit. - scoped_ptr clean_up_fetcher(fetcher_.release()); - - // Don't update the URL if the request didn't succeed. - if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) { - already_fetched_ = false; - return; - } - - // See if the response data was valid. It should be - // "://[www.]google./". - std::string url_str; - source->GetResponseAsString(&url_str); - base::TrimWhitespace(url_str, base::TRIM_ALL, &url_str); - GURL url(url_str); - if (!url.is_valid() || (url.path().length() > 1) || url.has_query() || - url.has_ref() || - !google_util::IsGoogleDomainUrl(url, - google_util::DISALLOW_SUBDOMAIN, - google_util::DISALLOW_NON_STANDARD_PORTS)) - return; - - std::swap(url, fetched_google_url_); - GURL last_prompted_url( - client_->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); - - if (last_prompted_url.is_empty()) { - // On the very first run of Chrome, when we've never looked up the URL at - // all, we should just silently switch over to whatever we get immediately. - AcceptGoogleURL(true); // Arg is irrelevant. - return; - } - - base::string16 fetched_host(net::StripWWWFromHost(fetched_google_url_)); - if (fetched_google_url_ == google_url_) { - // Either the user has continually been on this URL, or we prompted for a - // different URL but have now changed back before they responded to any of - // the prompts. In this latter case we want to close any infobars and stop - // prompting. - CancelGoogleURL(); - } else if (fetched_host == net::StripWWWFromHost(google_url_)) { - // Similar to the above case, but this time the new URL differs from the - // existing one, probably due to switching between HTTP and HTTPS searching. - // Like before we want to close any infobars and stop prompting; we also - // want to silently accept the change in scheme. We don't redo open - // searches so as to avoid suddenly changing a page the user might be - // interacting with; it's enough to simply get future searches right. - AcceptGoogleURL(false); - } else if (fetched_host == net::StripWWWFromHost(last_prompted_url)) { - // We've re-fetched a TLD the user previously turned down. Although the new - // URL might have a different scheme than the old, we want to preserve the - // user's decision. Note that it's possible that, like in the above two - // cases, we fetched yet another different URL in the meantime, which we - // have infobars prompting about; in this case, as in those above, we want - // to go ahead and close the infobars and stop prompting, since we've - // switched back away from that URL. - CancelGoogleURL(); - } else { - // We've fetched a URL with a different TLD than the user is currently using - // or was previously prompted about. This means we need to prompt again. - need_to_prompt_ = true; - - // As in all the above cases, there could be infobars prompting about some - // URL. If these URLs have the same TLD (e.g. for scheme changes), we can - // simply leave the existing infobars open as their messages will still be - // accurate. Otherwise we go ahead and close them because we need to - // display a new message. - // Note: |url| is the previous |fetched_google_url_|. - if (url.is_valid() && (fetched_host != net::StripWWWFromHost(url))) - CloseAllEntries(false); - } -} - -void GoogleURLTracker::OnNetworkChanged( - net::NetworkChangeNotifier::ConnectionType type) { - // Ignore destructive signals. - if (type == net::NetworkChangeNotifier::CONNECTION_NONE) - return; - already_fetched_ = false; - StartFetchIfDesirable(); -} - -void GoogleURLTracker::Shutdown() { - client_.reset(); - fetcher_.reset(); - weak_ptr_factory_.InvalidateWeakPtrs(); - net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); -} - -void GoogleURLTracker::DeleteMapEntryForManager( - const infobars::InfoBarManager* infobar_manager) { - // WARNING: |infobar_manager| may point to a deleted object. Do not - // dereference it! See OnTabClosed(). - EntryMap::iterator i(entry_map_.find(infobar_manager)); - DCHECK(i != entry_map_.end()); - GoogleURLTrackerMapEntry* map_entry = i->second; - - UnregisterForEntrySpecificNotifications(map_entry, false); - entry_map_.erase(i); - delete map_entry; -} - -void GoogleURLTracker::SetNeedToFetch() { - need_to_fetch_ = true; - StartFetchIfDesirable(); -} - -void GoogleURLTracker::FinishSleep() { - in_startup_sleep_ = false; - StartFetchIfDesirable(); -} - -void GoogleURLTracker::StartFetchIfDesirable() { - // Bail if a fetch isn't appropriate right now. This function will be called - // again each time one of the preconditions changes, so we'll fetch - // immediately once all of them are met. - // - // See comments in header on the class, on RequestServerCheck(), and on the - // various members here for more detail on exactly what the conditions are. - if (in_startup_sleep_ || already_fetched_ || !need_to_fetch_) - return; - - // Some switches should disable the Google URL tracker entirely. If we can't - // do background networking, we can't do the necessary fetch, and if the user - // specified a Google base URL manually, we shouldn't bother to look up any - // alternatives or offer to switch to them. - if (!client_->IsBackgroundNetworkingEnabled() || - base::CommandLine::ForCurrentProcess()->HasSwitch( - switches::kGoogleBaseURL)) - return; - - already_fetched_ = true; - fetcher_.reset(net::URLFetcher::Create( - fetcher_id_, GURL(kSearchDomainCheckURL), net::URLFetcher::GET, this)); - ++fetcher_id_; - // We don't want this fetch to set new entries in the cache or cookies, lest - // we alarm the user. - fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE | - net::LOAD_DO_NOT_SAVE_COOKIES); - fetcher_->SetRequestContext(client_->GetRequestContext()); - - // Configure to retry at most kMaxRetries times for 5xx errors. - static const int kMaxRetries = 5; - fetcher_->SetMaxRetriesOn5xx(kMaxRetries); - - // Also retry kMaxRetries times on network change errors. A network change can - // propagate through Chrome in various stages, so it's possible for this code - // to be reached via OnNetworkChanged(), and then have the fetch we kick off - // be canceled due to e.g. the DNS server changing at a later time. In general - // it's not possible to ensure that by the time we reach here any requests we - // start won't be canceled in this fashion, so retrying is the best we can do. - fetcher_->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries); - - fetcher_->Start(); -} - -void GoogleURLTracker::OnNavigationPending( - scoped_ptr nav_helper, - infobars::InfoBarManager* infobar_manager, - int pending_id) { - GoogleURLTrackerMapEntry* map_entry = NULL; - - EntryMap::iterator i(entry_map_.find(infobar_manager)); - if (i != entry_map_.end()) - map_entry = i->second; - - if (search_committed_) { - search_committed_ = false; - if (!map_entry) { - // This is a search on a tab that doesn't have one of our infobars, so - // prepare to add one. Note that we only listen for the tab's destruction - // on this path; if there was already a map entry, then either it doesn't - // yet have an infobar and we're already registered for this, or it has an - // infobar and the infobar's owner will handle tearing it down when the - // tab is destroyed. - map_entry = new GoogleURLTrackerMapEntry( - this, infobar_manager, nav_helper.Pass()); - map_entry->navigation_helper()->SetListeningForTabDestruction(true); - entry_map_.insert(std::make_pair(infobar_manager, map_entry)); - } else if (map_entry->infobar_delegate()) { - // This is a new search on a tab where we already have an infobar. - map_entry->infobar_delegate()->set_pending_id(pending_id); - } - - // Whether there's an existing infobar or not, we need to listen for the - // load to commit, so we can show and/or update the infobar when it does. - // (We may already be registered for this if there is an existing infobar - // that had a previous pending search that hasn't yet committed.) - if (!map_entry->navigation_helper()->IsListeningForNavigationCommit()) - map_entry->navigation_helper()->SetListeningForNavigationCommit(true); - } else if (map_entry) { - if (map_entry->has_infobar_delegate()) { - // This is a non-search navigation on a tab with an infobar. If there was - // a previous pending search on this tab, this means it won't commit, so - // undo anything we did in response to seeing that. Note that if there - // was no pending search on this tab, these statements are effectively a - // no-op. - // - // If this navigation actually commits, that will trigger the infobar's - // owner to expire the infobar if need be. If it doesn't commit, then - // simply leaving the infobar as-is will have been the right thing. - UnregisterForEntrySpecificNotifications(map_entry, false); - map_entry->infobar_delegate()->set_pending_id(0); - } else { - // Non-search navigation on a tab with an entry that has not yet created - // an infobar. This means the original search won't commit, so delete the - // entry. - map_entry->Close(false); - } - } else { - // Non-search navigation on a tab without an infobars. This is irrelevant - // to us. - } -} - -void GoogleURLTracker::OnNavigationCommitted( - infobars::InfoBarManager* infobar_manager, - const GURL& search_url) { - EntryMap::iterator i(entry_map_.find(infobar_manager)); - DCHECK(i != entry_map_.end()); - GoogleURLTrackerMapEntry* map_entry = i->second; - DCHECK(search_url.is_valid()); - - UnregisterForEntrySpecificNotifications(map_entry, true); - if (map_entry->has_infobar_delegate()) { - map_entry->infobar_delegate()->Update(search_url); - } else { - infobars::InfoBar* infobar = GoogleURLTrackerInfoBarDelegate::Create( - infobar_manager, this, search_url); - if (infobar) { - map_entry->SetInfoBarDelegate( - static_cast(infobar->delegate())); - } else { - map_entry->Close(false); - } - } -} - -void GoogleURLTracker::OnTabClosed( - GoogleURLTrackerNavigationHelper* nav_helper) { - // Because InfoBarManager tears itself down on tab destruction, it's possible - // to get a non-NULL InfoBarManager pointer here, depending on which order - // notifications fired in. Likewise, the pointer in |entry_map_| (and in its - // associated MapEntry) may point to deleted memory. Therefore, if we were - // to access the InfoBarManager* we have for this tab, we'd need to ensure we - // just looked at the raw pointer value, and never dereferenced it. This - // function doesn't need to do even that, but others in the call chain from - // here might (and have comments pointing back here). - for (EntryMap::iterator i(entry_map_.begin()); i != entry_map_.end(); ++i) { - if (i->second->navigation_helper() == nav_helper) { - i->second->Close(false); - return; - } - } - NOTREACHED(); -} - -scoped_ptr GoogleURLTracker::RegisterCallback( - const OnGoogleURLUpdatedCallback& cb) { - return callback_list_.Add(cb); -} - -void GoogleURLTracker::CloseAllEntries(bool redo_searches) { - // Delete all entries, whether they have infobars or not. - while (!entry_map_.empty()) - entry_map_.begin()->second->Close(redo_searches); -} - -void GoogleURLTracker::UnregisterForEntrySpecificNotifications( - GoogleURLTrackerMapEntry* map_entry, - bool must_be_listening_for_commit) { - // For tabs with map entries but no infobars, we should always be listening - // for both these notifications. For tabs with infobars, we may be listening - // for navigation commits if the user has performed a new search on this tab. - if (map_entry->navigation_helper()->IsListeningForNavigationCommit()) { - map_entry->navigation_helper()->SetListeningForNavigationCommit(false); - } else { - DCHECK(!must_be_listening_for_commit); - DCHECK(map_entry->has_infobar_delegate()); - } - const bool registered_for_tab_destruction = - map_entry->navigation_helper()->IsListeningForTabDestruction(); - DCHECK_NE(registered_for_tab_destruction, map_entry->has_infobar_delegate()); - if (registered_for_tab_destruction) { - map_entry->navigation_helper()->SetListeningForTabDestruction(false); - } - - // Our global listeners for these other notifications should be in place iff - // we have any tabs still listening for commits. These tabs either have no - // infobars or have received new pending searches atop existing infobars; in - // either case we want to catch subsequent pending non-search navigations. - // See the various cases inside OnNavigationPending(). - for (EntryMap::const_iterator i(entry_map_.begin()); i != entry_map_.end(); - ++i) { - if (i->second->navigation_helper()->IsListeningForNavigationCommit()) { - DCHECK(client_->IsListeningForNavigationStart()); - return; - } - } - if (client_->IsListeningForNavigationStart()) { - DCHECK(!search_committed_); - client_->SetListeningForNavigationStart(false); - } -} - -void GoogleURLTracker::NotifyGoogleURLUpdated() { - callback_list_.Notify(); -} +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/google/core/browser/google_url_tracker.h" + +#include "base/bind.h" +#include "base/command_line.h" +#include "base/prefs/pref_service.h" +#include "base/strings/string_util.h" +#include "components/google/core/browser/google_pref_names.h" +#include "components/google/core/browser/google_switches.h" +#include "components/google/core/browser/google_util.h" +#include "net/base/load_flags.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_request_status.h" + + +const char GoogleURLTracker::kDefaultGoogleHomepage[] = + "http://www.google.com/"; +const char GoogleURLTracker::kSearchDomainCheckURL[] = + "https://www.google.com/searchdomaincheck?format=url&type=chrome"; + +GoogleURLTracker::GoogleURLTracker(scoped_ptr client, + Mode mode) + : client_(client.Pass()), + google_url_(mode == UNIT_TEST_MODE ? + kDefaultGoogleHomepage : + client_->GetPrefs()->GetString(prefs::kLastKnownGoogleURL)), + fetcher_id_(0), + in_startup_sleep_(true), + already_fetched_(false), + need_to_fetch_(false), + weak_ptr_factory_(this) { + net::NetworkChangeNotifier::AddNetworkChangeObserver(this); + client_->set_google_url_tracker(this); + + // Because this function can be called during startup, when kicking off a URL + // fetch can eat up 20 ms of time, we delay five seconds, which is hopefully + // long enough to be after startup, but still get results back quickly. + // Ideally, instead of this timer, we'd do something like "check if the + // browser is starting up, and if so, come back later", but there is currently + // no function to do this. + // + // In UNIT_TEST_MODE, where we want to explicitly control when the tracker + // "wakes up", we do nothing at all. + if (mode == NORMAL_MODE) { + static const int kStartFetchDelayMS = 5000; + base::MessageLoop::current()->PostDelayedTask(FROM_HERE, + base::Bind(&GoogleURLTracker::FinishSleep, + weak_ptr_factory_.GetWeakPtr()), + base::TimeDelta::FromMilliseconds(kStartFetchDelayMS)); + } +} + +GoogleURLTracker::~GoogleURLTracker() { +} + +void GoogleURLTracker::RequestServerCheck(bool force) { + // If this instance already has a fetcher, SetNeedToFetch() is unnecessary, + // and changing |already_fetched_| is wrong. + if (!fetcher_) { + if (force) + already_fetched_ = false; + SetNeedToFetch(); + } +} + +scoped_ptr GoogleURLTracker::RegisterCallback( + const OnGoogleURLUpdatedCallback& cb) { + return callback_list_.Add(cb); +} + +void GoogleURLTracker::OnURLFetchComplete(const net::URLFetcher* source) { + // Delete the fetcher on this function's exit. + scoped_ptr clean_up_fetcher(fetcher_.release()); + + // Don't update the URL if the request didn't succeed. + if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200)) { + already_fetched_ = false; + return; + } + + // See if the response data was valid. It should be + // "://[www.]google./". + std::string url_str; + source->GetResponseAsString(&url_str); + base::TrimWhitespace(url_str, base::TRIM_ALL, &url_str); + GURL url(url_str); + if (!url.is_valid() || (url.path().length() > 1) || url.has_query() || + url.has_ref() || + !google_util::IsGoogleDomainUrl(url, + google_util::DISALLOW_SUBDOMAIN, + google_util::DISALLOW_NON_STANDARD_PORTS)) + return; + + if (url != google_url_) { + google_url_ = url; + client_->GetPrefs()->SetString(prefs::kLastKnownGoogleURL, + google_url_.spec()); + callback_list_.Notify(); + } +} + +void GoogleURLTracker::OnNetworkChanged( + net::NetworkChangeNotifier::ConnectionType type) { + // Ignore destructive signals. + if (type == net::NetworkChangeNotifier::CONNECTION_NONE) + return; + already_fetched_ = false; + StartFetchIfDesirable(); +} + +void GoogleURLTracker::Shutdown() { + client_.reset(); + fetcher_.reset(); + weak_ptr_factory_.InvalidateWeakPtrs(); + net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); +} + +void GoogleURLTracker::SetNeedToFetch() { + need_to_fetch_ = true; + StartFetchIfDesirable(); +} + +void GoogleURLTracker::FinishSleep() { + in_startup_sleep_ = false; + StartFetchIfDesirable(); +} + +void GoogleURLTracker::StartFetchIfDesirable() { + // Bail if a fetch isn't appropriate right now. This function will be called + // again each time one of the preconditions changes, so we'll fetch + // immediately once all of them are met. + // + // See comments in header on the class, on RequestServerCheck(), and on the + // various members here for more detail on exactly what the conditions are. + if (in_startup_sleep_ || already_fetched_ || !need_to_fetch_) + return; + + // Some switches should disable the Google URL tracker entirely. If we can't + // do background networking, we can't do the necessary fetch, and if the user + // specified a Google base URL manually, we shouldn't bother to look up any + // alternatives or offer to switch to them. + if (!client_->IsBackgroundNetworkingEnabled() || + base::CommandLine::ForCurrentProcess()->HasSwitch( + switches::kGoogleBaseURL)) + return; + + already_fetched_ = true; + fetcher_.reset(net::URLFetcher::Create( + fetcher_id_, GURL(kSearchDomainCheckURL), net::URLFetcher::GET, this)); + ++fetcher_id_; + // We don't want this fetch to set new entries in the cache or cookies, lest + // we alarm the user. + fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE | + net::LOAD_DO_NOT_SAVE_COOKIES); + fetcher_->SetRequestContext(client_->GetRequestContext()); + + // Configure to retry at most kMaxRetries times for 5xx errors. + static const int kMaxRetries = 5; + fetcher_->SetMaxRetriesOn5xx(kMaxRetries); + + // Also retry kMaxRetries times on network change errors. A network change can + // propagate through Chrome in various stages, so it's possible for this code + // to be reached via OnNetworkChanged(), and then have the fetch we kick off + // be canceled due to e.g. the DNS server changing at a later time. In general + // it's not possible to ensure that by the time we reach here any requests we + // start won't be canceled in this fashion, so retrying is the best we can do. + fetcher_->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries); + + fetcher_->Start(); +} diff --git a/components/google/core/browser/google_url_tracker.h b/components/google/core/browser/google_url_tracker.h index aaa2c4ba53c7..ad30b3ac5753 100644 --- a/components/google/core/browser/google_url_tracker.h +++ b/components/google/core/browser/google_url_tracker.h @@ -5,17 +5,11 @@ #ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ #define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_ -#include -#include -#include - #include "base/callback_forward.h" #include "base/callback_list.h" -#include "base/gtest_prod_util.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "components/google/core/browser/google_url_tracker_client.h" -#include "components/google/core/browser/google_url_tracker_map_entry.h" #include "components/keyed_service/core/keyed_service.h" #include "net/base/network_change_notifier.h" #include "net/url_request/url_fetcher.h" @@ -30,8 +24,7 @@ class InfoBar; } // This object is responsible for checking the Google URL once per network -// change, and if necessary prompting the user to see if they want to change to -// using it. The current and last prompted values are saved to prefs. +// change. The current value is saved to prefs. // // Most consumers should only call google_url(). Consumers who need to be // notified when things change should register a callback that provides the @@ -77,44 +70,6 @@ class GoogleURLTracker // check, it will check again. void RequestServerCheck(bool force); - // Notifies the tracker that the user has started a Google search. - // If prompting is necessary, we then listen for the subsequent pending - // navigation to get the appropriate NavigationHelper. When the load - // commits, we'll show the infobar. - void SearchCommitted(); - - // No one but GoogleURLTrackerInfoBarDelegate or test code should call these. - void AcceptGoogleURL(bool redo_searches); - void CancelGoogleURL(); - const GURL& fetched_google_url() const { return fetched_google_url_; } - GoogleURLTrackerClient* client() { return client_.get(); } - - // No one but GoogleURLTrackerMapEntry should call this. - void DeleteMapEntryForManager( - const infobars::InfoBarManager* infobar_manager); - - // Called by the client after SearchCommitted() registers listeners, - // to indicate that we've received the "load now pending" notification. - // |nav_helper| is the GoogleURLTrackerNavigationHelper associated with this - // navigation; |infobar_manager| is the InfoBarManager of the associated tab; - // and |pending_id| is the unique ID of the newly pending NavigationEntry. - // If there is already a visible GoogleURLTracker infobar for this tab, this - // function resets its associated pending entry ID to the new ID. Otherwise - // this function creates a map entry for the associated tab. - virtual void OnNavigationPending( - scoped_ptr nav_helper, - infobars::InfoBarManager* infobar_manager, - int pending_id); - - // Called by the navigation observer once a load we're watching commits. - // |infobar_manager| is the same as for OnNavigationPending(); - // |search_url| is guaranteed to be valid. - virtual void OnNavigationCommitted(infobars::InfoBarManager* infobar_manager, - const GURL& search_url); - - // Called by the navigation observer when a tab closes. - virtual void OnTabClosed(GoogleURLTrackerNavigationHelper* nav_helper); - scoped_ptr RegisterCallback( const OnGoogleURLUpdatedCallback& cb); @@ -122,9 +77,6 @@ class GoogleURLTracker friend class GoogleURLTrackerTest; friend class SyncTest; - typedef std::map - EntryMap; - static const char kSearchDomainCheckURL[]; // net::URLFetcherDelegate: @@ -137,9 +89,7 @@ class GoogleURLTracker // KeyedService: void Shutdown() override; - // Registers consumer interest in getting an updated URL from the server. - // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL - // changes. + // Sets |need_to_fetch_| and attempts to start a fetch. void SetNeedToFetch(); // Called when the five second startup sleep has finished. Runs any pending @@ -150,29 +100,11 @@ class GoogleURLTracker // it and can currently do so. void StartFetchIfDesirable(); - // Closes all map entries. If |redo_searches| is true, this also triggers - // each tab with an infobar to re-perform the user's search, but on the new - // Google TLD. - void CloseAllEntries(bool redo_searches); - - // Unregisters any listeners for the navigation helper in |map_entry|. - // This sanity-DCHECKs that these are registered (or not) in the specific - // cases we expect. (|must_be_listening_for_commit| is used purely for this - // sanity-checking.) This also unregisters the global navigation pending - // listener if there are no remaining listeners for navigation commits, as we - // no longer need them until another search is committed. - void UnregisterForEntrySpecificNotifications( - GoogleURLTrackerMapEntry* map_entry, - bool must_be_listening_for_commit); - - void NotifyGoogleURLUpdated(); - CallbackList callback_list_; scoped_ptr client_; GURL google_url_; - GURL fetched_google_url_; scoped_ptr fetcher_; int fetcher_id_; bool in_startup_sleep_; // True if we're in the five-second "no fetching" @@ -184,12 +116,6 @@ class GoogleURLTracker // bother to fetch anything. // Consumers should register a callback via // RegisterCallback(). - bool need_to_prompt_; // True if the last fetched Google URL is not - // matched with current user's default Google URL - // nor the last prompted Google URL. - bool search_committed_; // True when we're expecting a notification of a new - // pending search navigation. - EntryMap entry_map_; base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); diff --git a/components/google/core/browser/google_url_tracker_client.h b/components/google/core/browser/google_url_tracker_client.h index 45c101b5fabf..58f51a7d74ba 100644 --- a/components/google/core/browser/google_url_tracker_client.h +++ b/components/google/core/browser/google_url_tracker_client.h @@ -26,14 +26,6 @@ class GoogleURLTrackerClient { google_url_tracker_ = google_url_tracker; } - // Enables or disables listening for navigation starts. OnNavigationPending - // will be called for each navigation start if listening is enabled. - virtual void SetListeningForNavigationStart(bool listen) = 0; - - // Returns whether or not the client is currently listening for navigation - // starts. - virtual bool IsListeningForNavigationStart() = 0; - // Returns whether background networking is enabled. virtual bool IsBackgroundNetworkingEnabled() = 0; diff --git a/components/google/core/browser/google_url_tracker_infobar_delegate.cc b/components/google/core/browser/google_url_tracker_infobar_delegate.cc deleted file mode 100644 index be8f4b1fa482..000000000000 --- a/components/google/core/browser/google_url_tracker_infobar_delegate.cc +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/google/core/browser/google_url_tracker_infobar_delegate.h" - -#include "components/google/core/browser/google_url_tracker.h" -#include "components/google/core/browser/google_url_tracker_navigation_helper.h" -#include "components/infobars/core/infobar.h" -#include "components/infobars/core/infobar_manager.h" -#include "grit/components_strings.h" -#include "net/base/net_util.h" -#include "ui/base/l10n/l10n_util.h" - - -// static -infobars::InfoBar* GoogleURLTrackerInfoBarDelegate::Create( - infobars::InfoBarManager* infobar_manager, - GoogleURLTracker* google_url_tracker, - const GURL& search_url) { - return infobar_manager->AddInfoBar(infobar_manager->CreateConfirmInfoBar( - scoped_ptr(new GoogleURLTrackerInfoBarDelegate( - google_url_tracker, search_url)))); -} - -bool GoogleURLTrackerInfoBarDelegate::Accept() { - google_url_tracker_->AcceptGoogleURL(true); - return false; -} - -bool GoogleURLTrackerInfoBarDelegate::Cancel() { - google_url_tracker_->CancelGoogleURL(); - return false; -} - -void GoogleURLTrackerInfoBarDelegate::Update(const GURL& search_url) { - StoreActiveEntryUniqueID(); - search_url_ = search_url; - pending_id_ = 0; -} - -void GoogleURLTrackerInfoBarDelegate::Close(bool redo_search) { - // Calling OpenURL() will auto-close us asynchronously. It's easier for - // various classes (e.g. GoogleURLTrackerMapEntry) to reason about things if - // the closure always happens synchronously, so we always call RemoveInfoBar() - // directly, then OpenURL() if desirable. (This calling order is safer if - // for some reason in the future OpenURL() were to close us synchronously.) - GURL new_search_url; - if (redo_search) { - // Re-do the user's search on the new domain. - DCHECK(search_url_.is_valid()); - url::Replacements replacements; - const std::string& host(google_url_tracker_->fetched_google_url().host()); - replacements.SetHost(host.data(), url::Component(0, host.length())); - new_search_url = search_url_.ReplaceComponents(replacements); - } - - // Take ownership of |navigation_helper_| in order to ensure that it stays - // alive for the duration of this method. - scoped_ptr navigation_helper = - navigation_helper_.Pass(); - infobar()->RemoveSelf(); - // WARNING: |this| may be deleted at this point! Do not access any members! - - if (new_search_url.is_valid()) - navigation_helper->OpenURL(new_search_url, CURRENT_TAB, false); -} - -GoogleURLTrackerInfoBarDelegate::GoogleURLTrackerInfoBarDelegate( - GoogleURLTracker* google_url_tracker, - const GURL& search_url) - : ConfirmInfoBarDelegate(), - google_url_tracker_(google_url_tracker), - navigation_helper_weak_ptr_(NULL), - search_url_(search_url), - pending_id_(0) { -} - -GoogleURLTrackerInfoBarDelegate::~GoogleURLTrackerInfoBarDelegate() { -} - -base::string16 GoogleURLTrackerInfoBarDelegate::GetMessageText() const { - return l10n_util::GetStringFUTF16( - IDS_GOOGLE_URL_TRACKER_INFOBAR_MESSAGE, - net::StripWWWFromHost(google_url_tracker_->fetched_google_url()), - net::StripWWWFromHost(google_url_tracker_->google_url())); -} - -base::string16 GoogleURLTrackerInfoBarDelegate::GetButtonLabel( - InfoBarButton button) const { - if (button == BUTTON_OK) { - return l10n_util::GetStringFUTF16( - IDS_GOOGLE_URL_TRACKER_INFOBAR_SWITCH, - net::StripWWWFromHost(google_url_tracker_->fetched_google_url())); - } - return l10n_util::GetStringFUTF16( - IDS_GOOGLE_URL_TRACKER_INFOBAR_DONT_SWITCH, - net::StripWWWFromHost(google_url_tracker_->google_url())); -} - -base::string16 GoogleURLTrackerInfoBarDelegate::GetLinkText() const { - return l10n_util::GetStringUTF16(IDS_GOOGLE_URL_TRACKER_INFOBAR_LEARN_MORE); -} - -bool GoogleURLTrackerInfoBarDelegate::LinkClicked( - WindowOpenDisposition disposition) { - navigation_helper_->OpenURL( - GURL("https://www.google.com/support/chrome/bin/answer.py?" - "answer=1618699"), - (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, - true); - return false; -} - -bool GoogleURLTrackerInfoBarDelegate::ShouldExpireInternal( - const NavigationDetails& details) const { - return (details.entry_id != contents_unique_id()) && - (details.entry_id != pending_id_); -} diff --git a/components/google/core/browser/google_url_tracker_infobar_delegate.h b/components/google/core/browser/google_url_tracker_infobar_delegate.h deleted file mode 100644 index dac9874c0e23..000000000000 --- a/components/google/core/browser/google_url_tracker_infobar_delegate.h +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_INFOBAR_DELEGATE_H_ -#define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_INFOBAR_DELEGATE_H_ - -#include "components/infobars/core/confirm_infobar_delegate.h" -#include "url/gurl.h" - -class GoogleURLTracker; -class GoogleURLTrackerNavigationHelper; - -namespace infobars { -class InfoBarManager; -} - -// This infobar is shown by the GoogleURLTracker when the Google base URL has -// changed. -class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { - public: - // Creates a Google URL tracker infobar and delegate and adds the infobar to - // |infobar_manager|. Returns the infobar if it was successfully added. - static infobars::InfoBar* Create( - infobars::InfoBarManager* infobar_manager, - GoogleURLTracker* google_url_tracker, - const GURL& search_url); - - // ConfirmInfoBarDelegate: - bool Accept() override; - bool Cancel() override; - - GoogleURLTrackerNavigationHelper* navigation_helper() { - return navigation_helper_weak_ptr_; - } - - void set_navigation_helper( - scoped_ptr navigation_helper) { - navigation_helper_ = navigation_helper.Pass(); - navigation_helper_weak_ptr_ = navigation_helper_.get(); - } - - // Other than set_pending_id(), these accessors are only used by test code. - const GURL& search_url() const { return search_url_; } - void set_search_url(const GURL& search_url) { search_url_ = search_url; } - int pending_id() const { return pending_id_; } - void set_pending_id(int pending_id) { pending_id_ = pending_id; } - - // These are virtual so test code can override them in a subclass. - virtual void Update(const GURL& search_url); - virtual void Close(bool redo_search); - - protected: - GoogleURLTrackerInfoBarDelegate( - GoogleURLTracker* google_url_tracker, - const GURL& search_url); - ~GoogleURLTrackerInfoBarDelegate() override; - - private: - // ConfirmInfoBarDelegate: - base::string16 GetMessageText() const override; - base::string16 GetButtonLabel(InfoBarButton button) const override; - base::string16 GetLinkText() const override; - bool LinkClicked(WindowOpenDisposition disposition) override; - bool ShouldExpireInternal(const NavigationDetails& details) const override; - - GoogleURLTracker* google_url_tracker_; - scoped_ptr navigation_helper_; - - // During Close(), this object gives up ownership of |navigation_helper_|, - // which then outlives this object. Sometimes after this point, other classes - // still attempt to call navigation_helper() to access the (still-valid) - // instance. The NavigationHelper instance is stored as a weak pointer in - // addition to a strong pointer to facilitate this case. - GoogleURLTrackerNavigationHelper* navigation_helper_weak_ptr_; - GURL search_url_; - int pending_id_; - - DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); -}; - -#endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_INFOBAR_DELEGATE_H_ diff --git a/components/google/core/browser/google_url_tracker_map_entry.cc b/components/google/core/browser/google_url_tracker_map_entry.cc deleted file mode 100644 index ef7228153f15..000000000000 --- a/components/google/core/browser/google_url_tracker_map_entry.cc +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/google/core/browser/google_url_tracker_map_entry.h" - -#include "components/google/core/browser/google_url_tracker.h" -#include "components/infobars/core/infobar.h" - -GoogleURLTrackerMapEntry::GoogleURLTrackerMapEntry( - GoogleURLTracker* google_url_tracker, - infobars::InfoBarManager* infobar_manager, - scoped_ptr navigation_helper) - : google_url_tracker_(google_url_tracker), - infobar_manager_(infobar_manager), - infobar_delegate_(NULL), - navigation_helper_(navigation_helper.Pass()), - observing_(false) { - DCHECK(infobar_manager_); -} - -GoogleURLTrackerMapEntry::~GoogleURLTrackerMapEntry() { - if (observing_) - infobar_manager_->RemoveObserver(this); -} - -void GoogleURLTrackerMapEntry::SetInfoBarDelegate( - GoogleURLTrackerInfoBarDelegate* infobar_delegate) { - DCHECK(!infobar_delegate_); - - // Transfer ownership of |navigation_helper_| to the infobar delegate as the - // infobar delegate has need of it after this object has been destroyed in - // the case where the user accepts the new Google URL. - infobar_delegate->set_navigation_helper(navigation_helper_.Pass()); - infobar_delegate_ = infobar_delegate; - infobar_manager_->AddObserver(this); - observing_ = true; -} - -void GoogleURLTrackerMapEntry::Close(bool redo_search) { - if (infobar_delegate_) { - infobar_delegate_->Close(redo_search); - } else { - // WARNING: |infobar_manager_| may point to a deleted object. Do not - // dereference it! See GoogleURLTracker::OnTabClosed(). - google_url_tracker_->DeleteMapEntryForManager(infobar_manager_); - } - // WARNING: At this point |this| has been deleted! -} - -void GoogleURLTrackerMapEntry::OnInfoBarRemoved(infobars::InfoBar* infobar, - bool animate) { - DCHECK(infobar_delegate_); - if (infobar->delegate() == infobar_delegate_) { - google_url_tracker_->DeleteMapEntryForManager(infobar_manager_); - // WARNING: At this point |this| has been deleted! - } -} - -void GoogleURLTrackerMapEntry::OnManagerShuttingDown( - infobars::InfoBarManager* manager) { - DCHECK(observing_); - DCHECK_EQ(infobar_manager_, manager); - manager->RemoveObserver(this); - observing_ = false; -} diff --git a/components/google/core/browser/google_url_tracker_map_entry.h b/components/google/core/browser/google_url_tracker_map_entry.h deleted file mode 100644 index b19b224c1f26..000000000000 --- a/components/google/core/browser/google_url_tracker_map_entry.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_MAP_ENTRY_H_ -#define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_MAP_ENTRY_H_ - -#include "base/memory/scoped_ptr.h" -#include "components/google/core/browser/google_url_tracker_infobar_delegate.h" -#include "components/google/core/browser/google_url_tracker_navigation_helper.h" -#include "components/infobars/core/infobar_manager.h" - -class GoogleURLTracker; - -namespace infobars { -class InfoBarManager; -} - -class GoogleURLTrackerMapEntry : public infobars::InfoBarManager::Observer { - public: - GoogleURLTrackerMapEntry( - GoogleURLTracker* google_url_tracker, - infobars::InfoBarManager* infobar_manager, - scoped_ptr navigation_helper); - ~GoogleURLTrackerMapEntry() override; - - bool has_infobar_delegate() const { return !!infobar_delegate_; } - GoogleURLTrackerInfoBarDelegate* infobar_delegate() { - return infobar_delegate_; - } - void SetInfoBarDelegate(GoogleURLTrackerInfoBarDelegate* infobar_delegate); - - GoogleURLTrackerNavigationHelper* navigation_helper() { - // This object gives ownership of |navigation_helper_| to the infobar - // delegate in SetInfoBarDelegate(). - return has_infobar_delegate() ? - infobar_delegate_->navigation_helper() : navigation_helper_.get(); - } - - const infobars::InfoBarManager* infobar_manager() const { - return infobar_manager_; - } - - void Close(bool redo_search); - - private: - friend class GoogleURLTrackerTest; - - // infobars::InfoBarManager::Observer: - void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override; - void OnManagerShuttingDown(infobars::InfoBarManager* manager) override; - - GoogleURLTracker* const google_url_tracker_; - infobars::InfoBarManager* const infobar_manager_; - GoogleURLTrackerInfoBarDelegate* infobar_delegate_; - scoped_ptr navigation_helper_; - bool observing_; - - DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerMapEntry); -}; - -#endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_MAP_ENTRY_H_ diff --git a/components/google/core/browser/google_url_tracker_navigation_helper.cc b/components/google/core/browser/google_url_tracker_navigation_helper.cc deleted file mode 100644 index c420da6d3bb2..000000000000 --- a/components/google/core/browser/google_url_tracker_navigation_helper.cc +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/google/core/browser/google_url_tracker_navigation_helper.h" - -GoogleURLTrackerNavigationHelper::GoogleURLTrackerNavigationHelper( - GoogleURLTracker* google_url_tracker) - : google_url_tracker_(google_url_tracker) { -} - -GoogleURLTrackerNavigationHelper::~GoogleURLTrackerNavigationHelper() { -} diff --git a/components/google/core/browser/google_url_tracker_navigation_helper.h b/components/google/core/browser/google_url_tracker_navigation_helper.h deleted file mode 100644 index 2f643205e821..000000000000 --- a/components/google/core/browser/google_url_tracker_navigation_helper.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_NAVIGATION_HELPER_H_ -#define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_NAVIGATION_HELPER_H_ - -#include "base/macros.h" -#include "ui/base/window_open_disposition.h" - -class GoogleURLTracker; -class GURL; - -// Interface via which GoogleURLTracker communicates with its driver. -// TODO(blundell): Rename this class to GoogleURLTrackerDriver. -// crbug.com/373221 -class GoogleURLTrackerNavigationHelper { - public: - explicit GoogleURLTrackerNavigationHelper( - GoogleURLTracker* google_url_tracker); - virtual ~GoogleURLTrackerNavigationHelper(); - - // Enables or disables listening for navigation commits. - // OnNavigationCommitted will be called for each navigation commit if - // listening is enabled. - virtual void SetListeningForNavigationCommit(bool listen) = 0; - - // Returns whether or not this object is currently listening for navigation - // commits. - virtual bool IsListeningForNavigationCommit() = 0; - - // Enables or disables listening for tab destruction. OnTabClosed will be - // called on tab destruction if listening is enabled. - virtual void SetListeningForTabDestruction(bool listen) = 0; - - // Returns whether or not this object is currently listening for tab - // destruction. - virtual bool IsListeningForTabDestruction() = 0; - - // Opens |url| with the given window disposition. - virtual void OpenURL(GURL url, - WindowOpenDisposition disposition, - bool user_clicked_on_link) = 0; - - protected: - GoogleURLTracker* google_url_tracker() { return google_url_tracker_; } - - private: - GoogleURLTracker* google_url_tracker_; - - DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerNavigationHelper); -}; - -#endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_NAVIGATION_HELPER_H_ diff --git a/components/google/core/browser/google_url_tracker_unittest.cc b/components/google/core/browser/google_url_tracker_unittest.cc dissimilarity index 75% index 184468a7977a..a89ddedbb287 100644 --- a/components/google/core/browser/google_url_tracker_unittest.cc +++ b/components/google/core/browser/google_url_tracker_unittest.cc @@ -1,1063 +1,345 @@ -// Copyright 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/google/core/browser/google_url_tracker.h" - -#include -#include - -#include "base/memory/scoped_ptr.h" -#include "base/message_loop/message_loop.h" -#include "base/prefs/pref_registry_simple.h" -#include "base/prefs/pref_service.h" -#include "base/prefs/testing_pref_service.h" -#include "components/google/core/browser/google_pref_names.h" -#include "components/google/core/browser/google_url_tracker_client.h" -#include "components/google/core/browser/google_url_tracker_infobar_delegate.h" -#include "components/google/core/browser/google_url_tracker_navigation_helper.h" -#include "components/infobars/core/confirm_infobar_delegate.h" -#include "components/infobars/core/infobar.h" -#include "components/infobars/core/infobar_delegate.h" -#include "net/url_request/test_url_fetcher_factory.h" -#include "net/url_request/url_fetcher.h" -#include "net/url_request/url_request_test_util.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -// TestCallbackListener --------------------------------------------------- - -class TestCallbackListener { - public: - TestCallbackListener(); - virtual ~TestCallbackListener(); - - bool HasRegisteredCallback(); - void RegisterCallback(GoogleURLTracker* google_url_tracker); - - bool notified() const { return notified_; } - void clear_notified() { notified_ = false; } - - private: - void OnGoogleURLUpdated(); - - bool notified_; - scoped_ptr google_url_updated_subscription_; -}; - -TestCallbackListener::TestCallbackListener() : notified_(false) { -} - -TestCallbackListener::~TestCallbackListener() { -} - -void TestCallbackListener::OnGoogleURLUpdated() { - notified_ = true; -} - -bool TestCallbackListener::HasRegisteredCallback() { - return google_url_updated_subscription_.get(); -} - -void TestCallbackListener::RegisterCallback( - GoogleURLTracker* google_url_tracker) { - google_url_updated_subscription_ = - google_url_tracker->RegisterCallback(base::Bind( - &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this))); -} - - -// TestGoogleURLTrackerClient ------------------------------------------------- - -class TestGoogleURLTrackerClient : public GoogleURLTrackerClient { - public: - explicit TestGoogleURLTrackerClient(PrefService* prefs_); - ~TestGoogleURLTrackerClient() override; - - void SetListeningForNavigationStart(bool listen) override; - bool IsListeningForNavigationStart() override; - bool IsBackgroundNetworkingEnabled() override; - PrefService* GetPrefs() override; - net::URLRequestContextGetter* GetRequestContext() override; - - private: - PrefService* prefs_; - scoped_refptr request_context_; - bool observe_nav_start_; - - DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient); -}; - -TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs) - : prefs_(prefs), - request_context_(new net::TestURLRequestContextGetter( - base::MessageLoopProxy::current())), - observe_nav_start_(false) { -} - -TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() { -} - -void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) { - observe_nav_start_ = listen; -} - -bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() { - return observe_nav_start_; -} - -bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { - return true; -} - -PrefService* TestGoogleURLTrackerClient::GetPrefs() { - return prefs_; -} - -net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() { - return request_context_.get(); -} - - -// TestGoogleURLTrackerNavigationHelper --------------------------------------- - -class TestGoogleURLTrackerNavigationHelper - : public GoogleURLTrackerNavigationHelper { - public: - explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker); - ~TestGoogleURLTrackerNavigationHelper() override; - - void SetListeningForNavigationCommit(bool listen) override; - bool IsListeningForNavigationCommit() override; - void SetListeningForTabDestruction(bool listen) override; - bool IsListeningForTabDestruction() override; - void OpenURL(GURL url, - WindowOpenDisposition disposition, - bool user_clicked_on_link) override; - - private: - bool listening_for_nav_commit_; - bool listening_for_tab_destruction_; - - DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerNavigationHelper); -}; - -TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper( - GoogleURLTracker* tracker) - : GoogleURLTrackerNavigationHelper(tracker), - listening_for_nav_commit_(false), - listening_for_tab_destruction_(false) { -} - -TestGoogleURLTrackerNavigationHelper::~TestGoogleURLTrackerNavigationHelper() { -} - -void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit( - bool listen) { - listening_for_nav_commit_ = listen; -} - -bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit() { - return listening_for_nav_commit_; -} - -void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction( - bool listen) { - listening_for_tab_destruction_ = listen; -} - -bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() { - return listening_for_tab_destruction_; -} - -void TestGoogleURLTrackerNavigationHelper::OpenURL( - GURL url, - WindowOpenDisposition disposition, - bool user_clicked_on_link) { -} - -// TestInfoBarManager --------------------------------------------------------- - -class TestInfoBarManager : public infobars::InfoBarManager { - public: - explicit TestInfoBarManager(int unique_id); - ~TestInfoBarManager() override; - int GetActiveEntryID() override; - - private: - // infobars::InfoBarManager: - scoped_ptr CreateConfirmInfoBar( - scoped_ptr delegate) override; - - int unique_id_; - DISALLOW_COPY_AND_ASSIGN(TestInfoBarManager); -}; - -TestInfoBarManager::TestInfoBarManager(int unique_id) : unique_id_(unique_id) { -} - -TestInfoBarManager::~TestInfoBarManager() { - ShutDown(); -} - -int TestInfoBarManager::GetActiveEntryID() { - return unique_id_; -} - -scoped_ptr TestInfoBarManager::CreateConfirmInfoBar( - scoped_ptr delegate) { - return make_scoped_ptr(new infobars::InfoBar(delegate.Pass())); -} - -} // namespace - -// GoogleURLTrackerTest ------------------------------------------------------- - -class GoogleURLTrackerTest : public testing::Test { - protected: - GoogleURLTrackerTest(); - ~GoogleURLTrackerTest() override; - - // testing::Test - void SetUp() override; - void TearDown() override; - - net::TestURLFetcher* GetFetcher(); - void MockSearchDomainCheckResponse(const std::string& domain); - void RequestServerCheck(); - void FinishSleep(); - void NotifyNetworkChanged(); - GURL fetched_google_url() const { - return google_url_tracker_->fetched_google_url(); - } - void set_google_url(const GURL& url) { - google_url_tracker_->google_url_ = url; - } - GURL google_url() const { return google_url_tracker_->google_url(); } - void SetLastPromptedGoogleURL(const GURL& url); - GURL GetLastPromptedGoogleURL(); - void SetNavigationPending(infobars::InfoBarManager* infobar_manager, - bool is_search); - void CommitNonSearch(infobars::InfoBarManager* infobar_manager); - void CommitSearch(infobars::InfoBarManager* infobar_manager, - const GURL& search_url); - void CloseTab(infobars::InfoBarManager* infobar_manager); - GoogleURLTrackerMapEntry* GetMapEntry( - infobars::InfoBarManager* infobar_manager); - GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate( - infobars::InfoBarManager* infobar_manager); - GoogleURLTrackerNavigationHelper* GetNavigationHelper( - infobars::InfoBarManager* infobar_manager); - void ExpectDefaultURLs() const; - void ExpectListeningForCommit(infobars::InfoBarManager* infobar_manager, - bool listening); - bool listener_notified() const { return listener_.notified(); } - void clear_listener_notified() { listener_.clear_notified(); } - - private: - base::MessageLoop message_loop_; - TestingPrefServiceSimple prefs_; - - // Creating this allows us to call - // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(). - scoped_ptr network_change_notifier_; - net::TestURLFetcherFactory fetcher_factory_; - GoogleURLTrackerClient* client_; - scoped_ptr google_url_tracker_; - TestCallbackListener listener_; - // This tracks the different "tabs" a test has "opened", so we can close them - // properly before shutting down |google_url_tracker_|, which expects that. - std::set infobar_managers_seen_; -}; - -GoogleURLTrackerTest::GoogleURLTrackerTest() { - prefs_.registry()->RegisterStringPref( - prefs::kLastKnownGoogleURL, - GoogleURLTracker::kDefaultGoogleHomepage); - prefs_.registry()->RegisterStringPref( - prefs::kLastPromptedGoogleURL, - std::string()); -} - -GoogleURLTrackerTest::~GoogleURLTrackerTest() { -} - -void GoogleURLTrackerTest::SetUp() { - network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); - // Ownership is passed to google_url_tracker_, but a weak pointer is kept; - // this is safe since GoogleURLTracker keeps the client for its lifetime. - client_ = new TestGoogleURLTrackerClient(&prefs_); - scoped_ptr client(client_); - google_url_tracker_.reset(new GoogleURLTracker( - client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); -} - -void GoogleURLTrackerTest::TearDown() { - while (!infobar_managers_seen_.empty()) - CloseTab(*infobar_managers_seen_.begin()); - google_url_tracker_->Shutdown(); -} - -net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { - // This will return the last fetcher created. If no fetchers have been - // created, we'll pass GetFetcherByID() "-1", and it will return NULL. - return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); -} - -void GoogleURLTrackerTest::MockSearchDomainCheckResponse( - const std::string& domain) { - net::TestURLFetcher* fetcher = GetFetcher(); - if (!fetcher) - return; - fetcher_factory_.RemoveFetcherFromMap(fetcher->id()); - fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL)); - fetcher->set_response_code(200); - fetcher->SetResponseString(domain); - fetcher->delegate()->OnURLFetchComplete(fetcher); - // At this point, |fetcher| is deleted. -} - -void GoogleURLTrackerTest::RequestServerCheck() { - if (!listener_.HasRegisteredCallback()) - listener_.RegisterCallback(google_url_tracker_.get()); - google_url_tracker_->SetNeedToFetch(); -} - -void GoogleURLTrackerTest::FinishSleep() { - google_url_tracker_->FinishSleep(); -} - -void GoogleURLTrackerTest::NotifyNetworkChanged() { - net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( - net::NetworkChangeNotifier::CONNECTION_UNKNOWN); - // For thread safety, the NCN queues tasks to do the actual notifications, so - // we need to spin the message loop so the tracker will actually be notified. - base::MessageLoop::current()->RunUntilIdle(); -} - -void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { - prefs_.SetString(prefs::kLastPromptedGoogleURL, url.spec()); -} - -GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { - return GURL(prefs_.GetString(prefs::kLastPromptedGoogleURL)); -} - -void GoogleURLTrackerTest::SetNavigationPending( - infobars::InfoBarManager* infobar_manager, - bool is_search) { - if (is_search) { - google_url_tracker_->SearchCommitted(); - // Note that the call above might not have actually registered a listener - // for navigation starts if the searchdomaincheck response was bogus. - } - infobar_managers_seen_.insert(infobar_manager); - if (client_->IsListeningForNavigationStart()) { - google_url_tracker_->OnNavigationPending( - scoped_ptr( - new TestGoogleURLTrackerNavigationHelper( - google_url_tracker_.get())), - infobar_manager, - infobar_manager->GetActiveEntryID()); - } -} - -void GoogleURLTrackerTest::CommitNonSearch( - infobars::InfoBarManager* infobar_manager) { - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); - if (!map_entry) - return; - - ExpectListeningForCommit(infobar_manager, false); - - // The infobar should be showing; otherwise the pending non-search should - // have closed it. - ASSERT_TRUE(map_entry->has_infobar_delegate()); - - // The pending_id should have been reset to 0 when the non-search became - // pending. - EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); - - // Committing the navigation would close the infobar. - map_entry->infobar_delegate()->Close(false); -} - -void GoogleURLTrackerTest::CommitSearch( - infobars::InfoBarManager* infobar_manager, - const GURL& search_url) { - DCHECK(search_url.is_valid()); - GoogleURLTrackerNavigationHelper* nav_helper = - GetNavigationHelper(infobar_manager); - if (nav_helper && nav_helper->IsListeningForNavigationCommit()) { - google_url_tracker_->OnNavigationCommitted(infobar_manager, search_url); - } -} - -void GoogleURLTrackerTest::CloseTab(infobars::InfoBarManager* infobar_manager) { - infobar_managers_seen_.erase(infobar_manager); - GoogleURLTrackerNavigationHelper* nav_helper = - GetNavigationHelper(infobar_manager); - if (nav_helper && nav_helper->IsListeningForTabDestruction()) { - google_url_tracker_->OnTabClosed(nav_helper); - } else { - // Closing a tab with an infobar showing would close the infobar. - GoogleURLTrackerInfoBarDelegate* delegate = - GetInfoBarDelegate(infobar_manager); - if (delegate) - delegate->Close(false); - } -} - -GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( - infobars::InfoBarManager* infobar_manager) { - GoogleURLTracker::EntryMap::const_iterator i = - google_url_tracker_->entry_map_.find(infobar_manager); - return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; -} - -GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( - infobars::InfoBarManager* infobar_manager) { - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); - return map_entry ? map_entry->infobar_delegate() : NULL; -} - -GoogleURLTrackerNavigationHelper* GoogleURLTrackerTest::GetNavigationHelper( - infobars::InfoBarManager* infobar_manager) { - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); - return map_entry ? map_entry->navigation_helper() : NULL; -} - -void GoogleURLTrackerTest::ExpectDefaultURLs() const { - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL(), fetched_google_url()); -} - -void GoogleURLTrackerTest::ExpectListeningForCommit( - infobars::InfoBarManager* infobar_manager, - bool listening) { - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); - if (map_entry) { - EXPECT_EQ(listening, - map_entry->navigation_helper()->IsListeningForNavigationCommit()); - } else { - EXPECT_FALSE(listening); - } -} - -// Tests ---------------------------------------------------------------------- - -TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { - ExpectDefaultURLs(); - FinishSleep(); - // No one called RequestServerCheck() so nothing should have happened. - EXPECT_FALSE(GetFetcher()); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - ExpectDefaultURLs(); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) { - RequestServerCheck(); - EXPECT_FALSE(GetFetcher()); - ExpectDefaultURLs(); - EXPECT_FALSE(listener_notified()); - - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - // GoogleURL should be updated, becase there was no last prompted URL. - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_TRUE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) { - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcher()); - ExpectDefaultURLs(); - EXPECT_FALSE(listener_notified()); - - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - // GoogleURL should not be updated, because the fetched and prompted URLs - // match. - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcher()); - ExpectDefaultURLs(); - EXPECT_FALSE(listener_notified()); - - // Old-style domain string. - FinishSleep(); - MockSearchDomainCheckResponse(".google.co.in"); - EXPECT_EQ(GURL(), fetched_google_url()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // Bad subdomain. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://mail.google.com/"); - EXPECT_EQ(GURL(), fetched_google_url()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // Non-empty path. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.com/search"); - EXPECT_EQ(GURL(), fetched_google_url()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // Non-empty query. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.com/?q=foo"); - EXPECT_EQ(GURL(), fetched_google_url()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // Non-empty ref. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.com/#anchor"); - EXPECT_EQ(GURL(), fetched_google_url()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // Complete garbage. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("HJ)*qF)_*&@f1"); - EXPECT_EQ(GURL(), fetched_google_url()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_FALSE(listener_notified()); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); -} - -TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) { - SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); - set_google_url(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, SilentlyAcceptSchemeChange) { - // We should auto-accept changes to the current Google URL that merely change - // the scheme, regardless of what the last prompted URL was. - SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); - set_google_url(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("https://www.google.co.uk/"); - EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("https://www.google.co.uk/"), google_url()); - EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(listener_notified()); - - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) { - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_TRUE(listener_notified()); - clear_listener_notified(); - - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.in/"); - EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url()); - // Just fetching a new URL shouldn't reset things without a prompt. - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) { - FinishSleep(); - NotifyNetworkChanged(); - // No one called RequestServerCheck() so nothing should have happened. - EXPECT_FALSE(GetFetcher()); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - ExpectDefaultURLs(); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) { - FinishSleep(); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - RequestServerCheck(); - // The first request for a check should trigger a fetch if it hasn't happened - // already. - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_TRUE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) { - FinishSleep(); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - RequestServerCheck(); - // The first request for a check should trigger a fetch if it hasn't happened - // already. - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_TRUE(listener_notified()); - clear_listener_notified(); - - RequestServerCheck(); - // The second request should be ignored. - EXPECT_FALSE(GetFetcher()); - MockSearchDomainCheckResponse("http://www.google.co.in/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) { - TestInfoBarManager infobar_manager(1); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(listener_notified()); - clear_listener_notified(); - - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); - - SetNavigationPending(&infobar_manager, true); - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); - ASSERT_FALSE(map_entry == NULL); - EXPECT_FALSE(map_entry->has_infobar_delegate()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); - - CloseTab(&infobar_manager); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - - CloseTab(&infobar_manager); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, InfoBarClosed) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - GoogleURLTrackerInfoBarDelegate* infobar = - GetInfoBarDelegate(&infobar_manager); - ASSERT_FALSE(infobar == NULL); - - infobar->Close(false); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, InfoBarRefused) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - GoogleURLTrackerInfoBarDelegate* infobar = - GetInfoBarDelegate(&infobar_manager); - ASSERT_FALSE(infobar == NULL); - - infobar->Cancel(); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, InfoBarAccepted) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - GoogleURLTrackerInfoBarDelegate* infobar = - GetInfoBarDelegate(&infobar_manager); - ASSERT_FALSE(infobar == NULL); - - infobar->Accept(); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) { - TestInfoBarManager infobar_manager(1); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse(google_url().spec()); - - // Re-fetching the accepted URL after showing an infobar for another URL - // should close the infobar. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse(google_url().spec()); - EXPECT_EQ(google_url(), GetLastPromptedGoogleURL()); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // As should fetching a URL that differs from the accepted only by the scheme. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - NotifyNetworkChanged(); - url::Replacements replacements; - const std::string& scheme("https"); - replacements.SetScheme(scheme.data(), url::Component(0, scheme.length())); - GURL new_google_url(google_url().ReplaceComponents(replacements)); - MockSearchDomainCheckResponse(new_google_url.spec()); - EXPECT_EQ(new_google_url, GetLastPromptedGoogleURL()); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // As should re-fetching the last prompted URL. - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - EXPECT_EQ(new_google_url, google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // And one that differs from the last prompted URL only by the scheme. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("https://www.google.co.uk/"); - EXPECT_EQ(new_google_url, google_url()); - EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // And fetching a different URL entirely. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("https://www.google.co.in/"); - EXPECT_EQ(new_google_url, google_url()); - EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); -} - -TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) { - TestInfoBarManager infobar_manager(1); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse(google_url().spec()); - - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("http://www.google.co.uk/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); - GoogleURLTrackerInfoBarDelegate* delegate = - GetInfoBarDelegate(&infobar_manager); - ASSERT_FALSE(delegate == NULL); - EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); - - // If while an infobar is showing we fetch a new URL that differs from the - // infobar's only by scheme, the infobar should stay showing. - NotifyNetworkChanged(); - MockSearchDomainCheckResponse("https://www.google.co.uk/"); - EXPECT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url()); -} - -TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - // A pending non-search after a pending search should delete the map entry. - SetNavigationPending(&infobar_manager, true); - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); - ASSERT_FALSE(map_entry == NULL); - EXPECT_FALSE(map_entry->has_infobar_delegate()); - SetNavigationPending(&infobar_manager, false); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // A pending search after a pending search should leave the map entry alive. - SetNavigationPending(&infobar_manager, true); - map_entry = GetMapEntry(&infobar_manager); - ASSERT_FALSE(map_entry == NULL); - EXPECT_FALSE(map_entry->has_infobar_delegate()); - SetNavigationPending(&infobar_manager, true); - ASSERT_EQ(map_entry, GetMapEntry(&infobar_manager)); - EXPECT_FALSE(map_entry->has_infobar_delegate()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); - - // Committing this search should show an infobar. - CommitSearch(&infobar_manager, - GURL("http://www.google.co.uk/search?q=test2")); - EXPECT_TRUE(map_entry->has_infobar_delegate()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); -} - -TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) { - TestInfoBarManager infobar_manager(1); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - GoogleURLTrackerInfoBarDelegate* delegate = - GetInfoBarDelegate(&infobar_manager); - ASSERT_FALSE(delegate == NULL); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); - - // A pending non-search on a visible infobar should basically do nothing. - SetNavigationPending(&infobar_manager, false); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(0, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); - - // As should another pending non-search after the first. - SetNavigationPending(&infobar_manager, false); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(0, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); - - // Committing this non-search should close the infobar. The control flow in - // these tests is not really comparable to in the real browser, but at least a - // few sanity-checks will be performed. - ASSERT_NO_FATAL_FAILURE(CommitNonSearch(&infobar_manager)); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - - // A pending search on a visible infobar should cause the infobar to listen - // for the search to commit. - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - delegate = GetInfoBarDelegate(&infobar_manager); - ASSERT_FALSE(delegate == NULL); - SetNavigationPending(&infobar_manager, true); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(1, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); - - // But a non-search after this should cancel that state. - SetNavigationPending(&infobar_manager, false); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(0, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); - - // Another pending search after the non-search should put us back into - // "waiting for commit" mode. - SetNavigationPending(&infobar_manager, true); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(1, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); - - // A second pending search after the first should not really change anything. - SetNavigationPending(&infobar_manager, true); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(1, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); - - // Committing this search should change the visible infobar's search_url. - CommitSearch(&infobar_manager, - GURL("http://www.google.co.uk/search?q=test2")); - ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); - EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"), - delegate->search_url()); - EXPECT_EQ(0, delegate->pending_id()); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - EXPECT_FALSE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, MultipleMapEntries) { - TestInfoBarManager infobar_manager(1); - TestInfoBarManager infobar_manager2(2); - TestInfoBarManager infobar_manager3(3); - TestInfoBarManager infobar_manager4(4); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - SetNavigationPending(&infobar_manager, true); - GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); - ASSERT_FALSE(map_entry == NULL); - EXPECT_FALSE(map_entry->has_infobar_delegate()); - - SetNavigationPending(&infobar_manager2, true); - CommitSearch(&infobar_manager2, - GURL("http://www.google.co.uk/search?q=test2")); - GoogleURLTrackerInfoBarDelegate* delegate2 = - GetInfoBarDelegate(&infobar_manager2); - ASSERT_FALSE(delegate2 == NULL); - EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"), - delegate2->search_url()); - - SetNavigationPending(&infobar_manager3, true); - GoogleURLTrackerMapEntry* map_entry3 = GetMapEntry(&infobar_manager3); - ASSERT_FALSE(map_entry3 == NULL); - EXPECT_FALSE(map_entry3->has_infobar_delegate()); - - SetNavigationPending(&infobar_manager4, true); - CommitSearch(&infobar_manager4, - GURL("http://www.google.co.uk/search?q=test4")); - GoogleURLTrackerInfoBarDelegate* delegate4 = - GetInfoBarDelegate(&infobar_manager4); - ASSERT_FALSE(delegate4 == NULL); - EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"), - delegate4->search_url()); - - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - EXPECT_TRUE(map_entry->has_infobar_delegate()); - - delegate2->Close(false); - EXPECT_TRUE(GetMapEntry(&infobar_manager2) == NULL); - EXPECT_FALSE(listener_notified()); - - delegate4->Accept(); - EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); - EXPECT_TRUE(GetMapEntry(&infobar_manager3) == NULL); - EXPECT_TRUE(GetMapEntry(&infobar_manager4) == NULL); - EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); - EXPECT_TRUE(listener_notified()); -} - -TEST_F(GoogleURLTrackerTest, IgnoreIrrelevantNavigation) { - TestInfoBarManager infobar_manager(1); - TestInfoBarManager infobar_manager2(2); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse("http://www.google.co.jp/"); - - // This tests a particularly gnarly sequence of events that used to cause us - // to erroneously listen for a non-search navigation to commit. - SetNavigationPending(&infobar_manager, true); - CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); - SetNavigationPending(&infobar_manager2, true); - CommitSearch(&infobar_manager2, - GURL("http://www.google.co.uk/search?q=test2")); - EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); - GoogleURLTrackerInfoBarDelegate* delegate2 = - GetInfoBarDelegate(&infobar_manager2); - ASSERT_FALSE(delegate2 == NULL); - SetNavigationPending(&infobar_manager, true); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); - delegate2->Close(false); - SetNavigationPending(&infobar_manager, false); - ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); -} +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/google/core/browser/google_url_tracker.h" + +#include + +#include "base/memory/scoped_ptr.h" +#include "base/message_loop/message_loop.h" +#include "base/prefs/pref_registry_simple.h" +#include "base/prefs/pref_service.h" +#include "base/prefs/testing_pref_service.h" +#include "components/google/core/browser/google_pref_names.h" +#include "components/google/core/browser/google_url_tracker_client.h" +#include "net/url_request/test_url_fetcher_factory.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_request_test_util.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +// TestCallbackListener --------------------------------------------------- + +class TestCallbackListener { + public: + TestCallbackListener(); + virtual ~TestCallbackListener(); + + bool HasRegisteredCallback(); + void RegisterCallback(GoogleURLTracker* google_url_tracker); + + bool notified() const { return notified_; } + void clear_notified() { notified_ = false; } + + private: + void OnGoogleURLUpdated(); + + bool notified_; + scoped_ptr google_url_updated_subscription_; +}; + +TestCallbackListener::TestCallbackListener() : notified_(false) { +} + +TestCallbackListener::~TestCallbackListener() { +} + +void TestCallbackListener::OnGoogleURLUpdated() { + notified_ = true; +} + +bool TestCallbackListener::HasRegisteredCallback() { + return google_url_updated_subscription_.get(); +} + +void TestCallbackListener::RegisterCallback( + GoogleURLTracker* google_url_tracker) { + google_url_updated_subscription_ = + google_url_tracker->RegisterCallback(base::Bind( + &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this))); +} + + +// TestGoogleURLTrackerClient ------------------------------------------------- + +class TestGoogleURLTrackerClient : public GoogleURLTrackerClient { + public: + explicit TestGoogleURLTrackerClient(PrefService* prefs_); + ~TestGoogleURLTrackerClient() override; + + bool IsBackgroundNetworkingEnabled() override; + PrefService* GetPrefs() override; + net::URLRequestContextGetter* GetRequestContext() override; + + private: + PrefService* prefs_; + scoped_refptr request_context_; + + DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient); +}; + +TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs) + : prefs_(prefs), + request_context_(new net::TestURLRequestContextGetter( + base::MessageLoopProxy::current())) { +} + +TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() { +} + +bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { + return true; +} + +PrefService* TestGoogleURLTrackerClient::GetPrefs() { + return prefs_; +} + +net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() { + return request_context_.get(); +} + + +} // namespace + +// GoogleURLTrackerTest ------------------------------------------------------- + +class GoogleURLTrackerTest : public testing::Test { + protected: + GoogleURLTrackerTest(); + ~GoogleURLTrackerTest() override; + + // testing::Test + void SetUp() override; + void TearDown() override; + + net::TestURLFetcher* GetFetcher(); + void MockSearchDomainCheckResponse(const std::string& domain); + void RequestServerCheck(); + void FinishSleep(); + void NotifyNetworkChanged(); + void set_google_url(const GURL& url) { + google_url_tracker_->google_url_ = url; + } + GURL google_url() const { return google_url_tracker_->google_url(); } + bool listener_notified() const { return listener_.notified(); } + void clear_listener_notified() { listener_.clear_notified(); } + + private: + base::MessageLoop message_loop_; + TestingPrefServiceSimple prefs_; + + // Creating this allows us to call + // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(). + scoped_ptr network_change_notifier_; + net::TestURLFetcherFactory fetcher_factory_; + GoogleURLTrackerClient* client_; + scoped_ptr google_url_tracker_; + TestCallbackListener listener_; +}; + +GoogleURLTrackerTest::GoogleURLTrackerTest() { + prefs_.registry()->RegisterStringPref( + prefs::kLastKnownGoogleURL, + GoogleURLTracker::kDefaultGoogleHomepage); +} + +GoogleURLTrackerTest::~GoogleURLTrackerTest() { +} + +void GoogleURLTrackerTest::SetUp() { + network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); + // Ownership is passed to google_url_tracker_, but a weak pointer is kept; + // this is safe since GoogleURLTracker keeps the client for its lifetime. + client_ = new TestGoogleURLTrackerClient(&prefs_); + scoped_ptr client(client_); + google_url_tracker_.reset(new GoogleURLTracker( + client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); +} + +void GoogleURLTrackerTest::TearDown() { + google_url_tracker_->Shutdown(); +} + +net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { + // This will return the last fetcher created. If no fetchers have been + // created, we'll pass GetFetcherByID() "-1", and it will return NULL. + return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); +} + +void GoogleURLTrackerTest::MockSearchDomainCheckResponse( + const std::string& domain) { + net::TestURLFetcher* fetcher = GetFetcher(); + if (!fetcher) + return; + fetcher_factory_.RemoveFetcherFromMap(fetcher->id()); + fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL)); + fetcher->set_response_code(200); + fetcher->SetResponseString(domain); + fetcher->delegate()->OnURLFetchComplete(fetcher); + // At this point, |fetcher| is deleted. +} + +void GoogleURLTrackerTest::RequestServerCheck() { + if (!listener_.HasRegisteredCallback()) + listener_.RegisterCallback(google_url_tracker_.get()); + google_url_tracker_->SetNeedToFetch(); +} + +void GoogleURLTrackerTest::FinishSleep() { + google_url_tracker_->FinishSleep(); +} + +void GoogleURLTrackerTest::NotifyNetworkChanged() { + net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( + net::NetworkChangeNotifier::CONNECTION_UNKNOWN); + // For thread safety, the NCN queues tasks to do the actual notifications, so + // we need to spin the message loop so the tracker will actually be notified. + base::MessageLoop::current()->RunUntilIdle(); +} + +// Tests ---------------------------------------------------------------------- + +TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { + EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); + FinishSleep(); + // No one called RequestServerCheck() so nothing should have happened. + EXPECT_FALSE(GetFetcher()); + MockSearchDomainCheckResponse("http://www.google.co.uk/"); + EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); + EXPECT_FALSE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, Update) { + RequestServerCheck(); + EXPECT_FALSE(GetFetcher()); + EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); + EXPECT_FALSE(listener_notified()); + + FinishSleep(); + MockSearchDomainCheckResponse("http://www.google.co.uk/"); + EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); + EXPECT_TRUE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) { + GURL original_google_url("http://www.google.co.uk/"); + set_google_url(original_google_url); + + RequestServerCheck(); + EXPECT_FALSE(GetFetcher()); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + FinishSleep(); + MockSearchDomainCheckResponse(original_google_url.spec()); + EXPECT_EQ(original_google_url, google_url()); + // No one should be notified, because the new URL matches the old. + EXPECT_FALSE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) { + GURL original_google_url("http://www.google.co.uk/"); + set_google_url(original_google_url); + + RequestServerCheck(); + EXPECT_FALSE(GetFetcher()); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + // Old-style domain string. + FinishSleep(); + MockSearchDomainCheckResponse(".google.co.in"); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + // Bad subdomain. + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://mail.google.com/"); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + // Non-empty path. + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://www.google.com/search"); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + // Non-empty query. + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://www.google.com/?q=foo"); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + // Non-empty ref. + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://www.google.com/#anchor"); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); + + // Complete garbage. + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("HJ)*qF)_*&@f1"); + EXPECT_EQ(original_google_url, google_url()); + EXPECT_FALSE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) { + RequestServerCheck(); + FinishSleep(); + MockSearchDomainCheckResponse("http://www.google.co.uk/"); + EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); + EXPECT_TRUE(listener_notified()); + clear_listener_notified(); + + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://www.google.co.in/"); + EXPECT_EQ(GURL("http://www.google.co.in/"), google_url()); + EXPECT_TRUE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) { + FinishSleep(); + NotifyNetworkChanged(); + // No one called RequestServerCheck() so nothing should have happened. + EXPECT_FALSE(GetFetcher()); + MockSearchDomainCheckResponse("http://www.google.co.uk/"); + EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); + EXPECT_FALSE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) { + FinishSleep(); + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://www.google.co.jp/"); + + RequestServerCheck(); + // The first request for a check should trigger a fetch if it hasn't happened + // already. + MockSearchDomainCheckResponse("http://www.google.co.uk/"); + EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); + EXPECT_TRUE(listener_notified()); +} + +TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) { + FinishSleep(); + NotifyNetworkChanged(); + MockSearchDomainCheckResponse("http://www.google.co.jp/"); + + RequestServerCheck(); + // The first request for a check should trigger a fetch if it hasn't happened + // already. + MockSearchDomainCheckResponse("http://www.google.co.uk/"); + EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); + EXPECT_TRUE(listener_notified()); + clear_listener_notified(); + + RequestServerCheck(); + // The second request should be ignored. + EXPECT_FALSE(GetFetcher()); + MockSearchDomainCheckResponse("http://www.google.co.in/"); + EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); + EXPECT_FALSE(listener_notified()); +} diff --git a/components/google/core/browser/google_util.cc b/components/google/core/browser/google_util.cc index a1cf2f2c279a..307cebb3f787 100644 --- a/components/google/core/browser/google_util.cc +++ b/components/google/core/browser/google_util.cc @@ -79,7 +79,7 @@ namespace google_util { // Global functions ----------------------------------------------------------- bool HasGoogleSearchQueryParam(const std::string& str) { - url::Component query(0, str.length()), key, value; + url::Component query(0, static_cast(str.length())), key, value; while (url::ExtractQueryKeyValue(str.c_str(), &query, &key, &value)) { if ((key.len == 1) && (str[key.begin] == 'q') && value.is_nonempty()) return true; diff --git a/components/google_strings.grdp b/components/google_strings.grdp deleted file mode 100644 index b5a4a6a67b05..000000000000 --- a/components/google_strings.grdp +++ /dev/null @@ -1,17 +0,0 @@ - - - - - Would you like to search with $1google.de instead of $2google.fr? - - - Switch to $1google.de - - - Keep using $1google.fr - - - Learn more - - - -- 2.11.4.GIT