| 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( | 28 pref_change_registrar_.Add(prefs::kAlternateErrorPagesEnabled, this); |
| 29 prefs::kAlternateErrorPagesEnabled, | |
| 30 base::Bind(&AlternateErrorPageTabObserver:: | |
| 31 OnAlternateErrorPagesEnabledChanged, | |
| 32 base::Unretained(this))); | |
| 33 } | 29 } |
| 34 | 30 |
| 35 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 31 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 36 content::Source<Profile>(profile_->GetOriginalProfile())); | 32 content::Source<Profile>(profile_->GetOriginalProfile())); |
| 37 } | 33 } |
| 38 | 34 |
| 39 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() { | 35 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() { |
| 40 } | 36 } |
| 41 | 37 |
| 42 // static | 38 // static |
| (...skipping 15 matching lines...) Expand all Loading... |
| 58 | 54 |
| 59 void AlternateErrorPageTabObserver::Observe( | 55 void AlternateErrorPageTabObserver::Observe( |
| 60 int type, | 56 int type, |
| 61 const content::NotificationSource& source, | 57 const content::NotificationSource& source, |
| 62 const content::NotificationDetails& details) { | 58 const content::NotificationDetails& details) { |
| 63 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); | 59 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); |
| 64 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); | 60 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); |
| 65 } | 61 } |
| 66 | 62 |
| 67 //////////////////////////////////////////////////////////////////////////////// | 63 //////////////////////////////////////////////////////////////////////////////// |
| 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 //////////////////////////////////////////////////////////////////////////////// |
| 68 // Internal helpers | 75 // Internal helpers |
| 69 | 76 |
| 70 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const { | 77 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const { |
| 71 GURL url; | 78 GURL url; |
| 72 // Disable alternate error pages when in Incognito mode. | 79 // Disable alternate error pages when in Incognito mode. |
| 73 if (profile_->IsOffTheRecord()) | 80 if (profile_->IsOffTheRecord()) |
| 74 return url; | 81 return url; |
| 75 | 82 |
| 76 if (profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { | 83 if (profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { |
| 77 url = google_util::LinkDoctorBaseURL(); | 84 url = google_util::LinkDoctorBaseURL(); |
| 78 if (!url.is_valid()) | 85 if (!url.is_valid()) |
| 79 return url; | 86 return url; |
| 80 url = google_util::AppendGoogleLocaleParam(url); | 87 url = google_util::AppendGoogleLocaleParam(url); |
| 81 url = google_util::AppendGoogleTLDParam(profile_, url); | 88 url = google_util::AppendGoogleTLDParam(profile_, url); |
| 82 } | 89 } |
| 83 return url; | 90 return url; |
| 84 } | 91 } |
| 85 | 92 |
| 86 void AlternateErrorPageTabObserver::OnAlternateErrorPagesEnabledChanged() { | |
| 87 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); | |
| 88 } | |
| 89 | |
| 90 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL( | 93 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL( |
| 91 RenderViewHost* rvh) { | 94 RenderViewHost* rvh) { |
| 92 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); | 95 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); |
| 93 } | 96 } |
| OLD | NEW |