blob: 18f6c81f79419d41cc11199ce92a34f8b9491c38 [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]d09a4ce1c2013-07-24 17:37:0212#include "base/process/process.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]05602fa32013-08-01 06:30:4315#include "chrome/browser/extensions/activity_log/activity_action_constants.h"
[email protected]17263b22013-05-16 03:29:2216#include "chrome/browser/extensions/activity_log/activity_log.h"
[email protected]05602fa32013-08-01 06:30:4317#include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.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]d0fcff72013-07-23 02:45:4339#include "webkit/common/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1040
[email protected]05602fa32013-08-01 06:30:4341using extensions::api::activity_log_private::BlockedChromeActivityDetail;
[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]83820d42011-11-12 22:03:1145
[email protected]5bc248a2012-04-04 23:38:1146namespace {
47
[email protected]e5a440c2013-06-04 21:55:1248void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1749 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4150 scoped_ptr<base::ListValue> args,
[email protected]4b64d712013-01-17 17:53:1751 Profile* profile) {
52 // The ActivityLog can only be accessed from the main (UI) thread. If we're
53 // running on the wrong thread, re-dispatch from the main thread.
54 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
55 BrowserThread::PostTask(BrowserThread::UI,
56 FROM_HERE,
57 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1258 extension_id,
[email protected]4b64d712013-01-17 17:53:1759 api_name,
[email protected]c02087b512013-02-04 03:09:2060 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1761 profile));
62 } else {
63 extensions::ActivityLog* activity_log =
64 extensions::ActivityLog::GetInstance(profile);
[email protected]05602fa32013-08-01 06:30:4365 scoped_refptr<extensions::Action> action =
66 new extensions::Action(extension_id,
67 base::Time::Now(),
68 extensions::Action::ACTION_API_CALL,
69 api_name);
70 action->set_args(args.Pass());
71 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:4272 }
73}
74
[email protected]e5a440c2013-06-04 21:55:1275void LogFailure(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1776 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4177 scoped_ptr<base::ListValue> args,
[email protected]05602fa32013-08-01 06:30:4378 BlockedChromeActivityDetail::Reason reason,
[email protected]4b64d712013-01-17 17:53:1779 Profile* profile) {
80 // The ActivityLog can only be accessed from the main (UI) thread. If we're
81 // running on the wrong thread, re-dispatch from the main thread.
82 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
83 BrowserThread::PostTask(BrowserThread::UI,
84 FROM_HERE,
85 base::Bind(&LogFailure,
[email protected]e5a440c2013-06-04 21:55:1286 extension_id,
[email protected]4b64d712013-01-17 17:53:1787 api_name,
[email protected]c02087b512013-02-04 03:09:2088 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1789 reason,
90 profile));
91 } else {
92 extensions::ActivityLog* activity_log =
93 extensions::ActivityLog::GetInstance(profile);
[email protected]05602fa32013-08-01 06:30:4394 scoped_refptr<extensions::Action> action =
95 new extensions::Action(extension_id,
96 base::Time::Now(),
97 extensions::Action::ACTION_API_BLOCKED,
98 api_name);
99 action->set_args(args.Pass());
100 action->mutable_other()
101 ->SetString(activity_log_constants::kActionBlockedReason,
102 BlockedChromeActivityDetail::ToString(reason));
103 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:42104 }
105}
106
[email protected]4b64d712013-01-17 17:53:17107
[email protected]5bc248a2012-04-04 23:38:11108// Separate copy of ExtensionAPI used for IO thread extension functions. We need
109// this because ExtensionAPI has mutable data. It should be possible to remove
110// this once all the extension APIs are updated to the feature system.
111struct Static {
112 Static()
113 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
114 }
115 scoped_ptr<extensions::ExtensionAPI> api;
116};
117base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
118
[email protected]35548ab2013-05-15 08:59:47119// Kills the specified process because it sends us a malformed message.
120void KillBadMessageSender(base::ProcessHandle process) {
121 NOTREACHED();
122 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
123 if (process)
124 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
125}
126
127void CommonResponseCallback(IPC::Sender* ipc_sender,
128 int routing_id,
129 base::ProcessHandle peer_process,
130 int request_id,
131 ExtensionFunction::ResponseType type,
132 const base::ListValue& results,
133 const std::string& error) {
134 DCHECK(ipc_sender);
135
136 if (type == ExtensionFunction::BAD_MESSAGE) {
137 // The renderer has done validation before sending extension api requests.
138 // Therefore, we should never receive a request that is invalid in a way
139 // that JSON validation in the renderer should have caught. It could be an
140 // attacker trying to exploit the browser, so we crash the renderer instead.
141 LOG(ERROR) <<
142 "Terminating renderer because of malformed extension message.";
143 if (content::RenderProcessHost::run_renderer_in_process()) {
144 // In single process mode it is better if we don't suicide but just crash.
145 CHECK(false);
146 } else {
147 KillBadMessageSender(peer_process);
148 }
149
150 return;
151 }
152
153 ipc_sender->Send(new ExtensionMsg_Response(
154 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
155 error));
156}
157
158void IOThreadResponseCallback(
159 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
160 int routing_id,
161 int request_id,
162 ExtensionFunction::ResponseType type,
163 const base::ListValue& results,
164 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45165 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47166 return;
167
[email protected]e8dad9b2013-06-04 04:43:45168 CommonResponseCallback(ipc_sender.get(),
169 routing_id,
[email protected]950be552013-07-10 19:13:02170 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45171 request_id,
172 type,
173 results,
174 error);
[email protected]35548ab2013-05-15 08:59:47175}
176
[email protected]5bc248a2012-04-04 23:38:11177} // namespace
178
[email protected]35548ab2013-05-15 08:59:47179class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
180 : public content::RenderViewHostObserver {
181 public:
182 UIThreadResponseCallbackWrapper(
183 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
184 RenderViewHost* render_view_host)
185 : content::RenderViewHostObserver(render_view_host),
186 dispatcher_(dispatcher),
187 weak_ptr_factory_(this) {
188 }
189
190 virtual ~UIThreadResponseCallbackWrapper() {
191 }
192
193 // content::RenderViewHostObserver overrides.
194 virtual void RenderViewHostDestroyed(
195 RenderViewHost* render_view_host) OVERRIDE {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]e8dad9b2013-06-04 04:43:45197 if (dispatcher_.get()) {
198 dispatcher_->ui_thread_response_callback_wrappers_
199 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47200 }
201
202 // This call will delete |this|.
203 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
204 }
205
206 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
207 return base::Bind(
208 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
209 weak_ptr_factory_.GetWeakPtr(),
210 request_id);
211 }
212
213 private:
214 void OnExtensionFunctionCompleted(int request_id,
215 ExtensionFunction::ResponseType type,
216 const base::ListValue& results,
217 const std::string& error) {
218 CommonResponseCallback(
219 render_view_host(), render_view_host()->GetRoutingID(),
220 render_view_host()->GetProcess()->GetHandle(), request_id, type,
221 results, error);
222 }
223
224 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
225 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
226
227 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
228};
229
[email protected]44f4b132012-07-17 20:36:57230extensions::WindowController*
231ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08232 const {
233 return NULL;
234}
235
236content::WebContents*
[email protected]44f4b132012-07-17 20:36:57237ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08238 return NULL;
239}
[email protected]5bc248a2012-04-04 23:38:11240
[email protected]1ce88e82013-06-28 05:17:10241content::WebContents*
242ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
243 return GetAssociatedWebContents();
244}
245
[email protected]bfdffe2b2009-04-24 22:05:35246void ExtensionFunctionDispatcher::GetAllFunctionNames(
247 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35248 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35249}
250
[email protected]b83e4602009-05-15 22:58:33251bool ExtensionFunctionDispatcher::OverrideFunction(
252 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35253 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
254 factory);
[email protected]b83e4602009-05-15 22:58:33255}
256
257void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35258 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33259}
260
[email protected]c357acb42011-06-09 20:52:42261// static
262void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25263 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18264 void* profile,
[email protected]c357acb42011-06-09 20:52:42265 int render_process_id,
266 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53267 int routing_id,
[email protected]c357acb42011-06-09 20:52:42268 const ExtensionHostMsg_Request_Params& params) {
269 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11270 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17271 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47272
273 ExtensionFunction::ResponseCallback callback(
274 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
275 params.request_id));
276
[email protected]6f371442011-11-09 06:45:46277 scoped_refptr<ExtensionFunction> function(
278 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11279 extension_info_map->process_map(),
280 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47281 profile, callback));
[email protected]4b64d712013-01-17 17:53:17282 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
283
[email protected]dc24976f2013-06-02 21:15:09284 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12285 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17286 params.name,
287 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43288 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17289 profile_cast);
[email protected]c357acb42011-06-09 20:52:42290 return;
[email protected]efd75992011-12-15 22:42:42291 }
[email protected]c357acb42011-06-09 20:52:42292
293 IOThreadExtensionFunction* function_io =
294 function->AsIOThreadExtensionFunction();
295 if (!function_io) {
296 NOTREACHED();
297 return;
298 }
[email protected]44295a12013-06-05 08:45:46299 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42300 function_io->set_extension_info_map(extension_info_map);
301 function->set_include_incognito(
302 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25303
[email protected]dc24976f2013-06-02 21:15:09304 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12305 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17306 params.name,
307 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43308 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17309 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07310 return;
311 }
312
[email protected]36296912012-03-20 11:08:49313 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29314 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09315 function.get(),
[email protected]85231d72012-08-31 09:45:29316 &params.arguments,
317 base::TimeTicks::Now());
318 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12319 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17320 params.name,
321 args.Pass(),
322 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25323 function->Run();
324 } else {
[email protected]e5a440c2013-06-04 21:55:12325 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17326 params.name,
327 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43328 BlockedChromeActivityDetail::REASON_QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17329 profile_cast);
[email protected]85231d72012-08-31 09:45:29330 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25331 }
[email protected]c357acb42011-06-09 20:52:42332}
333
[email protected]c5dbef02011-05-13 05:06:09334ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
335 Delegate* delegate)
336 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27337 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35338}
339
[email protected]32dda362009-06-05 19:07:01340ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01341}
342
[email protected]c5dbef02011-05-13 05:06:09343void ExtensionFunctionDispatcher::Dispatch(
344 const ExtensionHostMsg_Request_Params& params,
345 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47346 UIThreadResponseCallbackWrapperMap::const_iterator
347 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
348 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
349 if (iter == ui_thread_response_callback_wrappers_.end()) {
350 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
351 render_view_host);
352 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
353 } else {
354 callback_wrapper = iter->second;
355 }
356
357 DispatchWithCallback(params, render_view_host,
358 callback_wrapper->CreateCallback(params.request_id));
359}
360
361void ExtensionFunctionDispatcher::DispatchWithCallback(
362 const ExtensionHostMsg_Request_Params& params,
363 RenderViewHost* render_view_host,
364 const ExtensionFunction::ResponseCallback& callback) {
365 // TODO(yzshen): There is some shared logic between this method and
366 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09367 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28368 ExtensionProcessManager* process_manager =
369 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46370 extensions::ProcessMap* process_map = service->process_map();
371 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09372 return;
373
[email protected]615d88f2011-12-13 01:47:44374 const Extension* extension = service->extensions()->GetByID(
375 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09376 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55377 extension = service->extensions()->GetHostedAppByURL(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09378
[email protected]8add5412011-10-01 21:02:14379 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53380 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58381 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46382 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11383 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47384 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17385 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
386
[email protected]dc24976f2013-06-02 21:15:09387 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12388 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17389 params.name,
390 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43391 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17392 profile());
[email protected]f82d57b52011-04-27 19:13:17393 return;
[email protected]efd75992011-12-15 22:42:42394 }
[email protected]f82d57b52011-04-27 19:13:17395
[email protected]a2aef2e2011-05-26 22:48:12396 UIThreadExtensionFunction* function_ui =
397 function->AsUIThreadExtensionFunction();
398 if (!function_ui) {
399 NOTREACHED();
400 return;
401 }
[email protected]35548ab2013-05-15 08:59:47402 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12403 function_ui->set_dispatcher(AsWeakPtr());
404 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05405 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41406
[email protected]dc24976f2013-06-02 21:15:09407 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12408 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17409 params.name,
410 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43411 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17412 profile());
[email protected]d2fe22ff2012-10-03 00:40:07413 return;
414 }
415
[email protected]d13950e2009-12-04 01:43:02416 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29417 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09418 function.get(),
[email protected]85231d72012-08-31 09:45:29419 &params.arguments,
420 base::TimeTicks::Now());
421 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26422 // See crbug.com/39178.
423 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]e5a440c2013-06-04 21:55:12424 LogSuccess(extension->id(), params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02425 function->Run();
426 } else {
[email protected]e5a440c2013-06-04 21:55:12427 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17428 params.name,
429 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43430 BlockedChromeActivityDetail::REASON_QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17431 profile());
[email protected]85231d72012-08-31 09:45:29432 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02433 }
[email protected]720ad1312012-02-27 23:07:36434
[email protected]efb4b082012-10-17 22:28:28435 // Note: do not access |this| after this point. We may have been deleted
436 // if function->Run() ended up closing the tab that owns us.
437
[email protected]5734e882012-05-04 22:17:56438 // Check if extension was uninstalled by management.uninstall.
439 if (!service->extensions()->GetByID(params.extension_id))
440 return;
441
[email protected]720ad1312012-02-27 23:07:36442 // We only adjust the keepalive count for UIThreadExtensionFunction for
443 // now, largely for simplicity's sake. This is OK because currently, only
444 // the webRequest API uses IOThreadExtensionFunction, and that API is not
445 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28446 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36447}
448
449void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
450 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03451 extensions::ExtensionSystem::Get(profile())->process_manager()->
452 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35453}
454
[email protected]c357acb42011-06-09 20:52:42455// static
[email protected]d2fe22ff2012-10-03 00:40:07456bool ExtensionFunctionDispatcher::CheckPermissions(
457 ExtensionFunction* function,
458 const Extension* extension,
459 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47460 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07461 if (!function->HasPermission()) {
462 LOG(ERROR) << "Extension " << extension->id() << " does not have "
463 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47464 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07465 return false;
466 }
467 return true;
468}
469
[email protected]f33542112013-02-04 16:52:38470namespace {
471
472// Only COMPONENT hosted apps may call extension APIs, and they are limited
473// to just the permissions they explicitly request. They should not have access
474// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
475// are available without permission.
476// TODO(asargent/kalman) - get rid of this when the features system can express
477// the "non permission" permissions.
478bool AllowHostedAppAPICall(const Extension& extension,
479 const GURL& source_url,
480 const std::string& function_name) {
481 if (extension.location() != extensions::Manifest::COMPONENT)
482 return false;
483
484 if (!extension.web_extent().MatchesURL(source_url))
485 return false;
486
487 // We just allow the hosted app's explicit permissions, plus chrome.test.
488 scoped_refptr<const extensions::PermissionSet> permissions =
489 extension.GetActivePermissions();
490 return (permissions->HasAccessToFunction(function_name, false) ||
491 StartsWithASCII(function_name, "test.", true /*case_sensitive*/));
492}
493
494} // namespace
495
496
[email protected]d2fe22ff2012-10-03 00:40:07497// static
[email protected]c357acb42011-06-09 20:52:42498ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
499 const ExtensionHostMsg_Request_Params& params,
500 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46501 int requesting_process_id,
502 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11503 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18504 void* profile,
[email protected]35548ab2013-05-15 08:59:47505 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42506 if (!extension) {
[email protected]6f371442011-11-09 06:45:46507 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47508 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46509 return NULL;
510 }
511
[email protected]f33542112013-02-04 16:52:38512 // Most hosted apps can't call APIs.
513 bool allowed = true;
514 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47515 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38516
517 // Privileged APIs can only be called from the process the extension
518 // is running in.
519 if (allowed && api->IsPrivileged(params.name))
520 allowed = process_map.Contains(extension->id(), requesting_process_id);
521
522 if (!allowed) {
523 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
524 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46525 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47526 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42527 return NULL;
528 }
529
[email protected]c357acb42011-06-09 20:52:42530 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35531 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14532 if (!function) {
533 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47534 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14535 return NULL;
536 }
537
[email protected]c357acb42011-06-09 20:52:42538 function->SetArgs(&params.arguments);
539 function->set_source_url(params.source_url);
540 function->set_request_id(params.request_id);
541 function->set_has_callback(params.has_callback);
542 function->set_user_gesture(params.user_gesture);
543 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32544 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47545 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16546
[email protected]c357acb42011-06-09 20:52:42547 return function;
548}
549
550// static
[email protected]c5dbef02011-05-13 05:06:09551void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47552 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01553 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47554 callback.Run(ExtensionFunction::FAILED, empty_list,
555 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35556}