21415ce20c1335758ac95f2215c0cf5619c6dabc
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / alternate_error_tab_observer.cc
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 "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"
17
18 using content::RenderViewHost;
19 using content::WebContents;
20
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AlternateErrorPageTabObserver);
22
23 AlternateErrorPageTabObserver::AlternateErrorPageTabObserver(
24     WebContents* web_contents)
25     : content::WebContentsObserver(web_contents),
26       profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
27   PrefService* prefs = profile_->GetPrefs();
28   if (prefs) {
29     pref_change_registrar_.Init(prefs);
30     pref_change_registrar_.Add(
31         prefs::kAlternateErrorPagesEnabled,
32         base::Bind(&AlternateErrorPageTabObserver::
33                        OnAlternateErrorPagesEnabledChanged,
34                    base::Unretained(this)));
35   }
36
37   registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
38                  content::Source<Profile>(profile_->GetOriginalProfile()));
39 }
40
41 AlternateErrorPageTabObserver::~AlternateErrorPageTabObserver() {
42 }
43
44 // static
45 void AlternateErrorPageTabObserver::RegisterProfilePrefs(
46     user_prefs::PrefRegistrySyncable* prefs) {
47   prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled,
48                              true,
49                              user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
50 }
51
52 ////////////////////////////////////////////////////////////////////////////////
53 // WebContentsObserver overrides
54
55 void AlternateErrorPageTabObserver::RenderViewCreated(
56     RenderViewHost* render_view_host) {
57   UpdateAlternateErrorPageURL(render_view_host);
58 }
59
60 ////////////////////////////////////////////////////////////////////////////////
61 // content::NotificationObserver overrides
62
63 void AlternateErrorPageTabObserver::Observe(
64     int type,
65     const content::NotificationSource& source,
66     const content::NotificationDetails& details) {
67   DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_URL_UPDATED, type);
68   UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost());
69 }
70
71 ////////////////////////////////////////////////////////////////////////////////
72 // Internal helpers
73
74 GURL AlternateErrorPageTabObserver::GetAlternateErrorPageURL() const {
75   GURL url;
76   // Disable alternate error pages when in Incognito mode.
77   if (profile_->IsOffTheRecord())
78     return url;
79
80   if (profile_->GetPrefs()->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
81     url = google_util::LinkDoctorBaseURL();
82     if (!url.is_valid())
83       return url;
84     url = google_util::AppendGoogleLocaleParam(url);
85     url = google_util::AppendGoogleTLDParam(profile_, url);
86   }
87   return url;
88 }
89
90 void AlternateErrorPageTabObserver::OnAlternateErrorPagesEnabledChanged() {
91   UpdateAlternateErrorPageURL(web_contents()->GetRenderViewHost());
92 }
93
94 void AlternateErrorPageTabObserver::UpdateAlternateErrorPageURL(
95     RenderViewHost* rvh) {
96   rvh->Send(new ChromeViewMsg_SetAltErrorPageURL(
97                 rvh->GetRoutingID(), GetAlternateErrorPageURL()));
98 }