summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_url_tracker.cc
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-15 18:28:09 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-15 18:28:09 +0000
commit3dd1f6d55213a0c14590d5db0236b5472d2adfab (patch)
tree64410565ab648e88940c606c3823f2ba46c6cfa2 /chrome/browser/google_url_tracker.cc
parentdfaa8db80cfdf7c4f04dfedf87642a0f333da0e1 (diff)
downloadchromium_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.cc')
-rw-r--r--chrome/browser/google_url_tracker.cc61
1 files changed, 57 insertions, 4 deletions
diff --git a/chrome/browser/google_url_tracker.cc b/chrome/browser/google_url_tracker.cc
index 5c2830d..be092c6 100644
--- a/chrome/browser/google_url_tracker.cc
+++ b/chrome/browser/google_url_tracker.cc
@@ -7,7 +7,6 @@
#include "base/string_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profile.h"
-#include "chrome/common/notification_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "net/base/load_flags.h"
@@ -19,7 +18,15 @@ GoogleURLTracker::GoogleURLTracker()
: google_url_(g_browser_process->local_state()->GetString(
prefs::kLastKnownGoogleURL)),
#pragma warning(suppress: 4355) // Okay to pass "this" here.
- fetcher_factory_(this) {
+ fetcher_factory_(this),
+ in_startup_sleep_(true),
+ already_fetched_(false),
+ need_to_fetch_(false),
+ request_context_available_(!!Profile::GetDefaultRequestContext()) {
+ NotificationService::current()->AddObserver(this,
+ NOTIFY_DEFAULT_REQUEST_CONTEXT_AVAILABLE,
+ NotificationService::AllSources());
+
// 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.
@@ -28,16 +35,31 @@ GoogleURLTracker::GoogleURLTracker()
// no function to do this.
static const int kStartFetchDelayMS = 5000;
MessageLoop::current()->PostDelayedTask(FROM_HERE,
- fetcher_factory_.NewRunnableMethod(&GoogleURLTracker::StartFetch),
+ fetcher_factory_.NewRunnableMethod(&GoogleURLTracker::FinishSleep),
kStartFetchDelayMS);
}
+GoogleURLTracker::~GoogleURLTracker() {
+ NotificationService::current()->RemoveObserver(this,
+ NOTIFY_DEFAULT_REQUEST_CONTEXT_AVAILABLE,
+ NotificationService::AllSources());
+}
+
+// static
GURL GoogleURLTracker::GoogleURL() {
const GoogleURLTracker* const tracker =
g_browser_process->google_url_tracker();
return tracker ? tracker->google_url_ : GURL(kDefaultGoogleHomepage);
}
+// static
+void GoogleURLTracker::RequestServerCheck() {
+ GoogleURLTracker* const tracker = g_browser_process->google_url_tracker();
+ if (tracker)
+ tracker->SetNeedToFetch();
+}
+
+// static
void GoogleURLTracker::RegisterPrefs(PrefService* prefs) {
prefs->RegisterStringPref(prefs::kLastKnownGoogleURL,
ASCIIToWide(kDefaultGoogleHomepage));
@@ -71,7 +93,31 @@ bool GoogleURLTracker::CheckAndConvertToGoogleBaseURL(const GURL& url,
return true;
}
-void GoogleURLTracker::StartFetch() {
+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_ ||
+ !request_context_available_)
+ return;
+
+ 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));
fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE);
@@ -111,3 +157,10 @@ void GoogleURLTracker::OnURLFetchComplete(const URLFetcher* source,
}
}
+void GoogleURLTracker::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK_EQ(NOTIFY_DEFAULT_REQUEST_CONTEXT_AVAILABLE, type);
+ request_context_available_ = true;
+ StartFetchIfDesirable();
+}