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.
5 #include "chrome/browser/ui/navigation_correction_tab_observer.h"
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"
22 using content::RenderFrameHost;
23 using content::RenderViewHost;
24 using content::WebContents;
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver);
28 NavigationCorrectionTabObserver::NavigationCorrectionTabObserver(
29 WebContents* web_contents)
30 : content::WebContentsObserver(web_contents),
31 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
32 PrefService* prefs = profile_->GetPrefs();
34 pref_change_registrar_.Init(prefs);
35 pref_change_registrar_.Add(
36 prefs::kAlternateErrorPagesEnabled,
37 base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged,
38 base::Unretained(this)));
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)));
50 NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
54 void NavigationCorrectionTabObserver::RegisterProfilePrefs(
55 user_prefs::PrefRegistrySyncable* prefs) {
56 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled,
58 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
61 ////////////////////////////////////////////////////////////////////////////////
62 // WebContentsObserver overrides
64 void NavigationCorrectionTabObserver::RenderViewCreated(
65 RenderViewHost* render_view_host) {
66 UpdateNavigationCorrectionInfo(render_view_host);
69 ////////////////////////////////////////////////////////////////////////////////
72 void NavigationCorrectionTabObserver::OnGoogleURLUpdated(GURL old_url,
74 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
77 GURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
78 // Disable navigation corrections when the preference is disabled or when in
80 if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) ||
81 profile_->IsOffTheRecord()) {
85 return google_util::LinkDoctorBaseURL();
88 void NavigationCorrectionTabObserver::OnEnabledChanged() {
89 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
92 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
93 RenderViewHost* rvh) {
94 RenderFrameHost* rfh = rvh->GetMainFrame();
95 rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
97 GetNavigationCorrectionURL(),
98 google_util::GetGoogleLocale(g_browser_process->GetApplicationLocale()),
99 google_util::GetGoogleCountryCode(
100 google_profile_helper::GetGoogleHomePageURL(profile_)),
101 google_apis::GetAPIKey(),
102 google_util::GetGoogleSearchURL(
103 google_profile_helper::GetGoogleHomePageURL(profile_))));