Home | History | Annotate | Download | only in ui
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/navigation_correction_tab_observer.h"
      6 
      7 #include "base/prefs/pref_service.h"
      8 #include "chrome/browser/browser_process.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/google/google_profile_helper.h"
     11 #include "chrome/browser/google/google_url_tracker_factory.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "chrome/common/pref_names.h"
     14 #include "chrome/common/render_messages.h"
     15 #include "components/google/core/browser/google_util.h"
     16 #include "components/pref_registry/pref_registry_syncable.h"
     17 #include "content/public/browser/render_frame_host.h"
     18 #include "content/public/browser/render_view_host.h"
     19 #include "content/public/browser/web_contents.h"
     20 #include "google_apis/google_api_keys.h"
     21 
     22 using content::RenderFrameHost;
     23 using content::RenderViewHost;
     24 using content::WebContents;
     25 
     26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver);
     27 
     28 NavigationCorrectionTabObserver::NavigationCorrectionTabObserver(
     29     WebContents* web_contents)
     30     : content::WebContentsObserver(web_contents),
     31       profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
     32   PrefService* prefs = profile_->GetPrefs();
     33   if (prefs) {
     34     pref_change_registrar_.Init(prefs);
     35     pref_change_registrar_.Add(
     36         prefs::kAlternateErrorPagesEnabled,
     37         base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged,
     38                    base::Unretained(this)));
     39   }
     40 
     41   GoogleURLTracker* google_url_tracker =
     42       GoogleURLTrackerFactory::GetForProfile(profile_);
     43   if (google_url_tracker) {
     44     google_url_updated_subscription_ = google_url_tracker->RegisterCallback(
     45         base::Bind(&NavigationCorrectionTabObserver::OnGoogleURLUpdated,
     46                    base::Unretained(this)));
     47   }
     48 }
     49 
     50 NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
     51 }
     52 
     53 // static
     54 void NavigationCorrectionTabObserver::RegisterProfilePrefs(
     55     user_prefs::PrefRegistrySyncable* prefs) {
     56   prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled,
     57                              true,
     58                              user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     59 }
     60 
     61 ////////////////////////////////////////////////////////////////////////////////
     62 // WebContentsObserver overrides
     63 
     64 void NavigationCorrectionTabObserver::RenderViewCreated(
     65     RenderViewHost* render_view_host) {
     66   UpdateNavigationCorrectionInfo(render_view_host);
     67 }
     68 
     69 ////////////////////////////////////////////////////////////////////////////////
     70 // Internal helpers
     71 
     72 void NavigationCorrectionTabObserver::OnGoogleURLUpdated() {
     73   UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
     74 }
     75 
     76 GURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
     77   // Disable navigation corrections when the preference is disabled or when in
     78   // Incognito mode.
     79   if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) ||
     80       profile_->IsOffTheRecord()) {
     81     return GURL();
     82   }
     83 
     84   return google_util::LinkDoctorBaseURL();
     85 }
     86 
     87 void NavigationCorrectionTabObserver::OnEnabledChanged() {
     88   UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
     89 }
     90 
     91 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
     92     RenderViewHost* rvh) {
     93   RenderFrameHost* rfh = rvh->GetMainFrame();
     94   rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
     95       rfh->GetRoutingID(),
     96       GetNavigationCorrectionURL(),
     97       google_util::GetGoogleLocale(g_browser_process->GetApplicationLocale()),
     98       google_util::GetGoogleCountryCode(
     99           google_profile_helper::GetGoogleHomePageURL(profile_)),
    100       google_apis::GetAPIKey(),
    101       google_util::GetGoogleSearchURL(
    102           google_profile_helper::GetGoogleHomePageURL(profile_))));
    103 }
    104