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