| 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 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver( | 19 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver( |
| 20 WebContents* web_contents) | 20 WebContents* web_contents, |
| 21 : content::WebContentsObserver(web_contents) { | 21 Profile* profile) |
| 22 PrefService* prefs = GetProfile()->GetPrefs(); | 22 : content::WebContentsObserver(web_contents), |
| 23 profile_(profile) { |
| 24 PrefService* prefs = profile_->GetPrefs(); |
| 23 if (prefs) { | 25 if (prefs) { |
| 24 pref_change_registrar_.Init(prefs); | 26 pref_change_registrar_.Init(prefs); |
| 25 pref_change_registrar_.Add(prefs::kAlternateErrorPagesEnabled, this); | 27 pref_change_registrar_.Add(prefs::kAlternateErrorPagesEnabled, this); |
| 26 } | 28 } |
| 27 | 29 |
| 28 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, | 30 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 29 content::NotificationService::AllSources()); | 31 content::NotificationService::AllSources()); |
| 30 } | 32 } |
| 31 | 33 |
| 32 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() { | 34 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() { |
| 33 } | 35 } |
| 34 | 36 |
| 35 // static | 37 // static |
| 36 void AlternateErrorPageTabObserver::RegisterUserPrefs(PrefService* prefs) { | 38 void AlternateErrorPageTabObserver::RegisterUserPrefs(PrefService* prefs) { |
| 37 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, | 39 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true, |
| 38 true, | |
| 39 PrefService::SYNCABLE_PREF); | 40 PrefService::SYNCABLE_PREF); |
| 40 } | 41 } |
| 41 | 42 |
| 42 Profile* AlternateErrorPageTabObserver::GetProfile() const { | |
| 43 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 44 } | |
| 45 | |
| 46 //////////////////////////////////////////////////////////////////////////////// | 43 //////////////////////////////////////////////////////////////////////////////// |
| 47 // WebContentsObserver overrides | 44 // WebContentsObserver overrides |
| 48 | 45 |
| 49 void AlternateErrorPageTabObserver::RenderViewCreated( | 46 void AlternateErrorPageTabObserver::RenderViewCreated( |
| 50 RenderViewHost* render_view_host) { | 47 RenderViewHost* render_view_host) { |
| 51 UpdateAlternateErrorPageURL(render_view_host); | 48 UpdateAlternateErrorPageURL(render_view_host); |
| 52 } | 49 } |
| 53 | 50 |
| 54 //////////////////////////////////////////////////////////////////////////////// | 51 //////////////////////////////////////////////////////////////////////////////// |
| 55 // content::NotificationObserver overrides | 52 // content::NotificationObserver overrides |
| 56 | 53 |
| 57 void AlternateErrorPageTabObserver::Observe(int type, | 54 void AlternateErrorPageTabObserver::Observe(int type, |
| 58 const content::NotificationSource& source, | 55 const content::NotificationSource& source, |
| 59 const content::NotificationDetails& details) { | 56 const content::NotificationDetails& details) { |
| 60 switch (type) { | 57 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 61 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: | 58 DCHECK_EQ(profile_->GetPrefs(), content::Source<PrefService>(source).ptr()); |
| 62 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); | 59 DCHECK_EQ(std::string(prefs::kAlternateErrorPagesEnabled), |
| 63 break; | 60 *content::Details<std::string>(details).ptr()); |
| 64 case chrome::NOTIFICATION_PREF_CHANGED: { | 61 } else { |
| 65 std::string* pref_name = content::Details<std::string>(details).ptr(); | 62 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type); |
| 66 DCHECK(content::Source<PrefService>(source).ptr() == | |
| 67 GetProfile()->GetPrefs()); | |
| 68 if (*pref_name == prefs::kAlternateErrorPagesEnabled) { | |
| 69 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); | |
| 70 } else { | |
| 71 NOTREACHED() << "unexpected pref change notification" << *pref_name; | |
| 72 } | |
| 73 break; | |
| 74 } | |
| 75 default: | |
| 76 NOTREACHED(); | |
| 77 } | 63 } |
| 64 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost()); |
| 78 } | 65 } |
| 79 | 66 |
| 80 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
| 81 // Internal helpers | 68 // Internal helpers |
| 82 | 69 |
| 83 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const { | 70 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const { |
| 84 GURL url; | 71 GURL url; |
| 85 // Disable alternate error pages when in Incognito mode. | 72 // Disable alternate error pages when in Incognito mode. |
| 86 Profile* profile = GetProfile(); | 73 if (profile_->IsOffTheRecord()) |
| 87 if (profile->IsOffTheRecord()) | |
| 88 return url; | 74 return url; |
| 89 | 75 |
| 90 if (profile->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { | 76 if (profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) { |
| 91 url = google_util::AppendGoogleLocaleParam( | 77 url = google_util::AppendGoogleLocaleParam( |
| 92 GURL(google_util::kLinkDoctorBaseURL)); | 78 GURL(google_util::kLinkDoctorBaseURL)); |
| 93 url = google_util::AppendGoogleTLDParam(url); | 79 url = google_util::AppendGoogleTLDParam(url); |
| 94 } | 80 } |
| 95 return url; | 81 return url; |
| 96 } | 82 } |
| 97 | 83 |
| 98 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL( | 84 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL( |
| 99 RenderViewHost* rvh) { | 85 RenderViewHost* rvh) { |
| 100 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); | 86 rvh->SetAltErrorPageURL(GetAlternateErrorPageURL()); |
| 101 } | 87 } |
| OLD | NEW |