blob: 2e2935ddbd1d5f60530411dc05658f4315860276 [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]ae33d322012-03-19 22:24:3534#include "webkit/glue/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1035
[email protected]1c321ee2012-05-21 03:02:3436using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1137using extensions::ExtensionAPI;
[email protected]eaabba22012-03-07 15:02:1138using content::RenderViewHost;
[email protected]615d88f2011-12-13 01:47:4439using WebKit::WebSecurityOrigin;
[email protected]83820d42011-11-12 22:03:1140
[email protected]5bc248a2012-04-04 23:38:1141namespace {
42
[email protected]efd75992011-12-15 22:42:4243const char kAccessDenied[] = "access denied";
44const char kQuotaExceeded[] = "quota exceeded";
45
46void LogSuccess(const Extension* extension,
47 const ExtensionHostMsg_Request_Params& params) {
48 ExtensionActivityLog* extension_activity_log =
49 ExtensionActivityLog::GetInstance();
50 if (extension_activity_log->HasObservers(extension)) {
51 std::string call_signature = params.name + "(";
52 ListValue::const_iterator it = params.arguments.begin();
53 for (; it != params.arguments.end(); ++it) {
54 std::string arg;
55 JSONStringValueSerializer serializer(&arg);
56 if (serializer.SerializeAndOmitBinaryValues(**it)) {
57 if (it != params.arguments.begin())
58 call_signature += ", ";
59 call_signature += arg;
60 }
61 }
62 call_signature += ")";
63
64 extension_activity_log->Log(
65 extension,
66 ExtensionActivityLog::ACTIVITY_EXTENSION_API_CALL,
67 call_signature);
68 }
69}
70
71void LogFailure(const Extension* extension,
72 const std::string& func_name,
73 const char* reason) {
74 ExtensionActivityLog* extension_activity_log =
75 ExtensionActivityLog::GetInstance();
76 if (extension_activity_log->HasObservers(extension)) {
77 extension_activity_log->Log(
78 extension,
79 ExtensionActivityLog::ACTIVITY_EXTENSION_API_BLOCK,
80 func_name + ": " + reason);
81 }
82}
83
[email protected]5bc248a2012-04-04 23:38:1184// Separate copy of ExtensionAPI used for IO thread extension functions. We need
85// this because ExtensionAPI has mutable data. It should be possible to remove
86// this once all the extension APIs are updated to the feature system.
87struct Static {
88 Static()
89 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
90 }
91 scoped_ptr<extensions::ExtensionAPI> api;
92};
93base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
94
95} // namespace
96
[email protected]44f4b132012-07-17 20:36:5797extensions::WindowController*
98ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:0899 const {
100 return NULL;
101}
102
103content::WebContents*
[email protected]44f4b132012-07-17 20:36:57104ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08105 return NULL;
106}
[email protected]5bc248a2012-04-04 23:38:11107
[email protected]bfdffe2b2009-04-24 22:05:35108void ExtensionFunctionDispatcher::GetAllFunctionNames(
109 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35110 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35111}
112
[email protected]b83e4602009-05-15 22:58:33113bool ExtensionFunctionDispatcher::OverrideFunction(
114 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35115 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
116 factory);
[email protected]b83e4602009-05-15 22:58:33117}
118
119void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35120 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33121}
122
[email protected]c357acb42011-06-09 20:52:42123// static
124void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25125 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18126 void* profile,
[email protected]c357acb42011-06-09 20:52:42127 int render_process_id,
128 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53129 int routing_id,
[email protected]c357acb42011-06-09 20:52:42130 const ExtensionHostMsg_Request_Params& params) {
131 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11132 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]c357acb42011-06-09 20:52:42133
[email protected]6f371442011-11-09 06:45:46134 scoped_refptr<ExtensionFunction> function(
135 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11136 extension_info_map->process_map(),
137 g_global_io_data.Get().api.get(),
138 profile,
[email protected]74e21e72012-07-09 21:20:53139 ipc_sender, routing_id));
[email protected]efd75992011-12-15 22:42:42140 if (!function) {
141 LogFailure(extension, params.name, kAccessDenied);
[email protected]c357acb42011-06-09 20:52:42142 return;
[email protected]efd75992011-12-15 22:42:42143 }
[email protected]c357acb42011-06-09 20:52:42144
145 IOThreadExtensionFunction* function_io =
146 function->AsIOThreadExtensionFunction();
147 if (!function_io) {
148 NOTREACHED();
149 return;
150 }
[email protected]74e21e72012-07-09 21:20:53151 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42152 function_io->set_extension_info_map(extension_info_map);
153 function->set_include_incognito(
154 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25155
[email protected]36296912012-03-20 11:08:49156 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]fd50e7b2011-11-03 09:20:25157 if (quota->Assess(extension->id(), function, &params.arguments,
158 base::TimeTicks::Now())) {
159 function->Run();
[email protected]efd75992011-12-15 22:42:42160 LogSuccess(extension, params);
[email protected]fd50e7b2011-11-03 09:20:25161 } else {
162 function->OnQuotaExceeded();
[email protected]efd75992011-12-15 22:42:42163 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]fd50e7b2011-11-03 09:20:25164 }
[email protected]c357acb42011-06-09 20:52:42165}
166
[email protected]c5dbef02011-05-13 05:06:09167ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
168 Delegate* delegate)
169 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27170 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35171}
172
[email protected]32dda362009-06-05 19:07:01173ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01174}
175
[email protected]c5dbef02011-05-13 05:06:09176void ExtensionFunctionDispatcher::Dispatch(
177 const ExtensionHostMsg_Request_Params& params,
178 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09179 ExtensionService* service = profile()->GetExtensionService();
[email protected]6f371442011-11-09 06:45:46180 extensions::ProcessMap* process_map = service->process_map();
181 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09182 return;
183
[email protected]615d88f2011-12-13 01:47:44184 const Extension* extension = service->extensions()->GetByID(
185 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09186 if (!extension)
[email protected]615d88f2011-12-13 01:47:44187 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
188 WebSecurityOrigin::createFromString(params.source_origin),
189 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09190
[email protected]8add5412011-10-01 21:02:14191 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53192 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58193 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46194 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11195 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]74e21e72012-07-09 21:20:53196 profile(), render_view_host,
197 render_view_host->GetRoutingID()));
[email protected]efd75992011-12-15 22:42:42198 if (!function) {
199 LogFailure(extension, params.name, kAccessDenied);
[email protected]f82d57b52011-04-27 19:13:17200 return;
[email protected]efd75992011-12-15 22:42:42201 }
[email protected]f82d57b52011-04-27 19:13:17202
[email protected]a2aef2e2011-05-26 22:48:12203 UIThreadExtensionFunction* function_ui =
204 function->AsUIThreadExtensionFunction();
205 if (!function_ui) {
206 NOTREACHED();
207 return;
208 }
209 function_ui->SetRenderViewHost(render_view_host);
210 function_ui->set_dispatcher(AsWeakPtr());
211 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05212 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41213
[email protected]d13950e2009-12-04 01:43:02214 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09215 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56216 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26217 // See crbug.com/39178.
218 ExternalProtocolHandler::PermitLaunchUrl();
219
[email protected]d13950e2009-12-04 01:43:02220 function->Run();
[email protected]efd75992011-12-15 22:42:42221 LogSuccess(extension, params);
[email protected]d13950e2009-12-04 01:43:02222 } else {
[email protected]fd50e7b2011-11-03 09:20:25223 function->OnQuotaExceeded();
[email protected]efd75992011-12-15 22:42:42224 LogFailure(extension, params.name, kQuotaExceeded);
[email protected]d13950e2009-12-04 01:43:02225 }
[email protected]720ad1312012-02-27 23:07:36226
[email protected]5734e882012-05-04 22:17:56227 // Check if extension was uninstalled by management.uninstall.
228 if (!service->extensions()->GetByID(params.extension_id))
229 return;
230
[email protected]720ad1312012-02-27 23:07:36231 // We only adjust the keepalive count for UIThreadExtensionFunction for
232 // now, largely for simplicity's sake. This is OK because currently, only
233 // the webRequest API uses IOThreadExtensionFunction, and that API is not
234 // compatible with lazy background pages.
235 profile()->GetExtensionProcessManager()->IncrementLazyKeepaliveCount(
236 extension);
237}
238
239void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
240 const Extension* extension) {
241 profile()->GetExtensionProcessManager()->DecrementLazyKeepaliveCount(
242 extension);
[email protected]bfdffe2b2009-04-24 22:05:35243}
244
[email protected]c357acb42011-06-09 20:52:42245// static
246ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
247 const ExtensionHostMsg_Request_Params& params,
248 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46249 int requesting_process_id,
250 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11251 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18252 void* profile,
[email protected]74e21e72012-07-09 21:20:53253 IPC::Sender* ipc_sender,
254 int routing_id) {
[email protected]c357acb42011-06-09 20:52:42255 if (!extension) {
[email protected]6f371442011-11-09 06:45:46256 LOG(ERROR) << "Specified extension does not exist.";
[email protected]74e21e72012-07-09 21:20:53257 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]6f371442011-11-09 06:45:46258 return NULL;
259 }
260
[email protected]5bc248a2012-04-04 23:38:11261 if (api->IsPrivileged(params.name) &&
[email protected]83820d42011-11-12 22:03:11262 !process_map.Contains(extension->id(), requesting_process_id)) {
[email protected]6f371442011-11-09 06:45:46263 LOG(ERROR) << "Extension API called from incorrect process "
264 << requesting_process_id
265 << " from URL " << params.source_url.spec();
[email protected]74e21e72012-07-09 21:20:53266 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]c357acb42011-06-09 20:52:42267 return NULL;
268 }
269
[email protected]0d3e4a22011-06-23 19:02:52270 if (!extension->HasAPIPermission(params.name)) {
[email protected]c357acb42011-06-09 20:52:42271 LOG(ERROR) << "Extension " << extension->id() << " does not have "
272 << "permission to function: " << params.name;
[email protected]74e21e72012-07-09 21:20:53273 SendAccessDenied(ipc_sender, routing_id, params.request_id);
[email protected]c357acb42011-06-09 20:52:42274 return NULL;
275 }
276
277 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35278 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]c357acb42011-06-09 20:52:42279 function->SetArgs(&params.arguments);
280 function->set_source_url(params.source_url);
281 function->set_request_id(params.request_id);
282 function->set_has_callback(params.has_callback);
283 function->set_user_gesture(params.user_gesture);
284 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32285 function->set_profile_id(profile);
[email protected]c357acb42011-06-09 20:52:42286 return function;
287}
288
289// static
[email protected]c5dbef02011-05-13 05:06:09290void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]74e21e72012-07-09 21:20:53291 IPC::Sender* ipc_sender, int routing_id, int request_id) {
[email protected]602542d2012-04-20 02:48:01292 ListValue empty_list;
[email protected]c357acb42011-06-09 20:52:42293 ipc_sender->Send(new ExtensionMsg_Response(
[email protected]74e21e72012-07-09 21:20:53294 routing_id, request_id, false, empty_list,
[email protected]c5dbef02011-05-13 05:06:09295 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35296}