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