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/chrome_notification_types.h"
9 #include "chrome/browser/google/google_url_tracker_factory.h"
10 #include "chrome/browser/google/google_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/common/render_messages.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "google_apis/google_api_keys.h"
20 using content::RenderFrameHost
;
21 using content::RenderViewHost
;
22 using content::WebContents
;
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver
);
26 NavigationCorrectionTabObserver::NavigationCorrectionTabObserver(
27 WebContents
* web_contents
)
28 : content::WebContentsObserver(web_contents
),
29 profile_(Profile::FromBrowserContext(web_contents
->GetBrowserContext())) {
30 PrefService
* prefs
= profile_
->GetPrefs();
32 pref_change_registrar_
.Init(prefs
);
33 pref_change_registrar_
.Add(
34 prefs::kAlternateErrorPagesEnabled
,
35 base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged
,
36 base::Unretained(this)));
39 GoogleURLTracker
* google_url_tracker
=
40 GoogleURLTrackerFactory::GetForProfile(profile_
);
41 if (google_url_tracker
) {
42 google_url_updated_subscription_
= google_url_tracker
->RegisterCallback(
43 base::Bind(&NavigationCorrectionTabObserver::OnGoogleURLUpdated
,
44 base::Unretained(this)));
48 NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
52 void NavigationCorrectionTabObserver::RegisterProfilePrefs(
53 user_prefs::PrefRegistrySyncable
* prefs
) {
54 prefs
->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled
,
56 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
59 ////////////////////////////////////////////////////////////////////////////////
60 // WebContentsObserver overrides
62 void NavigationCorrectionTabObserver::RenderViewCreated(
63 RenderViewHost
* render_view_host
) {
64 UpdateNavigationCorrectionInfo(render_view_host
);
67 ////////////////////////////////////////////////////////////////////////////////
70 void NavigationCorrectionTabObserver::OnGoogleURLUpdated(GURL old_url
,
72 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
75 GURL
NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
76 // Disable navigation corrections when the preference is disabled or when in
78 if (!profile_
->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled
) ||
79 profile_
->IsOffTheRecord()) {
83 return google_util::LinkDoctorBaseURL();
86 void NavigationCorrectionTabObserver::OnEnabledChanged() {
87 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
90 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
91 RenderViewHost
* rvh
) {
92 RenderFrameHost
* rfh
= rvh
->GetMainFrame();
93 rfh
->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
94 rfh
->GetRoutingID(), GetNavigationCorrectionURL(),
95 google_util::GetGoogleLocale(),
96 google_util::GetGoogleCountryCode(profile_
), google_apis::GetAPIKey(),
97 google_util::GetGoogleSearchURL(profile_
)));