[Extensions] Propagate activeTab hosts to extension background pages
[chromium-blink-merge.git] / chrome / renderer / extensions / chrome_extensions_dispatcher_delegate.cc
blob9045848f4163462d5308f7490ae81be10fda2edc
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/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/chrome_v8_context.h"
19 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h"
20 #include "chrome/renderer/extensions/file_manager_private_custom_bindings.h"
21 #include "chrome/renderer/extensions/media_galleries_custom_bindings.h"
22 #include "chrome/renderer/extensions/notifications_native_handler.h"
23 #include "chrome/renderer/extensions/page_capture_custom_bindings.h"
24 #include "chrome/renderer/extensions/platform_keys_natives.h"
25 #include "chrome/renderer/extensions/sync_file_system_custom_bindings.h"
26 #include "chrome/renderer/extensions/tabs_custom_bindings.h"
27 #include "chrome/renderer/extensions/webstore_bindings.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 scoped_ptr<extensions::ScriptContext>
56 ChromeExtensionsDispatcherDelegate::CreateScriptContext(
57 const v8::Handle<v8::Context>& v8_context,
58 blink::WebFrame* frame,
59 const extensions::Extension* extension,
60 extensions::Feature::Context context_type,
61 const extensions::Extension* effective_extension,
62 extensions::Feature::Context effective_context_type) {
63 return scoped_ptr<extensions::ScriptContext>(
64 new extensions::ChromeV8Context(v8_context,
65 frame,
66 extension,
67 context_type,
68 effective_extension,
69 effective_context_type));
72 void ChromeExtensionsDispatcherDelegate::InitOriginPermissions(
73 const extensions::Extension* extension,
74 bool is_extension_active) {
75 // TODO(jstritar): We should try to remove this special case. Also, these
76 // whitelist entries need to be updated when the kManagement permission
77 // changes.
78 if (is_extension_active &&
79 extension->permissions_data()->HasAPIPermission(
80 extensions::APIPermission::kManagement)) {
81 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
82 extension->url(),
83 blink::WebString::fromUTF8(content::kChromeUIScheme),
84 blink::WebString::fromUTF8(chrome::kChromeUIExtensionIconHost),
85 false);
89 void ChromeExtensionsDispatcherDelegate::RegisterNativeHandlers(
90 extensions::Dispatcher* dispatcher,
91 extensions::ModuleSystem* module_system,
92 extensions::ScriptContext* context) {
93 module_system->RegisterNativeHandler(
94 "app",
95 scoped_ptr<NativeHandler>(
96 new extensions::AppBindings(dispatcher, context)));
97 module_system->RegisterNativeHandler(
98 "sync_file_system",
99 scoped_ptr<NativeHandler>(
100 new extensions::SyncFileSystemCustomBindings(context)));
101 module_system->RegisterNativeHandler(
102 "file_browser_handler",
103 scoped_ptr<NativeHandler>(
104 new extensions::FileBrowserHandlerCustomBindings(context)));
105 module_system->RegisterNativeHandler(
106 "file_manager_private",
107 scoped_ptr<NativeHandler>(
108 new extensions::FileManagerPrivateCustomBindings(context)));
109 module_system->RegisterNativeHandler(
110 "notifications_private",
111 scoped_ptr<NativeHandler>(
112 new extensions::NotificationsNativeHandler(context)));
113 module_system->RegisterNativeHandler(
114 "mediaGalleries",
115 scoped_ptr<NativeHandler>(
116 new extensions::MediaGalleriesCustomBindings(context)));
117 module_system->RegisterNativeHandler(
118 "page_capture",
119 scoped_ptr<NativeHandler>(
120 new extensions::PageCaptureCustomBindings(context)));
121 module_system->RegisterNativeHandler(
122 "platform_keys_natives",
123 scoped_ptr<NativeHandler>(new extensions::PlatformKeysNatives(context)));
124 module_system->RegisterNativeHandler(
125 "tabs",
126 scoped_ptr<NativeHandler>(new extensions::TabsCustomBindings(context)));
127 module_system->RegisterNativeHandler(
128 "webstore",
129 scoped_ptr<NativeHandler>(new extensions::WebstoreBindings(context)));
130 #if defined(ENABLE_WEBRTC)
131 module_system->RegisterNativeHandler(
132 "cast_streaming_natives",
133 scoped_ptr<NativeHandler>(
134 new extensions::CastStreamingNativeHandler(context)));
135 #endif
136 module_system->RegisterNativeHandler(
137 "automationInternal",
138 scoped_ptr<NativeHandler>(
139 new extensions::AutomationInternalCustomBindings(context)));
142 void ChromeExtensionsDispatcherDelegate::PopulateSourceMap(
143 extensions::ResourceBundleSourceMap* source_map) {
144 // Custom bindings.
145 source_map->RegisterSource("app", IDR_APP_CUSTOM_BINDINGS_JS);
146 source_map->RegisterSource("automation", IDR_AUTOMATION_CUSTOM_BINDINGS_JS);
147 source_map->RegisterSource("automationEvent", IDR_AUTOMATION_EVENT_JS);
148 source_map->RegisterSource("automationNode", IDR_AUTOMATION_NODE_JS);
149 source_map->RegisterSource("browserAction",
150 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS);
151 source_map->RegisterSource("declarativeContent",
152 IDR_DECLARATIVE_CONTENT_CUSTOM_BINDINGS_JS);
153 source_map->RegisterSource("desktopCapture",
154 IDR_DESKTOP_CAPTURE_CUSTOM_BINDINGS_JS);
155 source_map->RegisterSource("developerPrivate",
156 IDR_DEVELOPER_PRIVATE_CUSTOM_BINDINGS_JS);
157 source_map->RegisterSource("downloads", IDR_DOWNLOADS_CUSTOM_BINDINGS_JS);
158 source_map->RegisterSource("enterprise.platformKeys",
159 IDR_ENTERPRISE_PLATFORM_KEYS_CUSTOM_BINDINGS_JS);
160 source_map->RegisterSource("enterprise.platformKeys.internalAPI",
161 IDR_ENTERPRISE_PLATFORM_KEYS_INTERNAL_API_JS);
162 source_map->RegisterSource("enterprise.platformKeys.KeyPair",
163 IDR_ENTERPRISE_PLATFORM_KEYS_KEY_PAIR_JS);
164 source_map->RegisterSource("enterprise.platformKeys.SubtleCrypto",
165 IDR_ENTERPRISE_PLATFORM_KEYS_SUBTLE_CRYPTO_JS);
166 source_map->RegisterSource("enterprise.platformKeys.Token",
167 IDR_ENTERPRISE_PLATFORM_KEYS_TOKEN_JS);
168 source_map->RegisterSource("feedbackPrivate",
169 IDR_FEEDBACK_PRIVATE_CUSTOM_BINDINGS_JS);
170 source_map->RegisterSource("fileBrowserHandler",
171 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS);
172 source_map->RegisterSource("fileManagerPrivate",
173 IDR_FILE_MANAGER_PRIVATE_CUSTOM_BINDINGS_JS);
174 source_map->RegisterSource("fileSystem", IDR_FILE_SYSTEM_CUSTOM_BINDINGS_JS);
175 source_map->RegisterSource("fileSystemProvider",
176 IDR_FILE_SYSTEM_PROVIDER_CUSTOM_BINDINGS_JS);
177 source_map->RegisterSource("gcm", IDR_GCM_CUSTOM_BINDINGS_JS);
178 source_map->RegisterSource("identity", IDR_IDENTITY_CUSTOM_BINDINGS_JS);
179 source_map->RegisterSource("imageWriterPrivate",
180 IDR_IMAGE_WRITER_PRIVATE_CUSTOM_BINDINGS_JS);
181 source_map->RegisterSource("input.ime", IDR_INPUT_IME_CUSTOM_BINDINGS_JS);
182 source_map->RegisterSource("logPrivate", IDR_LOG_PRIVATE_CUSTOM_BINDINGS_JS);
183 source_map->RegisterSource("mediaGalleries",
184 IDR_MEDIA_GALLERIES_CUSTOM_BINDINGS_JS);
185 source_map->RegisterSource("notifications",
186 IDR_NOTIFICATIONS_CUSTOM_BINDINGS_JS);
187 source_map->RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS);
188 source_map->RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS);
189 source_map->RegisterSource("pageCapture",
190 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS);
191 source_map->RegisterSource("platformKeys",
192 IDR_PLATFORM_KEYS_CUSTOM_BINDINGS_JS);
193 source_map->RegisterSource("platformKeys.internalAPI",
194 IDR_PLATFORM_KEYS_INTERNAL_API_JS);
195 source_map->RegisterSource("platformKeys.Key", IDR_PLATFORM_KEYS_KEY_JS);
196 source_map->RegisterSource("platformKeys.SubtleCrypto",
197 IDR_PLATFORM_KEYS_SUBTLE_CRYPTO_JS);
198 source_map->RegisterSource("platformKeys.utils", IDR_PLATFORM_KEYS_UTILS_JS);
199 source_map->RegisterSource("syncFileSystem",
200 IDR_SYNC_FILE_SYSTEM_CUSTOM_BINDINGS_JS);
201 source_map->RegisterSource("systemIndicator",
202 IDR_SYSTEM_INDICATOR_CUSTOM_BINDINGS_JS);
203 source_map->RegisterSource("tabCapture", IDR_TAB_CAPTURE_CUSTOM_BINDINGS_JS);
204 source_map->RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS);
205 source_map->RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS);
206 source_map->RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS);
207 #if defined(ENABLE_WEBRTC)
208 source_map->RegisterSource("cast.streaming.rtpStream",
209 IDR_CAST_STREAMING_RTP_STREAM_CUSTOM_BINDINGS_JS);
210 source_map->RegisterSource("cast.streaming.session",
211 IDR_CAST_STREAMING_SESSION_CUSTOM_BINDINGS_JS);
212 source_map->RegisterSource(
213 "cast.streaming.udpTransport",
214 IDR_CAST_STREAMING_UDP_TRANSPORT_CUSTOM_BINDINGS_JS);
215 #endif
216 source_map->RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS);
218 // Custom types sources.
219 source_map->RegisterSource("ChromeSetting", IDR_CHROME_SETTING_JS);
220 source_map->RegisterSource("ContentSetting", IDR_CONTENT_SETTING_JS);
221 source_map->RegisterSource("ChromeDirectSetting",
222 IDR_CHROME_DIRECT_SETTING_JS);
224 // Platform app sources that are not API-specific..
225 source_map->RegisterSource("fileEntryBindingUtil",
226 IDR_FILE_ENTRY_BINDING_UTIL_JS);
227 source_map->RegisterSource("extensionOptions", IDR_EXTENSION_OPTIONS_JS);
228 source_map->RegisterSource("extensionOptionsEvents",
229 IDR_EXTENSION_OPTIONS_EVENTS_JS);
230 source_map->RegisterSource("tagWatcher", IDR_TAG_WATCHER_JS);
231 source_map->RegisterSource("chromeWebViewInternal",
232 IDR_CHROME_WEB_VIEW_INTERNAL_CUSTOM_BINDINGS_JS);
233 source_map->RegisterSource("chromeWebView", IDR_CHROME_WEB_VIEW_JS);
234 source_map->RegisterSource("chromeWebViewExperimental",
235 IDR_CHROME_WEB_VIEW_EXPERIMENTAL_JS);
236 source_map->RegisterSource("injectAppTitlebar", IDR_INJECT_APP_TITLEBAR_JS);
239 void ChromeExtensionsDispatcherDelegate::RequireAdditionalModules(
240 extensions::ScriptContext* context,
241 bool is_within_platform_app) {
242 extensions::ModuleSystem* module_system = context->module_system();
243 extensions::Feature::Context context_type = context->context_type();
245 // TODO(kalman, fsamuel): Eagerly calling Require on context startup is
246 // expensive. It would be better if there were a light way of detecting when
247 // a webview or appview is created and only then set up the infrastructure.
248 if (context_type == extensions::Feature::BLESSED_EXTENSION_CONTEXT &&
249 is_within_platform_app &&
250 extensions::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV &&
251 base::CommandLine::ForCurrentProcess()->HasSwitch(
252 extensions::switches::kEnableAppWindowControls)) {
253 module_system->Require("windowControls");
256 // Note: setting up the WebView class here, not the chrome.webview API.
257 // The API will be automatically set up when first used.
258 if (context->GetAvailability("webViewInternal").is_available()) {
259 module_system->Require("chromeWebView");
260 if (context->GetAvailability("webViewExperimentalInternal")
261 .is_available()) {
262 module_system->Require("chromeWebViewExperimental");
266 if (context->GetAvailability("extensionOptionsInternal").is_available()) {
267 module_system->Require("extensionOptions");
271 void ChromeExtensionsDispatcherDelegate::OnActiveExtensionsUpdated(
272 const std::set<std::string>& extension_ids) {
273 // In single-process mode, the browser process reports the active extensions.
274 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
275 ::switches::kSingleProcess))
276 return;
277 crash_keys::SetActiveExtensions(extension_ids);
280 void ChromeExtensionsDispatcherDelegate::SetChannel(int channel) {
281 extensions::SetCurrentChannel(
282 static_cast<chrome::VersionInfo::Channel>(channel));