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/renderer/extensions/app_bindings.h"
16 #include "chrome/renderer/extensions/app_window_custom_bindings.h"
17 #include "chrome/renderer/extensions/chrome_v8_context.h"
18 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h"
19 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h"
20 #include "chrome/renderer/extensions/media_galleries_custom_bindings.h"
21 #include "chrome/renderer/extensions/page_actions_custom_bindings.h"
22 #include "chrome/renderer/extensions/page_capture_custom_bindings.h"
23 #include "chrome/renderer/extensions/pepper_request_natives.h"
24 #include "chrome/renderer/extensions/sync_file_system_custom_bindings.h"
25 #include "chrome/renderer/extensions/tab_finder.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/permissions/api_permission_set.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/url_pattern_set.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 "grit/renderer_resources.h"
41 #include "third_party/WebKit/public/platform/WebString.h"
42 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
44 #if defined(ENABLE_WEBRTC)
45 #include "chrome/renderer/extensions/cast_streaming_native_handler.h"
48 using extensions::NativeHandler
;
50 ChromeExtensionsDispatcherDelegate::ChromeExtensionsDispatcherDelegate()
51 : webrequest_adblock_(false),
52 webrequest_adblock_plus_(false),
53 webrequest_other_(false) {
56 ChromeExtensionsDispatcherDelegate::~ChromeExtensionsDispatcherDelegate() {
59 scoped_ptr
<extensions::ScriptContext
>
60 ChromeExtensionsDispatcherDelegate::CreateScriptContext(
61 const v8::Handle
<v8::Context
>& v8_context
,
62 blink::WebFrame
* frame
,
63 const extensions::Extension
* extension
,
64 extensions::Feature::Context context_type
) {
65 return scoped_ptr
<extensions::ScriptContext
>(new extensions::ChromeV8Context(
66 v8_context
, frame
, extension
, context_type
));
69 void ChromeExtensionsDispatcherDelegate::InitOriginPermissions(
70 const extensions::Extension
* extension
,
71 extensions::Feature::Context context_type
) {
72 // TODO(jstritar): We should try to remove this special case. Also, these
73 // whitelist entries need to be updated when the kManagement permission
75 if (context_type
== extensions::Feature::BLESSED_EXTENSION_CONTEXT
&&
76 extension
->HasAPIPermission(extensions::APIPermission::kManagement
)) {
77 blink::WebSecurityPolicy::addOriginAccessWhitelistEntry(
79 blink::WebString::fromUTF8(content::kChromeUIScheme
),
80 blink::WebString::fromUTF8(chrome::kChromeUIExtensionIconHost
),
85 void ChromeExtensionsDispatcherDelegate::RegisterNativeHandlers(
86 extensions::Dispatcher
* dispatcher
,
87 extensions::ModuleSystem
* module_system
,
88 extensions::ScriptContext
* context
) {
89 #if !defined(ENABLE_EXTENSIONS)
92 module_system
->RegisterNativeHandler(
94 scoped_ptr
<NativeHandler
>(
95 new extensions::AppBindings(dispatcher
, context
)));
96 module_system
->RegisterNativeHandler(
98 scoped_ptr
<NativeHandler
>(
99 new extensions::AppWindowCustomBindings(dispatcher
, context
)));
100 module_system
->RegisterNativeHandler(
102 scoped_ptr
<NativeHandler
>(
103 new extensions::SyncFileSystemCustomBindings(context
)));
104 module_system
->RegisterNativeHandler(
105 "file_browser_handler",
106 scoped_ptr
<NativeHandler
>(
107 new extensions::FileBrowserHandlerCustomBindings(context
)));
108 module_system
->RegisterNativeHandler(
109 "file_browser_private",
110 scoped_ptr
<NativeHandler
>(
111 new extensions::FileBrowserPrivateCustomBindings(context
)));
112 module_system
->RegisterNativeHandler(
114 scoped_ptr
<NativeHandler
>(
115 new extensions::MediaGalleriesCustomBindings(context
)));
116 module_system
->RegisterNativeHandler(
118 scoped_ptr
<NativeHandler
>(
119 new extensions::PageActionsCustomBindings(dispatcher
, context
)));
120 module_system
->RegisterNativeHandler(
122 scoped_ptr
<NativeHandler
>(
123 new extensions::PageCaptureCustomBindings(context
)));
124 module_system
->RegisterNativeHandler(
125 "pepper_request_natives",
126 scoped_ptr
<NativeHandler
>(new extensions::PepperRequestNatives(context
)));
127 module_system
->RegisterNativeHandler(
129 scoped_ptr
<NativeHandler
>(new extensions::TabsCustomBindings(context
)));
130 module_system
->RegisterNativeHandler(
132 scoped_ptr
<NativeHandler
>(new extensions::WebstoreBindings(context
)));
133 #if defined(ENABLE_WEBRTC)
134 module_system
->RegisterNativeHandler(
135 "cast_streaming_natives",
136 scoped_ptr
<NativeHandler
>(
137 new extensions::CastStreamingNativeHandler(context
)));
141 void ChromeExtensionsDispatcherDelegate::PopulateSourceMap(
142 extensions::ResourceBundleSourceMap
* source_map
) {
144 source_map
->RegisterSource("pepper_request", IDR_PEPPER_REQUEST_JS
);
147 source_map
->RegisterSource("app", IDR_APP_CUSTOM_BINDINGS_JS
);
148 source_map
->RegisterSource("app.window", IDR_APP_WINDOW_CUSTOM_BINDINGS_JS
);
149 source_map
->RegisterSource("automation", IDR_AUTOMATION_CUSTOM_BINDINGS_JS
);
150 source_map
->RegisterSource("automationEvent", IDR_AUTOMATION_EVENT_JS
);
151 source_map
->RegisterSource("automationNode", IDR_AUTOMATION_NODE_JS
);
152 source_map
->RegisterSource("automationTree", IDR_AUTOMATION_TREE_JS
);
153 source_map
->RegisterSource("browserAction",
154 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS
);
155 source_map
->RegisterSource("declarativeContent",
156 IDR_DECLARATIVE_CONTENT_CUSTOM_BINDINGS_JS
);
157 source_map
->RegisterSource("declarativeWebRequest",
158 IDR_DECLARATIVE_WEBREQUEST_CUSTOM_BINDINGS_JS
);
159 source_map
->RegisterSource("desktopCapture",
160 IDR_DESKTOP_CAPTURE_CUSTOM_BINDINGS_JS
);
161 source_map
->RegisterSource("developerPrivate",
162 IDR_DEVELOPER_PRIVATE_CUSTOM_BINDINGS_JS
);
163 source_map
->RegisterSource("downloads", IDR_DOWNLOADS_CUSTOM_BINDINGS_JS
);
164 source_map
->RegisterSource("feedbackPrivate",
165 IDR_FEEDBACK_PRIVATE_CUSTOM_BINDINGS_JS
);
166 source_map
->RegisterSource("fileBrowserHandler",
167 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS
);
168 source_map
->RegisterSource("fileBrowserPrivate",
169 IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS
);
170 source_map
->RegisterSource("fileSystem", IDR_FILE_SYSTEM_CUSTOM_BINDINGS_JS
);
171 source_map
->RegisterSource("fileSystemProvider",
172 IDR_FILE_SYSTEM_PROVIDER_CUSTOM_BINDINGS_JS
);
173 source_map
->RegisterSource("gcm", IDR_GCM_CUSTOM_BINDINGS_JS
);
174 source_map
->RegisterSource("identity", IDR_IDENTITY_CUSTOM_BINDINGS_JS
);
175 source_map
->RegisterSource("imageWriterPrivate",
176 IDR_IMAGE_WRITER_PRIVATE_CUSTOM_BINDINGS_JS
);
177 source_map
->RegisterSource("input.ime", IDR_INPUT_IME_CUSTOM_BINDINGS_JS
);
178 source_map
->RegisterSource("mediaGalleries",
179 IDR_MEDIA_GALLERIES_CUSTOM_BINDINGS_JS
);
180 source_map
->RegisterSource("notifications",
181 IDR_NOTIFICATIONS_CUSTOM_BINDINGS_JS
);
182 source_map
->RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS
);
183 source_map
->RegisterSource("pageActions",
184 IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS
);
185 source_map
->RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS
);
186 source_map
->RegisterSource("pageCapture",
187 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS
);
188 source_map
->RegisterSource("syncFileSystem",
189 IDR_SYNC_FILE_SYSTEM_CUSTOM_BINDINGS_JS
);
190 source_map
->RegisterSource("systemIndicator",
191 IDR_SYSTEM_INDICATOR_CUSTOM_BINDINGS_JS
);
192 source_map
->RegisterSource("tabCapture", IDR_TAB_CAPTURE_CUSTOM_BINDINGS_JS
);
193 source_map
->RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS
);
194 source_map
->RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS
);
195 source_map
->RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS
);
196 source_map
->RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS
);
197 source_map
->RegisterSource("webRequestInternal",
198 IDR_WEB_REQUEST_INTERNAL_CUSTOM_BINDINGS_JS
);
199 #if defined(ENABLE_WEBRTC)
200 source_map
->RegisterSource("cast.streaming.rtpStream",
201 IDR_CAST_STREAMING_RTP_STREAM_CUSTOM_BINDINGS_JS
);
202 source_map
->RegisterSource("cast.streaming.session",
203 IDR_CAST_STREAMING_SESSION_CUSTOM_BINDINGS_JS
);
204 source_map
->RegisterSource(
205 "cast.streaming.udpTransport",
206 IDR_CAST_STREAMING_UDP_TRANSPORT_CUSTOM_BINDINGS_JS
);
208 source_map
->RegisterSource("webstore", IDR_WEBSTORE_CUSTOM_BINDINGS_JS
);
209 source_map
->RegisterSource("windowControls", IDR_WINDOW_CONTROLS_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("tagWatcher", IDR_TAG_WATCHER_JS
);
219 source_map
->RegisterSource("webview", IDR_WEBVIEW_CUSTOM_BINDINGS_JS
);
220 // Note: webView not webview so that this doesn't interfere with the
221 // chrome.webview API bindings.
222 source_map
->RegisterSource("webView", IDR_WEB_VIEW_JS
);
223 source_map
->RegisterSource("webViewExperimental",
224 IDR_WEB_VIEW_EXPERIMENTAL_JS
);
225 source_map
->RegisterSource("webViewRequest",
226 IDR_WEB_VIEW_REQUEST_CUSTOM_BINDINGS_JS
);
227 source_map
->RegisterSource("denyWebView", IDR_WEB_VIEW_DENY_JS
);
228 source_map
->RegisterSource("adView", IDR_AD_VIEW_JS
);
229 source_map
->RegisterSource("denyAdView", IDR_AD_VIEW_DENY_JS
);
230 source_map
->RegisterSource("injectAppTitlebar", IDR_INJECT_APP_TITLEBAR_JS
);
233 void ChromeExtensionsDispatcherDelegate::RequireAdditionalModules(
234 extensions::ModuleSystem
* module_system
,
235 const extensions::Extension
* extension
,
236 extensions::Feature::Context context_type
,
237 bool is_within_platform_app
) {
238 if (context_type
== extensions::Feature::BLESSED_EXTENSION_CONTEXT
&&
239 is_within_platform_app
&&
240 extensions::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV
&&
241 CommandLine::ForCurrentProcess()->HasSwitch(
242 ::switches::kEnableAppWindowControls
)) {
243 module_system
->Require("windowControls");
246 // We used to limit WebView to |BLESSED_EXTENSION_CONTEXT| within platform
247 // apps. An ext/app runs in a blessed extension context, if it is the active
248 // extension in the current process, in other words, if it is loaded in a top
249 // frame. To support webview in a non-frame extension, we have to allow
250 // unblessed extension context as well.
251 // Note: setting up the WebView class here, not the chrome.webview API.
252 // The API will be automatically set up when first used.
253 if (context_type
== extensions::Feature::BLESSED_EXTENSION_CONTEXT
||
254 context_type
== extensions::Feature::UNBLESSED_EXTENSION_CONTEXT
) {
255 if (extension
->HasAPIPermission(extensions::APIPermission::kWebView
)) {
256 module_system
->Require("webView");
257 if (extensions::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV
) {
258 module_system
->Require("webViewExperimental");
260 // TODO(asargent) We need a whitelist for webview experimental.
262 std::string id_hash
= base::SHA1HashString(extension
->id());
263 std::string hexencoded_id_hash
=
264 base::HexEncode(id_hash
.c_str(), id_hash
.length());
265 if (hexencoded_id_hash
== "8C3741E3AF0B93B6E8E0DDD499BB0B74839EA578" ||
266 hexencoded_id_hash
== "E703483CEF33DEC18B4B6DD84B5C776FB9182BDB" ||
267 hexencoded_id_hash
== "1A26E32DE447A17CBE5E9750CDBA78F58539B39C" ||
268 hexencoded_id_hash
== "59048028102D7B4C681DBC7BC6CD980C3DC66DA3") {
269 module_system
->Require("webViewExperimental");
273 module_system
->Require("denyWebView");
277 // Same comment as above for <adview> tag.
278 if (context_type
== extensions::Feature::BLESSED_EXTENSION_CONTEXT
&&
279 is_within_platform_app
) {
280 if (CommandLine::ForCurrentProcess()->HasSwitch(
281 ::switches::kEnableAdview
)) {
282 if (extension
->HasAPIPermission(extensions::APIPermission::kAdView
)) {
283 module_system
->Require("adView");
285 module_system
->Require("denyAdView");
291 void ChromeExtensionsDispatcherDelegate::OnActiveExtensionsUpdated(
292 const std::set
<std::string
>& extension_ids
) {
293 // In single-process mode, the browser process reports the active extensions.
294 if (CommandLine::ForCurrentProcess()->HasSwitch(::switches::kSingleProcess
))
296 crash_keys::SetActiveExtensions(extension_ids
);
299 void ChromeExtensionsDispatcherDelegate::SetChannel(int channel
) {
300 extensions::SetCurrentChannel(
301 static_cast<chrome::VersionInfo::Channel
>(channel
));
304 void ChromeExtensionsDispatcherDelegate::ClearTabSpecificPermissions(
305 const extensions::Dispatcher
* dispatcher
,
307 const std::vector
<std::string
>& extension_ids
) {
308 for (std::vector
<std::string
>::const_iterator it
= extension_ids
.begin();
309 it
!= extension_ids
.end();
311 const extensions::Extension
* extension
=
312 dispatcher
->extensions()->GetByID(*it
);
314 extensions::PermissionsData::ClearTabSpecificPermissions(extension
,
319 void ChromeExtensionsDispatcherDelegate::UpdateTabSpecificPermissions(
320 const extensions::Dispatcher
* dispatcher
,
323 const std::string
& extension_id
,
324 const extensions::URLPatternSet
& origin_set
) {
325 content::RenderView
* view
= extensions::TabFinder::Find(tab_id
);
327 // For now, the message should only be sent to the render view that contains
328 // the target tab. This may change. Either way, if this is the target tab it
329 // gives us the chance to check against the page ID to avoid races.
331 if (view
&& view
->GetPageId() != page_id
)
334 const extensions::Extension
* extension
=
335 dispatcher
->extensions()->GetByID(extension_id
);
339 extensions::PermissionsData::UpdateTabSpecificPermissions(
342 new extensions::PermissionSet(extensions::APIPermissionSet(),
343 extensions::ManifestPermissionSet(),
345 extensions::URLPatternSet()));
348 void ChromeExtensionsDispatcherDelegate::HandleWebRequestAPIUsage(
352 webrequest_adblock_
= adblock
;
353 webrequest_adblock_plus_
= adblock_plus
;
354 webrequest_other_
= other
;