diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-15 18:28:09 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-15 18:28:09 +0000 |
commit | 3dd1f6d55213a0c14590d5db0236b5472d2adfab (patch) | |
tree | 64410565ab648e88940c606c3823f2ba46c6cfa2 /chrome/browser/google_url_tracker.h | |
parent | dfaa8db80cfdf7c4f04dfedf87642a0f333da0e1 (diff) | |
download | chromium_src-3dd1f6d55213a0c14590d5db0236b5472d2adfab.zip chromium_src-3dd1f6d55213a0c14590d5db0236b5472d2adfab.tar.gz chromium_src-3dd1f6d55213a0c14590d5db0236b5472d2adfab.tar.bz2 |
Make the GoogleURLTracker only fetch the Google hostname if the user's default search engine is Google. Our existing restrictions still apply: no fetches before five seconds after startup, and no more than one fetch per run.
Because of lazy initialization everywhere, this was hairier than I'd hoped. We have to ensure we don't try to fetch until the profile has been created, lest GetDefaultRequestContext() return NULL. Note that this was actually a bug in the existing product: if you set your startup page to, say, about:blank, and started the browser and did nothing at all for five seconds, we'd crash.
BUG=1291
Review URL: http://codereview.chromium.org/1942
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2223 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/google_url_tracker.h')
-rw-r--r-- | chrome/browser/google_url_tracker.h | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/chrome/browser/google_url_tracker.h b/chrome/browser/google_url_tracker.h index 04e6c81..e270397 100644 --- a/chrome/browser/google_url_tracker.h +++ b/chrome/browser/google_url_tracker.h @@ -3,13 +3,24 @@ // found in the LICENSE file. #include "chrome/browser/url_fetcher.h" +#include "chrome/common/notification_service.h" class PrefService; -// This object is responsible for updating the Google URL exactly once (when -// first constructed) and tracking the currently known value, which is also -// saved to a pref. -class GoogleURLTracker : public URLFetcher::Delegate { +// 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 +// mode). Consumers who need to be notified when things change should listen to +// the notification service for NOTIFY_GOOGLE_URL_UPDATED, and call GoogleURL() +// again after receiving it, in order to get the updated value. +// +// To protect users' privacy and reduce server load, no updates will be +// performed (ever) unless at least one consumer registers interest by calling +// RequestServerCheck(). +class GoogleURLTracker : public URLFetcher::Delegate, + public NotificationObserver { public: // Only the main browser process loop should call this, when setting up // g_browser_process->google_url_tracker_. No code other than the @@ -19,15 +30,23 @@ class GoogleURLTracker : public URLFetcher::Delegate { // anyway). GoogleURLTracker(); - // This returns the current Google URL. If this is the first call to it in - // this session, it also kicks off a timer to fetch the correct Google URL; - // when that fetch completes, it will fire NOTIFY_GOOGLE_URL_UPDATED. + ~GoogleURLTracker(); + + // Returns the current Google URL. This will return a valid URL even in + // unittest mode. // // This is the only function most code should ever call. - // - // This will return a valid URL even in unittest mode. 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 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); private: @@ -38,8 +57,16 @@ class GoogleURLTracker : public URLFetcher::Delegate { // check succeeded (and thus whether |base_url| was actually updated). static bool CheckAndConvertToGoogleBaseURL(const GURL& url, GURL* base_url); - // Starts the fetch of the up-to-date Google URL. - void StartFetch(); + // Registers consumer interest in getting an updated URL from the server. + void SetNeedToFetch(); + + // Called when the five second startup sleep has finished. Runs any pending + // fetch. + void FinishSleep(); + + // Starts the fetch of the up-to-date Google URL if we actually want to fetch + // it and can currently do so. + void StartFetchIfDesirable(); // URLFetcher::Delegate virtual void OnURLFetchComplete(const URLFetcher *source, @@ -49,11 +76,27 @@ class GoogleURLTracker : public URLFetcher::Delegate { const ResponseCookies& cookies, const std::string& data); + // NotificationObserver + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + static const char kDefaultGoogleHomepage[]; GURL google_url_; ScopedRunnableMethodFactory<GoogleURLTracker> fetcher_factory_; scoped_ptr<URLFetcher> fetcher_; + 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; + // we won't fetch again until after a restart. + 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. + 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. DISALLOW_EVIL_CONSTRUCTORS(GoogleURLTracker); }; |