blob: 00a1f39e8c4a645fa31fef82dd72485aa1638988 [file] [log] [blame]
[email protected]64c820732012-01-05 20:50:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bfdffe2b2009-04-24 22:05:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_function_dispatcher.h"
6
[email protected]745feedb2010-08-02 04:08:077#include <map>
8
[email protected]4b64d712013-01-17 17:53:179#include "base/bind.h"
[email protected]ffbec692012-02-26 20:26:4210#include "base/json/json_string_value_serializer.h"
[email protected]5bc248a2012-04-04 23:38:1111#include "base/lazy_instance.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
[email protected]bfdffe2b2009-04-24 22:05:3513#include "base/process_util.h"
[email protected]bfdffe2b2009-04-24 22:05:3514#include "base/values.h"
[email protected]17d40f02010-07-01 01:18:0615#include "build/build_config.h"
[email protected]b1912d592012-08-17 22:29:3816#include "chrome/browser/extensions/activity_log.h"
[email protected]bfdffe2b2009-04-24 22:05:3517#include "chrome/browser/extensions/extension_function.h"
[email protected]ae33d322012-03-19 22:24:3518#include "chrome/browser/extensions/extension_function_registry.h"
[email protected]2f69b382011-02-19 00:34:2519#include "chrome/browser/extensions/extension_service.h"
[email protected]efb4b082012-10-17 22:28:2820#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1021#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]d13950e2009-12-04 01:43:0222#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]83820d42011-11-12 22:03:1123#include "chrome/browser/extensions/process_map.h"
[email protected]ed2b1002011-05-25 14:12:1024#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3325#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4226#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]83820d42011-11-12 22:03:1127#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2328#include "chrome/common/extensions/extension_messages.h"
[email protected]615d88f2011-12-13 01:47:4429#include "chrome/common/extensions/extension_set.h"
[email protected]9c45b7182009-08-04 16:44:4330#include "chrome/common/url_constants.h"
[email protected]4b64d712013-01-17 17:53:1731#include "content/public/browser/browser_thread.h"
[email protected]c333e792012-01-06 16:57:3932#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3333#include "content/public/browser/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1734#include "ipc/ipc_message.h"
35#include "ipc/ipc_message_macros.h"
[email protected]615d88f2011-12-13 01:47:4436#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
[email protected]ae33d322012-03-19 22:24:3537#include "webkit/glue/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1038
[email protected]1c321ee2012-05-21 03:02:3439using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1140using extensions::ExtensionAPI;
[email protected]eaabba22012-03-07 15:02:1141using content::RenderViewHost;
[email protected]615d88f2011-12-13 01:47:4442using WebKit::WebSecurityOrigin;
[email protected]83820d42011-11-12 22:03:1143
[email protected]5bc248a2012-04-04 23:38:1144namespace {
45
[email protected]efd75992011-12-15 22:42:4246const char kAccessDenied[] = "access denied";
47const char kQuotaExceeded[] = "quota exceeded";
48
49void LogSuccess(const Extension* extension,
[email protected]4b64d712013-01-17 17:53:1750 const std::string& api_name,
51 scoped_ptr<ListValue> args,
52 Profile* profile) {
53 // The ActivityLog can only be accessed from the main (UI) thread. If we're
54 // running on the wrong thread, re-dispatch from the main thread.
55 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
56 BrowserThread::PostTask(BrowserThread::UI,
57 FROM_HERE,
58 base::Bind(&LogSuccess,
59 extension,
60 api_name,
[email protected]c02087b512013-02-04 03:09:2061 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1762 profile));
63 } else {
64 extensions::ActivityLog* activity_log =
65 extensions::ActivityLog::GetInstance(profile);
[email protected]73633762013-01-18 05:42:5666 activity_log->LogAPIAction(extension, api_name, args.get(), "");
[email protected]efd75992011-12-15 22:42:4267 }
68}
69
70void LogFailure(const Extension* extension,
[email protected]4b64d712013-01-17 17:53:1771 const std::string& api_name,
72 scoped_ptr<ListValue> args,
73 const char* reason,
74 Profile* profile) {
75 // The ActivityLog can only be accessed from the main (UI) thread. If we're
76 // running on the wrong thread, re-dispatch from the main thread.
77 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
78 BrowserThread::PostTask(BrowserThread::UI,
79 FROM_HERE,
80 base::Bind(&LogFailure,
81 extension,
82 api_name,
[email protected]c02087b512013-02-04 03:09:2083 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1784 reason,
85 profile));
86 } else {
87 extensions::ActivityLog* activity_log =
88 extensions::ActivityLog::GetInstance(profile);
[email protected]73633762013-01-18 05:42:5689 activity_log->LogBlockedAction(extension,
90 api_name,
91 args.get(),
92 reason,
93 "");
[email protected]efd75992011-12-15 22:42:4294 }
95}
96
[email protected]4b64d712013-01-17 17:53:1797
[email protected]5bc248a2012-04-04 23:38:1198// Separate copy of ExtensionAPI used for IO thread extension functions. We need
99// this because ExtensionAPI has mutable data. It should be possible to remove
100// this once all the extension APIs are updated to the feature system.
101struct Static {
102 Static()
103 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
104 }
105 scoped_ptr<extensions::ExtensionAPI> api;
106};
107base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
108
109} // namespace
110
[email protected]44f4b132012-07-17 20:36:57111extensions::WindowController*
112ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08113 const {
114 return NULL;
115}
116
117content::WebContents*
[email protected]44f4b132012-07-17 20:36:57118ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08119 return NULL;
120}
[email protected]5bc248a2012-04-04 23:38:11121
[email protected]bfdffe2b2009-04-24 22:05:35122void ExtensionFunctionDispatcher::GetAllFunctionNames(
123 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35124 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35125}
126
[email protected]b83e4602009-05-15 22:58:33127bool ExtensionFunctionDispatcher::OverrideFunction(
128 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35129 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
130 factory);
[email protected]b83e4602009-05-15 22:58:33131}
132
133void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35134 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33135}
136
[email protected]c357acb42011-06-09 20:52:42137// static
138void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25139 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18140 void* profile,
[email protected]c357acb42011-06-09 20:52:42141 int render_process_id,
142 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53143 int routing_id,
[email protected]c357acb42011-06-09 20:52:42144 const ExtensionHostMsg_Request_Params& params) {
145 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11146 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17147 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]6f371442011-11-09 06:45:46148 scoped_refptr<ExtensionFunction> function(
149 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11150 extension_info_map->process_map(),
151 g_global_io_data.Get().api.get(),
152 profile,
[email protected]3d0e2262012-08-02 15:32:16153 ipc_sender, NULL, routing_id));
[email protected]4b64d712013-01-17 17:53:17154 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
155
[email protected]efd75992011-12-15 22:42:42156 if (!function) {
[email protected]4b64d712013-01-17 17:53:17157 LogFailure(extension,
158 params.name,
159 args.Pass(),
160 kAccessDenied,
161 profile_cast);
[email protected]c357acb42011-06-09 20:52:42162 return;
[email protected]efd75992011-12-15 22:42:42163 }
[email protected]c357acb42011-06-09 20:52:42164
165 IOThreadExtensionFunction* function_io =
166 function->AsIOThreadExtensionFunction();
167 if (!function_io) {
168 NOTREACHED();
169 return;
170 }
[email protected]74e21e72012-07-09 21:20:53171 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42172 function_io->set_extension_info_map(extension_info_map);
173 function->set_include_incognito(
174 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25175
[email protected]d2fe22ff2012-10-03 00:40:07176 if (!CheckPermissions(function, extension, params, ipc_sender, routing_id)) {
[email protected]4b64d712013-01-17 17:53:17177 LogFailure(extension,
178 params.name,
179 args.Pass(),
180 kAccessDenied,
181 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07182 return;
183 }
184
[email protected]36296912012-03-20 11:08:49185 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29186 std::string violation_error = quota->Assess(extension->id(),
187 function,
188 &params.arguments,
189 base::TimeTicks::Now());
190 if (violation_error.empty()) {
[email protected]4b64d712013-01-17 17:53:17191 LogSuccess(extension,
192 params.name,
193 args.Pass(),
194 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25195 function->Run();
[email protected]fd50e7b2011-11-03 09:20:25196 } else {
[email protected]4b64d712013-01-17 17:53:17197 LogFailure(extension,
198 params.name,
199 args.Pass(),
200 kQuotaExceeded,
201 profile_cast);
[email protected]85231d72012-08-31 09:45:29202 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25203 }
[email protected]c357acb42011-06-09 20:52:42204}
205
[email protected]c5dbef02011-05-13 05:06:09206ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
207 Delegate* delegate)
208 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27209 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35210}
211
[email protected]32dda362009-06-05 19:07:01212ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01213}
214
[email protected]c5dbef02011-05-13 05:06:09215void ExtensionFunctionDispatcher::Dispatch(
216 const ExtensionHostMsg_Request_Params& params,
217 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09218 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28219 ExtensionProcessManager* process_manager =
220 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46221 extensions::ProcessMap* process_map = service->process_map();
222 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09223 return;
224
[email protected]615d88f2011-12-13 01:47:44225 const Extension* extension = service->extensions()->GetByID(
226 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09227 if (!extension)
[email protected]615d88f2011-12-13 01:47:44228 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
229 WebSecurityOrigin::createFromString(params.source_origin),
230 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09231
[email protected]8add5412011-10-01 21:02:14232 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53233 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58234 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46235 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11236 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]3d0e2262012-08-02 15:32:16237 profile(), render_view_host, render_view_host,
[email protected]74e21e72012-07-09 21:20:53238 render_view_host->GetRoutingID()));
[email protected]4b64d712013-01-17 17:53:17239 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
240
[email protected]efd75992011-12-15 22:42:42241 if (!function) {
[email protected]4b64d712013-01-17 17:53:17242 LogFailure(extension,
243 params.name,
244 args.Pass(),
245 kAccessDenied,
246 profile());
[email protected]f82d57b52011-04-27 19:13:17247 return;
[email protected]efd75992011-12-15 22:42:42248 }
[email protected]f82d57b52011-04-27 19:13:17249
[email protected]a2aef2e2011-05-26 22:48:12250 UIThreadExtensionFunction* function_ui =
251 function->AsUIThreadExtensionFunction();
252 if (!function_ui) {
253 NOTREACHED();
254 return;
255 }
[email protected]a2aef2e2011-05-26 22:48:12256 function_ui->set_dispatcher(AsWeakPtr());
257 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05258 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41259
[email protected]d2fe22ff2012-10-03 00:40:07260 if (!CheckPermissions(function, extension, params, render_view_host,
261 render_view_host->GetRoutingID())) {
[email protected]4b64d712013-01-17 17:53:17262 LogFailure(extension,
263 params.name,
264 args.Pass(),
265 kAccessDenied,
266 profile());
[email protected]d2fe22ff2012-10-03 00:40:07267 return;
268 }
269
[email protected]d13950e2009-12-04 01:43:02270 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29271 std::string violation_error = quota->Assess(extension->id(),
272 function,
273 &params.arguments,
274 base::TimeTicks::Now());
275 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26276 // See crbug.com/39178.
277 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]4b64d712013-01-17 17:53:17278 LogSuccess(extension, params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02279 function->Run();
[email protected]d13950e2009-12-04 01:43:02280 } else {
[email protected]4b64d712013-01-17 17:53:17281 LogFailure(extension,
282 params.name,
283 args.Pass(),
284 kQuotaExceeded,
285 profile());
[email protected]85231d72012-08-31 09:45:29286 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02287 }
[email protected]720ad1312012-02-27 23:07:36288
[email protected]efb4b082012-10-17 22:28:28289 // Note: do not access |this| after this point. We may have been deleted
290 // if function->Run() ended up closing the tab that owns us.
291
[email protected]5734e882012-05-04 22:17:56292 // Check if extension was uninstalled by management.uninstall.
293 if (!service->extensions()->GetByID(params.extension_id))
294 return;
295
[email protected]720ad1312012-02-27 23:07:36296 // We only adjust the keepalive count for UIThreadExtensionFunction for
297 // now, largely for simplicity's sake. This is OK because currently, only
298 // the webRequest API uses IOThreadExtensionFunction, and that API is not
299 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28300 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36301}
302
303void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
304 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03305 extensions::ExtensionSystem::Get(profile())->process_manager()->
306 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35307}
308
[email protected]c357acb42011-06-09 20:52:42309// static
[email protected]d2fe22ff2012-10-03 00:40:07310bool ExtensionFunctionDispatcher::CheckPermissions(
311 ExtensionFunction* function,
312 const Extension* extension,
313 const ExtensionHostMsg_Request_Params& params,
314 IPC::Sender* ipc_sender,
315 int routing_id) {
316 if (!function->HasPermission()) {
317 LOG(ERROR) << "Extension " << extension->id() << " does not have "
318 << "permission to function: " << params.name;
319 SendAccessDenied(ipc_sender, routing_id, params.request_id);
320 return false;
321 }
322 return true;
323}
324
[email protected]f33542112013-02-04 16:52:38325namespace {
326
327// Only COMPONENT hosted apps may call extension APIs, and they are limited
328// to just the permissions they explicitly request. They should not have access
329// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
330// are available without permission.
331// TODO(asargent/kalman) - get rid of this when the features system can express
332// the "non permission" permissions.
333bool AllowHostedAppAPICall(const Extension& extension,
334 const GURL& source_url,
335 const std::string& function_name) {
336 if (extension.location() != extensions::Manifest::COMPONENT)
337 return false;
338
339 if (!extension.web_extent().MatchesURL(source_url))
340 return false;
341
342 // We just allow the hosted app's explicit permissions, plus chrome.test.
343 scoped_refptr<const extensions::PermissionSet> permissions =
344 extension.GetActivePermissions();
345 return (permissions->HasAccessToFunction(function_name, false) ||
346 StartsWithASCII(function_name, "test.", true /*case_sensitive*/));
347}
348
349} // namespace
350
351
[email protected]d2fe22ff2012-10-03 00:40:07352// static
[email protected]c357acb42011-06-09 20:52:42353ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
354 const ExtensionHostMsg_Request_Params& params,
355 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46356 int requesting_process_id,
357 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11358 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18359 void* profile,
[email protected]74e21e72012-07-09 21:20:53360 IPC::Sender* ipc_sender,
[email protected]3d0e2262012-08-02 15:32:16361 RenderViewHost* render_view_host,
[email protected]74e21e72012-07-09 21:20:53362 int routing_id) {
[email protected]c357acb42011-06-09 20:52:42363 if (!extension) {
[email protected]6f371442011-11-09 06:45:46364 LOG(ERROR) << "Specified extension does not exist.";
[email protected]74e21e72012-07-09 21:20:53365 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]6f371442011-11-09 06:45:46366 return NULL;
367 }
368
[email protected]f33542112013-02-04 16:52:38369 // Most hosted apps can't call APIs.
370 bool allowed = true;
371 if (extension->is_hosted_app())
372 allowed = AllowHostedAppAPICall(*extension, params.source_url,
373 params.name);
374
375 // Privileged APIs can only be called from the process the extension
376 // is running in.
377 if (allowed && api->IsPrivileged(params.name))
378 allowed = process_map.Contains(extension->id(), requesting_process_id);
379
380 if (!allowed) {
381 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
382 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46383 << " from URL " << params.source_url.spec();
[email protected]74e21e72012-07-09 21:20:53384 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]c357acb42011-06-09 20:52:42385 return NULL;
386 }
387
[email protected]c357acb42011-06-09 20:52:42388 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35389 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]c357acb42011-06-09 20:52:42390 function->SetArgs(&params.arguments);
391 function->set_source_url(params.source_url);
392 function->set_request_id(params.request_id);
393 function->set_has_callback(params.has_callback);
394 function->set_user_gesture(params.user_gesture);
395 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32396 function->set_profile_id(profile);
[email protected]3d0e2262012-08-02 15:32:16397
398 UIThreadExtensionFunction* function_ui =
399 function->AsUIThreadExtensionFunction();
400 if (function_ui) {
401 function_ui->SetRenderViewHost(render_view_host);
402 }
403
[email protected]c357acb42011-06-09 20:52:42404 return function;
405}
406
407// static
[email protected]c5dbef02011-05-13 05:06:09408void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]74e21e72012-07-09 21:20:53409 IPC::Sender* ipc_sender, int routing_id, int request_id) {
[email protected]602542d2012-04-20 02:48:01410 ListValue empty_list;
[email protected]c357acb42011-06-09 20:52:42411 ipc_sender->Send(new ExtensionMsg_Response(
[email protected]74e21e72012-07-09 21:20:53412 routing_id, request_id, false, empty_list,
[email protected]c5dbef02011-05-13 05:06:09413 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35414}