blob: 232c31c88491c4d7b6bd426186a3d39daf99fbce [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,
158 ipc_sender->peer_handle(),
159 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]bfdffe2b2009-04-24 22:05:35229void ExtensionFunctionDispatcher::GetAllFunctionNames(
230 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35231 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35232}
233
[email protected]b83e4602009-05-15 22:58:33234bool ExtensionFunctionDispatcher::OverrideFunction(
235 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35236 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
237 factory);
[email protected]b83e4602009-05-15 22:58:33238}
239
240void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35241 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33242}
243
[email protected]c357acb42011-06-09 20:52:42244// static
245void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25246 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18247 void* profile,
[email protected]c357acb42011-06-09 20:52:42248 int render_process_id,
249 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53250 int routing_id,
[email protected]c357acb42011-06-09 20:52:42251 const ExtensionHostMsg_Request_Params& params) {
252 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11253 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17254 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47255
256 ExtensionFunction::ResponseCallback callback(
257 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
258 params.request_id));
259
[email protected]6f371442011-11-09 06:45:46260 scoped_refptr<ExtensionFunction> function(
261 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11262 extension_info_map->process_map(),
263 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47264 profile, callback));
[email protected]4b64d712013-01-17 17:53:17265 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
266
[email protected]dc24976f2013-06-02 21:15:09267 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12268 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17269 params.name,
270 args.Pass(),
[email protected]78216e12013-05-17 01:11:25271 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17272 profile_cast);
[email protected]c357acb42011-06-09 20:52:42273 return;
[email protected]efd75992011-12-15 22:42:42274 }
[email protected]c357acb42011-06-09 20:52:42275
276 IOThreadExtensionFunction* function_io =
277 function->AsIOThreadExtensionFunction();
278 if (!function_io) {
279 NOTREACHED();
280 return;
281 }
[email protected]44295a12013-06-05 08:45:46282 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42283 function_io->set_extension_info_map(extension_info_map);
284 function->set_include_incognito(
285 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25286
[email protected]dc24976f2013-06-02 21:15:09287 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12288 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17289 params.name,
290 args.Pass(),
[email protected]78216e12013-05-17 01:11:25291 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17292 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07293 return;
294 }
295
[email protected]36296912012-03-20 11:08:49296 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29297 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09298 function.get(),
[email protected]85231d72012-08-31 09:45:29299 &params.arguments,
300 base::TimeTicks::Now());
301 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12302 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17303 params.name,
304 args.Pass(),
305 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25306 function->Run();
307 } else {
[email protected]e5a440c2013-06-04 21:55:12308 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17309 params.name,
310 args.Pass(),
[email protected]78216e12013-05-17 01:11:25311 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17312 profile_cast);
[email protected]85231d72012-08-31 09:45:29313 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25314 }
[email protected]c357acb42011-06-09 20:52:42315}
316
[email protected]c5dbef02011-05-13 05:06:09317ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
318 Delegate* delegate)
319 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27320 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35321}
322
[email protected]32dda362009-06-05 19:07:01323ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01324}
325
[email protected]c5dbef02011-05-13 05:06:09326void ExtensionFunctionDispatcher::Dispatch(
327 const ExtensionHostMsg_Request_Params& params,
328 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47329 UIThreadResponseCallbackWrapperMap::const_iterator
330 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
331 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
332 if (iter == ui_thread_response_callback_wrappers_.end()) {
333 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
334 render_view_host);
335 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
336 } else {
337 callback_wrapper = iter->second;
338 }
339
340 DispatchWithCallback(params, render_view_host,
341 callback_wrapper->CreateCallback(params.request_id));
342}
343
344void ExtensionFunctionDispatcher::DispatchWithCallback(
345 const ExtensionHostMsg_Request_Params& params,
346 RenderViewHost* render_view_host,
347 const ExtensionFunction::ResponseCallback& callback) {
348 // TODO(yzshen): There is some shared logic between this method and
349 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09350 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28351 ExtensionProcessManager* process_manager =
352 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46353 extensions::ProcessMap* process_map = service->process_map();
354 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09355 return;
356
[email protected]615d88f2011-12-13 01:47:44357 const Extension* extension = service->extensions()->GetByID(
358 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09359 if (!extension)
[email protected]615d88f2011-12-13 01:47:44360 extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo(
361 WebSecurityOrigin::createFromString(params.source_origin),
362 params.source_url));
[email protected]c5dbef02011-05-13 05:06:09363
[email protected]8add5412011-10-01 21:02:14364 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53365 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58366 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46367 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11368 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47369 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17370 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
371
[email protected]dc24976f2013-06-02 21:15:09372 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12373 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17374 params.name,
375 args.Pass(),
[email protected]78216e12013-05-17 01:11:25376 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17377 profile());
[email protected]f82d57b52011-04-27 19:13:17378 return;
[email protected]efd75992011-12-15 22:42:42379 }
[email protected]f82d57b52011-04-27 19:13:17380
[email protected]a2aef2e2011-05-26 22:48:12381 UIThreadExtensionFunction* function_ui =
382 function->AsUIThreadExtensionFunction();
383 if (!function_ui) {
384 NOTREACHED();
385 return;
386 }
[email protected]35548ab2013-05-15 08:59:47387 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12388 function_ui->set_dispatcher(AsWeakPtr());
389 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05390 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41391
[email protected]dc24976f2013-06-02 21:15:09392 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12393 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17394 params.name,
395 args.Pass(),
[email protected]78216e12013-05-17 01:11:25396 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17397 profile());
[email protected]d2fe22ff2012-10-03 00:40:07398 return;
399 }
400
[email protected]d13950e2009-12-04 01:43:02401 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29402 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09403 function.get(),
[email protected]85231d72012-08-31 09:45:29404 &params.arguments,
405 base::TimeTicks::Now());
406 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26407 // See crbug.com/39178.
408 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]e5a440c2013-06-04 21:55:12409 LogSuccess(extension->id(), params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02410 function->Run();
411 } else {
[email protected]e5a440c2013-06-04 21:55:12412 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17413 params.name,
414 args.Pass(),
[email protected]78216e12013-05-17 01:11:25415 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17416 profile());
[email protected]85231d72012-08-31 09:45:29417 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02418 }
[email protected]720ad1312012-02-27 23:07:36419
[email protected]efb4b082012-10-17 22:28:28420 // Note: do not access |this| after this point. We may have been deleted
421 // if function->Run() ended up closing the tab that owns us.
422
[email protected]5734e882012-05-04 22:17:56423 // Check if extension was uninstalled by management.uninstall.
424 if (!service->extensions()->GetByID(params.extension_id))
425 return;
426
[email protected]720ad1312012-02-27 23:07:36427 // We only adjust the keepalive count for UIThreadExtensionFunction for
428 // now, largely for simplicity's sake. This is OK because currently, only
429 // the webRequest API uses IOThreadExtensionFunction, and that API is not
430 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28431 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36432}
433
434void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
435 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03436 extensions::ExtensionSystem::Get(profile())->process_manager()->
437 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35438}
439
[email protected]c357acb42011-06-09 20:52:42440// static
[email protected]d2fe22ff2012-10-03 00:40:07441bool ExtensionFunctionDispatcher::CheckPermissions(
442 ExtensionFunction* function,
443 const Extension* extension,
444 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47445 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07446 if (!function->HasPermission()) {
447 LOG(ERROR) << "Extension " << extension->id() << " does not have "
448 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47449 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07450 return false;
451 }
452 return true;
453}
454
[email protected]f33542112013-02-04 16:52:38455namespace {
456
457// Only COMPONENT hosted apps may call extension APIs, and they are limited
458// to just the permissions they explicitly request. They should not have access
459// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
460// are available without permission.
461// TODO(asargent/kalman) - get rid of this when the features system can express
462// the "non permission" permissions.
463bool AllowHostedAppAPICall(const Extension& extension,
464 const GURL& source_url,
465 const std::string& function_name) {
466 if (extension.location() != extensions::Manifest::COMPONENT)
467 return false;
468
469 if (!extension.web_extent().MatchesURL(source_url))
470 return false;
471
472 // We just allow the hosted app's explicit permissions, plus chrome.test.
473 scoped_refptr<const extensions::PermissionSet> permissions =
474 extension.GetActivePermissions();
475 return (permissions->HasAccessToFunction(function_name, false) ||
476 StartsWithASCII(function_name, "test.", true /*case_sensitive*/));
477}
478
479} // namespace
480
481
[email protected]d2fe22ff2012-10-03 00:40:07482// static
[email protected]c357acb42011-06-09 20:52:42483ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
484 const ExtensionHostMsg_Request_Params& params,
485 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46486 int requesting_process_id,
487 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11488 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18489 void* profile,
[email protected]35548ab2013-05-15 08:59:47490 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42491 if (!extension) {
[email protected]6f371442011-11-09 06:45:46492 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47493 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46494 return NULL;
495 }
496
[email protected]f33542112013-02-04 16:52:38497 // Most hosted apps can't call APIs.
498 bool allowed = true;
499 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47500 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38501
502 // Privileged APIs can only be called from the process the extension
503 // is running in.
504 if (allowed && api->IsPrivileged(params.name))
505 allowed = process_map.Contains(extension->id(), requesting_process_id);
506
507 if (!allowed) {
508 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
509 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46510 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47511 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42512 return NULL;
513 }
514
[email protected]c357acb42011-06-09 20:52:42515 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35516 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14517 if (!function) {
518 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47519 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14520 return NULL;
521 }
522
[email protected]c357acb42011-06-09 20:52:42523 function->SetArgs(&params.arguments);
524 function->set_source_url(params.source_url);
525 function->set_request_id(params.request_id);
526 function->set_has_callback(params.has_callback);
527 function->set_user_gesture(params.user_gesture);
528 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32529 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47530 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16531
[email protected]c357acb42011-06-09 20:52:42532 return function;
533}
534
535// static
[email protected]c5dbef02011-05-13 05:06:09536void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47537 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01538 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47539 callback.Run(ExtensionFunction::FAILED, empty_list,
540 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35541}