OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/uber/uber_ui.h" | 5 #include "chrome/browser/ui/webui/uber/uber_ui.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/extensions/extension_service.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | 8 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
12 #include "chrome/browser/ui/webui/extensions/extensions_ui.h" | 9 #include "chrome/browser/ui/webui/extensions/extensions_ui.h" |
13 #include "chrome/browser/ui/webui/options/options_ui.h" | 10 #include "chrome/browser/ui/webui/options/options_ui.h" |
14 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/extensions/chrome_manifest_url_handlers.h" | 12 #include "chrome/common/extensions/chrome_manifest_url_handlers.h" |
16 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
17 #include "chrome/grit/chromium_strings.h" | 14 #include "chrome/grit/chromium_strings.h" |
18 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
20 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
21 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
22 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
23 #include "content/public/browser/web_ui.h" | 21 #include "content/public/browser/web_ui.h" |
24 #include "content/public/browser/web_ui_data_source.h" | 22 #include "content/public/browser/web_ui_data_source.h" |
25 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
26 #include "extensions/common/extension_set.h" | 24 #include "extensions/common/extension_set.h" |
27 #include "grit/browser_resources.h" | 25 #include "grit/browser_resources.h" |
28 | 26 |
(...skipping 23 matching lines...) Expand all Loading... |
52 source->AddString("helpHost", chrome::kChromeUIHelpHost); | 50 source->AddString("helpHost", chrome::kChromeUIHelpHost); |
53 source->AddString("historyFrameURL", chrome::kChromeUIHistoryFrameURL); | 51 source->AddString("historyFrameURL", chrome::kChromeUIHistoryFrameURL); |
54 source->AddString("historyHost", chrome::kChromeUIHistoryHost); | 52 source->AddString("historyHost", chrome::kChromeUIHistoryHost); |
55 source->AddString("settingsFrameURL", chrome::kChromeUISettingsFrameURL); | 53 source->AddString("settingsFrameURL", chrome::kChromeUISettingsFrameURL); |
56 source->AddString("settingsHost", chrome::kChromeUISettingsHost); | 54 source->AddString("settingsHost", chrome::kChromeUISettingsHost); |
57 | 55 |
58 return source; | 56 return source; |
59 } | 57 } |
60 | 58 |
61 // Determines whether the user has an active extension of the given type. | 59 // Determines whether the user has an active extension of the given type. |
62 bool HasExtensionType(Profile* profile, const std::string& extension_type) { | 60 bool HasExtensionType(content::BrowserContext* browser_context, |
| 61 const std::string& extension_type) { |
63 const extensions::ExtensionSet& extension_set = | 62 const extensions::ExtensionSet& extension_set = |
64 extensions::ExtensionRegistry::Get(profile)->enabled_extensions(); | 63 extensions::ExtensionRegistry::Get(browser_context)->enabled_extensions(); |
65 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin(); | 64 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin(); |
66 iter != extension_set.end(); ++iter) { | 65 iter != extension_set.end(); ++iter) { |
67 const extensions::URLOverrides::URLOverrideMap& map = | 66 const extensions::URLOverrides::URLOverrideMap& map = |
68 extensions::URLOverrides::GetChromeURLOverrides(iter->get()); | 67 extensions::URLOverrides::GetChromeURLOverrides(iter->get()); |
69 if (ContainsKey(map, extension_type)) | 68 if (ContainsKey(map, extension_type)) |
70 return true; | 69 return true; |
71 } | 70 } |
72 | 71 |
73 return false; | 72 return false; |
74 } | 73 } |
75 | 74 |
76 content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) { | 75 content::WebUIDataSource* CreateUberFrameHTMLSource( |
| 76 content::BrowserContext* browser_context) { |
77 content::WebUIDataSource* source = | 77 content::WebUIDataSource* source = |
78 content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost); | 78 content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost); |
79 | 79 |
80 source->SetJsonPath("strings.js"); | 80 source->SetJsonPath("strings.js"); |
81 source->AddResourcePath("uber_frame.js", IDR_UBER_FRAME_JS); | 81 source->AddResourcePath("uber_frame.js", IDR_UBER_FRAME_JS); |
82 source->SetDefaultResource(IDR_UBER_FRAME_HTML); | 82 source->SetDefaultResource(IDR_UBER_FRAME_HTML); |
83 | 83 |
84 // TODO(jhawkins): Attempt to get rid of IDS_SHORT_PRODUCT_OS_NAME. | 84 // TODO(jhawkins): Attempt to get rid of IDS_SHORT_PRODUCT_OS_NAME. |
85 #if defined(OS_CHROMEOS) | 85 #if defined(OS_CHROMEOS) |
86 source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_OS_NAME); | 86 source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_OS_NAME); |
(...skipping 12 matching lines...) Expand all Loading... |
99 source->AddString("helpHost", chrome::kChromeUIHelpHost); | 99 source->AddString("helpHost", chrome::kChromeUIHelpHost); |
100 source->AddLocalizedString("helpDisplayName", IDS_ABOUT_TITLE); | 100 source->AddLocalizedString("helpDisplayName", IDS_ABOUT_TITLE); |
101 source->AddString("helpGroup", settings_group); | 101 source->AddString("helpGroup", settings_group); |
102 source->AddString("historyHost", chrome::kChromeUIHistoryHost); | 102 source->AddString("historyHost", chrome::kChromeUIHistoryHost); |
103 source->AddLocalizedString("historyDisplayName", IDS_HISTORY_TITLE); | 103 source->AddLocalizedString("historyDisplayName", IDS_HISTORY_TITLE); |
104 source->AddString("historyGroup", other_group); | 104 source->AddString("historyGroup", other_group); |
105 source->AddString("settingsHost", chrome::kChromeUISettingsHost); | 105 source->AddString("settingsHost", chrome::kChromeUISettingsHost); |
106 source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE); | 106 source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE); |
107 source->AddString("settingsGroup", settings_group); | 107 source->AddString("settingsGroup", settings_group); |
108 bool overridesHistory = | 108 bool overridesHistory = |
109 HasExtensionType(profile, chrome::kChromeUIHistoryHost); | 109 HasExtensionType(browser_context, chrome::kChromeUIHistoryHost); |
110 source->AddString("overridesHistory", overridesHistory ? "yes" : "no"); | 110 source->AddString("overridesHistory", overridesHistory ? "yes" : "no"); |
111 source->DisableDenyXFrameOptions(); | 111 source->DisableDenyXFrameOptions(); |
112 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;"); | 112 source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;"); |
113 | 113 |
114 return source; | 114 return source; |
115 } | 115 } |
116 | 116 |
| 117 void UpdateHistoryNavigation(content::WebUI* web_ui) { |
| 118 bool overrides_history = |
| 119 HasExtensionType(web_ui->GetWebContents()->GetBrowserContext(), |
| 120 chrome::kChromeUIHistoryHost); |
| 121 web_ui->CallJavascriptFunction( |
| 122 "uber_frame.setNavigationOverride", |
| 123 base::StringValue(chrome::kChromeUIHistoryHost), |
| 124 base::StringValue(overrides_history ? "yes" : "no")); |
| 125 } |
| 126 |
117 } // namespace | 127 } // namespace |
118 | 128 |
119 UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 129 UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
120 Profile* profile = Profile::FromWebUI(web_ui); | 130 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), |
121 content::WebUIDataSource::Add(profile, CreateUberHTMLSource()); | 131 CreateUberHTMLSource()); |
122 | 132 |
123 RegisterSubpage(chrome::kChromeUIExtensionsFrameURL, | 133 RegisterSubpage(chrome::kChromeUIExtensionsFrameURL, |
124 chrome::kChromeUIExtensionsHost); | 134 chrome::kChromeUIExtensionsHost); |
125 RegisterSubpage(chrome::kChromeUIHelpFrameURL, | 135 RegisterSubpage(chrome::kChromeUIHelpFrameURL, |
126 chrome::kChromeUIHelpHost); | 136 chrome::kChromeUIHelpHost); |
127 RegisterSubpage(chrome::kChromeUIHistoryFrameURL, | 137 RegisterSubpage(chrome::kChromeUIHistoryFrameURL, |
128 chrome::kChromeUIHistoryHost); | 138 chrome::kChromeUIHistoryHost); |
129 RegisterSubpage(chrome::kChromeUISettingsFrameURL, | 139 RegisterSubpage(chrome::kChromeUISettingsFrameURL, |
130 chrome::kChromeUISettingsHost); | 140 chrome::kChromeUISettingsHost); |
131 RegisterSubpage(chrome::kChromeUIUberFrameURL, | 141 RegisterSubpage(chrome::kChromeUIUberFrameURL, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // The message was sent from a subpage. | 189 // The message was sent from a subpage. |
180 // TODO(jam) fix this to use interface | 190 // TODO(jam) fix this to use interface |
181 // return subpage->second->GetController()->OverrideHandleWebUIMessage( | 191 // return subpage->second->GetController()->OverrideHandleWebUIMessage( |
182 // source_url, message, args); | 192 // source_url, message, args); |
183 subpage->second->ProcessWebUIMessage(source_url, message, args); | 193 subpage->second->ProcessWebUIMessage(source_url, message, args); |
184 return true; | 194 return true; |
185 } | 195 } |
186 | 196 |
187 // UberFrameUI | 197 // UberFrameUI |
188 | 198 |
189 UberFrameUI::UberFrameUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 199 UberFrameUI::UberFrameUI(content::WebUI* web_ui) |
190 Profile* profile = Profile::FromWebUI(web_ui); | 200 : WebUIController(web_ui), |
191 content::WebUIDataSource::Add(profile, CreateUberFrameHTMLSource(profile)); | 201 extension_registry_observer_(this) { |
| 202 content::BrowserContext* browser_context = |
| 203 web_ui->GetWebContents()->GetBrowserContext(); |
| 204 content::WebUIDataSource::Add(browser_context, |
| 205 CreateUberFrameHTMLSource(browser_context)); |
192 | 206 |
193 // Register as an observer for when extensions are loaded and unloaded. | 207 // Register as an observer for when extensions are loaded and unloaded. |
194 registrar_.Add(this, | 208 extension_registry_observer_.Add( |
195 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 209 extensions::ExtensionRegistry::Get(browser_context)); |
196 content::Source<Profile>(profile)); | |
197 registrar_.Add(this, | |
198 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
199 content::Source<Profile>(profile)); | |
200 } | 210 } |
201 | 211 |
202 UberFrameUI::~UberFrameUI() { | 212 UberFrameUI::~UberFrameUI() { |
203 } | 213 } |
204 | 214 |
205 void UberFrameUI::Observe(int type, | 215 void UberFrameUI::OnExtensionLoaded(content::BrowserContext* browser_context, |
206 const content::NotificationSource& source, | 216 const extensions::Extension* extension) { |
207 const content::NotificationDetails& details) { | 217 // We listen for notifications that indicate an extension has been loaded |
208 switch (type) { | 218 // (i.e., has been installed and/or enabled) or unloaded (i.e., has been |
209 // We listen for notifications that indicate an extension has been loaded | 219 // uninstalled and/or disabled). If one of these events has occurred, then |
210 // (i.e., has been installed and/or enabled) or unloaded (i.e., has been | 220 // we must update the behavior of the History navigation element so that |
211 // uninstalled and/or disabled). If one of these events has occurred, then | 221 // it opens the history extension if one is installed and enabled or |
212 // we must update the behavior of the History navigation element so that | 222 // opens the default history page if one is uninstalled or disabled. |
213 // it opens the history extension if one is installed and enabled or | 223 UpdateHistoryNavigation(web_ui()); |
214 // opens the default history page if one is uninstalled or disabled. | |
215 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: | |
216 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
217 Profile* profile = Profile::FromWebUI(web_ui()); | |
218 bool overrides_history = | |
219 HasExtensionType(profile, chrome::kChromeUIHistoryHost); | |
220 web_ui()->CallJavascriptFunction( | |
221 "uber_frame.setNavigationOverride", | |
222 base::StringValue(chrome::kChromeUIHistoryHost), | |
223 base::StringValue(overrides_history ? "yes" : "no")); | |
224 break; | |
225 } | |
226 default: | |
227 NOTREACHED(); | |
228 } | |
229 } | 224 } |
| 225 |
| 226 void UberFrameUI::OnExtensionUnloaded( |
| 227 content::BrowserContext* browser_context, |
| 228 const extensions::Extension* extension, |
| 229 extensions::UnloadedExtensionInfo::Reason reason) { |
| 230 UpdateHistoryNavigation(web_ui()); |
| 231 } |
OLD | NEW |