diff options
author | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-18 17:08:03 +0000 |
---|---|---|
committer | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-18 17:08:03 +0000 |
commit | a7128fa0a1d669745883964e69ff35084ac223ff (patch) | |
tree | 93e34fa8140821c6d71c0efc52ef926def483dc2 | |
parent | dcfd2ec3e43257fe7ec3b6e07faec300aeeda510 (diff) | |
download | chromium_src-a7128fa0a1d669745883964e69ff35084ac223ff.zip chromium_src-a7128fa0a1d669745883964e69ff35084ac223ff.tar.gz chromium_src-a7128fa0a1d669745883964e69ff35084ac223ff.tar.bz2 |
Win IO Perf Regressions
http://build.chromium.org/buildbot/perf/xp-release-dual-core/moz/report.html?history=150&rev=-1&graph=total_byte_b
Revert 56483 - Monitor network change in GoogleURLTracker
BUG=48688,15141
TEST=GoogleURLTrackerTest.MonitorNetworkChange passes
Review URL: http://codereview.chromium.org/3034018
TBR=ukai@chromium.org
Review URL: http://codereview.chromium.org/3176017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56544 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/app/generated_resources.grd | 5 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit.cc | 11 | ||||
-rw-r--r-- | chrome/browser/google_url_tracker.cc | 295 | ||||
-rw-r--r-- | chrome/browser/google_url_tracker.h | 79 | ||||
-rw-r--r-- | chrome/browser/google_url_tracker_unittest.cc | 469 | ||||
-rw-r--r-- | chrome/common/net/url_fetcher_protect.h | 7 | ||||
-rw-r--r-- | chrome/common/pref_names.cc | 6 | ||||
-rw-r--r-- | chrome/common/pref_names.h | 1 | ||||
-rw-r--r-- | chrome/test/testing_browser_process.h | 19 |
9 files changed, 104 insertions, 788 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index 281ce58..f51b30d 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -9120,11 +9120,6 @@ Keep your key file in a safe place. You will need it to create new versions of y </message> </if> - <!-- GoogleURL tracker info bar --> - <message name="IDS_GOOGLE_URL_TRACKER_INFOBAR_MESSAGE" desc="Message displayed when the user's current Google TLD doesn't match the default for their location."> - It looks like you've moved. Would you like to use <ph name="NEW_GOOGLE_URL">$1<ex>google.com</ex></ph>? - </message> - </messages> <structures fallback_to_english="true"> diff --git a/chrome/browser/autocomplete/autocomplete_edit.cc b/chrome/browser/autocomplete/autocomplete_edit.cc index dc4fde3..50321ba 100644 --- a/chrome/browser/autocomplete/autocomplete_edit.cc +++ b/chrome/browser/autocomplete/autocomplete_edit.cc @@ -15,10 +15,8 @@ #include "chrome/browser/autocomplete/autocomplete_edit_view.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/autocomplete/keyword_provider.h" -#include "chrome/browser/browser_list.h" #include "chrome/browser/command_updater.h" #include "chrome/browser/extensions/extension_omnibox_api.h" -#include "chrome/browser/google_url_tracker.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/net/predictor_api.h" #include "chrome/browser/net/url_fixer_upper.h" @@ -322,15 +320,6 @@ void AutocompleteEditModel::AcceptInput(WindowOpenDisposition disposition, match.transition = PageTransition::LINK; } - if (match.type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || - match.type == AutocompleteMatch::SEARCH_HISTORY || - match.type == AutocompleteMatch::SEARCH_SUGGEST) { - const TemplateURL* default_provider = - profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); - if (default_provider && default_provider->url() && - default_provider->url()->HasGoogleBaseURLs()) - GoogleURLTracker::GoogleURLSearchCommitted(); - } view_->OpenURL(match.destination_url, disposition, match.transition, alternate_nav_url, AutocompletePopupModel::kNoMatch, is_keyword_hint_ ? std::wstring() : keyword_); diff --git a/chrome/browser/google_url_tracker.cc b/chrome/browser/google_url_tracker.cc index ea2e225..b703393 100644 --- a/chrome/browser/google_url_tracker.cc +++ b/chrome/browser/google_url_tracker.cc @@ -6,116 +6,31 @@ #include <vector> -#include "app/l10n_util.h" #include "base/compiler_specific.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/pref_service.h" #include "chrome/browser/profile.h" -#include "chrome/browser/search_engines/template_url.h" -#include "chrome/browser/tab_contents/infobar_delegate.h" -#include "chrome/browser/tab_contents/navigation_controller.h" -#include "chrome/browser/tab_contents/tab_contents.h" -#include "chrome/common/net/url_fetcher_protect.h" #include "chrome/common/notification_service.h" #include "chrome/common/pref_names.h" -#include "grit/generated_resources.h" #include "net/base/load_flags.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=domain&type=chrome"; - -namespace { - -class GoogleURLTrackerInfoBarDelegate : public ConfirmInfoBarDelegate { - public: - GoogleURLTrackerInfoBarDelegate(TabContents* tab_contents, - GoogleURLTracker* google_url_tracker, - const GURL& new_google_url) - : ConfirmInfoBarDelegate(tab_contents), - google_url_tracker_(google_url_tracker), - new_google_url_(new_google_url) {} - - // ConfirmInfoBarDelegate - virtual string16 GetMessageText() const { - // TODO(ukai): change new_google_url to google_base_domain? - return l10n_util::GetStringFUTF16(IDS_GOOGLE_URL_TRACKER_INFOBAR_MESSAGE, - UTF8ToUTF16(new_google_url_.spec())); - } - - virtual int GetButtons() const { - return BUTTON_OK | BUTTON_CANCEL; - } - - virtual string16 GetButtonLabel(InfoBarButton button) const { - return l10n_util::GetStringUTF16((button == BUTTON_OK) ? - IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL : - IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); - } - - virtual bool Accept() { - google_url_tracker_->AcceptGoogleURL(new_google_url_); - google_url_tracker_->RedoSearch(); - return true; - } - - virtual void InfoBarClosed() { - google_url_tracker_->InfoBarClosed(); - delete this; - } - - private: - virtual ~GoogleURLTrackerInfoBarDelegate() {} - - GoogleURLTracker* google_url_tracker_; - const GURL new_google_url_; - - DISALLOW_COPY_AND_ASSIGN(GoogleURLTrackerInfoBarDelegate); -}; - -} // anonymous namespace - -InfoBarDelegate* GoogleURLTracker::InfoBarDelegateFactory::CreateInfoBar( - TabContents* tab_contents, - GoogleURLTracker* google_url_tracker, - const GURL& new_google_url) { - InfoBarDelegate* infobar = - new GoogleURLTrackerInfoBarDelegate(tab_contents, - google_url_tracker, - new_google_url); - tab_contents->AddInfoBar(infobar); - return infobar; -} GoogleURLTracker::GoogleURLTracker() : google_url_(g_browser_process->local_state()->GetString( prefs::kLastKnownGoogleURL)), - ALLOW_THIS_IN_INITIALIZER_LIST(runnable_method_factory_(this)), - fetcher_id_(0), + ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), in_startup_sleep_(true), already_fetched_(false), need_to_fetch_(false), - request_context_available_(!!Profile::GetDefaultRequestContext()), - need_to_prompt_(false), - controller_(NULL), - infobar_factory_(new InfoBarDelegateFactory), - infobar_(NULL) { + request_context_available_(!!Profile::GetDefaultRequestContext()) { registrar_.Add(this, NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, NotificationService::AllSources()); - net::NetworkChangeNotifier::AddObserver(this); - - // Configure to max_retries at most kMaxRetries times for 5xx errors. - URLFetcherProtectEntry* protect = - URLFetcherProtectManager::GetInstance()->Register( - GURL(kSearchDomainCheckURL).host()); - static const int kMaxRetries = 5; - protect->SetMaxRetries(kMaxRetries); - // 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. @@ -124,14 +39,11 @@ GoogleURLTracker::GoogleURLTracker() // no function to do this. static const int kStartFetchDelayMS = 5000; MessageLoop::current()->PostDelayedTask(FROM_HERE, - runnable_method_factory_.NewRunnableMethod( - &GoogleURLTracker::FinishSleep), + fetcher_factory_.NewRunnableMethod(&GoogleURLTracker::FinishSleep), kStartFetchDelayMS); } GoogleURLTracker::~GoogleURLTracker() { - runnable_method_factory_.RevokeAll(); - net::NetworkChangeNotifier::RemoveObserver(this); } // static @@ -152,14 +64,42 @@ void GoogleURLTracker::RequestServerCheck() { void GoogleURLTracker::RegisterPrefs(PrefService* prefs) { prefs->RegisterStringPref(prefs::kLastKnownGoogleURL, kDefaultGoogleHomepage); - prefs->RegisterStringPref(prefs::kLastPromptedGoogleURL, std::string()); } // static -void GoogleURLTracker::GoogleURLSearchCommitted() { - GoogleURLTracker* tracker = g_browser_process->google_url_tracker(); - if (tracker) - tracker->SearchCommitted(); +bool GoogleURLTracker::CheckAndConvertToGoogleBaseURL(const GURL& url, + GURL* base_url) { + // Only allow updates if the new URL appears to be on google.xx, google.co.xx, + // or google.com.xx. Cases other than this are either malicious, or doorway + // pages for hotel WiFi connections and the like. + // NOTE: Obviously the above is not as secure as whitelisting all known Google + // frontpage domains, but for now we're trying to prevent login pages etc. + // from ruining the user experience, rather than preventing hijacking. + std::vector<std::string> host_components; + SplitStringDontTrim(url.host(), '.', &host_components); + if (host_components.size() < 2) + return false; + size_t google_component = host_components.size() - 2; + const std::string& component = host_components[google_component]; + if (component != "google") { + if ((host_components.size() < 3) || + ((component != "co") && (component != "com"))) + return false; + google_component = host_components.size() - 3; + if (host_components[google_component] != "google") + return false; + } + // For Google employees only: If the URL appears to be on + // [*.]corp.google.com, it's likely a doorway (e.g. + // wifi.corp.google.com), so ignore it. + if ((google_component > 0) && + (host_components[google_component - 1] == "corp")) + return false; + + // If the url's path does not begin "/intl/", reset it to "/". Other paths + // represent services such as iGoogle that are irrelevant to the baseURL. + *base_url = url.path().compare(0, 6, "/intl/") ? url.GetWithEmptyPath() : url; + return true; } void GoogleURLTracker::SetNeedToFetch() { @@ -183,10 +123,12 @@ void GoogleURLTracker::StartFetchIfDesirable() { !request_context_available_) return; - already_fetched_ = true; - fetcher_.reset(URLFetcher::Create(fetcher_id_, GURL(kSearchDomainCheckURL), - URLFetcher::GET, this)); - ++fetcher_id_; + need_to_fetch_ = false; + already_fetched_ = true; // If fetching fails, we don't bother to reset this + // flag; we just live with an outdated URL for this + // run of the browser. + fetcher_.reset(new URLFetcher(GURL(kDefaultGoogleHomepage), URLFetcher::HEAD, + this)); // We don't want this fetch to affect existing state in the profile. For // example, if a user has no Google cookies, this automatic check should not // cause one to be set, lest we alarm the user. @@ -206,153 +148,32 @@ void GoogleURLTracker::OnURLFetchComplete(const URLFetcher* source, scoped_ptr<URLFetcher> clean_up_fetcher(fetcher_.release()); // Don't update the URL if the request didn't succeed. - if (!status.is_success() || (response_code != 200)) { - already_fetched_ = false; - + if (!status.is_success() || (response_code != 200)) return; - } - // See if the response data was one we want to use, and if so, convert to the + // See if the response URL was one we want to use, and if so, convert to the // appropriate Google base URL. - std::string url_str; - TrimWhitespace(data, TRIM_ALL, &url_str); - - if (!StartsWithASCII(url_str, ".google.", false)) - return; - - fetched_google_url_ = GURL("http://www" + url_str); - GURL last_prompted_url( - g_browser_process->local_state()->GetString( - prefs::kLastPromptedGoogleURL)); - need_to_prompt_ = false; - // 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. - if (last_prompted_url.is_empty()) { - AcceptGoogleURL(fetched_google_url_); - // Set fetched_google_url_ as an initial value of last prompted URL. - g_browser_process->local_state()->SetString(prefs::kLastPromptedGoogleURL, - fetched_google_url_.spec()); + GURL base_url; + if (!CheckAndConvertToGoogleBaseURL(url, &base_url)) return; - } - if (fetched_google_url_ == last_prompted_url) - return; - if (fetched_google_url_ == google_url_) { - // The user came back to their original location after having temporarily - // moved. Reset the prompted URL so we'll prompt again if they move again. - g_browser_process->local_state()->SetString(prefs::kLastPromptedGoogleURL, - fetched_google_url_.spec()); - return; + // Update the saved base URL if it has changed. + const std::string base_url_str(base_url.spec()); + if (g_browser_process->local_state()->GetString(prefs::kLastKnownGoogleURL) != + base_url_str) { + g_browser_process->local_state()->SetString(prefs::kLastKnownGoogleURL, + base_url_str); + google_url_ = base_url; + NotificationService::current()->Notify(NotificationType::GOOGLE_URL_UPDATED, + NotificationService::AllSources(), + NotificationService::NoDetails()); } - - need_to_prompt_ = true; -} - -void GoogleURLTracker::AcceptGoogleURL(const GURL& new_google_url) { - google_url_ = new_google_url; - g_browser_process->local_state()->SetString(prefs::kLastKnownGoogleURL, - google_url_.spec()); - NotificationService::current()->Notify(NotificationType::GOOGLE_URL_UPDATED, - NotificationService::AllSources(), - NotificationService::NoDetails()); - need_to_prompt_ = false; -} - -void GoogleURLTracker::InfoBarClosed() { - controller_ = NULL; - infobar_ = NULL; - search_url_ = GURL(); -} - -void GoogleURLTracker::RedoSearch() { - // re-do the user's search on the new domain. - DCHECK(controller_); - url_canon::Replacements<char> replacements; - replacements.SetHost(google_url_.host().data(), - url_parse::Component(0, google_url_.host().length())); - search_url_ = search_url_.ReplaceComponents(replacements); - if (search_url_.is_valid()) - controller_->tab_contents()->OpenURL(search_url_, GURL(), CURRENT_TAB, - PageTransition::GENERATED); } void GoogleURLTracker::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { - switch (type.value) { - case NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE: - request_context_available_ = true; - StartFetchIfDesirable(); - break; - - case NotificationType::NAV_ENTRY_PENDING: - // If we've already received a notification for the same controller, we - // should reset infobar as that indicates that the page is being - // re-loaded - if (!infobar_ && - controller_ == Source<NavigationController>(source).ptr()) { - infobar_ = NULL; - } else if (!controller_) { - controller_ = Source<NavigationController>(source).ptr(); - NavigationEntry* entry = controller_->pending_entry(); - DCHECK(entry); - search_url_ = entry->url(); - - // Start listening for the commit notification. We also need to listen - // for the tab close command since that means the load will never - // commit! - registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, - Source<NavigationController>(controller_)); - registrar_.Add(this, NotificationType::TAB_CLOSED, - Source<NavigationController>(controller_)); - } - break; - - case NotificationType::NAV_ENTRY_COMMITTED: - DCHECK(controller_); - registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, - Source<NavigationController>(controller_)); - ShowGoogleURLInfoBarIfNecessary(controller_->tab_contents()); - break; - - case NotificationType::TAB_CLOSED: - registrar_.Remove(this, NotificationType::NAV_ENTRY_PENDING, - Source<NavigationController>(controller_)); - registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, - Source<NavigationController>(controller_)); - registrar_.Remove(this, NotificationType::TAB_CLOSED, - Source<NavigationController>(controller_)); - controller_ = NULL; - infobar_ = NULL; - break; - - default: - NOTREACHED() << "Unknown notification received:" << type.value; - } -} - -void GoogleURLTracker::OnIPAddressChanged() { - already_fetched_ = false; + DCHECK_EQ(NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, type.value); + request_context_available_ = true; StartFetchIfDesirable(); } - -void GoogleURLTracker::SearchCommitted() { - registrar_.Add(this, NotificationType::NAV_ENTRY_PENDING, - NotificationService::AllSources()); -} - -void GoogleURLTracker::ShowGoogleURLInfoBarIfNecessary( - TabContents* tab_contents) { - if (!need_to_prompt_) - return; - if (infobar_) - return; - DCHECK(!fetched_google_url_.is_empty()); - DCHECK(infobar_factory_.get()); - - infobar_ = infobar_factory_->CreateInfoBar(tab_contents, - this, - fetched_google_url_); - g_browser_process->local_state()->SetString(prefs::kLastPromptedGoogleURL, - fetched_google_url_.spec()); -} diff --git a/chrome/browser/google_url_tracker.h b/chrome/browser/google_url_tracker.h index 9d6b218..06274c0 100644 --- a/chrome/browser/google_url_tracker.h +++ b/chrome/browser/google_url_tracker.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2008 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. @@ -13,17 +13,11 @@ #include "chrome/common/net/url_fetcher.h" #include "chrome/common/notification_registrar.h" #include "googleurl/src/gurl.h" -#include "net/base/network_change_notifier.h" -class InfoBarDelegate; -class NavigationController; class PrefService; -class TabContents; -class TemplateURL; -// 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. +// This object is responsible for updating the Google URL at most once per run, +// and tracking the currently known value, which is also saved to a pref. // // Most consumers should only call GoogleURL(), which is guaranteed to // synchronously return a value at all times (even during startup or in unittest @@ -35,17 +29,8 @@ class TemplateURL; // performed (ever) unless at least one consumer registers interest by calling // RequestServerCheck(). class GoogleURLTracker : public URLFetcher::Delegate, - public NotificationObserver, - public net::NetworkChangeNotifier::Observer { + public NotificationObserver { public: - class InfoBarDelegateFactory { - public: - virtual ~InfoBarDelegateFactory() {} - virtual InfoBarDelegate* CreateInfoBar(TabContents* tab_contents, - GoogleURLTracker* google_url_tracker, - const GURL& new_google_url); - }; - // Only the main browser process loop should call this, when setting up // g_browser_process->google_url_tracker_. No code other than the // GoogleURLTracker itself should actually use @@ -54,7 +39,7 @@ class GoogleURLTracker : public URLFetcher::Delegate, // anyway). GoogleURLTracker(); - virtual ~GoogleURLTracker(); + ~GoogleURLTracker(); // Returns the current Google URL. This will return a valid URL even in // unittest mode. @@ -63,38 +48,27 @@ class GoogleURLTracker : public URLFetcher::Delegate, static GURL GoogleURL(); // Requests that the tracker perform a server check to update the Google URL - // as necessary. This will happen at most once per network change, not - // sooner than five seconds after startup (checks requested before that time - // will occur then; checks requested afterwards will occur immediately, if - // no other checks have been made during this run). + // as necessary. This will happen at most once per run, not sooner than five + // seconds after startup (checks requested before that time will occur then; + // checks requested afterwards will occur immediately, if no other checks have + // been made during this run). // // In unittest mode, this function does nothing. static void RequestServerCheck(); static void RegisterPrefs(PrefService* prefs); - // Notifies the tracker that the user has started a Google search. - // If prompting is necessary, we then listen for the subsequent - // NAV_ENTRY_PENDING notification to get the appropriate NavigationController. - // When the load commits, we'll show the infobar. - static void GoogleURLSearchCommitted(); - static const char kDefaultGoogleHomepage[]; - static const char kSearchDomainCheckURL[]; - - // Methods called from InfoBar delegate. - void AcceptGoogleURL(const GURL& google_url); - void InfoBarClosed(); - void RedoSearch(); - - NavigationController* controller() const { return controller_; } private: - friend class GoogleURLTrackerTest; + FRIEND_TEST_ALL_PREFIXES(GoogleURLTrackerTest, CheckAndConvertURL); + + // Determines if |url| is an appropriate source for a new Google base URL, and + // update |base_url| to the appropriate base URL if so. Returns whether the + // check succeeded (and thus whether |base_url| was actually updated). + static bool CheckAndConvertToGoogleBaseURL(const GURL& url, GURL* base_url); // Registers consumer interest in getting an updated URL from the server. - // It will be notified as NotificationType::GOOGLE_URL_UPDATED, so the - // consumer should observe this notification before calling this. void SetNeedToFetch(); // Called when the five second startup sleep has finished. Runs any pending @@ -118,22 +92,10 @@ class GoogleURLTracker : public URLFetcher::Delegate, const NotificationSource& source, const NotificationDetails& details); - // NetworkChangeNotifier::Observer - virtual void OnIPAddressChanged(); - - void SearchCommitted(); - - void ShowGoogleURLInfoBarIfNecessary(TabContents* tab_contents); - NotificationRegistrar registrar_; - // TODO(ukai): GoogleURLTracker should track google domain (e.g. google.co.uk) - // rather than URL (e.g. http://www.google.co.uk/), so that user could - // configure to use https in search engine templates. GURL google_url_; - GURL fetched_google_url_; - ScopedRunnableMethodFactory<GoogleURLTracker> runnable_method_factory_; + ScopedRunnableMethodFactory<GoogleURLTracker> fetcher_factory_; scoped_ptr<URLFetcher> fetcher_; - int fetcher_id_; bool in_startup_sleep_; // True if we're in the five-second "no fetching" // period that begins at browser start. bool already_fetched_; // True if we've already fetched a URL once this run; @@ -141,19 +103,10 @@ class GoogleURLTracker : public URLFetcher::Delegate, bool need_to_fetch_; // True if a consumer actually wants us to fetch an // updated URL. If this is never set, we won't // bother to fetch anything. - // Consumers should observe - // NotificationType::GOOGLE_URL_UPDATED. bool request_context_available_; // True when the profile has been loaded and the // default request context created, so we can // actually do the fetch with the right data. - 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. - NavigationController* controller_; - scoped_ptr<InfoBarDelegateFactory> infobar_factory_; - InfoBarDelegate* infobar_; - GURL search_url_; DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker); }; diff --git a/chrome/browser/google_url_tracker_unittest.cc b/chrome/browser/google_url_tracker_unittest.cc index 428a3e3..9de0b88 100644 --- a/chrome/browser/google_url_tracker_unittest.cc +++ b/chrome/browser/google_url_tracker_unittest.cc @@ -3,450 +3,33 @@ // found in the LICENSE file. #include "chrome/browser/google_url_tracker.h" -#include "base/command_line.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/profile.h" -#include "chrome/browser/tab_contents/infobar_delegate.h" -#include "chrome/common/net/url_fetcher.h" -#include "chrome/common/net/url_request_context_getter.h" -#include "chrome/common/net/test_url_fetcher_factory.h" -#include "chrome/common/notification_service.h" -#include "chrome/common/pref_names.h" -#include "chrome/test/testing_browser_process.h" -#include "chrome/test/testing_pref_service.h" -#include "chrome/test/testing_profile.h" -#include "net/url_request/url_request.h" -#include "net/url_request/url_request_unittest.h" #include "testing/gtest/include/gtest/gtest.h" -namespace { - -class TestNotificationObserver : public NotificationObserver { - public: - TestNotificationObserver() : notified_(false) {} - virtual ~TestNotificationObserver() {} - - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - notified_ = true; - } - bool notified() const { return notified_; } - void ClearNotified() { notified_ = false; } - private: - bool notified_; -}; - -class TestInfoBar : public InfoBarDelegate { - public: - TestInfoBar(GoogleURLTracker* google_url_tracker, - const GURL& new_google_url) - : InfoBarDelegate(NULL), - google_url_tracker_(google_url_tracker), - new_google_url_(new_google_url) {} - virtual ~TestInfoBar() {} - GoogleURLTracker* google_url_tracker() const { return google_url_tracker_; } - const GURL& new_google_url() const { return new_google_url_; } - - virtual InfoBar* CreateInfoBar() { return NULL; } - - private: - GoogleURLTracker* google_url_tracker_; - GURL new_google_url_; -}; - -class TestInfoBarDelegateFactory - : public GoogleURLTracker::InfoBarDelegateFactory { - public: - virtual InfoBarDelegate* CreateInfoBar(TabContents* tab_contents, - GoogleURLTracker* google_url_tracker, - const GURL& new_google_url) { - return new TestInfoBar(google_url_tracker, new_google_url); - } -}; - -} // anonymous namespace - -class GoogleURLTrackerTest : public testing::Test { - protected: - GoogleURLTrackerTest() - : original_default_request_context_(NULL) { - } - - void SetUp() { - original_default_request_context_ = Profile::GetDefaultRequestContext(); - Profile::set_default_request_context(NULL); - message_loop_ = new MessageLoop(MessageLoop::TYPE_IO); - network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); - testing_profile_.reset(new TestingProfile); - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - PrefService* pref_service = testing_profile_->GetPrefs(); - testing_browser_process->SetPrefService(pref_service); - testing_browser_process->SetGoogleURLTracker(new GoogleURLTracker); - - URLFetcher::set_factory(&fetcher_factory_); - observer_.reset(new TestNotificationObserver); - g_browser_process->google_url_tracker()->infobar_factory_.reset( - new TestInfoBarDelegateFactory); - } - - void TearDown() { - URLFetcher::set_factory(NULL); - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetGoogleURLTracker(NULL); - testing_browser_process->SetPrefService(NULL); - testing_profile_.reset(); - network_change_notifier_.reset(); - delete message_loop_; - Profile::set_default_request_context(original_default_request_context_); - } - - void CreateRequestContext() { - testing_profile_->CreateRequestContext(); - Profile::set_default_request_context(testing_profile_->GetRequestContext()); - NotificationService::current()->Notify( - NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, - NotificationService::AllSources(), NotificationService::NoDetails()); - } - - TestURLFetcher* GetFetcherByID(int expected_id) { - return fetcher_factory_.GetFetcherByID(expected_id); - } - - void MockSearchDomainCheckResponse( - int expected_id, const std::string& domain) { - TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(expected_id); - ASSERT_TRUE(fetcher); - fetcher->delegate()->OnURLFetchComplete( - fetcher, - GURL(GoogleURLTracker::kSearchDomainCheckURL), - URLRequestStatus(), - 200, - ResponseCookies(), - domain); - MessageLoop::current()->RunAllPending(); - } - - void RequestServerCheck() { - registrar_.Add(observer_.get(), NotificationType::GOOGLE_URL_UPDATED, - NotificationService::AllSources()); - GoogleURLTracker::RequestServerCheck(); - MessageLoop::current()->RunAllPending(); - } - - void FinishSleep() { - g_browser_process->google_url_tracker()->FinishSleep(); - MessageLoop::current()->RunAllPending(); - } - - void NotifyIPAddressChanged() { - net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); - MessageLoop::current()->RunAllPending(); - } - - GURL GetFetchedGoogleURL() { - return g_browser_process->google_url_tracker()->fetched_google_url_; - } - - void SetGoogleURL(const GURL& url) { - g_browser_process->google_url_tracker()->google_url_ = url; +TEST(GoogleURLTrackerTest, CheckAndConvertURL) { + static const struct { + const char* const source_url; + const bool can_convert; + const char* const base_url; + } data[] = { + { "http://www.google.com/", true, "http://www.google.com/", }, + { "http://google.fr/", true, "http://google.fr/", }, + { "http://google.co.uk/", true, "http://google.co.uk/", }, + { "http://www.google.com.by/", true, "http://www.google.com.by/", }, + { "http://www.google.com/ig", true, "http://www.google.com/", }, + { "http://www.google.com/intl/xx", true, "http://www.google.com/intl/xx", }, + { "http://a.b.c.google.com/", true, "http://a.b.c.google.com/", }, + { "http://www.yahoo.com/", false, "", }, + { "http://google.evil.com/", false, "", }, + { "http://google.com.com.com/", false, "", }, + { "http://google/", false, "", }, + { "http://wifi.corp.google.com/", false, "" }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { + GURL base_url; + const bool can_convert = GoogleURLTracker::CheckAndConvertToGoogleBaseURL( + GURL(data[i].source_url), &base_url); + EXPECT_EQ(data[i].can_convert, can_convert); + EXPECT_STREQ(data[i].base_url, base_url.spec().c_str()); } - - void SetLastPromptedGoogleURL(const GURL& url) { - g_browser_process->local_state()->SetString( - prefs::kLastPromptedGoogleURL, url.spec()); - } - - GURL GetLastPromptedGoogleURL() { - return GURL(g_browser_process->local_state()->GetString( - prefs::kLastPromptedGoogleURL)); - } - - void SearchCommitted(const GURL& search_url) { - GoogleURLTracker* google_url_tracker = - g_browser_process->google_url_tracker(); - - google_url_tracker->SearchCommitted(); - // GoogleURLTracker wait for NAV_ENTRY_PENDING. - // In NAV_ENTRY_PENDING, it will set search_url_. - google_url_tracker->search_url_ = search_url; - } - - void NavEntryCommitted() { - GoogleURLTracker* google_url_tracker = - g_browser_process->google_url_tracker(); - google_url_tracker->ShowGoogleURLInfoBarIfNecessary(NULL); - } - - bool InfoBarIsShown() { - return (g_browser_process->google_url_tracker()->infobar_ != NULL); - } - - GURL GetInfoBarShowingURL() { - TestInfoBar* infobar = static_cast<TestInfoBar*>( - g_browser_process->google_url_tracker()->infobar_); - return infobar->new_google_url(); - } - - void AcceptGoogleURL() { - TestInfoBar* infobar = static_cast<TestInfoBar*>( - g_browser_process->google_url_tracker()->infobar_); - ASSERT_TRUE(infobar); - ASSERT_TRUE(infobar->google_url_tracker()); - infobar->google_url_tracker()->AcceptGoogleURL(infobar->new_google_url()); - } - - void InfoBarClosed() { - TestInfoBar* infobar = static_cast<TestInfoBar*>( - g_browser_process->google_url_tracker()->infobar_); - ASSERT_TRUE(infobar); - ASSERT_TRUE(infobar->google_url_tracker()); - infobar->google_url_tracker()->InfoBarClosed(); - delete infobar; - } - - void ExpectDefaultURLs() { - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL(), GetFetchedGoogleURL()); - } - - private: - MessageLoop* message_loop_; - scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; - scoped_ptr<TestingProfile> testing_profile_; - - TestURLFetcherFactory fetcher_factory_; - NotificationRegistrar registrar_; - scoped_ptr<NotificationObserver> observer_; - - URLRequestContextGetter* original_default_request_context_; -}; - -TEST_F(GoogleURLTrackerTest, StartupSleepFinish) { - CreateRequestContext(); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.uk"); - - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - // GoogleURL is updated, becase it was not the last prompted URL. - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, StartupSleepFinishWithLastPrompted) { - CreateRequestContext(); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.uk"); - - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - // GoogleURL should not be updated. - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, StartupSleepFinishNoObserver) { - CreateRequestContext(); - - ExpectDefaultURLs(); - - FinishSleep(); - EXPECT_FALSE(GetFetcherByID(0)); - - ExpectDefaultURLs(); -} - -TEST_F(GoogleURLTrackerTest, MonitorNetworkChange) { - CreateRequestContext(); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.uk"); - - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - // GoogleURL is updated, becase it was not the last prompted URL. - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); - - NotifyIPAddressChanged(); - MockSearchDomainCheckResponse(1, ".google.co.in"); - - EXPECT_EQ(GURL("http://www.google.co.in/"), GetFetchedGoogleURL()); - // Don't update GoogleURL. - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, MonitorNetworkChangeNoObserver) { - CreateRequestContext(); - - ExpectDefaultURLs(); - - FinishSleep(); - NotifyIPAddressChanged(); - EXPECT_FALSE(GetFetcherByID(0)); - - ExpectDefaultURLs(); -} - -TEST_F(GoogleURLTrackerTest, MonitorNetworkChangeAndObserverRegister) { - CreateRequestContext(); - - ExpectDefaultURLs(); - - FinishSleep(); - NotifyIPAddressChanged(); - EXPECT_FALSE(GetFetcherByID(0)); - - ExpectDefaultURLs(); - - RequestServerCheck(); - MockSearchDomainCheckResponse(0, ".google.co.uk"); - - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, NoSearchCommitedAndPromptedNotChanged) { - CreateRequestContext(); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.jp"); - - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, SearchCommitedAndUserSayNo) { - CreateRequestContext(); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - - ExpectDefaultURLs(); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.jp"); - - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - - SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); - NavEntryCommitted(); - - EXPECT_TRUE(InfoBarIsShown()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetInfoBarShowingURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); - - InfoBarClosed(); - EXPECT_FALSE(InfoBarIsShown()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, SearchCommitedAndUserSayYes) { - CreateRequestContext(); - SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); - - ExpectDefaultURLs(); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.jp"); - - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - - SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); - NavEntryCommitted(); - - EXPECT_TRUE(InfoBarIsShown()); - EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), - GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetInfoBarShowingURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); - - AcceptGoogleURL(); - InfoBarClosed(); - - EXPECT_FALSE(InfoBarIsShown()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, InitialUpdate) { - CreateRequestContext(); - ExpectDefaultURLs(); - EXPECT_EQ(GURL(), GetLastPromptedGoogleURL()); - - RequestServerCheck(); - EXPECT_FALSE(GetFetcherByID(0)); - ExpectDefaultURLs(); - EXPECT_EQ(GURL(), GetLastPromptedGoogleURL()); - - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.uk"); - - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - - SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); - NavEntryCommitted(); - - EXPECT_FALSE(InfoBarIsShown()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); -} - -TEST_F(GoogleURLTrackerTest, UpdatePromptedURLWhenBack) { - CreateRequestContext(); - SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); - SetGoogleURL(GURL("http://www.google.co.uk/")); - - RequestServerCheck(); - FinishSleep(); - MockSearchDomainCheckResponse(0, ".google.co.uk"); - - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); - - SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); - NavEntryCommitted(); - - EXPECT_FALSE(InfoBarIsShown()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL()); - EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); } diff --git a/chrome/common/net/url_fetcher_protect.h b/chrome/common/net/url_fetcher_protect.h index 980353c..95ecf97 100644 --- a/chrome/common/net/url_fetcher_protect.h +++ b/chrome/common/net/url_fetcher_protect.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2008 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. // @@ -61,11 +61,6 @@ class URLFetcherProtectEntry { return max_retries_; } - // Sets the max retries. - void SetMaxRetries(int max_retries) { - max_retries_ = max_retries; - } - private: // When a request comes, calculate the release time for it. // Returns the backoff time before sending. diff --git a/chrome/common/pref_names.cc b/chrome/common/pref_names.cc index 75086de..40183d1 100644 --- a/chrome/common/pref_names.cc +++ b/chrome/common/pref_names.cc @@ -763,12 +763,6 @@ const char kShouldShowWelcomePage[] = "show-welcome-page"; // 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"; - // String containing the last known intranet redirect URL, if any. See // intranet_redirect_detector.h for more information. const char kLastKnownIntranetRedirectOrigin[] = ""; diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h index 45e9c74..f27005a 100644 --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -277,7 +277,6 @@ extern const char kShouldUseMinimalFirstRunBubble[]; extern const char kShouldShowWelcomePage[]; extern const char kLastKnownGoogleURL[]; -extern const char kLastPromptedGoogleURL[]; extern const char kLastKnownIntranetRedirectOrigin[]; extern const char kCountryIDAtInstall[]; diff --git a/chrome/test/testing_browser_process.h b/chrome/test/testing_browser_process.h index 05979e5..82db3af 100644 --- a/chrome/test/testing_browser_process.h +++ b/chrome/test/testing_browser_process.h @@ -19,8 +19,6 @@ #include "base/string_util.h" #include "base/waitable_event.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/google_url_tracker.h" -#include "chrome/browser/pref_service.h" #include "chrome/common/notification_service.h" class IOThread; @@ -30,8 +28,7 @@ class TestingBrowserProcess : public BrowserProcess { TestingBrowserProcess() : shutdown_event_(new base::WaitableEvent(true, false)), module_ref_count_(0), - app_locale_("en"), - pref_service_(NULL) { + app_locale_("en") { } virtual ~TestingBrowserProcess() { @@ -75,7 +72,7 @@ class TestingBrowserProcess : public BrowserProcess { } virtual PrefService* local_state() { - return pref_service_; + return NULL; } virtual IconManager* icon_manager() { @@ -111,7 +108,7 @@ class TestingBrowserProcess : public BrowserProcess { } virtual GoogleURLTracker* google_url_tracker() { - return google_url_tracker_.get(); + return NULL; } virtual IntranetRedirectDetector* intranet_redirect_detector() { @@ -164,13 +161,6 @@ class TestingBrowserProcess : public BrowserProcess { virtual void SetIPCLoggingEnabled(bool enable) {} #endif - void SetPrefService(PrefService* pref_service) { - pref_service_ = pref_service; - } - void SetGoogleURLTracker(GoogleURLTracker* google_url_tracker) { - google_url_tracker_.reset(google_url_tracker); - } - private: NotificationService notification_service_; scoped_ptr<base::WaitableEvent> shutdown_event_; @@ -178,9 +168,6 @@ class TestingBrowserProcess : public BrowserProcess { scoped_ptr<Clipboard> clipboard_; std::string app_locale_; - PrefService* pref_service_; - scoped_ptr<GoogleURLTracker> google_url_tracker_; - DISALLOW_COPY_AND_ASSIGN(TestingBrowserProcess); }; |