blob: 3bde203c7ca1658898646314b96026444bbe8247 [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]efd75992011-12-15 22:42:4215#include "chrome/browser/extensions/extension_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]8f9d4eb2011-02-05 01:39:1019#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]d13950e2009-12-04 01:43:0220#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]83820d42011-11-12 22:03:1121#include "chrome/browser/extensions/process_map.h"
[email protected]ed2b1002011-05-25 14:12:1022#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3323#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4224#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]83820d42011-11-12 22:03:1125#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2326#include "chrome/common/extensions/extension_messages.h"
[email protected]615d88f2011-12-13 01:47:4427#include "chrome/common/extensions/extension_set.h"
[email protected]9c45b7182009-08-04 16:44:4328#include "chrome/common/url_constants.h"
[email protected]c333e792012-01-06 16:57:3929#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3330#include "content/public/browser/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1731#include "ipc/ipc_message.h"
32#include "ipc/ipc_message_macros.h"
[email protected]615d88f2011-12-13 01:47:4433#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
[email protected]9c1662b2012-03-06 15:44:3334#include "third_party/skia/include/core/SkBitmap.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) {
49 ExtensionActivityLog* extension_activity_log =
50 ExtensionActivityLog::GetInstance();
51 if (extension_activity_log->HasObservers(extension)) {
52 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
65 extension_activity_log->Log(
66 extension,
67 ExtensionActivityLog::ACTIVITY_EXTENSION_API_CALL,
68 call_signature);
69 }
70}
71
72void LogFailure(const Extension* extension,
73 const std::string& func_name,
74 const char* reason) {
75 ExtensionActivityLog* extension_activity_log =
76 ExtensionActivityLog::GetInstance();
77 if (extension_activity_log->HasObservers(extension)) {
78 extension_activity_log->Log(
79 extension,
80 ExtensionActivityLog::ACTIVITY_EXTENSION_API_BLOCK,
81 func_name + ": " + reason);
82 }
83}
84
[email protected]5bc248a2012-04-04 23:38:1185// Separate copy of ExtensionAPI used for IO thread extension functions. We need
86// this because ExtensionAPI has mutable data. It should be possible to remove
87// this once all the extension APIs are updated to the feature system.
88struct Static {
89 Static()
90 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
91 }
92 scoped_ptr<extensions::ExtensionAPI> api;
93};
94base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
95
96} // namespace
97
[email protected]d72d3a62012-05-10 03:45:0898ExtensionWindowController*
99 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
100 const {
101 return NULL;
102}
103
104content::WebContents*
105 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
106 return NULL;
107}
[email protected]5bc248a2012-04-04 23:38:11108
[email protected]bfdffe2b2009-04-24 22:05:35109void ExtensionFunctionDispatcher::GetAllFunctionNames(
110 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35111 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35112}
113
[email protected]b83e4602009-05-15 22:58:33114bool ExtensionFunctionDispatcher::OverrideFunction(
115 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35116 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
117 factory);
[email protected]b83e4602009-05-15 22:58:33118}
119
120void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35121 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33122}
123
[email protected]c357acb42011-06-09 20:52:42124// static
125void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25126 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18127 void* profile,
[email protected]c357acb42011-06-09 20:52:42128 int render_process_id,
129 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
130 int routing_id,
131 const ExtensionHostMsg_Request_Params& params) {
132 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11133 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]c357acb42011-06-09 20:52:42134
[email protected]6f371442011-11-09 06:45:46135 scoped_refptr<ExtensionFunction> function(
136 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11137 extension_info_map->process_map(),
138 g_global_io_data.Get().api.get(),
139 profile,
[email protected]6f371442011-11-09 06:45:46140 ipc_sender, routing_id));
[email protected]efd75992011-12-15 22:42:42141 if (!function) {
142 LogFailure(extension, params.name, kAccessDenied);
[email protected]c357acb42011-06-09 20:52:42143 return;
[email protected]efd75992011-12-15 22:42:42144 }
[email protected]c357acb42011-06-09 20:52:42145
146 IOThreadExtensionFunction* function_io =
147 function->AsIOThreadExtensionFunction();
148 if (!function_io) {
149 NOTREACHED();
150 return;
151 }
152 function_io->set_ipc_sender(ipc_sender, routing_id);
153 function_io->set_extension_info_map(extension_info_map);
154 function->set_include_incognito(
155 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25156
[email protected]36296912012-03-20 11:08:49157 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]fd50e7b2011-11-03 09:20:25158 if (quota->Assess(extension->id(), function, &params.arguments,
159 base::TimeTicks::Now())) {
160 function->Run();
[email protected]efd75992011-12-15 22:42:42161 LogSuccess(extension, params);
[email protected]fd50e7b2011-11-03 09:20:25162 } else {
163 function->OnQuotaExceeded();
[email protected]efd75992011-12-15 22:42:42164 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]fd50e7b2011-11-03 09:20:25165 }
[email protected]c357acb42011-06-09 20:52:42166}
167
[email protected]c5dbef02011-05-13 05:06:09168ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
169 Delegate* delegate)
170 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27171 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35172}
173
[email protected]32dda362009-06-05 19:07:01174ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01175}
176
[email protected]c5dbef02011-05-13 05:06:09177void ExtensionFunctionDispatcher::Dispatch(
178 const ExtensionHostMsg_Request_Params& params,
179 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09180 ExtensionService* service = profile()->GetExtensionService();
[email protected]6f371442011-11-09 06:45:46181 extensions::ProcessMap* process_map = service->process_map();
182 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09183 return;
184
[email protected]615d88f2011-12-13 01:47:44185 const Extension* extension = service->extensions()->GetByID(
186 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09187 if (!extension)
[email protected]615d88f2011-12-13 01:47:44188 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
189 WebSecurityOrigin::createFromString(params.source_origin),
190 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09191
[email protected]8add5412011-10-01 21:02:14192 scoped_refptr<ExtensionFunction> function(
[email protected]6f371442011-11-09 06:45:46193 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58194 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46195 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11196 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]6f371442011-11-09 06:45:46197 profile(), render_view_host,
[email protected]9f76c1e2012-03-05 15:15:58198 render_view_host->GetRoutingID()));
[email protected]efd75992011-12-15 22:42:42199 if (!function) {
200 LogFailure(extension, params.name, kAccessDenied);
[email protected]f82d57b52011-04-27 19:13:17201 return;
[email protected]efd75992011-12-15 22:42:42202 }
[email protected]f82d57b52011-04-27 19:13:17203
[email protected]a2aef2e2011-05-26 22:48:12204 UIThreadExtensionFunction* function_ui =
205 function->AsUIThreadExtensionFunction();
206 if (!function_ui) {
207 NOTREACHED();
208 return;
209 }
210 function_ui->SetRenderViewHost(render_view_host);
211 function_ui->set_dispatcher(AsWeakPtr());
212 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05213 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41214
[email protected]d13950e2009-12-04 01:43:02215 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09216 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56217 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26218 // See crbug.com/39178.
219 ExternalProtocolHandler::PermitLaunchUrl();
220
[email protected]d13950e2009-12-04 01:43:02221 function->Run();
[email protected]efd75992011-12-15 22:42:42222 LogSuccess(extension, params);
[email protected]d13950e2009-12-04 01:43:02223 } else {
[email protected]fd50e7b2011-11-03 09:20:25224 function->OnQuotaExceeded();
[email protected]efd75992011-12-15 22:42:42225 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]d13950e2009-12-04 01:43:02226 }
[email protected]720ad1312012-02-27 23:07:36227
[email protected]5734e882012-05-04 22:17:56228 // Check if extension was uninstalled by management.uninstall.
229 if (!service->extensions()->GetByID(params.extension_id))
230 return;
231
[email protected]720ad1312012-02-27 23:07:36232 // We only adjust the keepalive count for UIThreadExtensionFunction for
233 // now, largely for simplicity's sake. This is OK because currently, only
234 // the webRequest API uses IOThreadExtensionFunction, and that API is not
235 // compatible with lazy background pages.
236 profile()->GetExtensionProcessManager()->IncrementLazyKeepaliveCount(
237 extension);
238}
239
240void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
241 const Extension* extension) {
242 profile()->GetExtensionProcessManager()->DecrementLazyKeepaliveCount(
243 extension);
[email protected]bfdffe2b2009-04-24 22:05:35244}
245
[email protected]c357acb42011-06-09 20:52:42246// static
247ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
248 const ExtensionHostMsg_Request_Params& params,
249 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46250 int requesting_process_id,
251 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11252 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18253 void* profile,
[email protected]c357acb42011-06-09 20:52:42254 IPC::Message::Sender* ipc_sender,
255 int routing_id) {
[email protected]c357acb42011-06-09 20:52:42256 if (!extension) {
[email protected]6f371442011-11-09 06:45:46257 LOG(ERROR) << "Specified extension does not exist.";
258 SendAccessDenied(ipc_sender, routing_id, params.request_id);
259 return NULL;
260 }
261
[email protected]5bc248a2012-04-04 23:38:11262 if (api->IsPrivileged(params.name) &&
[email protected]83820d42011-11-12 22:03:11263 !process_map.Contains(extension->id(), requesting_process_id)) {
[email protected]6f371442011-11-09 06:45:46264 LOG(ERROR) << "Extension API called from incorrect process "
265 << requesting_process_id
266 << " from URL " << params.source_url.spec();
[email protected]c357acb42011-06-09 20:52:42267 SendAccessDenied(ipc_sender, routing_id, params.request_id);
268 return NULL;
269 }
270
[email protected]0d3e4a22011-06-23 19:02:52271 if (!extension->HasAPIPermission(params.name)) {
[email protected]c357acb42011-06-09 20:52:42272 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 NULL;
276 }
277
278 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35279 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]c357acb42011-06-09 20:52:42280 function->SetArgs(&params.arguments);
281 function->set_source_url(params.source_url);
282 function->set_request_id(params.request_id);
283 function->set_has_callback(params.has_callback);
284 function->set_user_gesture(params.user_gesture);
285 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32286 function->set_profile_id(profile);
[email protected]c357acb42011-06-09 20:52:42287 return function;
288}
289
290// static
[email protected]c5dbef02011-05-13 05:06:09291void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]c357acb42011-06-09 20:52:42292 IPC::Message::Sender* ipc_sender, int routing_id, int request_id) {
[email protected]602542d2012-04-20 02:48:01293 ListValue empty_list;
[email protected]c357acb42011-06-09 20:52:42294 ipc_sender->Send(new ExtensionMsg_Response(
[email protected]602542d2012-04-20 02:48:01295 routing_id, request_id, false, empty_list,
[email protected]c5dbef02011-05-13 05:06:09296 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35297}