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