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.
5 #include "chrome/browser/ui/alternate_error_tab_observer.h"
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 "chrome/common/render_messages.h"
13 #include "components/user_prefs/pref_registry_syncable.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
18 using content::RenderViewHost
;
19 using content::WebContents
;
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AlternateErrorPageTabObserver
);
23 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver(
24 WebContents
* web_contents
)
25 : content::WebContentsObserver(web_contents
),
26 profile_(Profile::FromBrowserContext(web_contents
->GetBrowserContext())) {
27 PrefService
* prefs
= profile_
->GetPrefs();
29 pref_change_registrar_
.Init(prefs
);
30 pref_change_registrar_
.Add(
31 prefs::kAlternateErrorPagesEnabled
,
32 base::Bind(&AlternateErrorPageTabObserver::
33 OnAlternateErrorPagesEnabledChanged
,
34 base::Unretained(this)));
37 registrar_
.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED
,
38 content::Source
<Profile
>(profile_
->GetOriginalProfile()));
41 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() {
45 void AlternateErrorPageTabObserver::RegisterProfilePrefs(
46 user_prefs::PrefRegistrySyncable
* prefs
) {
47 prefs
->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled
,
49 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
52 ////////////////////////////////////////////////////////////////////////////////
53 // WebContentsObserver overrides
55 void AlternateErrorPageTabObserver::RenderViewCreated(
56 RenderViewHost
* render_view_host
) {
57 UpdateAlternateErrorPageURL(render_view_host
);
60 ////////////////////////////////////////////////////////////////////////////////
61 // content::NotificationObserver overrides
63 void AlternateErrorPageTabObserver::Observe(
65 const content::NotificationSource
& source
,
66 const content::NotificationDetails
& details
) {
67 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED
, type
);
68 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost());
71 ////////////////////////////////////////////////////////////////////////////////
74 GURL
AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const {
76 // Disable alternate error pages when in Incognito mode.
77 if (profile_
->IsOffTheRecord())
80 if (profile_
->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled
)) {
81 url
= google_util::LinkDoctorBaseURL();
84 url
= google_util::AppendGoogleLocaleParam(url
);
85 url
= google_util::AppendGoogleTLDParam(profile_
, url
);
90 void AlternateErrorPageTabObserver::OnAlternateErrorPagesEnabledChanged() {
91 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost());
94 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL(
95 RenderViewHost
* rvh
) {
96 rvh
->Send(new ChromeViewMsg_SetAltErrorPageURL(
97 rvh
->GetRoutingID(), GetAlternateErrorPageURL()));