Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / renderer / extensions / chrome_extensions_dispatcher_delegate.cc
blob2b5a5dd1911263602e342a7fb0a2644ff8273f0c
1 // Copyright 2014 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/renderer/extensions/chrome_extensions_dispatcher_delegate.h"
7 #include "base/command_line.h"
8 #include "base/sha1.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/common/channel_info.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/crash_keys.h"
13 #include "chrome/common/extensions/features/feature_channel.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/grit/renderer_resources.h"
16 #include "chrome/renderer/extensions/app_bindings.h"
17 #include "chrome/renderer/extensions/automation_internal_custom_bindings.h"
18 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h"
19 #include "chrome/renderer/extensions/file_manager_private_custom_bindings.h"
20 #include "chrome/renderer/extensions/media_galleries_custom_bindings.h"
21 #include "chrome/renderer/extensions/notifications_native_handler.h"
22 #include "chrome/renderer/extensions/page_capture_custom_bindings.h"
23 #include "chrome/renderer/extensions/platform_keys_natives.h"
24 #include "chrome/renderer/extensions/sync_file_system_custom_bindings.h"
25 #include "chrome/renderer/extensions/tabs_custom_bindings.h"
26 #include "chrome/renderer/extensions/webstore_bindings.h"
27 #include "components/version_info/version_info.h"
28 #include "content/public/renderer/render_thread.h"
29 #include "content/public/renderer/render_view.h"
30 #include "extensions/common/extension.h"
31 #include "extensions/common/feature_switch.h"
32 #include "extensions/common/permissions/manifest_permission_set.h"
33 #include "extensions/common/permissions/permission_set.h"
34 #include "extensions/common/permissions/permissions_data.h"
35 #include "extensions/common/switches.h"
36 #include "extensions/renderer/dispatcher.h"
37 #include "extensions/renderer/native_handler.h"
38 #include "extensions/renderer/resource_bundle_source_map.h"
39 #include "extensions/renderer/script_context.h"
40 #include "third_party/WebKit/public/platform/WebString.h"
41 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
43 #if defined(ENABLE_WEBRTC)
44 #include "chrome/renderer/extensions/cast_streaming_native_handler.h"
45 #endif
47 using extensions::NativeHandler;
49 ChromeExtensionsDispatcherDelegate::ChromeExtensionsDispatcherDelegate() {
52 ChromeExtensionsDispatcherDelegate::~ChromeExtensionsDispatcherDelegate() {
55 void ChromeExtensionsDispatcherDelegate::InitOriginPermissions(
56 const extensions::Extension* extension,
57 bool is_extension_active) {
58 // TODO(jstritar): We should try to remove this special case. Also, these
59 // whitelist entries need to be updated when the kManagement permission
60 // changes.
61 if (is_extension_active &&
62 extension->permissions_data()->HasAPIPermission(
63 extensions::APIPermission::kManagement)) {
64 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
65 extension->url(),
66 blink::WebString::fromUTF8(content::kChromeUIScheme),
67 blink::WebString::fromUTF8(chrome::kChromeUIExtensionIconHost),
68 false);
72 void ChromeExtensionsDispatcherDelegate::RegisterNativeHandlers(
73 extensions::Dispatcher* dispatcher,
74 extensions::ModuleSystem* module_system,
75 extensions::ScriptContext* context) {
76 module_system->RegisterNativeHandler(
77 "app",
78 scoped_ptr<NativeHandler>(
79 new extensions::AppBindings(dispatcher, context)));
80 module_system->RegisterNativeHandler(
81 "sync_file_system",
82 scoped_ptr<NativeHandler>(
83 new extensions::SyncFileSystemCustomBindings(context)));
84 module_system->RegisterNativeHandler(
85 "file_browser_handler",
86 scoped_ptr<NativeHandler>(
87 new extensions::FileBrowserHandlerCustomBindings(context)));
88 module_system->RegisterNativeHandler(
89 "file_manager_private",
90 scoped_ptr<NativeHandler>(
91 new extensions::FileManagerPrivateCustomBindings(context)));
92 module_system->RegisterNativeHandler(
93 "notifications_private",
94 scoped_ptr<NativeHandler>(
95 new extensions::NotificationsNativeHandler(context)));
96 module_system->RegisterNativeHandler(
97 "mediaGalleries",
98 scoped_ptr<NativeHandler>(
99 new extensions::MediaGalleriesCustomBindings(context)));
100 module_system->RegisterNativeHandler(
101 "page_capture",
102 scoped_ptr<NativeHandler>(
103 new extensions::PageCaptureCustomBindings(context)));
104 module_system->RegisterNativeHandler(
105 "platform_keys_natives",
106 scoped_ptr<NativeHandler>(new extensions::PlatformKeysNatives(context)));
107 module_system->RegisterNativeHandler(
108 "tabs",
109 scoped_ptr<NativeHandler>(new extensions::TabsCustomBindings(context)));
110 module_system->RegisterNativeHandler(
111 "webstore",
112 scoped_ptr<NativeHandler>(new extensions::WebstoreBindings(context)));
113 #if defined(ENABLE_WEBRTC)
114 module_system->RegisterNativeHandler(
115 "cast_streaming_natives",
116 scoped_ptr<NativeHandler>(
117 new extensions::CastStreamingNativeHandler(context)));
118 #endif
119 module_system->RegisterNativeHandler(
120 "automationInternal",
121 scoped_ptr<NativeHandler>(
122 new extensions::AutomationInternalCustomBindings(context)));
125 void ChromeExtensionsDispatcherDelegate::PopulateSourceMap(
126 extensions::ResourceBundleSourceMap* source_map) {
127 // Custom bindings.
128 source_map->RegisterSource("app", IDR_APP_CUSTOM_BINDINGS_JS);
129 source_map->RegisterSource("automation", IDR_AUTOMATION_CUSTOM_BINDINGS_JS);
130 source_map->RegisterSource("automationEvent", IDR_AUTOMATION_EVENT_JS);
131 source_map->RegisterSource("automationNode", IDR_AUTOMATION_NODE_JS);
132 source_map->RegisterSource("browserAction",
133 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS);
134 source_map->RegisterSource("certificateProvider",
135 IDR_CERTIFICATE_PROVIDER_CUSTOM_BINDINGS_JS);
136 source_map->RegisterSource("declarativeContent",
137 IDR_DECLARATIVE_CONTENT_CUSTOM_BINDINGS_JS);
138 source_map->RegisterSource("desktopCapture",
139 IDR_DESKTOP_CAPTURE_CUSTOM_BINDINGS_JS);
140 source_map->RegisterSource("developerPrivate",
141 IDR_DEVELOPER_PRIVATE_CUSTOM_BINDINGS_JS);
142 source_map->RegisterSource("downloads", IDR_DOWNLOADS_CUSTOM_BINDINGS_JS);
143 source_map->RegisterSource("enterprise.platformKeys",
144 IDR_ENTERPRISE_PLATFORM_KEYS_CUSTOM_BINDINGS_JS);
145 source_map->RegisterSource("enterprise.platformKeys.internalAPI",
146 IDR_ENTERPRISE_PLATFORM_KEYS_INTERNAL_API_JS);
147 source_map->RegisterSource("enterprise.platformKeys.KeyPair",
148 IDR_ENTERPRISE_PLATFORM_KEYS_KEY_PAIR_JS);
149 source_map->RegisterSource("enterprise.platformKeys.SubtleCrypto",
150 IDR_ENTERPRISE_PLATFORM_KEYS_SUBTLE_CRYPTO_JS);
151 source_map->RegisterSource("enterprise.platformKeys.Token",
152 IDR_ENTERPRISE_PLATFORM_KEYS_TOKEN_JS);
153 source_map->RegisterSource("feedbackPrivate",
154 IDR_FEEDBACK_PRIVATE_CUSTOM_BINDINGS_JS);
155 source_map->RegisterSource("fileBrowserHandler",
156 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS);
157 source_map->RegisterSource("fileManagerPrivate",
158 IDR_FILE_MANAGER_PRIVATE_CUSTOM_BINDINGS_JS);
159 source_map->RegisterSource("fileSystem", IDR_FILE_SYSTEM_CUSTOM_BINDINGS_JS);
160 source_map->RegisterSource("fileSystemProvider",
161 IDR_FILE_SYSTEM_PROVIDER_CUSTOM_BINDINGS_JS);
162 source_map->RegisterSource("gcm", IDR_GCM_CUSTOM_BINDINGS_JS);
163 source_map->RegisterSource("identity", IDR_IDENTITY_CUSTOM_BINDINGS_JS);
164 source_map->RegisterSource("imageWriterPrivate",
165 IDR_IMAGE_WRITER_PRIVATE_CUSTOM_BINDINGS_JS);
166 source_map->RegisterSource("input.ime", IDR_INPUT_IME_CUSTOM_BINDINGS_JS);
167 source_map->RegisterSource("logPrivate", IDR_LOG_PRIVATE_CUSTOM_BINDINGS_JS);
168 source_map->RegisterSource("mediaGalleries",
169 IDR_MEDIA_GALLERIES_CUSTOM_BINDINGS_JS);
170 source_map->RegisterSource("notifications",
171 IDR_NOTIFICATIONS_CUSTOM_BINDINGS_JS);
172 source_map->RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS);
173 source_map->RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS);
174 source_map->RegisterSource("pageCapture",
175 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS);
176 source_map->RegisterSource("platformKeys",
177 IDR_PLATFORM_KEYS_CUSTOM_BINDINGS_JS);
178 source_map->RegisterSource("platformKeys.getPublicKey",
179 IDR_PLATFORM_KEYS_GET_PUBLIC_KEY_JS);
180 source_map->RegisterSource("platformKeys.internalAPI",
181 IDR_PLATFORM_KEYS_INTERNAL_API_JS);
182 source_map->RegisterSource("platformKeys.Key", IDR_PLATFORM_KEYS_KEY_JS);
183 source_map->RegisterSource("platformKeys.SubtleCrypto",
184 IDR_PLATFORM_KEYS_SUBTLE_CRYPTO_JS);
185 source_map->RegisterSource("platformKeys.utils", IDR_PLATFORM_KEYS_UTILS_JS);
186 source_map->RegisterSource("syncFileSystem",
187 IDR_SYNC_FILE_SYSTEM_CUSTOM_BINDINGS_JS);
188 source_map->RegisterSource("systemIndicator",
189 IDR_SYSTEM_INDICATOR_CUSTOM_BINDINGS_JS);
190 source_map->RegisterSource("tabCapture", IDR_TAB_CAPTURE_CUSTOM_BINDINGS_JS);
191 source_map->RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS);
192 source_map->RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS);
193 source_map->RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS);
194 #if defined(ENABLE_WEBRTC)
195 source_map->RegisterSource("cast.streaming.rtpStream",
196 IDR_CAST_STREAMING_RTP_STREAM_CUSTOM_BINDINGS_JS);
197 source_map->RegisterSource("cast.streaming.session",
198 IDR_CAST_STREAMING_SESSION_CUSTOM_BINDINGS_JS);
199 source_map->RegisterSource(
200 "cast.streaming.udpTransport",
201 IDR_CAST_STREAMING_UDP_TRANSPORT_CUSTOM_BINDINGS_JS);
202 source_map->RegisterSource(
203 "cast.streaming.receiverSession",
204 IDR_CAST_STREAMING_RECEIVER_SESSION_CUSTOM_BINDINGS_JS);
205 #endif
206 source_map->RegisterSource(
207 "webrtcDesktopCapturePrivate",
208 IDR_WEBRTC_DESKTOP_CAPTURE_PRIVATE_CUSTOM_BINDINGS_JS);
209 source_map->RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS);
211 // Custom types sources.
212 source_map->RegisterSource("ChromeSetting", IDR_CHROME_SETTING_JS);
213 source_map->RegisterSource("ContentSetting", IDR_CONTENT_SETTING_JS);
214 source_map->RegisterSource("ChromeDirectSetting",
215 IDR_CHROME_DIRECT_SETTING_JS);
217 // Platform app sources that are not API-specific..
218 source_map->RegisterSource("fileEntryBindingUtil",
219 IDR_FILE_ENTRY_BINDING_UTIL_JS);
220 source_map->RegisterSource("tagWatcher", IDR_TAG_WATCHER_JS);
221 source_map->RegisterSource("chromeWebViewInternal",
222 IDR_CHROME_WEB_VIEW_INTERNAL_CUSTOM_BINDINGS_JS);
223 source_map->RegisterSource("chromeWebView", IDR_CHROME_WEB_VIEW_JS);
224 source_map->RegisterSource("injectAppTitlebar", IDR_INJECT_APP_TITLEBAR_JS);
227 void ChromeExtensionsDispatcherDelegate::RequireAdditionalModules(
228 extensions::ScriptContext* context,
229 bool is_within_platform_app) {
230 extensions::ModuleSystem* module_system = context->module_system();
231 extensions::Feature::Context context_type = context->context_type();
233 // TODO(kalman, fsamuel): Eagerly calling Require on context startup is
234 // expensive. It would be better if there were a light way of detecting when
235 // a webview or appview is created and only then set up the infrastructure.
236 if (context_type == extensions::Feature::BLESSED_EXTENSION_CONTEXT &&
237 is_within_platform_app &&
238 extensions::GetCurrentChannel() <= version_info::Channel::DEV &&
239 base::CommandLine::ForCurrentProcess()->HasSwitch(
240 extensions::switches::kEnableAppWindowControls)) {
241 module_system->Require("windowControls");
244 // Note: setting up the WebView class here, not the chrome.webview API.
245 // The API will be automatically set up when first used.
246 if (context->GetAvailability("webViewInternal").is_available()) {
247 module_system->Require("chromeWebView");
251 void ChromeExtensionsDispatcherDelegate::OnActiveExtensionsUpdated(
252 const std::set<std::string>& extension_ids) {
253 // In single-process mode, the browser process reports the active extensions.
254 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
255 ::switches::kSingleProcess))
256 return;
257 crash_keys::SetActiveExtensions(extension_ids);
260 void ChromeExtensionsDispatcherDelegate::SetChannel(int channel) {
261 extensions::SetCurrentChannel(static_cast<version_info::Channel>(channel));