blob: b634303f685359632e034c5cc45dd0d8e211841e [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]ffbec692012-02-26 20:26:429#include "base/json/json_string_value_serializer.h"
[email protected]5bc248a2012-04-04 23:38:1110#include "base/lazy_instance.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
[email protected]bfdffe2b2009-04-24 22:05:3512#include "base/process_util.h"
[email protected]bfdffe2b2009-04-24 22:05:3513#include "base/values.h"
[email protected]17d40f02010-07-01 01:18:0614#include "build/build_config.h"
[email protected]b1912d592012-08-17 22:29:3815#include "chrome/browser/extensions/activity_log.h"
[email protected]bfdffe2b2009-04-24 22:05:3516#include "chrome/browser/extensions/extension_function.h"
[email protected]ae33d322012-03-19 22:24:3517#include "chrome/browser/extensions/extension_function_registry.h"
[email protected]2f69b382011-02-19 00:34:2518#include "chrome/browser/extensions/extension_service.h"
[email protected]efb4b082012-10-17 22:28:2819#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1020#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]d13950e2009-12-04 01:43:0221#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]83820d42011-11-12 22:03:1122#include "chrome/browser/extensions/process_map.h"
[email protected]ed2b1002011-05-25 14:12:1023#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3324#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4225#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]83820d42011-11-12 22:03:1126#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2327#include "chrome/common/extensions/extension_messages.h"
[email protected]615d88f2011-12-13 01:47:4428#include "chrome/common/extensions/extension_set.h"
[email protected]9c45b7182009-08-04 16:44:4329#include "chrome/common/url_constants.h"
[email protected]c333e792012-01-06 16:57:3930#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3331#include "content/public/browser/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1732#include "ipc/ipc_message.h"
33#include "ipc/ipc_message_macros.h"
[email protected]615d88f2011-12-13 01:47:4434#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
[email protected]ae33d322012-03-19 22:24:3535#include "webkit/glue/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1036
[email protected]1c321ee2012-05-21 03:02:3437using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1138using extensions::ExtensionAPI;
[email protected]eaabba22012-03-07 15:02:1139using content::RenderViewHost;
[email protected]615d88f2011-12-13 01:47:4440using WebKit::WebSecurityOrigin;
[email protected]83820d42011-11-12 22:03:1141
[email protected]5bc248a2012-04-04 23:38:1142namespace {
43
[email protected]efd75992011-12-15 22:42:4244const char kAccessDenied[] = "access denied";
45const char kQuotaExceeded[] = "quota exceeded";
46
47void LogSuccess(const Extension* extension,
48 const ExtensionHostMsg_Request_Params& params) {
[email protected]b1912d592012-08-17 22:29:3849 extensions::ActivityLog* activity_log =
50 extensions::ActivityLog::GetInstance();
51 if (activity_log->HasObservers(extension)) {
[email protected]efd75992011-12-15 22:42:4252 std::string call_signature = params.name + "(";
53 ListValue::const_iterator it = params.arguments.begin();
54 for (; it != params.arguments.end(); ++it) {
55 std::string arg;
56 JSONStringValueSerializer serializer(&arg);
57 if (serializer.SerializeAndOmitBinaryValues(**it)) {
58 if (it != params.arguments.begin())
59 call_signature += ", ";
60 call_signature += arg;
61 }
62 }
63 call_signature += ")";
64
[email protected]b1912d592012-08-17 22:29:3865 activity_log->Log(extension,
66 extensions::ActivityLog::ACTIVITY_EXTENSION_API_CALL,
67 call_signature);
[email protected]efd75992011-12-15 22:42:4268 }
69}
70
71void LogFailure(const Extension* extension,
72 const std::string& func_name,
73 const char* reason) {
[email protected]b1912d592012-08-17 22:29:3874 extensions::ActivityLog* activity_log =
75 extensions::ActivityLog::GetInstance();
76 if (activity_log->HasObservers(extension)) {
77 activity_log->Log(extension,
78 extensions::ActivityLog::ACTIVITY_EXTENSION_API_BLOCK,
79 func_name + ": " + reason);
[email protected]efd75992011-12-15 22:42:4280 }
81}
82
[email protected]5bc248a2012-04-04 23:38:1183// Separate copy of ExtensionAPI used for IO thread extension functions. We need
84// this because ExtensionAPI has mutable data. It should be possible to remove
85// this once all the extension APIs are updated to the feature system.
86struct Static {
87 Static()
88 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
89 }
90 scoped_ptr<extensions::ExtensionAPI> api;
91};
92base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
93
94} // namespace
95
[email protected]44f4b132012-07-17 20:36:5796extensions::WindowController*
97ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:0898 const {
99 return NULL;
100}
101
102content::WebContents*
[email protected]44f4b132012-07-17 20:36:57103ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08104 return NULL;
105}
[email protected]5bc248a2012-04-04 23:38:11106
[email protected]bfdffe2b2009-04-24 22:05:35107void ExtensionFunctionDispatcher::GetAllFunctionNames(
108 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35109 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35110}
111
[email protected]b83e4602009-05-15 22:58:33112bool ExtensionFunctionDispatcher::OverrideFunction(
113 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35114 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
115 factory);
[email protected]b83e4602009-05-15 22:58:33116}
117
118void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35119 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33120}
121
[email protected]c357acb42011-06-09 20:52:42122// static
123void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25124 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18125 void* profile,
[email protected]c357acb42011-06-09 20:52:42126 int render_process_id,
127 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53128 int routing_id,
[email protected]c357acb42011-06-09 20:52:42129 const ExtensionHostMsg_Request_Params& params) {
130 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11131 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]c357acb42011-06-09 20:52:42132
[email protected]6f371442011-11-09 06:45:46133 scoped_refptr<ExtensionFunction> function(
134 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11135 extension_info_map->process_map(),
136 g_global_io_data.Get().api.get(),
137 profile,
[email protected]3d0e2262012-08-02 15:32:16138 ipc_sender, NULL, routing_id));
[email protected]efd75992011-12-15 22:42:42139 if (!function) {
140 LogFailure(extension, params.name, kAccessDenied);
[email protected]c357acb42011-06-09 20:52:42141 return;
[email protected]efd75992011-12-15 22:42:42142 }
[email protected]c357acb42011-06-09 20:52:42143
144 IOThreadExtensionFunction* function_io =
145 function->AsIOThreadExtensionFunction();
146 if (!function_io) {
147 NOTREACHED();
148 return;
149 }
[email protected]74e21e72012-07-09 21:20:53150 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42151 function_io->set_extension_info_map(extension_info_map);
152 function->set_include_incognito(
153 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25154
[email protected]d2fe22ff2012-10-03 00:40:07155 if (!CheckPermissions(function, extension, params, ipc_sender, routing_id)) {
156 LogFailure(extension, params.name, kAccessDenied);
157 return;
158 }
159
[email protected]36296912012-03-20 11:08:49160 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29161 std::string violation_error = quota->Assess(extension->id(),
162 function,
163 &params.arguments,
164 base::TimeTicks::Now());
165 if (violation_error.empty()) {
[email protected]fd50e7b2011-11-03 09:20:25166 function->Run();
[email protected]efd75992011-12-15 22:42:42167 LogSuccess(extension, params);
[email protected]fd50e7b2011-11-03 09:20:25168 } else {
[email protected]85231d72012-08-31 09:45:29169 function->OnQuotaExceeded(violation_error);
[email protected]efd75992011-12-15 22:42:42170 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]fd50e7b2011-11-03 09:20:25171 }
[email protected]c357acb42011-06-09 20:52:42172}
173
[email protected]c5dbef02011-05-13 05:06:09174ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
175 Delegate* delegate)
176 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27177 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35178}
179
[email protected]32dda362009-06-05 19:07:01180ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01181}
182
[email protected]c5dbef02011-05-13 05:06:09183void ExtensionFunctionDispatcher::Dispatch(
184 const ExtensionHostMsg_Request_Params& params,
185 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09186 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28187 ExtensionProcessManager* process_manager =
188 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46189 extensions::ProcessMap* process_map = service->process_map();
190 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09191 return;
192
[email protected]615d88f2011-12-13 01:47:44193 const Extension* extension = service->extensions()->GetByID(
194 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09195 if (!extension)
[email protected]615d88f2011-12-13 01:47:44196 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
197 WebSecurityOrigin::createFromString(params.source_origin),
198 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09199
[email protected]8add5412011-10-01 21:02:14200 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53201 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58202 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46203 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11204 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]3d0e2262012-08-02 15:32:16205 profile(), render_view_host, render_view_host,
[email protected]74e21e72012-07-09 21:20:53206 render_view_host->GetRoutingID()));
[email protected]efd75992011-12-15 22:42:42207 if (!function) {
208 LogFailure(extension, params.name, kAccessDenied);
[email protected]f82d57b52011-04-27 19:13:17209 return;
[email protected]efd75992011-12-15 22:42:42210 }
[email protected]f82d57b52011-04-27 19:13:17211
[email protected]a2aef2e2011-05-26 22:48:12212 UIThreadExtensionFunction* function_ui =
213 function->AsUIThreadExtensionFunction();
214 if (!function_ui) {
215 NOTREACHED();
216 return;
217 }
[email protected]a2aef2e2011-05-26 22:48:12218 function_ui->set_dispatcher(AsWeakPtr());
219 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05220 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41221
[email protected]d2fe22ff2012-10-03 00:40:07222 if (!CheckPermissions(function, extension, params, render_view_host,
223 render_view_host->GetRoutingID())) {
224 LogFailure(extension, params.name, kAccessDenied);
225 return;
226 }
227
[email protected]d13950e2009-12-04 01:43:02228 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29229 std::string violation_error = quota->Assess(extension->id(),
230 function,
231 &params.arguments,
232 base::TimeTicks::Now());
233 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26234 // See crbug.com/39178.
235 ExternalProtocolHandler::PermitLaunchUrl();
236
[email protected]d13950e2009-12-04 01:43:02237 function->Run();
[email protected]efd75992011-12-15 22:42:42238 LogSuccess(extension, params);
[email protected]d13950e2009-12-04 01:43:02239 } else {
[email protected]85231d72012-08-31 09:45:29240 function->OnQuotaExceeded(violation_error);
[email protected]efd75992011-12-15 22:42:42241 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]d13950e2009-12-04 01:43:02242 }
[email protected]720ad1312012-02-27 23:07:36243
[email protected]efb4b082012-10-17 22:28:28244 // Note: do not access |this| after this point. We may have been deleted
245 // if function->Run() ended up closing the tab that owns us.
246
[email protected]5734e882012-05-04 22:17:56247 // Check if extension was uninstalled by management.uninstall.
248 if (!service->extensions()->GetByID(params.extension_id))
249 return;
250
[email protected]720ad1312012-02-27 23:07:36251 // We only adjust the keepalive count for UIThreadExtensionFunction for
252 // now, largely for simplicity's sake. This is OK because currently, only
253 // the webRequest API uses IOThreadExtensionFunction, and that API is not
254 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28255 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36256}
257
258void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
259 const Extension* extension) {
260 profile()->GetExtensionProcessManager()->DecrementLazyKeepaliveCount(
261 extension);
[email protected]bfdffe2b2009-04-24 22:05:35262}
263
[email protected]c357acb42011-06-09 20:52:42264// static
[email protected]d2fe22ff2012-10-03 00:40:07265bool ExtensionFunctionDispatcher::CheckPermissions(
266 ExtensionFunction* function,
267 const Extension* extension,
268 const ExtensionHostMsg_Request_Params& params,
269 IPC::Sender* ipc_sender,
270 int routing_id) {
271 if (!function->HasPermission()) {
272 LOG(ERROR) << "Extension " << extension->id() << " does not have "
273 << "permission to function: " << params.name;
274 SendAccessDenied(ipc_sender, routing_id, params.request_id);
275 return false;
276 }
277 return true;
278}
279
280// static
[email protected]c357acb42011-06-09 20:52:42281ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
282 const ExtensionHostMsg_Request_Params& params,
283 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46284 int requesting_process_id,
285 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11286 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18287 void* profile,
[email protected]74e21e72012-07-09 21:20:53288 IPC::Sender* ipc_sender,
[email protected]3d0e2262012-08-02 15:32:16289 RenderViewHost* render_view_host,
[email protected]74e21e72012-07-09 21:20:53290 int routing_id) {
[email protected]c357acb42011-06-09 20:52:42291 if (!extension) {
[email protected]6f371442011-11-09 06:45:46292 LOG(ERROR) << "Specified extension does not exist.";
[email protected]74e21e72012-07-09 21:20:53293 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]6f371442011-11-09 06:45:46294 return NULL;
295 }
296
[email protected]5bc248a2012-04-04 23:38:11297 if (api->IsPrivileged(params.name) &&
[email protected]83820d42011-11-12 22:03:11298 !process_map.Contains(extension->id(), requesting_process_id)) {
[email protected]6f371442011-11-09 06:45:46299 LOG(ERROR) << "Extension API called from incorrect process "
300 << requesting_process_id
301 << " from URL " << params.source_url.spec();
[email protected]74e21e72012-07-09 21:20:53302 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]c357acb42011-06-09 20:52:42303 return NULL;
304 }
305
[email protected]c357acb42011-06-09 20:52:42306 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35307 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]c357acb42011-06-09 20:52:42308 function->SetArgs(&params.arguments);
309 function->set_source_url(params.source_url);
310 function->set_request_id(params.request_id);
311 function->set_has_callback(params.has_callback);
312 function->set_user_gesture(params.user_gesture);
313 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32314 function->set_profile_id(profile);
[email protected]3d0e2262012-08-02 15:32:16315
316 UIThreadExtensionFunction* function_ui =
317 function->AsUIThreadExtensionFunction();
318 if (function_ui) {
319 function_ui->SetRenderViewHost(render_view_host);
320 }
321
[email protected]c357acb42011-06-09 20:52:42322 return function;
323}
324
325// static
[email protected]c5dbef02011-05-13 05:06:09326void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]74e21e72012-07-09 21:20:53327 IPC::Sender* ipc_sender, int routing_id, int request_id) {
[email protected]602542d2012-04-20 02:48:01328 ListValue empty_list;
[email protected]c357acb42011-06-09 20:52:42329 ipc_sender->Send(new ExtensionMsg_Response(
[email protected]74e21e72012-07-09 21:20:53330 routing_id, request_id, false, empty_list,
[email protected]c5dbef02011-05-13 05:06:09331 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35332}