Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / uber / uber_ui.cc
blobebef674da2063bbd6940740f8e582c646718a9e3
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/webui/uber/uber_ui.h"
7 #include "base/stl_util.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
10 #include "chrome/browser/ui/webui/extensions/extensions_ui.h"
11 #include "chrome/browser/ui/webui/options/options_ui.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/chrome_manifest_url_handlers.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/grit/chromium_strings.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/common/extension_set.h"
26 #include "grit/browser_resources.h"
28 using content::NavigationController;
29 using content::NavigationEntry;
30 using content::RenderViewHost;
31 using content::WebContents;
33 namespace {
35 content::WebUIDataSource* CreateUberHTMLSource() {
36 content::WebUIDataSource* source =
37 content::WebUIDataSource::Create(chrome::kChromeUIUberHost);
39 source->SetJsonPath("strings.js");
40 source->AddResourcePath("uber.js", IDR_UBER_JS);
41 source->AddResourcePath("uber_utils.js", IDR_UBER_UTILS_JS);
42 source->SetDefaultResource(IDR_UBER_HTML);
43 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;");
45 // Hack alert: continue showing "Loading..." until a real title is set.
46 source->AddLocalizedString("pageTitle", IDS_TAB_LOADING_TITLE);
48 source->AddString("extensionsFrameURL", chrome::kChromeUIExtensionsFrameURL);
49 source->AddString("extensionsHost", chrome::kChromeUIExtensionsHost);
50 source->AddString("helpFrameURL", chrome::kChromeUIHelpFrameURL);
51 source->AddString("helpHost", chrome::kChromeUIHelpHost);
52 source->AddString("historyFrameURL", chrome::kChromeUIHistoryFrameURL);
53 source->AddString("historyHost", chrome::kChromeUIHistoryHost);
54 source->AddString("settingsFrameURL", chrome::kChromeUISettingsFrameURL);
55 source->AddString("settingsHost", chrome::kChromeUISettingsHost);
57 return source;
60 // Determines whether the user has an active extension of the given type.
61 bool HasExtensionType(content::BrowserContext* browser_context,
62 const std::string& extension_type) {
63 const extensions::ExtensionSet& extension_set =
64 extensions::ExtensionRegistry::Get(browser_context)->enabled_extensions();
65 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
66 iter != extension_set.end(); ++iter) {
67 const extensions::URLOverrides::URLOverrideMap& map =
68 extensions::URLOverrides::GetChromeURLOverrides(iter->get());
69 if (ContainsKey(map, extension_type))
70 return true;
73 return false;
76 content::WebUIDataSource* CreateUberFrameHTMLSource(
77 content::BrowserContext* browser_context) {
78 content::WebUIDataSource* source =
79 content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost);
81 source->SetJsonPath("strings.js");
82 source->AddResourcePath("uber_frame.js", IDR_UBER_FRAME_JS);
83 source->SetDefaultResource(IDR_UBER_FRAME_HTML);
85 // TODO(jhawkins): Attempt to get rid of IDS_SHORT_PRODUCT_OS_NAME.
86 #if defined(OS_CHROMEOS)
87 source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_OS_NAME);
88 #else
89 source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_NAME);
90 #endif // defined(OS_CHROMEOS)
92 // Group settings and help separately if settings in a window is enabled.
93 std::string settings_group("settings_group");
94 std::string other_group(
95 ::switches::SettingsWindowEnabled() ? "other_group" : "settings_group");
96 source->AddString("extensionsHost", chrome::kChromeUIExtensionsHost);
97 source->AddLocalizedString("extensionsDisplayName",
98 IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE);
99 source->AddString("extensionsGroup", other_group);
100 source->AddString("helpHost", chrome::kChromeUIHelpHost);
101 source->AddLocalizedString("helpDisplayName", IDS_ABOUT_TITLE);
102 source->AddString("helpGroup", settings_group);
103 source->AddString("historyHost", chrome::kChromeUIHistoryHost);
104 source->AddLocalizedString("historyDisplayName", IDS_HISTORY_TITLE);
105 source->AddString("historyGroup", other_group);
106 source->AddString("settingsHost", chrome::kChromeUISettingsHost);
107 source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE);
108 source->AddString("settingsGroup", settings_group);
109 bool overridesHistory =
110 HasExtensionType(browser_context, chrome::kChromeUIHistoryHost);
111 source->AddString("overridesHistory", overridesHistory ? "yes" : "no");
112 source->DisableDenyXFrameOptions();
113 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;");
115 source->AddBoolean("profileIsGuest",
116 Profile::FromBrowserContext(browser_context)->IsGuestSession());
118 return source;
121 void UpdateHistoryNavigation(content::WebUI* web_ui) {
122 bool overrides_history =
123 HasExtensionType(web_ui->GetWebContents()->GetBrowserContext(),
124 chrome::kChromeUIHistoryHost);
125 web_ui->CallJavascriptFunction(
126 "uber_frame.setNavigationOverride",
127 base::StringValue(chrome::kChromeUIHistoryHost),
128 base::StringValue(overrides_history ? "yes" : "no"));
131 } // namespace
133 UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) {
134 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
135 CreateUberHTMLSource());
137 RegisterSubpage(chrome::kChromeUIExtensionsFrameURL,
138 chrome::kChromeUIExtensionsHost);
139 RegisterSubpage(chrome::kChromeUIHelpFrameURL,
140 chrome::kChromeUIHelpHost);
141 RegisterSubpage(chrome::kChromeUIHistoryFrameURL,
142 chrome::kChromeUIHistoryHost);
143 RegisterSubpage(chrome::kChromeUISettingsFrameURL,
144 chrome::kChromeUISettingsHost);
145 RegisterSubpage(chrome::kChromeUIUberFrameURL,
146 chrome::kChromeUIUberHost);
149 UberUI::~UberUI() {
150 STLDeleteValues(&sub_uis_);
153 void UberUI::RegisterSubpage(const std::string& page_url,
154 const std::string& page_host) {
155 sub_uis_[page_url] = web_ui()->GetWebContents()->CreateSubframeWebUI(
156 GURL(page_url), page_host);
159 content::WebUI* UberUI::GetSubpage(const std::string& page_url) {
160 if (!ContainsKey(sub_uis_, page_url))
161 return NULL;
162 return sub_uis_[page_url];
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);
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);
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;
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;
198 // UberFrameUI
200 UberFrameUI::UberFrameUI(content::WebUI* web_ui)
201 : WebUIController(web_ui),
202 extension_registry_observer_(this) {
203 content::BrowserContext* browser_context =
204 web_ui->GetWebContents()->GetBrowserContext();
205 content::WebUIDataSource::Add(browser_context,
206 CreateUberFrameHTMLSource(browser_context));
208 // Register as an observer for when extensions are loaded and unloaded.
209 extension_registry_observer_.Add(
210 extensions::ExtensionRegistry::Get(browser_context));
213 UberFrameUI::~UberFrameUI() {
216 void UberFrameUI::OnExtensionLoaded(content::BrowserContext* browser_context,
217 const extensions::Extension* extension) {
218 // We listen for notifications that indicate an extension has been loaded
219 // (i.e., has been installed and/or enabled) or unloaded (i.e., has been
220 // uninstalled and/or disabled). If one of these events has occurred, then
221 // we must update the behavior of the History navigation element so that
222 // it opens the history extension if one is installed and enabled or
223 // opens the default history page if one is uninstalled or disabled.
224 UpdateHistoryNavigation(web_ui());
227 void UberFrameUI::OnExtensionUnloaded(
228 content::BrowserContext* browser_context,
229 const extensions::Extension* extension,
230 extensions::UnloadedExtensionInfo::Reason reason) {
231 UpdateHistoryNavigation(web_ui());