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() {
73 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
76 GURL
NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
77 // Disable navigation corrections when the preference is disabled or when in
79 if (!profile_
->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled
) ||
80 profile_
->IsOffTheRecord()) {
84 return google_util::LinkDoctorBaseURL();
87 void NavigationCorrectionTabObserver::OnEnabledChanged() {
88 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
91 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
92 RenderViewHost
* rvh
) {
93 RenderFrameHost
* rfh
= rvh
->GetMainFrame();
94 rfh
->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
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_
))));