Upstream version 6.35.121.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                   chrome::kChromeUIExtensionsHost);
136   RegisterSubpage(chrome::kChromeUIHelpFrameURL,
137                   chrome::kChromeUIHelpHost);
138   RegisterSubpage(chrome::kChromeUIHistoryFrameURL,
139                   chrome::kChromeUIHistoryHost);
140   RegisterSubpage(chrome::kChromeUISettingsFrameURL,
141                   chrome::kChromeUISettingsHost);
142   RegisterSubpage(chrome::kChromeUIUberFrameURL,
143                   chrome::kChromeUIUberHost);
144 }
145
146 UberUI::~UberUI() {
147   STLDeleteValues(&sub_uis_);
148 }
149
150 void UberUI::RegisterSubpage(const std::string& page_url,
151                              const std::string& page_host) {
152   GURL page_gurl(page_url);
153   content::WebUI* webui = web_ui()->GetWebContents()->CreateWebUI(page_gurl);
154
155   webui->OverrideJavaScriptFrame(page_host);
156   sub_uis_[page_url] = webui;
157 }
158
159 content::WebUI* UberUI::GetSubpage(const std::string& page_url) {
160   if (!sub_uis_.count(page_url))
161     return NULL;
162   return sub_uis_[page_url];
163 }
164
165 void UberUI::RenderViewCreated(RenderViewHost* render_view_host) {
166   for (SubpageMap::iterator iter = sub_uis_.begin(); iter != sub_uis_.end();
167        ++iter) {
168     iter->second->GetController()->RenderViewCreated(render_view_host);
169   }
170 }
171
172 void UberUI::RenderViewReused(RenderViewHost* render_view_host) {
173   for (SubpageMap::iterator iter = sub_uis_.begin(); iter != sub_uis_.end();
174        ++iter) {
175     iter->second->GetController()->RenderViewReused(render_view_host);
176   }
177 }
178
179 bool UberUI::OverrideHandleWebUIMessage(const GURL& source_url,
180                                         const std::string& message,
181                                         const base::ListValue& args) {
182   // Find the appropriate subpage and forward the message.
183   SubpageMap::iterator subpage = sub_uis_.find(source_url.GetOrigin().spec());
184   if (subpage == sub_uis_.end()) {
185     // The message was sent from the uber page itself.
186     DCHECK_EQ(std::string(chrome::kChromeUIUberHost), source_url.host());
187     return false;
188   }
189
190   // The message was sent from a subpage.
191   // TODO(jam) fix this to use interface
192   // return subpage->second->GetController()->OverrideHandleWebUIMessage(
193   //     source_url, message, args);
194   subpage->second->ProcessWebUIMessage(source_url, message, args);
195   return true;
196 }
197
198 // UberFrameUI
199
200 UberFrameUI::UberFrameUI(content::WebUI* web_ui) : WebUIController(web_ui) {
201   Profile* profile = Profile::FromWebUI(web_ui);
202   content::WebUIDataSource::Add(profile, CreateUberFrameHTMLSource(profile));
203
204   // Register as an observer for when extensions are loaded and unloaded.
205   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
206       content::Source<Profile>(profile));
207   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
208       content::Source<Profile>(profile));
209 }
210
211 UberFrameUI::~UberFrameUI() {
212 }
213
214 void UberFrameUI::Observe(int type, const content::NotificationSource& source,
215                           const content::NotificationDetails& details) {
216   switch (type) {
217     // We listen for notifications that indicate an extension has been loaded
218     // (i.e., has been installed and/or enabled) or unloaded (i.e., has been
219     // uninstalled and/or disabled). If one of these events has occurred, then
220     // we must update the behavior of the History navigation element so that
221     // it opens the history extension if one is installed and enabled or
222     // opens the default history page if one is uninstalled or disabled.
223     case chrome::NOTIFICATION_EXTENSION_LOADED:
224     case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
225       Profile* profile = Profile::FromWebUI(web_ui());
226       bool overrides_history =
227           HasExtensionType(profile, chrome::kChromeUIHistoryHost);
228       web_ui()->CallJavascriptFunction(
229           "uber_frame.setNavigationOverride",
230           base::StringValue(chrome::kChromeUIHistoryHost),
231           base::StringValue(overrides_history ? "yes" : "no"));
232       break;
233     }
234     default:
235       NOTREACHED();
236   }
237 }