summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_url_tracker.cc
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2010-07-29 17:14:53 +0100
committerBen Murdoch <benm@google.com>2010-08-04 14:29:45 +0100
commitc407dc5cd9bdc5668497f21b26b09d988ab439de (patch)
tree7eaf8707c0309516bdb042ad976feedaf72b0bb1 /chrome/browser/google_url_tracker.cc
parent0998b1cdac5733f299c12d88bc31ef9c8035b8fa (diff)
downloadexternal_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.zip
external_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.tar.gz
external_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.tar.bz2
Merge Chromium src@r53293
Change-Id: Ia79acf8670f385cee48c45b0a75371d8e950af34
Diffstat (limited to 'chrome/browser/google_url_tracker.cc')
-rw-r--r--chrome/browser/google_url_tracker.cc179
1 files changed, 179 insertions, 0 deletions
diff --git a/chrome/browser/google_url_tracker.cc b/chrome/browser/google_url_tracker.cc
new file mode 100644
index 0000000..b703393
--- /dev/null
+++ b/chrome/browser/google_url_tracker.cc
@@ -0,0 +1,179 @@
+// Copyright (c) 2010 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_url_tracker.h"
+
+#include <vector>
+
+#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/common/notification_service.h"
+#include "chrome/common/pref_names.h"
+#include "net/base/load_flags.h"
+#include "net/url_request/url_request_status.h"
+
+const char GoogleURLTracker::kDefaultGoogleHomepage[] =
+ "http://www.google.com/";
+
+GoogleURLTracker::GoogleURLTracker()
+ : google_url_(g_browser_process->local_state()->GetString(
+ prefs::kLastKnownGoogleURL)),
+ ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)),
+ in_startup_sleep_(true),
+ already_fetched_(false),
+ need_to_fetch_(false),
+ request_context_available_(!!Profile::GetDefaultRequestContext()) {
+ registrar_.Add(this, NotificationType::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.
+ // 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.
+ static const int kStartFetchDelayMS = 5000;
+ MessageLoop::current()->PostDelayedTask(FROM_HERE,
+ fetcher_factory_.NewRunnableMethod(&GoogleURLTracker::FinishSleep),
+ kStartFetchDelayMS);
+}
+
+GoogleURLTracker::~GoogleURLTracker() {
+}
+
+// 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,
+ kDefaultGoogleHomepage);
+}
+
+// static
+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() {
+ 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));
+ // 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.
+ fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE |
+ net::LOAD_DO_NOT_SAVE_COOKIES);
+ fetcher_->set_request_context(Profile::GetDefaultRequestContext());
+ fetcher_->Start();
+}
+
+void GoogleURLTracker::OnURLFetchComplete(const URLFetcher* source,
+ const GURL& url,
+ const URLRequestStatus& status,
+ int response_code,
+ const ResponseCookies& cookies,
+ const std::string& data) {
+ // Delete the fetcher on this function's exit.
+ 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))
+ return;
+
+ // See if the response URL was one we want to use, and if so, convert to the
+ // appropriate Google base URL.
+ GURL base_url;
+ if (!CheckAndConvertToGoogleBaseURL(url, &base_url))
+ 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());
+ }
+}
+
+void GoogleURLTracker::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK_EQ(NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE, type.value);
+ request_context_available_ = true;
+ StartFetchIfDesirable();
+}