OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/navigation_correction_tab_observer.h" | 5 #include "chrome/browser/ui/navigation_correction_tab_observer.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/google/google_url_tracker_factory.h" |
9 #include "chrome/browser/google/google_util.h" | 10 #include "chrome/browser/google/google_util.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
12 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
13 #include "components/pref_registry/pref_registry_syncable.h" | 14 #include "components/pref_registry/pref_registry_syncable.h" |
14 #include "content/public/browser/notification_service.h" | |
15 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "google_apis/google_api_keys.h" | 18 #include "google_apis/google_api_keys.h" |
19 | 19 |
20 using content::RenderFrameHost; | 20 using content::RenderFrameHost; |
21 using content::RenderViewHost; | 21 using content::RenderViewHost; |
22 using content::WebContents; | 22 using content::WebContents; |
23 | 23 |
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver); | 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NavigationCorrectionTabObserver); |
25 | 25 |
26 NavigationCorrectionTabObserver::NavigationCorrectionTabObserver( | 26 NavigationCorrectionTabObserver::NavigationCorrectionTabObserver( |
27 WebContents* web_contents) | 27 WebContents* web_contents) |
28 : content::WebContentsObserver(web_contents), | 28 : content::WebContentsObserver(web_contents), |
29 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) { | 29 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) { |
30 PrefService* prefs = profile_->GetPrefs(); | 30 PrefService* prefs = profile_->GetPrefs(); |
31 if (prefs) { | 31 if (prefs) { |
32 pref_change_registrar_.Init(prefs); | 32 pref_change_registrar_.Init(prefs); |
33 pref_change_registrar_.Add( | 33 pref_change_registrar_.Add( |
34 prefs::kAlternateErrorPagesEnabled, | 34 prefs::kAlternateErrorPagesEnabled, |
35 base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged, | 35 base::Bind(&NavigationCorrectionTabObserver::OnEnabledChanged, |
36 base::Unretained(this))); | 36 base::Unretained(this))); |
37 } | 37 } |
38 | 38 |
39 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 39 GoogleURLTracker* google_url_tracker = |
40 content::Source<Profile>(profile_->GetOriginalProfile())); | 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))); |
| 45 } |
41 } | 46 } |
42 | 47 |
43 NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() { | 48 NavigationCorrectionTabObserver::~NavigationCorrectionTabObserver() { |
44 } | 49 } |
45 | 50 |
46 // static | 51 // static |
47 void NavigationCorrectionTabObserver::RegisterProfilePrefs( | 52 void NavigationCorrectionTabObserver::RegisterProfilePrefs( |
48 user_prefs::PrefRegistrySyncable* prefs) { | 53 user_prefs::PrefRegistrySyncable* prefs) { |
49 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, | 54 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, |
50 true, | 55 true, |
51 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 56 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
52 } | 57 } |
53 | 58 |
54 //////////////////////////////////////////////////////////////////////////////// | 59 //////////////////////////////////////////////////////////////////////////////// |
55 // WebContentsObserver overrides | 60 // WebContentsObserver overrides |
56 | 61 |
57 void NavigationCorrectionTabObserver::RenderViewCreated( | 62 void NavigationCorrectionTabObserver::RenderViewCreated( |
58 RenderViewHost* render_view_host) { | 63 RenderViewHost* render_view_host) { |
59 UpdateNavigationCorrectionInfo(render_view_host); | 64 UpdateNavigationCorrectionInfo(render_view_host); |
60 } | 65 } |
61 | 66 |
62 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
63 // content::NotificationObserver overrides | |
64 | |
65 void NavigationCorrectionTabObserver::Observe( | |
66 int type, | |
67 const content::NotificationSource& source, | |
68 const content::NotificationDetails& details) { | |
69 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); | |
70 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost()); | |
71 } | |
72 | |
73 //////////////////////////////////////////////////////////////////////////////// | |
74 // Internal helpers | 68 // Internal helpers |
75 | 69 |
| 70 void NavigationCorrectionTabObserver::OnGoogleURLUpdated(GURL old_url, |
| 71 GURL new_url) { |
| 72 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost()); |
| 73 } |
| 74 |
76 GURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const { | 75 GURL NavigationCorrectionTabObserver::GetNavigationCorrectionURL() const { |
77 // Disable navigation corrections when the preference is disabled or when in | 76 // Disable navigation corrections when the preference is disabled or when in |
78 // Incognito mode. | 77 // Incognito mode. |
79 if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) || | 78 if (!profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled) || |
80 profile_->IsOffTheRecord()) { | 79 profile_->IsOffTheRecord()) { |
81 return GURL(); | 80 return GURL(); |
82 } | 81 } |
83 | 82 |
84 return google_util::LinkDoctorBaseURL(); | 83 return google_util::LinkDoctorBaseURL(); |
85 } | 84 } |
86 | 85 |
87 void NavigationCorrectionTabObserver::OnEnabledChanged() { | 86 void NavigationCorrectionTabObserver::OnEnabledChanged() { |
88 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost()); | 87 UpdateNavigationCorrectionInfo(web_contents()->GetRenderViewHost()); |
89 } | 88 } |
90 | 89 |
91 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo( | 90 void NavigationCorrectionTabObserver::UpdateNavigationCorrectionInfo( |
92 RenderViewHost* rvh) { | 91 RenderViewHost* rvh) { |
93 RenderFrameHost* rfh = rvh->GetMainFrame(); | 92 RenderFrameHost* rfh = rvh->GetMainFrame(); |
94 rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo( | 93 rfh->Send(new ChromeViewMsg_SetNavigationCorrectionInfo( |
95 rfh->GetRoutingID(), GetNavigationCorrectionURL(), | 94 rfh->GetRoutingID(), GetNavigationCorrectionURL(), |
96 google_util::GetGoogleLocale(), | 95 google_util::GetGoogleLocale(), |
97 google_util::GetGoogleCountryCode(profile_), google_apis::GetAPIKey(), | 96 google_util::GetGoogleCountryCode(profile_), google_apis::GetAPIKey(), |
98 google_util::GetGoogleSearchURL(profile_))); | 97 google_util::GetGoogleSearchURL(profile_))); |
99 } | 98 } |
OLD | NEW |