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_frame_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
19 using content::RenderFrameHost
;
20 using content::RenderViewHost
;
21 using content::WebContents
;
23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AlternateErrorPageTabObserver
);
25 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver(
26 WebContents
* web_contents
)
27 : content::WebContentsObserver(web_contents
),
28 profile_(Profile::FromBrowserContext(web_contents
->GetBrowserContext())) {
29 PrefService
* prefs
= profile_
->GetPrefs();
31 pref_change_registrar_
.Init(prefs
);
32 pref_change_registrar_
.Add(
33 prefs::kAlternateErrorPagesEnabled
,
34 base::Bind(&AlternateErrorPageTabObserver::
35 OnAlternateErrorPagesEnabledChanged
,
36 base::Unretained(this)));
39 registrar_
.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED
,
40 content::Source
<Profile
>(profile_
->GetOriginalProfile()));
43 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() {
47 void AlternateErrorPageTabObserver::RegisterProfilePrefs(
48 user_prefs::PrefRegistrySyncable
* prefs
) {
49 prefs
->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled
,
51 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
54 ////////////////////////////////////////////////////////////////////////////////
55 // WebContentsObserver overrides
57 void AlternateErrorPageTabObserver::RenderViewCreated(
58 RenderViewHost
* render_view_host
) {
59 UpdateAlternateErrorPageURL(render_view_host
);
62 ////////////////////////////////////////////////////////////////////////////////
63 // content::NotificationObserver overrides
65 void AlternateErrorPageTabObserver::Observe(
67 const content::NotificationSource
& source
,
68 const content::NotificationDetails
& details
) {
69 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED
, type
);
70 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost());
73 ////////////////////////////////////////////////////////////////////////////////
76 GURL
AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const {
78 // Disable alternate error pages when in Incognito mode.
79 if (profile_
->IsOffTheRecord())
82 if (profile_
->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled
)) {
83 url
= google_util::LinkDoctorBaseURL();
86 url
= google_util::AppendGoogleLocaleParam(url
);
87 url
= google_util::AppendGoogleTLDParam(profile_
, url
);
92 void AlternateErrorPageTabObserver::OnAlternateErrorPagesEnabledChanged() {
93 UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost());
96 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL(
97 RenderViewHost
* rvh
) {
98 RenderFrameHost
* rfh
= rvh
->GetMainFrame();
99 rfh
->Send(new ChromeViewMsg_SetAltErrorPageURL(
100 rfh
->GetRoutingID(), GetAlternateErrorPageURL()));