navigation_correction_tab_observer.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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/chrome_notification_types.h"
9#include "chrome/browser/google/google_profile_helper.h"
10#include "chrome/browser/google/google_url_tracker_factory.h"
11#include "chrome/browser/google/google_util.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/pref_registry/pref_registry_syncable.h"
16#include "content/public/browser/render_frame_host.h"
17#include "content/public/browser/render_view_host.h"
18#include "content/public/browser/web_contents.h"
19#include "google_apis/google_api_keys.h"
20
21using content::RenderFrameHost;
22using content::RenderViewHost;
23using content::WebContents;
24
25DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver);
26
27NavigationCorrectionTabObserver::NavigationCorrectionTabObserver(
28    WebContents* web_contents)
29    : content::WebContentsObserver(web_contents),
30      profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
31  PrefService* prefs = profile_->GetPrefs();
32  if (prefs) {
33    pref_change_registrar_.Init(prefs);
34    pref_change_registrar_.Add(
35        prefs::kAlternateErrorPagesEnabled,
36        base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged,
37                   base::Unretained(this)));
38  }
39
40  GoogleURLTracker* google_url_tracker =
41      GoogleURLTrackerFactory::GetForProfile(profile_);
42  if (google_url_tracker) {
43    google_url_updated_subscription_ = google_url_tracker->RegisterCallback(
44        base::Bind(&NavigationCorrectionTabObserver::OnGoogleURLUpdated,
45                   base::Unretained(this)));
46  }
47}
48
49NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
50}
51
52// static
53void NavigationCorrectionTabObserver::RegisterProfilePrefs(
54    user_prefs::PrefRegistrySyncable* prefs) {
55  prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled,
56                             true,
57                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
58}
59
60////////////////////////////////////////////////////////////////////////////////
61// WebContentsObserver overrides
62
63void NavigationCorrectionTabObserver::RenderViewCreated(
64    RenderViewHost* render_view_host) {
65  UpdateNavigationCorrectionInfo(render_view_host);
66}
67
68////////////////////////////////////////////////////////////////////////////////
69// Internal helpers
70
71void NavigationCorrectionTabObserver::OnGoogleURLUpdated(GURL old_url,
72                                                         GURL new_url) {
73  UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
74}
75
76GURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
77  // Disable navigation corrections when the preference is disabled or when in
78  // Incognito mode.
79  if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) ||
80      profile_->IsOffTheRecord()) {
81    return GURL();
82  }
83
84  return google_util::LinkDoctorBaseURL();
85}
86
87void NavigationCorrectionTabObserver::OnEnabledChanged() {
88  UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
89}
90
91void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
92    RenderViewHost* rvh) {
93  RenderFrameHost* rfh = rvh->GetMainFrame();
94  rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
95      rfh->GetRoutingID(),
96      GetNavigationCorrectionURL(),
97      google_util::GetGoogleLocale(),
98      google_util::GetGoogleCountryCode(
99          google_profile_helper::GetGoogleHomePageURL(profile_)),
100      google_apis::GetAPIKey(),
101      google_util::GetGoogleSearchURL(
102          google_profile_helper::GetGoogleHomePageURL(profile_))));
103}
104