navigation_correction_tab_observer.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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
22using content::RenderFrameHost;
23using content::RenderViewHost;
24using content::WebContents;
25
26DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver);
27
28NavigationCorrectionTabObserver::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
50NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
51}
52
53// static
54void 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
64void NavigationCorrectionTabObserver::RenderViewCreated(
65    RenderViewHost* render_view_host) {
66  UpdateNavigationCorrectionInfo(render_view_host);
67}
68
69////////////////////////////////////////////////////////////////////////////////
70// Internal helpers
71
72void NavigationCorrectionTabObserver::OnGoogleURLUpdated(GURL old_url,
73                                                         GURL new_url) {
74  UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
75}
76
77GURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
78  // Disable navigation corrections when the preference is disabled or when in
79  // Incognito mode.
80  if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) ||
81      profile_->IsOffTheRecord()) {
82    return GURL();
83  }
84
85  return google_util::LinkDoctorBaseURL();
86}
87
88void NavigationCorrectionTabObserver::OnEnabledChanged() {
89  UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
90}
91
92void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
93    RenderViewHost* rvh) {
94  RenderFrameHost* rfh = rvh->GetMainFrame();
95  rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
96      rfh->GetRoutingID(),
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_))));
104}
105