Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / uber / uber_ui.cc
blobd88d62f376e79650d02aec9be96b45907bca8acf
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 GURL page_gurl(page_url);
156 content::WebUI* webui = web_ui()->GetWebContents()->CreateWebUI(page_gurl);
158 webui->OverrideJavaScriptFrame(page_host);
159 sub_uis_[page_url] = webui;
162 content::WebUI* UberUI::GetSubpage(const std::string& page_url) {
163 if (!ContainsKey(sub_uis_, page_url))
164 return NULL;
165 return sub_uis_[page_url];
168 void UberUI::RenderViewCreated(RenderViewHost* render_view_host) {
169 for (SubpageMap::iterator iter = sub_uis_.begin(); iter != sub_uis_.end();
170 ++iter) {
171 iter->second->GetController()->RenderViewCreated(render_view_host);
175 void UberUI::RenderViewReused(RenderViewHost* render_view_host) {
176 for (SubpageMap::iterator iter = sub_uis_.begin(); iter != sub_uis_.end();
177 ++iter) {
178 iter->second->GetController()->RenderViewReused(render_view_host);
182 bool UberUI::OverrideHandleWebUIMessage(const GURL& source_url,
183 const std::string& message,
184 const base::ListValue& args) {
185 // Find the appropriate subpage and forward the message.
186 SubpageMap::iterator subpage = sub_uis_.find(source_url.GetOrigin().spec());
187 if (subpage == sub_uis_.end()) {
188 // The message was sent from the uber page itself.
189 DCHECK_EQ(std::string(chrome::kChromeUIUberHost), source_url.host());
190 return false;
193 // The message was sent from a subpage.
194 // TODO(jam) fix this to use interface
195 // return subpage->second->GetController()->OverrideHandleWebUIMessage(
196 // source_url, message, args);
197 subpage->second->ProcessWebUIMessage(source_url, message, args);
198 return true;
201 // UberFrameUI
203 UberFrameUI::UberFrameUI(content::WebUI* web_ui)
204 : WebUIController(web_ui),
205 extension_registry_observer_(this) {
206 content::BrowserContext* browser_context =
207 web_ui->GetWebContents()->GetBrowserContext();
208 content::WebUIDataSource::Add(browser_context,
209 CreateUberFrameHTMLSource(browser_context));
211 // Register as an observer for when extensions are loaded and unloaded.
212 extension_registry_observer_.Add(
213 extensions::ExtensionRegistry::Get(browser_context));
216 UberFrameUI::~UberFrameUI() {
219 void UberFrameUI::OnExtensionLoaded(content::BrowserContext* browser_context,
220 const extensions::Extension* extension) {
221 // We listen for notifications that indicate an extension has been loaded
222 // (i.e., has been installed and/or enabled) or unloaded (i.e., has been
223 // uninstalled and/or disabled). If one of these events has occurred, then
224 // we must update the behavior of the History navigation element so that
225 // it opens the history extension if one is installed and enabled or
226 // opens the default history page if one is uninstalled or disabled.
227 UpdateHistoryNavigation(web_ui());
230 void UberFrameUI::OnExtensionUnloaded(
231 content::BrowserContext* browser_context,
232 const extensions::Extension* extension,
233 extensions::UnloadedExtensionInfo::Reason reason) {
234 UpdateHistoryNavigation(web_ui());