blob: a8a0108ef34b305725c674c4866e2ddd6864504e [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]78216e12013-05-17 01:11:2517#include "chrome/browser/extensions/activity_log/blocked_actions.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]35548ab2013-05-15 08:59:4734#include "content/public/browser/render_view_host_observer.h"
35#include "content/public/browser/user_metrics.h"
36#include "content/public/common/result_codes.h"
[email protected]f82d57b52011-04-27 19:13:1737#include "ipc/ipc_message.h"
38#include "ipc/ipc_message_macros.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]83820d42011-11-12 22:03:1144
[email protected]5bc248a2012-04-04 23:38:1145namespace {
46
[email protected]e5a440c2013-06-04 21:55:1247void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1748 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4149 scoped_ptr<base::ListValue> args,
[email protected]4b64d712013-01-17 17:53:1750 Profile* profile) {
51 // The ActivityLog can only be accessed from the main (UI) thread. If we're
52 // running on the wrong thread, re-dispatch from the main thread.
53 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
54 BrowserThread::PostTask(BrowserThread::UI,
55 FROM_HERE,
56 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1257 extension_id,
[email protected]4b64d712013-01-17 17:53:1758 api_name,
[email protected]c02087b512013-02-04 03:09:2059 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1760 profile));
61 } else {
62 extensions::ActivityLog* activity_log =
63 extensions::ActivityLog::GetInstance(profile);
[email protected]e5a440c2013-06-04 21:55:1264 activity_log->LogAPIAction(
65 extension_id, api_name, args.get(), std::string());
[email protected]efd75992011-12-15 22:42:4266 }
67}
68
[email protected]e5a440c2013-06-04 21:55:1269void LogFailure(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1770 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4171 scoped_ptr<base::ListValue> args,
[email protected]78216e12013-05-17 01:11:2572 extensions::BlockedAction::Reason reason,
[email protected]4b64d712013-01-17 17:53:1773 Profile* profile) {
74 // The ActivityLog can only be accessed from the main (UI) thread. If we're
75 // running on the wrong thread, re-dispatch from the main thread.
76 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
77 BrowserThread::PostTask(BrowserThread::UI,
78 FROM_HERE,
79 base::Bind(&LogFailure,
[email protected]e5a440c2013-06-04 21:55:1280 extension_id,
[email protected]4b64d712013-01-17 17:53:1781 api_name,
[email protected]c02087b512013-02-04 03:09:2082 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1783 reason,
84 profile));
85 } else {
86 extensions::ActivityLog* activity_log =
87 extensions::ActivityLog::GetInstance(profile);
[email protected]007b3f82013-04-09 08:46:4588 activity_log->LogBlockedAction(
[email protected]e5a440c2013-06-04 21:55:1289 extension_id, api_name, args.get(), reason, std::string());
[email protected]efd75992011-12-15 22:42:4290 }
91}
92
[email protected]4b64d712013-01-17 17:53:1793
[email protected]5bc248a2012-04-04 23:38:1194// Separate copy of ExtensionAPI used for IO thread extension functions. We need
95// this because ExtensionAPI has mutable data. It should be possible to remove
96// this once all the extension APIs are updated to the feature system.
97struct Static {
98 Static()
99 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
100 }
101 scoped_ptr<extensions::ExtensionAPI> api;
102};
103base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
104
[email protected]35548ab2013-05-15 08:59:47105// Kills the specified process because it sends us a malformed message.
106void KillBadMessageSender(base::ProcessHandle process) {
107 NOTREACHED();
108 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
109 if (process)
110 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
111}
112
113void CommonResponseCallback(IPC::Sender* ipc_sender,
114 int routing_id,
115 base::ProcessHandle peer_process,
116 int request_id,
117 ExtensionFunction::ResponseType type,
118 const base::ListValue& results,
119 const std::string& error) {
120 DCHECK(ipc_sender);
121
122 if (type == ExtensionFunction::BAD_MESSAGE) {
123 // The renderer has done validation before sending extension api requests.
124 // Therefore, we should never receive a request that is invalid in a way
125 // that JSON validation in the renderer should have caught. It could be an
126 // attacker trying to exploit the browser, so we crash the renderer instead.
127 LOG(ERROR) <<
128 "Terminating renderer because of malformed extension message.";
129 if (content::RenderProcessHost::run_renderer_in_process()) {
130 // In single process mode it is better if we don't suicide but just crash.
131 CHECK(false);
132 } else {
133 KillBadMessageSender(peer_process);
134 }
135
136 return;
137 }
138
139 ipc_sender->Send(new ExtensionMsg_Response(
140 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
141 error));
142}
143
144void IOThreadResponseCallback(
145 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
146 int routing_id,
147 int request_id,
148 ExtensionFunction::ResponseType type,
149 const base::ListValue& results,
150 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45151 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47152 return;
153
[email protected]e8dad9b2013-06-04 04:43:45154 CommonResponseCallback(ipc_sender.get(),
155 routing_id,
[email protected]950be552013-07-10 19:13:02156 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45157 request_id,
158 type,
159 results,
160 error);
[email protected]35548ab2013-05-15 08:59:47161}
162
[email protected]5bc248a2012-04-04 23:38:11163} // namespace
164
[email protected]35548ab2013-05-15 08:59:47165class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
166 : public content::RenderViewHostObserver {
167 public:
168 UIThreadResponseCallbackWrapper(
169 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
170 RenderViewHost* render_view_host)
171 : content::RenderViewHostObserver(render_view_host),
172 dispatcher_(dispatcher),
173 weak_ptr_factory_(this) {
174 }
175
176 virtual ~UIThreadResponseCallbackWrapper() {
177 }
178
179 // content::RenderViewHostObserver overrides.
180 virtual void RenderViewHostDestroyed(
181 RenderViewHost* render_view_host) OVERRIDE {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]e8dad9b2013-06-04 04:43:45183 if (dispatcher_.get()) {
184 dispatcher_->ui_thread_response_callback_wrappers_
185 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47186 }
187
188 // This call will delete |this|.
189 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
190 }
191
192 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
193 return base::Bind(
194 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
195 weak_ptr_factory_.GetWeakPtr(),
196 request_id);
197 }
198
199 private:
200 void OnExtensionFunctionCompleted(int request_id,
201 ExtensionFunction::ResponseType type,
202 const base::ListValue& results,
203 const std::string& error) {
204 CommonResponseCallback(
205 render_view_host(), render_view_host()->GetRoutingID(),
206 render_view_host()->GetProcess()->GetHandle(), request_id, type,
207 results, error);
208 }
209
210 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
211 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
212
213 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
214};
215
[email protected]44f4b132012-07-17 20:36:57216extensions::WindowController*
217ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08218 const {
219 return NULL;
220}
221
222content::WebContents*
[email protected]44f4b132012-07-17 20:36:57223ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08224 return NULL;
225}
[email protected]5bc248a2012-04-04 23:38:11226
[email protected]1ce88e82013-06-28 05:17:10227content::WebContents*
228ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
229 return GetAssociatedWebContents();
230}
231
[email protected]bfdffe2b2009-04-24 22:05:35232void ExtensionFunctionDispatcher::GetAllFunctionNames(
233 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35234 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35235}
236
[email protected]b83e4602009-05-15 22:58:33237bool ExtensionFunctionDispatcher::OverrideFunction(
238 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35239 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
240 factory);
[email protected]b83e4602009-05-15 22:58:33241}
242
243void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35244 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33245}
246
[email protected]c357acb42011-06-09 20:52:42247// static
248void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25249 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18250 void* profile,
[email protected]c357acb42011-06-09 20:52:42251 int render_process_id,
252 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53253 int routing_id,
[email protected]c357acb42011-06-09 20:52:42254 const ExtensionHostMsg_Request_Params& params) {
255 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11256 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17257 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47258
259 ExtensionFunction::ResponseCallback callback(
260 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
261 params.request_id));
262
[email protected]6f371442011-11-09 06:45:46263 scoped_refptr<ExtensionFunction> function(
264 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11265 extension_info_map->process_map(),
266 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47267 profile, callback));
[email protected]4b64d712013-01-17 17:53:17268 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
269
[email protected]dc24976f2013-06-02 21:15:09270 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12271 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17272 params.name,
273 args.Pass(),
[email protected]78216e12013-05-17 01:11:25274 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17275 profile_cast);
[email protected]c357acb42011-06-09 20:52:42276 return;
[email protected]efd75992011-12-15 22:42:42277 }
[email protected]c357acb42011-06-09 20:52:42278
279 IOThreadExtensionFunction* function_io =
280 function->AsIOThreadExtensionFunction();
281 if (!function_io) {
282 NOTREACHED();
283 return;
284 }
[email protected]44295a12013-06-05 08:45:46285 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42286 function_io->set_extension_info_map(extension_info_map);
287 function->set_include_incognito(
288 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25289
[email protected]dc24976f2013-06-02 21:15:09290 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12291 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17292 params.name,
293 args.Pass(),
[email protected]78216e12013-05-17 01:11:25294 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17295 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07296 return;
297 }
298
[email protected]36296912012-03-20 11:08:49299 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29300 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09301 function.get(),
[email protected]85231d72012-08-31 09:45:29302 &params.arguments,
303 base::TimeTicks::Now());
304 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12305 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17306 params.name,
307 args.Pass(),
308 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25309 function->Run();
310 } else {
[email protected]e5a440c2013-06-04 21:55:12311 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17312 params.name,
313 args.Pass(),
[email protected]78216e12013-05-17 01:11:25314 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17315 profile_cast);
[email protected]85231d72012-08-31 09:45:29316 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25317 }
[email protected]c357acb42011-06-09 20:52:42318}
319
[email protected]c5dbef02011-05-13 05:06:09320ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
321 Delegate* delegate)
322 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27323 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35324}
325
[email protected]32dda362009-06-05 19:07:01326ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01327}
328
[email protected]c5dbef02011-05-13 05:06:09329void ExtensionFunctionDispatcher::Dispatch(
330 const ExtensionHostMsg_Request_Params& params,
331 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47332 UIThreadResponseCallbackWrapperMap::const_iterator
333 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
334 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
335 if (iter == ui_thread_response_callback_wrappers_.end()) {
336 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
337 render_view_host);
338 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
339 } else {
340 callback_wrapper = iter->second;
341 }
342
343 DispatchWithCallback(params, render_view_host,
344 callback_wrapper->CreateCallback(params.request_id));
345}
346
347void ExtensionFunctionDispatcher::DispatchWithCallback(
348 const ExtensionHostMsg_Request_Params& params,
349 RenderViewHost* render_view_host,
350 const ExtensionFunction::ResponseCallback& callback) {
351 // TODO(yzshen): There is some shared logic between this method and
352 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09353 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28354 ExtensionProcessManager* process_manager =
355 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46356 extensions::ProcessMap* process_map = service->process_map();
357 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09358 return;
359
[email protected]615d88f2011-12-13 01:47:44360 const Extension* extension = service->extensions()->GetByID(
361 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09362 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55363 extension = service->extensions()->GetHostedAppByURL(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09364
[email protected]8add5412011-10-01 21:02:14365 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53366 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58367 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46368 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11369 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47370 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17371 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
372
[email protected]dc24976f2013-06-02 21:15:09373 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12374 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17375 params.name,
376 args.Pass(),
[email protected]78216e12013-05-17 01:11:25377 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17378 profile());
[email protected]f82d57b52011-04-27 19:13:17379 return;
[email protected]efd75992011-12-15 22:42:42380 }
[email protected]f82d57b52011-04-27 19:13:17381
[email protected]a2aef2e2011-05-26 22:48:12382 UIThreadExtensionFunction* function_ui =
383 function->AsUIThreadExtensionFunction();
384 if (!function_ui) {
385 NOTREACHED();
386 return;
387 }
[email protected]35548ab2013-05-15 08:59:47388 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12389 function_ui->set_dispatcher(AsWeakPtr());
390 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05391 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41392
[email protected]dc24976f2013-06-02 21:15:09393 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12394 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17395 params.name,
396 args.Pass(),
[email protected]78216e12013-05-17 01:11:25397 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17398 profile());
[email protected]d2fe22ff2012-10-03 00:40:07399 return;
400 }
401
[email protected]d13950e2009-12-04 01:43:02402 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29403 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09404 function.get(),
[email protected]85231d72012-08-31 09:45:29405 &params.arguments,
406 base::TimeTicks::Now());
407 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26408 // See crbug.com/39178.
409 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]e5a440c2013-06-04 21:55:12410 LogSuccess(extension->id(), params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02411 function->Run();
412 } else {
[email protected]e5a440c2013-06-04 21:55:12413 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17414 params.name,
415 args.Pass(),
[email protected]78216e12013-05-17 01:11:25416 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17417 profile());
[email protected]85231d72012-08-31 09:45:29418 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02419 }
[email protected]720ad1312012-02-27 23:07:36420
[email protected]efb4b082012-10-17 22:28:28421 // Note: do not access |this| after this point. We may have been deleted
422 // if function->Run() ended up closing the tab that owns us.
423
[email protected]5734e882012-05-04 22:17:56424 // Check if extension was uninstalled by management.uninstall.
425 if (!service->extensions()->GetByID(params.extension_id))
426 return;
427
[email protected]720ad1312012-02-27 23:07:36428 // We only adjust the keepalive count for UIThreadExtensionFunction for
429 // now, largely for simplicity's sake. This is OK because currently, only
430 // the webRequest API uses IOThreadExtensionFunction, and that API is not
431 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28432 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36433}
434
435void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
436 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03437 extensions::ExtensionSystem::Get(profile())->process_manager()->
438 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35439}
440
[email protected]c357acb42011-06-09 20:52:42441// static
[email protected]d2fe22ff2012-10-03 00:40:07442bool ExtensionFunctionDispatcher::CheckPermissions(
443 ExtensionFunction* function,
444 const Extension* extension,
445 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47446 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07447 if (!function->HasPermission()) {
448 LOG(ERROR) << "Extension " << extension->id() << " does not have "
449 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47450 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07451 return false;
452 }
453 return true;
454}
455
[email protected]f33542112013-02-04 16:52:38456namespace {
457
458// Only COMPONENT hosted apps may call extension APIs, and they are limited
459// to just the permissions they explicitly request. They should not have access
460// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
461// are available without permission.
462// TODO(asargent/kalman) - get rid of this when the features system can express
463// the "non permission" permissions.
464bool AllowHostedAppAPICall(const Extension& extension,
465 const GURL& source_url,
466 const std::string& function_name) {
467 if (extension.location() != extensions::Manifest::COMPONENT)
468 return false;
469
470 if (!extension.web_extent().MatchesURL(source_url))
471 return false;
472
473 // We just allow the hosted app's explicit permissions, plus chrome.test.
474 scoped_refptr<const extensions::PermissionSet> permissions =
475 extension.GetActivePermissions();
476 return (permissions->HasAccessToFunction(function_name, false) ||
477 StartsWithASCII(function_name, "test.", true /*case_sensitive*/));
478}
479
480} // namespace
481
482
[email protected]d2fe22ff2012-10-03 00:40:07483// static
[email protected]c357acb42011-06-09 20:52:42484ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
485 const ExtensionHostMsg_Request_Params& params,
486 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46487 int requesting_process_id,
488 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11489 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18490 void* profile,
[email protected]35548ab2013-05-15 08:59:47491 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42492 if (!extension) {
[email protected]6f371442011-11-09 06:45:46493 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47494 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46495 return NULL;
496 }
497
[email protected]f33542112013-02-04 16:52:38498 // Most hosted apps can't call APIs.
499 bool allowed = true;
500 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47501 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38502
503 // Privileged APIs can only be called from the process the extension
504 // is running in.
505 if (allowed && api->IsPrivileged(params.name))
506 allowed = process_map.Contains(extension->id(), requesting_process_id);
507
508 if (!allowed) {
509 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
510 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46511 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47512 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42513 return NULL;
514 }
515
[email protected]c357acb42011-06-09 20:52:42516 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35517 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14518 if (!function) {
519 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47520 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14521 return NULL;
522 }
523
[email protected]c357acb42011-06-09 20:52:42524 function->SetArgs(&params.arguments);
525 function->set_source_url(params.source_url);
526 function->set_request_id(params.request_id);
527 function->set_has_callback(params.has_callback);
528 function->set_user_gesture(params.user_gesture);
529 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32530 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47531 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16532
[email protected]c357acb42011-06-09 20:52:42533 return function;
534}
535
536// static
[email protected]c5dbef02011-05-13 05:06:09537void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47538 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01539 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47540 callback.Run(ExtensionFunction::FAILED, empty_list,
541 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35542}