Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / webui / uber / uber_ui.cc
blob31879268d87d75aebdb046dcee0dfab372a5a67f
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 "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"
28 using base::ASCIIToUTF16;
29 using content::NavigationController;
30 using content::NavigationEntry;
31 using content::RenderViewHost;
32 using content::WebContents;
34 namespace {
36 content::WebUIDataSource* CreateUberHTMLSource() {
37 content::WebUIDataSource* source =
38 content::WebUIDataSource::Create(chrome::kChromeUIUberHost);
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:;");
47 // Hack alert: continue showing "Loading..." until a real title is set.
48 source->AddLocalizedString("pageTitle", IDS_TAB_LOADING_TITLE);
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));
67 return source;
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();
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));
82 if (result != map.end())
83 return true;
86 return false;
89 content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) {
90 content::WebUIDataSource* source =
91 content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost);
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);
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)
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:;");
125 return source;
128 } // namespace
130 UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) {
131 Profile* profile = Profile::FromWebUI(web_ui);
132 content::WebUIDataSource::Add(profile, CreateUberHTMLSource());
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);
146 UberUI::~UberUI() {
147 STLDeleteValues(&sub_uis_);
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);
155 webui->OverrideJavaScriptFrame(page_host);
156 sub_uis_[page_url] = webui;
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];
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) : WebUIController(web_ui) {
201 Profile* profile = Profile::FromWebUI(web_ui);
202 content::WebUIDataSource::Add(profile, CreateUberFrameHTMLSource(profile));
204 // Register as an observer for when extensions are loaded and unloaded.
205 registrar_.Add(this,
206 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
207 content::Source<Profile>(profile));
208 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
209 content::Source<Profile>(profile));
212 UberFrameUI::~UberFrameUI() {
215 void UberFrameUI::Observe(int type, const content::NotificationSource& source,
216 const content::NotificationDetails& details) {
217 switch (type) {
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 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
225 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
226 Profile* profile = Profile::FromWebUI(web_ui());
227 bool overrides_history =
228 HasExtensionType(profile, chrome::kChromeUIHistoryHost);
229 web_ui()->CallJavascriptFunction(
230 "uber_frame.setNavigationOverride",
231 base::StringValue(chrome::kChromeUIHistoryHost),
232 base::StringValue(overrides_history ? "yes" : "no"));
233 break;
235 default:
236 NOTREACHED();