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_url_tracker_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.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 if (google_util::IsGoogleDomainUrl(GetNavigationCorrectionURL(),
45 google_util::ALLOW_SUBDOMAIN
,
46 google_util::ALLOW_NON_STANDARD_PORTS
))
47 google_url_tracker
->RequestServerCheck(false);
48 google_url_updated_subscription_
= google_url_tracker
->RegisterCallback(
49 base::Bind(&NavigationCorrectionTabObserver::OnGoogleURLUpdated
,
50 base::Unretained(this)));
54 NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
58 void NavigationCorrectionTabObserver::RegisterProfilePrefs(
59 user_prefs::PrefRegistrySyncable
* prefs
) {
60 prefs
->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled
,
62 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
65 ////////////////////////////////////////////////////////////////////////////////
66 // WebContentsObserver overrides
68 void NavigationCorrectionTabObserver::RenderViewCreated(
69 RenderViewHost
* render_view_host
) {
70 UpdateNavigationCorrectionInfo(render_view_host
);
73 ////////////////////////////////////////////////////////////////////////////////
76 void NavigationCorrectionTabObserver::OnGoogleURLUpdated() {
77 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
80 GURL
NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
81 // Disable navigation corrections when the preference is disabled or when in
83 if (!profile_
->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled
) ||
84 profile_
->IsOffTheRecord()) {
88 return google_util::LinkDoctorBaseURL();
91 void NavigationCorrectionTabObserver::OnEnabledChanged() {
92 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
95 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
96 RenderViewHost
* rvh
) {
97 RenderFrameHost
* rfh
= rvh
->GetMainFrame();
98 GURL
google_base_url(UIThreadSearchTermsData(profile_
).GoogleBaseURLValue());
99 rfh
->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
101 GetNavigationCorrectionURL(),
102 google_util::GetGoogleLocale(g_browser_process
->GetApplicationLocale()),
103 google_util::GetGoogleCountryCode(google_base_url
),
104 google_apis::GetAPIKey(),
105 google_util::GetGoogleSearchURL(google_base_url
)));