blob: c411026d090b3757a81bca8d07890ea869706c14 [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]4b64d712013-01-17 17:53:177#include "base/bind.h"
[email protected]ffbec692012-02-26 20:26:428#include "base/json/json_string_value_serializer.h"
[email protected]5bc248a2012-04-04 23:38:119#include "base/lazy_instance.h"
[email protected]35548ab2013-05-15 08:59:4710#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
[email protected]35548ab2013-05-15 08:59:4712#include "base/process.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]17263b22013-05-16 03:29:2216#include "chrome/browser/extensions/activity_log/activity_log.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]4b64d712013-01-17 17:53:1730#include "content/public/browser/browser_thread.h"
[email protected]c333e792012-01-06 16:57:3931#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3332#include "content/public/browser/render_view_host.h"
[email protected]35548ab2013-05-15 08:59:4733#include "content/public/browser/render_view_host_observer.h"
34#include "content/public/browser/user_metrics.h"
35#include "content/public/common/result_codes.h"
[email protected]f82d57b52011-04-27 19:13:1736#include "ipc/ipc_message.h"
37#include "ipc/ipc_message_macros.h"
[email protected]615d88f2011-12-13 01:47:4438#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
[email protected]ae33d322012-03-19 22:24:3539#include "webkit/glue/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1040
[email protected]1c321ee2012-05-21 03:02:3441using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1142using extensions::ExtensionAPI;
[email protected]eaabba22012-03-07 15:02:1143using content::RenderViewHost;
[email protected]615d88f2011-12-13 01:47:4444using WebKit::WebSecurityOrigin;
[email protected]83820d42011-11-12 22:03:1145
[email protected]5bc248a2012-04-04 23:38:1146namespace {
47
[email protected]efd75992011-12-15 22:42:4248const char kAccessDenied[] = "access denied";
49const char kQuotaExceeded[] = "quota exceeded";
50
51void LogSuccess(const Extension* extension,
[email protected]4b64d712013-01-17 17:53:1752 const std::string& api_name,
53 scoped_ptr<ListValue> args,
54 Profile* profile) {
55 // The ActivityLog can only be accessed from the main (UI) thread. If we're
56 // running on the wrong thread, re-dispatch from the main thread.
57 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
58 BrowserThread::PostTask(BrowserThread::UI,
59 FROM_HERE,
60 base::Bind(&LogSuccess,
61 extension,
62 api_name,
[email protected]c02087b512013-02-04 03:09:2063 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1764 profile));
65 } else {
66 extensions::ActivityLog* activity_log =
67 extensions::ActivityLog::GetInstance(profile);
[email protected]007b3f82013-04-09 08:46:4568 activity_log->LogAPIAction(extension, api_name, args.get(), std::string());
[email protected]efd75992011-12-15 22:42:4269 }
70}
71
72void LogFailure(const Extension* extension,
[email protected]4b64d712013-01-17 17:53:1773 const std::string& api_name,
74 scoped_ptr<ListValue> args,
75 const char* reason,
76 Profile* profile) {
77 // The ActivityLog can only be accessed from the main (UI) thread. If we're
78 // running on the wrong thread, re-dispatch from the main thread.
79 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
80 BrowserThread::PostTask(BrowserThread::UI,
81 FROM_HERE,
82 base::Bind(&LogFailure,
83 extension,
84 api_name,
[email protected]c02087b512013-02-04 03:09:2085 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1786 reason,
87 profile));
88 } else {
89 extensions::ActivityLog* activity_log =
90 extensions::ActivityLog::GetInstance(profile);
[email protected]007b3f82013-04-09 08:46:4591 activity_log->LogBlockedAction(
92 extension, api_name, args.get(), reason, std::string());
[email protected]efd75992011-12-15 22:42:4293 }
94}
95
[email protected]4b64d712013-01-17 17:53:1796
[email protected]5bc248a2012-04-04 23:38:1197// Separate copy of ExtensionAPI used for IO thread extension functions. We need
98// this because ExtensionAPI has mutable data. It should be possible to remove
99// this once all the extension APIs are updated to the feature system.
100struct Static {
101 Static()
102 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
103 }
104 scoped_ptr<extensions::ExtensionAPI> api;
105};
106base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
107
[email protected]35548ab2013-05-15 08:59:47108// Kills the specified process because it sends us a malformed message.
109void KillBadMessageSender(base::ProcessHandle process) {
110 NOTREACHED();
111 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
112 if (process)
113 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
114}
115
116void CommonResponseCallback(IPC::Sender* ipc_sender,
117 int routing_id,
118 base::ProcessHandle peer_process,
119 int request_id,
120 ExtensionFunction::ResponseType type,
121 const base::ListValue& results,
122 const std::string& error) {
123 DCHECK(ipc_sender);
124
125 if (type == ExtensionFunction::BAD_MESSAGE) {
126 // The renderer has done validation before sending extension api requests.
127 // Therefore, we should never receive a request that is invalid in a way
128 // that JSON validation in the renderer should have caught. It could be an
129 // attacker trying to exploit the browser, so we crash the renderer instead.
130 LOG(ERROR) <<
131 "Terminating renderer because of malformed extension message.";
132 if (content::RenderProcessHost::run_renderer_in_process()) {
133 // In single process mode it is better if we don't suicide but just crash.
134 CHECK(false);
135 } else {
136 KillBadMessageSender(peer_process);
137 }
138
139 return;
140 }
141
142 ipc_sender->Send(new ExtensionMsg_Response(
143 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
144 error));
145}
146
147void IOThreadResponseCallback(
148 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
149 int routing_id,
150 int request_id,
151 ExtensionFunction::ResponseType type,
152 const base::ListValue& results,
153 const std::string& error) {
154 if (!ipc_sender)
155 return;
156
157 CommonResponseCallback(ipc_sender, routing_id, ipc_sender->peer_handle(),
158 request_id, type, results, error);
159}
160
[email protected]5bc248a2012-04-04 23:38:11161} // namespace
162
[email protected]35548ab2013-05-15 08:59:47163class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
164 : public content::RenderViewHostObserver {
165 public:
166 UIThreadResponseCallbackWrapper(
167 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
168 RenderViewHost* render_view_host)
169 : content::RenderViewHostObserver(render_view_host),
170 dispatcher_(dispatcher),
171 weak_ptr_factory_(this) {
172 }
173
174 virtual ~UIThreadResponseCallbackWrapper() {
175 }
176
177 // content::RenderViewHostObserver overrides.
178 virtual void RenderViewHostDestroyed(
179 RenderViewHost* render_view_host) OVERRIDE {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
181 if (dispatcher_) {
182 dispatcher_->ui_thread_response_callback_wrappers_.erase(
183 render_view_host);
184 }
185
186 // This call will delete |this|.
187 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
188 }
189
190 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
191 return base::Bind(
192 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
193 weak_ptr_factory_.GetWeakPtr(),
194 request_id);
195 }
196
197 private:
198 void OnExtensionFunctionCompleted(int request_id,
199 ExtensionFunction::ResponseType type,
200 const base::ListValue& results,
201 const std::string& error) {
202 CommonResponseCallback(
203 render_view_host(), render_view_host()->GetRoutingID(),
204 render_view_host()->GetProcess()->GetHandle(), request_id, type,
205 results, error);
206 }
207
208 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
209 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
210
211 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
212};
213
[email protected]44f4b132012-07-17 20:36:57214extensions::WindowController*
215ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08216 const {
217 return NULL;
218}
219
220content::WebContents*
[email protected]44f4b132012-07-17 20:36:57221ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08222 return NULL;
223}
[email protected]5bc248a2012-04-04 23:38:11224
[email protected]bfdffe2b2009-04-24 22:05:35225void ExtensionFunctionDispatcher::GetAllFunctionNames(
226 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35227 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35228}
229
[email protected]b83e4602009-05-15 22:58:33230bool ExtensionFunctionDispatcher::OverrideFunction(
231 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35232 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
233 factory);
[email protected]b83e4602009-05-15 22:58:33234}
235
236void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35237 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33238}
239
[email protected]c357acb42011-06-09 20:52:42240// static
241void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25242 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18243 void* profile,
[email protected]c357acb42011-06-09 20:52:42244 int render_process_id,
245 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53246 int routing_id,
[email protected]c357acb42011-06-09 20:52:42247 const ExtensionHostMsg_Request_Params& params) {
248 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11249 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17250 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47251
252 ExtensionFunction::ResponseCallback callback(
253 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
254 params.request_id));
255
[email protected]6f371442011-11-09 06:45:46256 scoped_refptr<ExtensionFunction> function(
257 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11258 extension_info_map->process_map(),
259 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47260 profile, callback));
[email protected]4b64d712013-01-17 17:53:17261 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
262
[email protected]efd75992011-12-15 22:42:42263 if (!function) {
[email protected]4b64d712013-01-17 17:53:17264 LogFailure(extension,
265 params.name,
266 args.Pass(),
267 kAccessDenied,
268 profile_cast);
[email protected]c357acb42011-06-09 20:52:42269 return;
[email protected]efd75992011-12-15 22:42:42270 }
[email protected]c357acb42011-06-09 20:52:42271
272 IOThreadExtensionFunction* function_io =
273 function->AsIOThreadExtensionFunction();
274 if (!function_io) {
275 NOTREACHED();
276 return;
277 }
[email protected]35548ab2013-05-15 08:59:47278 function_io->set_ipc_sender(ipc_sender);
[email protected]c357acb42011-06-09 20:52:42279 function_io->set_extension_info_map(extension_info_map);
280 function->set_include_incognito(
281 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25282
[email protected]35548ab2013-05-15 08:59:47283 if (!CheckPermissions(function, extension, params, callback)) {
[email protected]4b64d712013-01-17 17:53:17284 LogFailure(extension,
285 params.name,
286 args.Pass(),
287 kAccessDenied,
288 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07289 return;
290 }
291
[email protected]36296912012-03-20 11:08:49292 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29293 std::string violation_error = quota->Assess(extension->id(),
294 function,
295 &params.arguments,
296 base::TimeTicks::Now());
297 if (violation_error.empty()) {
[email protected]4b64d712013-01-17 17:53:17298 LogSuccess(extension,
299 params.name,
300 args.Pass(),
301 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25302 function->Run();
303 } else {
[email protected]4b64d712013-01-17 17:53:17304 LogFailure(extension,
305 params.name,
306 args.Pass(),
307 kQuotaExceeded,
308 profile_cast);
[email protected]85231d72012-08-31 09:45:29309 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25310 }
[email protected]c357acb42011-06-09 20:52:42311}
312
[email protected]c5dbef02011-05-13 05:06:09313ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
314 Delegate* delegate)
315 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27316 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35317}
318
[email protected]32dda362009-06-05 19:07:01319ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01320}
321
[email protected]c5dbef02011-05-13 05:06:09322void ExtensionFunctionDispatcher::Dispatch(
323 const ExtensionHostMsg_Request_Params& params,
324 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47325 UIThreadResponseCallbackWrapperMap::const_iterator
326 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
327 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
328 if (iter == ui_thread_response_callback_wrappers_.end()) {
329 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
330 render_view_host);
331 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
332 } else {
333 callback_wrapper = iter->second;
334 }
335
336 DispatchWithCallback(params, render_view_host,
337 callback_wrapper->CreateCallback(params.request_id));
338}
339
340void ExtensionFunctionDispatcher::DispatchWithCallback(
341 const ExtensionHostMsg_Request_Params& params,
342 RenderViewHost* render_view_host,
343 const ExtensionFunction::ResponseCallback& callback) {
344 // TODO(yzshen): There is some shared logic between this method and
345 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09346 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28347 ExtensionProcessManager* process_manager =
348 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46349 extensions::ProcessMap* process_map = service->process_map();
350 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09351 return;
352
[email protected]615d88f2011-12-13 01:47:44353 const Extension* extension = service->extensions()->GetByID(
354 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09355 if (!extension)
[email protected]615d88f2011-12-13 01:47:44356 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
357 WebSecurityOrigin::createFromString(params.source_origin),
358 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09359
[email protected]8add5412011-10-01 21:02:14360 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53361 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58362 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46363 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11364 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47365 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17366 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
367
[email protected]efd75992011-12-15 22:42:42368 if (!function) {
[email protected]4b64d712013-01-17 17:53:17369 LogFailure(extension,
370 params.name,
371 args.Pass(),
372 kAccessDenied,
373 profile());
[email protected]f82d57b52011-04-27 19:13:17374 return;
[email protected]efd75992011-12-15 22:42:42375 }
[email protected]f82d57b52011-04-27 19:13:17376
[email protected]a2aef2e2011-05-26 22:48:12377 UIThreadExtensionFunction* function_ui =
378 function->AsUIThreadExtensionFunction();
379 if (!function_ui) {
380 NOTREACHED();
381 return;
382 }
[email protected]35548ab2013-05-15 08:59:47383 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12384 function_ui->set_dispatcher(AsWeakPtr());
385 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05386 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41387
[email protected]35548ab2013-05-15 08:59:47388 if (!CheckPermissions(function, extension, params, callback)) {
[email protected]4b64d712013-01-17 17:53:17389 LogFailure(extension,
390 params.name,
391 args.Pass(),
392 kAccessDenied,
393 profile());
[email protected]d2fe22ff2012-10-03 00:40:07394 return;
395 }
396
[email protected]d13950e2009-12-04 01:43:02397 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29398 std::string violation_error = quota->Assess(extension->id(),
399 function,
400 &params.arguments,
401 base::TimeTicks::Now());
402 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26403 // See crbug.com/39178.
404 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]4b64d712013-01-17 17:53:17405 LogSuccess(extension, params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02406 function->Run();
407 } else {
[email protected]4b64d712013-01-17 17:53:17408 LogFailure(extension,
409 params.name,
410 args.Pass(),
411 kQuotaExceeded,
412 profile());
[email protected]85231d72012-08-31 09:45:29413 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02414 }
[email protected]720ad1312012-02-27 23:07:36415
[email protected]efb4b082012-10-17 22:28:28416 // Note: do not access |this| after this point. We may have been deleted
417 // if function->Run() ended up closing the tab that owns us.
418
[email protected]5734e882012-05-04 22:17:56419 // Check if extension was uninstalled by management.uninstall.
420 if (!service->extensions()->GetByID(params.extension_id))
421 return;
422
[email protected]720ad1312012-02-27 23:07:36423 // We only adjust the keepalive count for UIThreadExtensionFunction for
424 // now, largely for simplicity's sake. This is OK because currently, only
425 // the webRequest API uses IOThreadExtensionFunction, and that API is not
426 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28427 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36428}
429
430void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
431 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03432 extensions::ExtensionSystem::Get(profile())->process_manager()->
433 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35434}
435
[email protected]c357acb42011-06-09 20:52:42436// static
[email protected]d2fe22ff2012-10-03 00:40:07437bool ExtensionFunctionDispatcher::CheckPermissions(
438 ExtensionFunction* function,
439 const Extension* extension,
440 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47441 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07442 if (!function->HasPermission()) {
443 LOG(ERROR) << "Extension " << extension->id() << " does not have "
444 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47445 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07446 return false;
447 }
448 return true;
449}
450
[email protected]f33542112013-02-04 16:52:38451namespace {
452
453// Only COMPONENT hosted apps may call extension APIs, and they are limited
454// to just the permissions they explicitly request. They should not have access
455// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
456// are available without permission.
457// TODO(asargent/kalman) - get rid of this when the features system can express
458// the "non permission" permissions.
459bool AllowHostedAppAPICall(const Extension& extension,
460 const GURL& source_url,
461 const std::string& function_name) {
462 if (extension.location() != extensions::Manifest::COMPONENT)
463 return false;
464
465 if (!extension.web_extent().MatchesURL(source_url))
466 return false;
467
468 // We just allow the hosted app's explicit permissions, plus chrome.test.
469 scoped_refptr<const extensions::PermissionSet> permissions =
470 extension.GetActivePermissions();
471 return (permissions->HasAccessToFunction(function_name, false) ||
472 StartsWithASCII(function_name, "test.", true /*case_sensitive*/));
473}
474
475} // namespace
476
477
[email protected]d2fe22ff2012-10-03 00:40:07478// static
[email protected]c357acb42011-06-09 20:52:42479ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
480 const ExtensionHostMsg_Request_Params& params,
481 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46482 int requesting_process_id,
483 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11484 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18485 void* profile,
[email protected]35548ab2013-05-15 08:59:47486 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42487 if (!extension) {
[email protected]6f371442011-11-09 06:45:46488 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47489 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46490 return NULL;
491 }
492
[email protected]f33542112013-02-04 16:52:38493 // Most hosted apps can't call APIs.
494 bool allowed = true;
495 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47496 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38497
498 // Privileged APIs can only be called from the process the extension
499 // is running in.
500 if (allowed && api->IsPrivileged(params.name))
501 allowed = process_map.Contains(extension->id(), requesting_process_id);
502
503 if (!allowed) {
504 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
505 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46506 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47507 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42508 return NULL;
509 }
510
[email protected]c357acb42011-06-09 20:52:42511 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35512 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14513 if (!function) {
514 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47515 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14516 return NULL;
517 }
518
[email protected]c357acb42011-06-09 20:52:42519 function->SetArgs(&params.arguments);
520 function->set_source_url(params.source_url);
521 function->set_request_id(params.request_id);
522 function->set_has_callback(params.has_callback);
523 function->set_user_gesture(params.user_gesture);
524 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32525 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47526 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16527
[email protected]c357acb42011-06-09 20:52:42528 return function;
529}
530
531// static
[email protected]c5dbef02011-05-13 05:06:09532void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47533 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01534 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47535 callback.Run(ExtensionFunction::FAILED, empty_list,
536 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35537}