15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "chrome/browser/ui/navigation_correction_tab_observer.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/browser_process.h"
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/google/google_profile_helper.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/google/google_url_tracker_factory.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/render_messages.h"
156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/google/core/browser/google_util.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/render_frame_host.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
20a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "google_apis/google_api_keys.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using content::RenderFrameHost;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::RenderViewHost;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
26a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochDEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochNavigationCorrectionTabObserver::NavigationCorrectionTabObserver(
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* web_contents)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : content::WebContentsObserver(web_contents),
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* prefs = profile_->GetPrefs();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (prefs) {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_change_registrar_.Init(prefs);
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    pref_change_registrar_.Add(
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        prefs::kAlternateErrorPagesEnabled,
37a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged,
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   base::Unretained(this)));
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GoogleURLTracker* google_url_tracker =
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GoogleURLTrackerFactory::GetForProfile(profile_);
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (google_url_tracker) {
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    google_url_updated_subscription_ = google_url_tracker->RegisterCallback(
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::Bind(&NavigationCorrectionTabObserver::OnGoogleURLUpdated,
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   base::Unretained(this)));
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
50a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochNavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid NavigationCorrectionTabObserver::RegisterProfilePrefs(
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* prefs) {
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled,
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             true,
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WebContentsObserver overrides
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
64a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid NavigationCorrectionTabObserver::RenderViewCreated(
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RenderViewHost* render_view_host) {
66a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  UpdateNavigationCorrectionInfo(render_view_host);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Internal helpers
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid NavigationCorrectionTabObserver::OnGoogleURLUpdated() {
73a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
76a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochGURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const {
77a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Disable navigation corrections when the preference is disabled or when in
78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Incognito mode.
79a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) ||
80a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      profile_->IsOffTheRecord()) {
81a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return GURL();
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
83a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
84a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return google_util::LinkDoctorBaseURL();
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
87a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid NavigationCorrectionTabObserver::OnEnabledChanged() {
88a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost());
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
91a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RenderViewHost* rvh) {
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderFrameHost* rfh = rvh->GetMainFrame();
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo(
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      rfh->GetRoutingID(),
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetNavigationCorrectionURL(),
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      google_util::GetGoogleLocale(g_browser_process->GetApplicationLocale()),
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      google_util::GetGoogleCountryCode(
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          google_profile_helper::GetGoogleHomePageURL(profile_)),
10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      google_apis::GetAPIKey(),
10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      google_util::GetGoogleSearchURL(
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          google_profile_helper::GetGoogleHomePageURL(profile_))));
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
104