blob: e846c00e46ee7ba848d62840c24b59d43375b854 [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]b5b26b72013-08-02 00:25:1144using extensions::Feature;
[email protected]eaabba22012-03-07 15:02:1145using content::RenderViewHost;
[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]05602fa32013-08-01 06:30:4366 scoped_refptr<extensions::Action> action =
67 new extensions::Action(extension_id,
68 base::Time::Now(),
69 extensions::Action::ACTION_API_CALL,
70 api_name);
71 action->set_args(args.Pass());
72 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:4273 }
74}
75
[email protected]e5a440c2013-06-04 21:55:1276void LogFailure(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1777 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4178 scoped_ptr<base::ListValue> args,
[email protected]05602fa32013-08-01 06:30:4379 BlockedChromeActivityDetail::Reason reason,
[email protected]4b64d712013-01-17 17:53:1780 Profile* profile) {
81 // The ActivityLog can only be accessed from the main (UI) thread. If we're
82 // running on the wrong thread, re-dispatch from the main thread.
83 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
84 BrowserThread::PostTask(BrowserThread::UI,
85 FROM_HERE,
86 base::Bind(&LogFailure,
[email protected]e5a440c2013-06-04 21:55:1287 extension_id,
[email protected]4b64d712013-01-17 17:53:1788 api_name,
[email protected]c02087b512013-02-04 03:09:2089 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1790 reason,
91 profile));
92 } else {
93 extensions::ActivityLog* activity_log =
94 extensions::ActivityLog::GetInstance(profile);
[email protected]05602fa32013-08-01 06:30:4395 scoped_refptr<extensions::Action> action =
96 new extensions::Action(extension_id,
97 base::Time::Now(),
98 extensions::Action::ACTION_API_BLOCKED,
99 api_name);
100 action->set_args(args.Pass());
101 action->mutable_other()
102 ->SetString(activity_log_constants::kActionBlockedReason,
103 BlockedChromeActivityDetail::ToString(reason));
104 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:42105 }
106}
107
[email protected]4b64d712013-01-17 17:53:17108
[email protected]5bc248a2012-04-04 23:38:11109// Separate copy of ExtensionAPI used for IO thread extension functions. We need
110// this because ExtensionAPI has mutable data. It should be possible to remove
111// this once all the extension APIs are updated to the feature system.
112struct Static {
113 Static()
114 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
115 }
116 scoped_ptr<extensions::ExtensionAPI> api;
117};
118base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
119
[email protected]35548ab2013-05-15 08:59:47120// Kills the specified process because it sends us a malformed message.
121void KillBadMessageSender(base::ProcessHandle process) {
122 NOTREACHED();
123 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
124 if (process)
125 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
126}
127
128void CommonResponseCallback(IPC::Sender* ipc_sender,
129 int routing_id,
130 base::ProcessHandle peer_process,
131 int request_id,
132 ExtensionFunction::ResponseType type,
133 const base::ListValue& results,
134 const std::string& error) {
135 DCHECK(ipc_sender);
136
137 if (type == ExtensionFunction::BAD_MESSAGE) {
138 // The renderer has done validation before sending extension api requests.
139 // Therefore, we should never receive a request that is invalid in a way
140 // that JSON validation in the renderer should have caught. It could be an
141 // attacker trying to exploit the browser, so we crash the renderer instead.
142 LOG(ERROR) <<
143 "Terminating renderer because of malformed extension message.";
144 if (content::RenderProcessHost::run_renderer_in_process()) {
145 // In single process mode it is better if we don't suicide but just crash.
146 CHECK(false);
147 } else {
148 KillBadMessageSender(peer_process);
149 }
150
151 return;
152 }
153
154 ipc_sender->Send(new ExtensionMsg_Response(
155 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
156 error));
157}
158
159void IOThreadResponseCallback(
160 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
161 int routing_id,
162 int request_id,
163 ExtensionFunction::ResponseType type,
164 const base::ListValue& results,
165 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45166 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47167 return;
168
[email protected]e8dad9b2013-06-04 04:43:45169 CommonResponseCallback(ipc_sender.get(),
170 routing_id,
[email protected]950be552013-07-10 19:13:02171 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45172 request_id,
173 type,
174 results,
175 error);
[email protected]35548ab2013-05-15 08:59:47176}
177
[email protected]5bc248a2012-04-04 23:38:11178} // namespace
179
[email protected]35548ab2013-05-15 08:59:47180class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
181 : public content::RenderViewHostObserver {
182 public:
183 UIThreadResponseCallbackWrapper(
184 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
185 RenderViewHost* render_view_host)
186 : content::RenderViewHostObserver(render_view_host),
187 dispatcher_(dispatcher),
188 weak_ptr_factory_(this) {
189 }
190
191 virtual ~UIThreadResponseCallbackWrapper() {
192 }
193
194 // content::RenderViewHostObserver overrides.
195 virtual void RenderViewHostDestroyed(
196 RenderViewHost* render_view_host) OVERRIDE {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]e8dad9b2013-06-04 04:43:45198 if (dispatcher_.get()) {
199 dispatcher_->ui_thread_response_callback_wrappers_
200 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47201 }
202
203 // This call will delete |this|.
204 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
205 }
206
207 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
208 return base::Bind(
209 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
210 weak_ptr_factory_.GetWeakPtr(),
211 request_id);
212 }
213
214 private:
215 void OnExtensionFunctionCompleted(int request_id,
216 ExtensionFunction::ResponseType type,
217 const base::ListValue& results,
218 const std::string& error) {
219 CommonResponseCallback(
220 render_view_host(), render_view_host()->GetRoutingID(),
221 render_view_host()->GetProcess()->GetHandle(), request_id, type,
222 results, error);
223 }
224
225 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
226 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
227
228 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
229};
230
[email protected]44f4b132012-07-17 20:36:57231extensions::WindowController*
232ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08233 const {
234 return NULL;
235}
236
237content::WebContents*
[email protected]44f4b132012-07-17 20:36:57238ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08239 return NULL;
240}
[email protected]5bc248a2012-04-04 23:38:11241
[email protected]1ce88e82013-06-28 05:17:10242content::WebContents*
243ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
244 return GetAssociatedWebContents();
245}
246
[email protected]bfdffe2b2009-04-24 22:05:35247void ExtensionFunctionDispatcher::GetAllFunctionNames(
248 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35249 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35250}
251
[email protected]b83e4602009-05-15 22:58:33252bool ExtensionFunctionDispatcher::OverrideFunction(
253 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35254 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
255 factory);
[email protected]b83e4602009-05-15 22:58:33256}
257
258void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35259 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33260}
261
[email protected]c357acb42011-06-09 20:52:42262// static
263void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25264 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18265 void* profile,
[email protected]c357acb42011-06-09 20:52:42266 int render_process_id,
267 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53268 int routing_id,
[email protected]c357acb42011-06-09 20:52:42269 const ExtensionHostMsg_Request_Params& params) {
270 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11271 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17272 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47273
274 ExtensionFunction::ResponseCallback callback(
275 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
276 params.request_id));
277
[email protected]6f371442011-11-09 06:45:46278 scoped_refptr<ExtensionFunction> function(
279 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11280 extension_info_map->process_map(),
281 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47282 profile, callback));
[email protected]4b64d712013-01-17 17:53:17283 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
284
[email protected]dc24976f2013-06-02 21:15:09285 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12286 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17287 params.name,
288 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43289 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17290 profile_cast);
[email protected]c357acb42011-06-09 20:52:42291 return;
[email protected]efd75992011-12-15 22:42:42292 }
[email protected]c357acb42011-06-09 20:52:42293
294 IOThreadExtensionFunction* function_io =
295 function->AsIOThreadExtensionFunction();
296 if (!function_io) {
297 NOTREACHED();
298 return;
299 }
[email protected]44295a12013-06-05 08:45:46300 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42301 function_io->set_extension_info_map(extension_info_map);
302 function->set_include_incognito(
303 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25304
[email protected]dc24976f2013-06-02 21:15:09305 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12306 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17307 params.name,
308 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43309 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17310 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07311 return;
312 }
313
[email protected]36296912012-03-20 11:08:49314 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29315 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09316 function.get(),
[email protected]85231d72012-08-31 09:45:29317 &params.arguments,
318 base::TimeTicks::Now());
319 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12320 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17321 params.name,
322 args.Pass(),
323 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25324 function->Run();
325 } else {
[email protected]e5a440c2013-06-04 21:55:12326 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17327 params.name,
328 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43329 BlockedChromeActivityDetail::REASON_QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17330 profile_cast);
[email protected]85231d72012-08-31 09:45:29331 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25332 }
[email protected]c357acb42011-06-09 20:52:42333}
334
[email protected]c5dbef02011-05-13 05:06:09335ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
336 Delegate* delegate)
337 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27338 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35339}
340
[email protected]32dda362009-06-05 19:07:01341ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01342}
343
[email protected]c5dbef02011-05-13 05:06:09344void ExtensionFunctionDispatcher::Dispatch(
345 const ExtensionHostMsg_Request_Params& params,
346 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47347 UIThreadResponseCallbackWrapperMap::const_iterator
348 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
349 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
350 if (iter == ui_thread_response_callback_wrappers_.end()) {
351 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
352 render_view_host);
353 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
354 } else {
355 callback_wrapper = iter->second;
356 }
357
358 DispatchWithCallback(params, render_view_host,
359 callback_wrapper->CreateCallback(params.request_id));
360}
361
362void ExtensionFunctionDispatcher::DispatchWithCallback(
363 const ExtensionHostMsg_Request_Params& params,
364 RenderViewHost* render_view_host,
365 const ExtensionFunction::ResponseCallback& callback) {
366 // TODO(yzshen): There is some shared logic between this method and
367 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09368 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28369 ExtensionProcessManager* process_manager =
370 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46371 extensions::ProcessMap* process_map = service->process_map();
372 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09373 return;
374
[email protected]615d88f2011-12-13 01:47:44375 const Extension* extension = service->extensions()->GetByID(
376 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09377 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55378 extension = service->extensions()->GetHostedAppByURL(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09379
[email protected]8add5412011-10-01 21:02:14380 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53381 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58382 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46383 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11384 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47385 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17386 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
387
[email protected]dc24976f2013-06-02 21:15:09388 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12389 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17390 params.name,
391 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43392 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17393 profile());
[email protected]f82d57b52011-04-27 19:13:17394 return;
[email protected]efd75992011-12-15 22:42:42395 }
[email protected]f82d57b52011-04-27 19:13:17396
[email protected]a2aef2e2011-05-26 22:48:12397 UIThreadExtensionFunction* function_ui =
398 function->AsUIThreadExtensionFunction();
399 if (!function_ui) {
400 NOTREACHED();
401 return;
402 }
[email protected]35548ab2013-05-15 08:59:47403 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12404 function_ui->set_dispatcher(AsWeakPtr());
405 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05406 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41407
[email protected]dc24976f2013-06-02 21:15:09408 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12409 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17410 params.name,
411 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43412 BlockedChromeActivityDetail::REASON_ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17413 profile());
[email protected]d2fe22ff2012-10-03 00:40:07414 return;
415 }
416
[email protected]d13950e2009-12-04 01:43:02417 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29418 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09419 function.get(),
[email protected]85231d72012-08-31 09:45:29420 &params.arguments,
421 base::TimeTicks::Now());
422 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26423 // See crbug.com/39178.
424 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]e5a440c2013-06-04 21:55:12425 LogSuccess(extension->id(), params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02426 function->Run();
427 } else {
[email protected]e5a440c2013-06-04 21:55:12428 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17429 params.name,
430 args.Pass(),
[email protected]05602fa32013-08-01 06:30:43431 BlockedChromeActivityDetail::REASON_QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17432 profile());
[email protected]85231d72012-08-31 09:45:29433 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02434 }
[email protected]720ad1312012-02-27 23:07:36435
[email protected]efb4b082012-10-17 22:28:28436 // Note: do not access |this| after this point. We may have been deleted
437 // if function->Run() ended up closing the tab that owns us.
438
[email protected]5734e882012-05-04 22:17:56439 // Check if extension was uninstalled by management.uninstall.
440 if (!service->extensions()->GetByID(params.extension_id))
441 return;
442
[email protected]720ad1312012-02-27 23:07:36443 // We only adjust the keepalive count for UIThreadExtensionFunction for
444 // now, largely for simplicity's sake. This is OK because currently, only
445 // the webRequest API uses IOThreadExtensionFunction, and that API is not
446 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28447 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36448}
449
450void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
451 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03452 extensions::ExtensionSystem::Get(profile())->process_manager()->
453 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35454}
455
[email protected]c357acb42011-06-09 20:52:42456// static
[email protected]d2fe22ff2012-10-03 00:40:07457bool ExtensionFunctionDispatcher::CheckPermissions(
458 ExtensionFunction* function,
459 const Extension* extension,
460 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47461 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07462 if (!function->HasPermission()) {
463 LOG(ERROR) << "Extension " << extension->id() << " does not have "
464 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47465 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07466 return false;
467 }
468 return true;
469}
470
[email protected]f33542112013-02-04 16:52:38471namespace {
472
473// Only COMPONENT hosted apps may call extension APIs, and they are limited
474// to just the permissions they explicitly request. They should not have access
475// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
476// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11477// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
478// it altogether).
[email protected]f33542112013-02-04 16:52:38479bool AllowHostedAppAPICall(const Extension& extension,
480 const GURL& source_url,
481 const std::string& function_name) {
482 if (extension.location() != extensions::Manifest::COMPONENT)
483 return false;
484
485 if (!extension.web_extent().MatchesURL(source_url))
486 return false;
487
[email protected]b5b26b72013-08-02 00:25:11488 Feature::Availability availability =
489 ExtensionAPI::GetSharedInstance()->IsAvailable(
490 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
491 source_url);
492 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38493}
494
495} // namespace
496
497
[email protected]d2fe22ff2012-10-03 00:40:07498// static
[email protected]c357acb42011-06-09 20:52:42499ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
500 const ExtensionHostMsg_Request_Params& params,
501 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46502 int requesting_process_id,
503 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11504 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18505 void* profile,
[email protected]35548ab2013-05-15 08:59:47506 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42507 if (!extension) {
[email protected]6f371442011-11-09 06:45:46508 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47509 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46510 return NULL;
511 }
512
[email protected]f33542112013-02-04 16:52:38513 // Most hosted apps can't call APIs.
514 bool allowed = true;
515 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47516 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38517
518 // Privileged APIs can only be called from the process the extension
519 // is running in.
520 if (allowed && api->IsPrivileged(params.name))
521 allowed = process_map.Contains(extension->id(), requesting_process_id);
522
523 if (!allowed) {
524 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
525 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46526 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47527 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42528 return NULL;
529 }
530
[email protected]c357acb42011-06-09 20:52:42531 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35532 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14533 if (!function) {
534 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47535 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14536 return NULL;
537 }
538
[email protected]c357acb42011-06-09 20:52:42539 function->SetArgs(&params.arguments);
540 function->set_source_url(params.source_url);
541 function->set_request_id(params.request_id);
542 function->set_has_callback(params.has_callback);
543 function->set_user_gesture(params.user_gesture);
544 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32545 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47546 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16547
[email protected]c357acb42011-06-09 20:52:42548 return function;
549}
550
551// static
[email protected]c5dbef02011-05-13 05:06:09552void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47553 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01554 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47555 callback.Run(ExtensionFunction::FAILED, empty_list,
556 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35557}