Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / uber / uber_ui.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/webui/uber/uber_ui.h"
6
7 #include "base/stl_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
13 #include "chrome/browser/ui/webui/extensions/extensions_ui.h"
14 #include "chrome/browser/ui/webui/options/options_ui.h"
15 #include "chrome/common/extensions/manifest_url_handler.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_ui.h"
22 #include "content/public/browser/web_ui_data_source.h"
23 #include "extensions/common/extension_set.h"
24 #include "grit/browser_resources.h"
25 #include "grit/chromium_strings.h"
26 #include "grit/generated_resources.h"
27
28 using base::ASCIIToUTF16;
29 using content::NavigationController;
30 using content::NavigationEntry;
31 using content::RenderViewHost;
32 using content::WebContents;
33
34 namespace {
35
36 content::WebUIDataSource* CreateUberHTMLSource() {
37   content::WebUIDataSource* source =
38       content::WebUIDataSource::Create(chrome::kChromeUIUberHost);
39
40   source->SetUseJsonJSFormatV2();
41   source->SetJsonPath("strings.js");
42   source->AddResourcePath("uber.js", IDR_UBER_JS);
43   source->AddResourcePath("uber_utils.js", IDR_UBER_UTILS_JS);
44   source->SetDefaultResource(IDR_UBER_HTML);
45   source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;");
46
47   // Hack alert: continue showing "Loading..." until a real title is set.
48   source->AddLocalizedString("pageTitle", IDS_TAB_LOADING_TITLE);
49
50   source->AddString("extensionsFrameURL",
51                     ASCIIToUTF16(chrome::kChromeUIExtensionsFrameURL));
52   source->AddString("extensionsHost",
53                     ASCIIToUTF16(chrome::kChromeUIExtensionsHost));
54   source->AddString("helpFrameURL",
55                     ASCIIToUTF16(chrome::kChromeUIHelpFrameURL));
56   source->AddString("helpHost",
57                     ASCIIToUTF16(chrome::kChromeUIHelpHost));
58   source->AddString("historyFrameURL",
59                     ASCIIToUTF16(chrome::kChromeUIHistoryFrameURL));
60   source->AddString("historyHost",
61                     ASCIIToUTF16(chrome::kChromeUIHistoryHost));
62   source->AddString("settingsFrameURL",
63                     ASCIIToUTF16(chrome::kChromeUISettingsFrameURL));
64   source->AddString("settingsHost",
65                     ASCIIToUTF16(chrome::kChromeUISettingsHost));
66
67   return source;
68 }
69
70 // Determines whether the user has an active extension of the given type.
71 bool HasExtensionType(Profile* profile, const char* extensionType) {
72   const extensions::ExtensionSet* extensionSet =
73       profile->GetExtensionService()->extensions();
74
75   for (extensions::ExtensionSet::const_iterator iter = extensionSet->begin();
76        iter != extensionSet->end(); ++iter) {
77     extensions::URLOverrides::URLOverrideMap map =
78         extensions::URLOverrides::GetChromeURLOverrides(iter->get());
79     extensions::URLOverrides::URLOverrideMap::const_iterator result =
80         map.find(std::string(extensionType));
81
82     if (result != map.end())
83       return true;
84   }
85
86   return false;
87 }
88
89 content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) {
90   content::WebUIDataSource* source =
91       content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost);
92
93   source->SetUseJsonJSFormatV2();
94   source->SetJsonPath("strings.js");
95   source->AddResourcePath("uber_frame.js", IDR_UBER_FRAME_JS);
96   source->SetDefaultResource(IDR_UBER_FRAME_HTML);
97
98   // TODO(jhawkins): Attempt to get rid of IDS_SHORT_PRODUCT_OS_NAME.
99 #if defined(OS_CHROMEOS)
100   source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_OS_NAME);
101 #else
102   source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_NAME);
103 #endif  // defined(OS_CHROMEOS)
104
105   source->AddString("extensionsHost",
106                     ASCIIToUTF16(chrome::kChromeUIExtensionsHost));
107   source->AddLocalizedString("extensionsDisplayName",
108                              IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE);
109   source->AddString("helpHost",
110                     ASCIIToUTF16(chrome::kChromeUIHelpHost));
111   source->AddLocalizedString("helpDisplayName", IDS_HELP_TITLE);
112   source->AddString("historyHost",
113                     ASCIIToUTF16(chrome::kChromeUIHistoryHost));
114   source->AddLocalizedString("historyDisplayName", IDS_HISTORY_TITLE);
115   source->AddString("settingsHost",
116                     ASCIIToUTF16(chrome::kChromeUISettingsHost));
117   source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE);
118   bool overridesHistory = HasExtensionType(profile,
119       chrome::kChromeUIHistoryHost);
120   source->AddString("overridesHistory",
121                     ASCIIToUTF16(overridesHistory ? "yes" : "no"));
122   source->DisableDenyXFrameOptions();
123   source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;");
124
125   return source;
126 }
127
128 }  // namespace
129
130 UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) {
131   Profile* profile = Profile::FromWebUI(web_ui);
132   content::WebUIDataSource::Add(profile, CreateUberHTMLSource());
133
134   RegisterSubpage(chrome::kChromeUIExtensionsFrameURL);
135   RegisterSubpage(chrome::kChromeUIHelpFrameURL);
136   RegisterSubpage(chrome::kChromeUIHistoryFrameURL);
137   RegisterSubpage(chrome::kChromeUISettingsFrameURL);
138   RegisterSubpage(chrome::kChromeUIUberFrameURL);
139 }
140
141 UberUI::~UberUI() {
142   STLDeleteValues(&sub_uis_);
143 }
144
145 void UberUI::RegisterSubpage(const std::string& page_url) {
146   content::WebUI* webui =
147       web_ui()->GetWebContents()->CreateWebUI(GURL(page_url));
148
149   webui->SetFrameXPath("//iframe[starts-with(@src,'" + page_url + "')]");
150   sub_uis_[page_url] = webui;
151 }
152
153 void UberUI::RenderViewCreated(RenderViewHost* render_view_host) {
154   for (SubpageMap::iterator iter = sub_uis_.begin(); iter != sub_uis_.end();
155        ++iter) {
156     iter->second->GetController()->RenderViewCreated(render_view_host);
157   }
158 }
159
160 void UberUI::RenderViewReused(RenderViewHost* render_view_host) {
161   for (SubpageMap::iterator iter = sub_uis_.begin(); iter != sub_uis_.end();
162        ++iter) {
163     iter->second->GetController()->RenderViewReused(render_view_host);
164   }
165 }
166
167 bool UberUI::OverrideHandleWebUIMessage(const GURL& source_url,
168                                         const std::string& message,
169                                         const base::ListValue& args) {
170   // Find the appropriate subpage and forward the message.
171   SubpageMap::iterator subpage = sub_uis_.find(source_url.GetOrigin().spec());
172   if (subpage == sub_uis_.end()) {
173     // The message was sent from the uber page itself.
174     DCHECK_EQ(std::string(chrome::kChromeUIUberHost), source_url.host());
175     return false;
176   }
177
178   // The message was sent from a subpage.
179   // TODO(jam) fix this to use interface
180   // return subpage->second->GetController()->OverrideHandleWebUIMessage(
181   //     source_url, message, args);
182   subpage->second->ProcessWebUIMessage(source_url, message, args);
183   return true;
184 }
185
186 // UberFrameUI
187
188 UberFrameUI::UberFrameUI(content::WebUI* web_ui) : WebUIController(web_ui) {
189   Profile* profile = Profile::FromWebUI(web_ui);
190   content::WebUIDataSource::Add(profile, CreateUberFrameHTMLSource(profile));
191
192   // Register as an observer for when extensions are loaded and unloaded.
193   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
194       content::Source<Profile>(profile));
195   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
196       content::Source<Profile>(profile));
197 }
198
199 UberFrameUI::~UberFrameUI() {
200 }
201
202 void UberFrameUI::Observe(int type, const content::NotificationSource& source,
203                           const content::NotificationDetails& details) {
204   switch (type) {
205     // We listen for notifications that indicate an extension has been loaded
206     // (i.e., has been installed and/or enabled) or unloaded (i.e., has been
207     // uninstalled and/or disabled). If one of these events has occurred, then
208     // we must update the behavior of the History navigation element so that
209     // it opens the history extension if one is installed and enabled or
210     // opens the default history page if one is uninstalled or disabled.
211     case chrome::NOTIFICATION_EXTENSION_LOADED:
212     case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
213       Profile* profile = Profile::FromWebUI(web_ui());
214       bool overrides_history =
215           HasExtensionType(profile, chrome::kChromeUIHistoryHost);
216       web_ui()->CallJavascriptFunction(
217           "uber_frame.setNavigationOverride",
218           base::StringValue(chrome::kChromeUIHistoryHost),
219           base::StringValue(overrides_history ? "yes" : "no"));
220       break;
221     }
222     default:
223       NOTREACHED();
224   }
225 }