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/alternate_error_tab_observer.h" | 5 #include "chrome/browser/ui/alternate_error_tab_observer.h" |
6 | 6 |
7 #include "chrome/browser/google/google_util.h" | 7 #include "chrome/browser/google/google_util.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
12 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
13 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 | 15 |
16 using content::RenderViewHost; | 16 using content::RenderViewHost; |
17 using content::WebContents; | 17 using content::WebContents; |
18 | 18 |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AlternateErrorPageTabObserver) | 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AlternateErrorPageTabObserver) |
20 | 20 |
21 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver( | 21 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver( |
22 WebContents* web_contents) | 22 WebContents* web_contents) |
23 : content::WebContentsObserver(web_contents), | 23 : content::WebContentsObserver(web_contents), |
24 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) { | 24 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) { |
25 PrefService* prefs = profile_->GetPrefs(); | 25 PrefService* prefs = profile_->GetPrefs(); |
26 if (prefs) { | 26 if (prefs) { |
27 pref_change_registrar_.Init(prefs); | 27 pref_change_registrar_.Init(prefs); |
28 pref_change_registrar_.Add(prefs::kAlternateErrorPagesEnabled, this); | 28 pref_change_registrar_.Add( |
| 29 prefs::kAlternateErrorPagesEnabled, |
| 30 base::Bind(&AlternateErrorPageTabObserver:: |
| 31 OnAlternateErrorPagesEnabledChanged, |
| 32 base::Unretained(this))); |
29 } | 33 } |
30 | 34 |
31 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 35 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
32 content::Source<Profile>(profile_->GetOriginalProfile())); | 36 content::Source<Profile>(profile_->GetOriginalProfile())); |
33 } | 37 } |
34 | 38 |
35 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() { | 39 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() { |
36 } | 40 } |
37 | 41 |
38 // static | 42 // static |
(...skipping 15 matching lines...) Expand all Loading... |
54 | 58 |
55 void AlternateErrorPageTabObserver::Observe( | 59 void AlternateErrorPageTabObserver::Observe( |
56 int type, | 60 int type, |
57 const content::NotificationSource& source, | 61 const content::NotificationSource& source, |
58 const content::NotificationDetails& details) { | 62 const content::NotificationDetails& details) { |
59 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); | 63 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); |
60 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); | 64 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); |
61 } | 65 } |
62 | 66 |
63 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
64 // PrefObserver overrides | |
65 | |
66 void AlternateErrorPageTabObserver::OnPreferenceChanged( | |
67 PrefServiceBase* service, | |
68 const std::string& pref_name) { | |
69 DCHECK_EQ(profile_->GetPrefs(), service); | |
70 DCHECK(prefs::kAlternateErrorPagesEnabled == pref_name); | |
71 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); | |
72 } | |
73 | |
74 //////////////////////////////////////////////////////////////////////////////// | |
75 // Internal helpers | 68 // Internal helpers |
76 | 69 |
77 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const { | 70 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const { |
78 GURL url; | 71 GURL url; |
79 // Disable alternate error pages when in Incognito mode. | 72 // Disable alternate error pages when in Incognito mode. |
80 if (profile_->IsOffTheRecord()) | 73 if (profile_->IsOffTheRecord()) |
81 return url; | 74 return url; |
82 | 75 |
83 if (profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { | 76 if (profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { |
84 url = google_util::LinkDoctorBaseURL(); | 77 url = google_util::LinkDoctorBaseURL(); |
85 if (!url.is_valid()) | 78 if (!url.is_valid()) |
86 return url; | 79 return url; |
87 url = google_util::AppendGoogleLocaleParam(url); | 80 url = google_util::AppendGoogleLocaleParam(url); |
88 url = google_util::AppendGoogleTLDParam(profile_, url); | 81 url = google_util::AppendGoogleTLDParam(profile_, url); |
89 } | 82 } |
90 return url; | 83 return url; |
91 } | 84 } |
92 | 85 |
| 86 void AlternateErrorPageTabObserver::OnAlternateErrorPagesEnabledChanged() { |
| 87 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); |
| 88 } |
| 89 |
93 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL( | 90 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL( |
94 RenderViewHost* rvh) { | 91 RenderViewHost* rvh) { |
95 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); | 92 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); |
96 } | 93 } |
OLD | NEW |