blob: 14e384ac49946a37d8de58d4aade187342ca3622 [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]2255a9332013-06-17 05:12:3139#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
[email protected]ae33d322012-03-19 22:24:3540#include "webkit/glue/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1041
[email protected]1c321ee2012-05-21 03:02:3442using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1143using extensions::ExtensionAPI;
[email protected]eaabba22012-03-07 15:02:1144using content::RenderViewHost;
[email protected]615d88f2011-12-13 01:47:4445using WebKit::WebSecurityOrigin;
[email protected]83820d42011-11-12 22:03:1146
[email protected]5bc248a2012-04-04 23:38:1147namespace {
48
[email protected]e5a440c2013-06-04 21:55:1249void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1750 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4151 scoped_ptr<base::ListValue> args,
[email protected]4b64d712013-01-17 17:53:1752 Profile* profile) {
53 // The ActivityLog can only be accessed from the main (UI) thread. If we're
54 // running on the wrong thread, re-dispatch from the main thread.
55 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
56 BrowserThread::PostTask(BrowserThread::UI,
57 FROM_HERE,
58 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1259 extension_id,
[email protected]4b64d712013-01-17 17:53:1760 api_name,
[email protected]c02087b512013-02-04 03:09:2061 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1762 profile));
63 } else {
64 extensions::ActivityLog* activity_log =
65 extensions::ActivityLog::GetInstance(profile);
[email protected]e5a440c2013-06-04 21:55:1266 activity_log->LogAPIAction(
67 extension_id, api_name, args.get(), std::string());
[email protected]efd75992011-12-15 22:42:4268 }
69}
70
[email protected]e5a440c2013-06-04 21:55:1271void LogFailure(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1772 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4173 scoped_ptr<base::ListValue> args,
[email protected]78216e12013-05-17 01:11:2574 extensions::BlockedAction::Reason reason,
[email protected]4b64d712013-01-17 17:53:1775 Profile* profile) {
76 // The ActivityLog can only be accessed from the main (UI) thread. If we're
77 // running on the wrong thread, re-dispatch from the main thread.
78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
79 BrowserThread::PostTask(BrowserThread::UI,
80 FROM_HERE,
81 base::Bind(&LogFailure,
[email protected]e5a440c2013-06-04 21:55:1282 extension_id,
[email protected]4b64d712013-01-17 17:53:1783 api_name,
[email protected]c02087b512013-02-04 03:09:2084 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1785 reason,
86 profile));
87 } else {
88 extensions::ActivityLog* activity_log =
89 extensions::ActivityLog::GetInstance(profile);
[email protected]007b3f82013-04-09 08:46:4590 activity_log->LogBlockedAction(
[email protected]e5a440c2013-06-04 21:55:1291 extension_id, api_name, args.get(), reason, std::string());
[email protected]efd75992011-12-15 22:42:4292 }
93}
94
[email protected]4b64d712013-01-17 17:53:1795
[email protected]5bc248a2012-04-04 23:38:1196// Separate copy of ExtensionAPI used for IO thread extension functions. We need
97// this because ExtensionAPI has mutable data. It should be possible to remove
98// this once all the extension APIs are updated to the feature system.
99struct Static {
100 Static()
101 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
102 }
103 scoped_ptr<extensions::ExtensionAPI> api;
104};
105base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
106
[email protected]35548ab2013-05-15 08:59:47107// Kills the specified process because it sends us a malformed message.
108void KillBadMessageSender(base::ProcessHandle process) {
109 NOTREACHED();
110 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
111 if (process)
112 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
113}
114
115void CommonResponseCallback(IPC::Sender* ipc_sender,
116 int routing_id,
117 base::ProcessHandle peer_process,
118 int request_id,
119 ExtensionFunction::ResponseType type,
120 const base::ListValue& results,
121 const std::string& error) {
122 DCHECK(ipc_sender);
123
124 if (type == ExtensionFunction::BAD_MESSAGE) {
125 // The renderer has done validation before sending extension api requests.
126 // Therefore, we should never receive a request that is invalid in a way
127 // that JSON validation in the renderer should have caught. It could be an
128 // attacker trying to exploit the browser, so we crash the renderer instead.
129 LOG(ERROR) <<
130 "Terminating renderer because of malformed extension message.";
131 if (content::RenderProcessHost::run_renderer_in_process()) {
132 // In single process mode it is better if we don't suicide but just crash.
133 CHECK(false);
134 } else {
135 KillBadMessageSender(peer_process);
136 }
137
138 return;
139 }
140
141 ipc_sender->Send(new ExtensionMsg_Response(
142 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
143 error));
144}
145
146void IOThreadResponseCallback(
147 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
148 int routing_id,
149 int request_id,
150 ExtensionFunction::ResponseType type,
151 const base::ListValue& results,
152 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45153 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47154 return;
155
[email protected]e8dad9b2013-06-04 04:43:45156 CommonResponseCallback(ipc_sender.get(),
157 routing_id,
[email protected]950be552013-07-10 19:13:02158 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45159 request_id,
160 type,
161 results,
162 error);
[email protected]35548ab2013-05-15 08:59:47163}
164
[email protected]5bc248a2012-04-04 23:38:11165} // namespace
166
[email protected]35548ab2013-05-15 08:59:47167class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
168 : public content::RenderViewHostObserver {
169 public:
170 UIThreadResponseCallbackWrapper(
171 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
172 RenderViewHost* render_view_host)
173 : content::RenderViewHostObserver(render_view_host),
174 dispatcher_(dispatcher),
175 weak_ptr_factory_(this) {
176 }
177
178 virtual ~UIThreadResponseCallbackWrapper() {
179 }
180
181 // content::RenderViewHostObserver overrides.
182 virtual void RenderViewHostDestroyed(
183 RenderViewHost* render_view_host) OVERRIDE {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]e8dad9b2013-06-04 04:43:45185 if (dispatcher_.get()) {
186 dispatcher_->ui_thread_response_callback_wrappers_
187 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47188 }
189
190 // This call will delete |this|.
191 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
192 }
193
194 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
195 return base::Bind(
196 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
197 weak_ptr_factory_.GetWeakPtr(),
198 request_id);
199 }
200
201 private:
202 void OnExtensionFunctionCompleted(int request_id,
203 ExtensionFunction::ResponseType type,
204 const base::ListValue& results,
205 const std::string& error) {
206 CommonResponseCallback(
207 render_view_host(), render_view_host()->GetRoutingID(),
208 render_view_host()->GetProcess()->GetHandle(), request_id, type,
209 results, error);
210 }
211
212 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
213 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
214
215 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
216};
217
[email protected]44f4b132012-07-17 20:36:57218extensions::WindowController*
219ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08220 const {
221 return NULL;
222}
223
224content::WebContents*
[email protected]44f4b132012-07-17 20:36:57225ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08226 return NULL;
227}
[email protected]5bc248a2012-04-04 23:38:11228
[email protected]1ce88e82013-06-28 05:17:10229content::WebContents*
230ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
231 return GetAssociatedWebContents();
232}
233
[email protected]bfdffe2b2009-04-24 22:05:35234void ExtensionFunctionDispatcher::GetAllFunctionNames(
235 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35236 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35237}
238
[email protected]b83e4602009-05-15 22:58:33239bool ExtensionFunctionDispatcher::OverrideFunction(
240 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35241 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
242 factory);
[email protected]b83e4602009-05-15 22:58:33243}
244
245void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35246 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33247}
248
[email protected]c357acb42011-06-09 20:52:42249// static
250void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25251 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18252 void* profile,
[email protected]c357acb42011-06-09 20:52:42253 int render_process_id,
254 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53255 int routing_id,
[email protected]c357acb42011-06-09 20:52:42256 const ExtensionHostMsg_Request_Params& params) {
257 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11258 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17259 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47260
261 ExtensionFunction::ResponseCallback callback(
262 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
263 params.request_id));
264
[email protected]6f371442011-11-09 06:45:46265 scoped_refptr<ExtensionFunction> function(
266 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11267 extension_info_map->process_map(),
268 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47269 profile, callback));
[email protected]4b64d712013-01-17 17:53:17270 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
271
[email protected]dc24976f2013-06-02 21:15:09272 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12273 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17274 params.name,
275 args.Pass(),
[email protected]78216e12013-05-17 01:11:25276 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17277 profile_cast);
[email protected]c357acb42011-06-09 20:52:42278 return;
[email protected]efd75992011-12-15 22:42:42279 }
[email protected]c357acb42011-06-09 20:52:42280
281 IOThreadExtensionFunction* function_io =
282 function->AsIOThreadExtensionFunction();
283 if (!function_io) {
284 NOTREACHED();
285 return;
286 }
[email protected]44295a12013-06-05 08:45:46287 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42288 function_io->set_extension_info_map(extension_info_map);
289 function->set_include_incognito(
290 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25291
[email protected]dc24976f2013-06-02 21:15:09292 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12293 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17294 params.name,
295 args.Pass(),
[email protected]78216e12013-05-17 01:11:25296 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17297 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07298 return;
299 }
300
[email protected]36296912012-03-20 11:08:49301 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29302 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09303 function.get(),
[email protected]85231d72012-08-31 09:45:29304 &params.arguments,
305 base::TimeTicks::Now());
306 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12307 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17308 params.name,
309 args.Pass(),
310 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25311 function->Run();
312 } else {
[email protected]e5a440c2013-06-04 21:55:12313 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17314 params.name,
315 args.Pass(),
[email protected]78216e12013-05-17 01:11:25316 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17317 profile_cast);
[email protected]85231d72012-08-31 09:45:29318 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25319 }
[email protected]c357acb42011-06-09 20:52:42320}
321
[email protected]c5dbef02011-05-13 05:06:09322ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
323 Delegate* delegate)
324 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27325 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35326}
327
[email protected]32dda362009-06-05 19:07:01328ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01329}
330
[email protected]c5dbef02011-05-13 05:06:09331void ExtensionFunctionDispatcher::Dispatch(
332 const ExtensionHostMsg_Request_Params& params,
333 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47334 UIThreadResponseCallbackWrapperMap::const_iterator
335 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
336 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
337 if (iter == ui_thread_response_callback_wrappers_.end()) {
338 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
339 render_view_host);
340 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
341 } else {
342 callback_wrapper = iter->second;
343 }
344
345 DispatchWithCallback(params, render_view_host,
346 callback_wrapper->CreateCallback(params.request_id));
347}
348
349void ExtensionFunctionDispatcher::DispatchWithCallback(
350 const ExtensionHostMsg_Request_Params& params,
351 RenderViewHost* render_view_host,
352 const ExtensionFunction::ResponseCallback& callback) {
353 // TODO(yzshen): There is some shared logic between this method and
354 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09355 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28356 ExtensionProcessManager* process_manager =
357 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46358 extensions::ProcessMap* process_map = service->process_map();
359 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09360 return;
361
[email protected]615d88f2011-12-13 01:47:44362 const Extension* extension = service->extensions()->GetByID(
363 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09364 if (!extension)
[email protected]615d88f2011-12-13 01:47:44365 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
366 WebSecurityOrigin::createFromString(params.source_origin),
367 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09368
[email protected]8add5412011-10-01 21:02:14369 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53370 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58371 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46372 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11373 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47374 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17375 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
376
[email protected]dc24976f2013-06-02 21:15:09377 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12378 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17379 params.name,
380 args.Pass(),
[email protected]78216e12013-05-17 01:11:25381 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17382 profile());
[email protected]f82d57b52011-04-27 19:13:17383 return;
[email protected]efd75992011-12-15 22:42:42384 }
[email protected]f82d57b52011-04-27 19:13:17385
[email protected]a2aef2e2011-05-26 22:48:12386 UIThreadExtensionFunction* function_ui =
387 function->AsUIThreadExtensionFunction();
388 if (!function_ui) {
389 NOTREACHED();
390 return;
391 }
[email protected]35548ab2013-05-15 08:59:47392 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12393 function_ui->set_dispatcher(AsWeakPtr());
394 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05395 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41396
[email protected]dc24976f2013-06-02 21:15:09397 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12398 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17399 params.name,
400 args.Pass(),
[email protected]78216e12013-05-17 01:11:25401 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17402 profile());
[email protected]d2fe22ff2012-10-03 00:40:07403 return;
404 }
405
[email protected]d13950e2009-12-04 01:43:02406 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29407 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09408 function.get(),
[email protected]85231d72012-08-31 09:45:29409 &params.arguments,
410 base::TimeTicks::Now());
411 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26412 // See crbug.com/39178.
413 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]e5a440c2013-06-04 21:55:12414 LogSuccess(extension->id(), params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02415 function->Run();
416 } else {
[email protected]e5a440c2013-06-04 21:55:12417 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17418 params.name,
419 args.Pass(),
[email protected]78216e12013-05-17 01:11:25420 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17421 profile());
[email protected]85231d72012-08-31 09:45:29422 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02423 }
[email protected]720ad1312012-02-27 23:07:36424
[email protected]efb4b082012-10-17 22:28:28425 // Note: do not access |this| after this point. We may have been deleted
426 // if function->Run() ended up closing the tab that owns us.
427
[email protected]5734e882012-05-04 22:17:56428 // Check if extension was uninstalled by management.uninstall.
429 if (!service->extensions()->GetByID(params.extension_id))
430 return;
431
[email protected]720ad1312012-02-27 23:07:36432 // We only adjust the keepalive count for UIThreadExtensionFunction for
433 // now, largely for simplicity's sake. This is OK because currently, only
434 // the webRequest API uses IOThreadExtensionFunction, and that API is not
435 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28436 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36437}
438
439void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
440 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03441 extensions::ExtensionSystem::Get(profile())->process_manager()->
442 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35443}
444
[email protected]c357acb42011-06-09 20:52:42445// static
[email protected]d2fe22ff2012-10-03 00:40:07446bool ExtensionFunctionDispatcher::CheckPermissions(
447 ExtensionFunction* function,
448 const Extension* extension,
449 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47450 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07451 if (!function->HasPermission()) {
452 LOG(ERROR) << "Extension " << extension->id() << " does not have "
453 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47454 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07455 return false;
456 }
457 return true;
458}
459
[email protected]f33542112013-02-04 16:52:38460namespace {
461
462// Only COMPONENT hosted apps may call extension APIs, and they are limited
463// to just the permissions they explicitly request. They should not have access
464// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
465// are available without permission.
466// TODO(asargent/kalman) - get rid of this when the features system can express
467// the "non permission" permissions.
468bool AllowHostedAppAPICall(const Extension& extension,
469 const GURL& source_url,
470 const std::string& function_name) {
471 if (extension.location() != extensions::Manifest::COMPONENT)
472 return false;
473
474 if (!extension.web_extent().MatchesURL(source_url))
475 return false;
476
477 // We just allow the hosted app's explicit permissions, plus chrome.test.
478 scoped_refptr<const extensions::PermissionSet> permissions =
479 extension.GetActivePermissions();
480 return (permissions->HasAccessToFunction(function_name, false) ||
481 StartsWithASCII(function_name, "test.", true /*case_sensitive*/));
482}
483
484} // namespace
485
486
[email protected]d2fe22ff2012-10-03 00:40:07487// static
[email protected]c357acb42011-06-09 20:52:42488ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
489 const ExtensionHostMsg_Request_Params& params,
490 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46491 int requesting_process_id,
492 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11493 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18494 void* profile,
[email protected]35548ab2013-05-15 08:59:47495 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42496 if (!extension) {
[email protected]6f371442011-11-09 06:45:46497 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47498 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46499 return NULL;
500 }
501
[email protected]f33542112013-02-04 16:52:38502 // Most hosted apps can't call APIs.
503 bool allowed = true;
504 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47505 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38506
507 // Privileged APIs can only be called from the process the extension
508 // is running in.
509 if (allowed && api->IsPrivileged(params.name))
510 allowed = process_map.Contains(extension->id(), requesting_process_id);
511
512 if (!allowed) {
513 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
514 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46515 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47516 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42517 return NULL;
518 }
519
[email protected]c357acb42011-06-09 20:52:42520 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35521 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14522 if (!function) {
523 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47524 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14525 return NULL;
526 }
527
[email protected]c357acb42011-06-09 20:52:42528 function->SetArgs(&params.arguments);
529 function->set_source_url(params.source_url);
530 function->set_request_id(params.request_id);
531 function->set_has_callback(params.has_callback);
532 function->set_user_gesture(params.user_gesture);
533 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32534 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47535 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16536
[email protected]c357acb42011-06-09 20:52:42537 return function;
538}
539
540// static
[email protected]c5dbef02011-05-13 05:06:09541void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47542 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01543 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47544 callback.Run(ExtensionFunction::FAILED, empty_list,
545 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35546}