blob: 110965296635d5abc30688bcbf1c6b8ddf17bc90 [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]17263b22013-05-16 03:29:2215#include "chrome/browser/extensions/activity_log/activity_log.h"
[email protected]78216e12013-05-17 01:11:2516#include "chrome/browser/extensions/activity_log/blocked_actions.h"
[email protected]ae33d322012-03-19 22:24:3517#include "chrome/browser/extensions/extension_function_registry.h"
[email protected]2f69b382011-02-19 00:34:2518#include "chrome/browser/extensions/extension_service.h"
[email protected]efb4b082012-10-17 22:28:2819#include "chrome/browser/extensions/extension_system.h"
[email protected]8f9d4eb2011-02-05 01:39:1020#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]d13950e2009-12-04 01:43:0221#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]83820d42011-11-12 22:03:1122#include "chrome/browser/extensions/process_map.h"
[email protected]ed2b1002011-05-25 14:12:1023#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3324#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4225#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]83820d42011-11-12 22:03:1126#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2327#include "chrome/common/extensions/extension_messages.h"
[email protected]615d88f2011-12-13 01:47:4428#include "chrome/common/extensions/extension_set.h"
[email protected]9c45b7182009-08-04 16:44:4329#include "chrome/common/url_constants.h"
[email protected]4b64d712013-01-17 17:53:1730#include "content/public/browser/browser_thread.h"
[email protected]c333e792012-01-06 16:57:3931#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3332#include "content/public/browser/render_view_host.h"
[email protected]35548ab2013-05-15 08:59:4733#include "content/public/browser/render_view_host_observer.h"
34#include "content/public/browser/user_metrics.h"
35#include "content/public/common/result_codes.h"
[email protected]f82d57b52011-04-27 19:13:1736#include "ipc/ipc_message.h"
37#include "ipc/ipc_message_macros.h"
[email protected]d0fcff72013-07-23 02:45:4338#include "webkit/common/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1039
[email protected]1c321ee2012-05-21 03:02:3440using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1141using extensions::ExtensionAPI;
[email protected]eaabba22012-03-07 15:02:1142using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1143
[email protected]5bc248a2012-04-04 23:38:1144namespace {
45
[email protected]e5a440c2013-06-04 21:55:1246void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1747 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4148 scoped_ptr<base::ListValue> args,
[email protected]4b64d712013-01-17 17:53:1749 Profile* profile) {
50 // The ActivityLog can only be accessed from the main (UI) thread. If we're
51 // running on the wrong thread, re-dispatch from the main thread.
52 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
53 BrowserThread::PostTask(BrowserThread::UI,
54 FROM_HERE,
55 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1256 extension_id,
[email protected]4b64d712013-01-17 17:53:1757 api_name,
[email protected]c02087b512013-02-04 03:09:2058 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1759 profile));
60 } else {
61 extensions::ActivityLog* activity_log =
62 extensions::ActivityLog::GetInstance(profile);
[email protected]e5a440c2013-06-04 21:55:1263 activity_log->LogAPIAction(
64 extension_id, api_name, args.get(), std::string());
[email protected]efd75992011-12-15 22:42:4265 }
66}
67
[email protected]e5a440c2013-06-04 21:55:1268void LogFailure(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1769 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4170 scoped_ptr<base::ListValue> args,
[email protected]78216e12013-05-17 01:11:2571 extensions::BlockedAction::Reason reason,
[email protected]4b64d712013-01-17 17:53:1772 Profile* profile) {
73 // The ActivityLog can only be accessed from the main (UI) thread. If we're
74 // running on the wrong thread, re-dispatch from the main thread.
75 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
76 BrowserThread::PostTask(BrowserThread::UI,
77 FROM_HERE,
78 base::Bind(&LogFailure,
[email protected]e5a440c2013-06-04 21:55:1279 extension_id,
[email protected]4b64d712013-01-17 17:53:1780 api_name,
[email protected]c02087b512013-02-04 03:09:2081 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1782 reason,
83 profile));
84 } else {
85 extensions::ActivityLog* activity_log =
86 extensions::ActivityLog::GetInstance(profile);
[email protected]007b3f82013-04-09 08:46:4587 activity_log->LogBlockedAction(
[email protected]e5a440c2013-06-04 21:55:1288 extension_id, api_name, args.get(), reason, std::string());
[email protected]efd75992011-12-15 22:42:4289 }
90}
91
[email protected]4b64d712013-01-17 17:53:1792
[email protected]5bc248a2012-04-04 23:38:1193// Separate copy of ExtensionAPI used for IO thread extension functions. We need
94// this because ExtensionAPI has mutable data. It should be possible to remove
95// this once all the extension APIs are updated to the feature system.
96struct Static {
97 Static()
98 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
99 }
100 scoped_ptr<extensions::ExtensionAPI> api;
101};
102base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
103
[email protected]35548ab2013-05-15 08:59:47104// Kills the specified process because it sends us a malformed message.
105void KillBadMessageSender(base::ProcessHandle process) {
106 NOTREACHED();
107 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
108 if (process)
109 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
110}
111
112void CommonResponseCallback(IPC::Sender* ipc_sender,
113 int routing_id,
114 base::ProcessHandle peer_process,
115 int request_id,
116 ExtensionFunction::ResponseType type,
117 const base::ListValue& results,
118 const std::string& error) {
119 DCHECK(ipc_sender);
120
121 if (type == ExtensionFunction::BAD_MESSAGE) {
122 // The renderer has done validation before sending extension api requests.
123 // Therefore, we should never receive a request that is invalid in a way
124 // that JSON validation in the renderer should have caught. It could be an
125 // attacker trying to exploit the browser, so we crash the renderer instead.
126 LOG(ERROR) <<
127 "Terminating renderer because of malformed extension message.";
128 if (content::RenderProcessHost::run_renderer_in_process()) {
129 // In single process mode it is better if we don't suicide but just crash.
130 CHECK(false);
131 } else {
132 KillBadMessageSender(peer_process);
133 }
134
135 return;
136 }
137
138 ipc_sender->Send(new ExtensionMsg_Response(
139 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
140 error));
141}
142
143void IOThreadResponseCallback(
144 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
145 int routing_id,
146 int request_id,
147 ExtensionFunction::ResponseType type,
148 const base::ListValue& results,
149 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45150 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47151 return;
152
[email protected]e8dad9b2013-06-04 04:43:45153 CommonResponseCallback(ipc_sender.get(),
154 routing_id,
[email protected]950be552013-07-10 19:13:02155 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45156 request_id,
157 type,
158 results,
159 error);
[email protected]35548ab2013-05-15 08:59:47160}
161
[email protected]5bc248a2012-04-04 23:38:11162} // namespace
163
[email protected]35548ab2013-05-15 08:59:47164class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
165 : public content::RenderViewHostObserver {
166 public:
167 UIThreadResponseCallbackWrapper(
168 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
169 RenderViewHost* render_view_host)
170 : content::RenderViewHostObserver(render_view_host),
171 dispatcher_(dispatcher),
172 weak_ptr_factory_(this) {
173 }
174
175 virtual ~UIThreadResponseCallbackWrapper() {
176 }
177
178 // content::RenderViewHostObserver overrides.
179 virtual void RenderViewHostDestroyed(
180 RenderViewHost* render_view_host) OVERRIDE {
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]e8dad9b2013-06-04 04:43:45182 if (dispatcher_.get()) {
183 dispatcher_->ui_thread_response_callback_wrappers_
184 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47185 }
186
187 // This call will delete |this|.
188 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
189 }
190
191 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
192 return base::Bind(
193 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
194 weak_ptr_factory_.GetWeakPtr(),
195 request_id);
196 }
197
198 private:
199 void OnExtensionFunctionCompleted(int request_id,
200 ExtensionFunction::ResponseType type,
201 const base::ListValue& results,
202 const std::string& error) {
203 CommonResponseCallback(
204 render_view_host(), render_view_host()->GetRoutingID(),
205 render_view_host()->GetProcess()->GetHandle(), request_id, type,
206 results, error);
207 }
208
209 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
210 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
211
212 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
213};
214
[email protected]44f4b132012-07-17 20:36:57215extensions::WindowController*
216ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08217 const {
218 return NULL;
219}
220
221content::WebContents*
[email protected]44f4b132012-07-17 20:36:57222ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08223 return NULL;
224}
[email protected]5bc248a2012-04-04 23:38:11225
[email protected]1ce88e82013-06-28 05:17:10226content::WebContents*
227ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
228 return GetAssociatedWebContents();
229}
230
[email protected]bfdffe2b2009-04-24 22:05:35231void ExtensionFunctionDispatcher::GetAllFunctionNames(
232 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35233 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35234}
235
[email protected]b83e4602009-05-15 22:58:33236bool ExtensionFunctionDispatcher::OverrideFunction(
237 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35238 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
239 factory);
[email protected]b83e4602009-05-15 22:58:33240}
241
242void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35243 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33244}
245
[email protected]c357acb42011-06-09 20:52:42246// static
247void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25248 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18249 void* profile,
[email protected]c357acb42011-06-09 20:52:42250 int render_process_id,
251 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53252 int routing_id,
[email protected]c357acb42011-06-09 20:52:42253 const ExtensionHostMsg_Request_Params& params) {
254 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11255 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17256 Profile* profile_cast = static_cast<Profile*>(profile);
[email protected]35548ab2013-05-15 08:59:47257
258 ExtensionFunction::ResponseCallback callback(
259 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
260 params.request_id));
261
[email protected]6f371442011-11-09 06:45:46262 scoped_refptr<ExtensionFunction> function(
263 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11264 extension_info_map->process_map(),
265 g_global_io_data.Get().api.get(),
[email protected]35548ab2013-05-15 08:59:47266 profile, callback));
[email protected]4b64d712013-01-17 17:53:17267 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
268
[email protected]dc24976f2013-06-02 21:15:09269 if (!function.get()) {
[email protected]e5a440c2013-06-04 21:55:12270 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17271 params.name,
272 args.Pass(),
[email protected]78216e12013-05-17 01:11:25273 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17274 profile_cast);
[email protected]c357acb42011-06-09 20:52:42275 return;
[email protected]efd75992011-12-15 22:42:42276 }
[email protected]c357acb42011-06-09 20:52:42277
278 IOThreadExtensionFunction* function_io =
279 function->AsIOThreadExtensionFunction();
280 if (!function_io) {
281 NOTREACHED();
282 return;
283 }
[email protected]44295a12013-06-05 08:45:46284 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42285 function_io->set_extension_info_map(extension_info_map);
286 function->set_include_incognito(
287 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25288
[email protected]dc24976f2013-06-02 21:15:09289 if (!CheckPermissions(function.get(), extension, params, callback)) {
[email protected]e5a440c2013-06-04 21:55:12290 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17291 params.name,
292 args.Pass(),
[email protected]78216e12013-05-17 01:11:25293 extensions::BlockedAction::ACCESS_DENIED,
[email protected]4b64d712013-01-17 17:53:17294 profile_cast);
[email protected]d2fe22ff2012-10-03 00:40:07295 return;
296 }
297
[email protected]36296912012-03-20 11:08:49298 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29299 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09300 function.get(),
[email protected]85231d72012-08-31 09:45:29301 &params.arguments,
302 base::TimeTicks::Now());
303 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12304 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17305 params.name,
306 args.Pass(),
307 profile_cast);
[email protected]fd50e7b2011-11-03 09:20:25308 function->Run();
309 } else {
[email protected]e5a440c2013-06-04 21:55:12310 LogFailure(extension->id(),
[email protected]4b64d712013-01-17 17:53:17311 params.name,
312 args.Pass(),
[email protected]78216e12013-05-17 01:11:25313 extensions::BlockedAction::QUOTA_EXCEEDED,
[email protected]4b64d712013-01-17 17:53:17314 profile_cast);
[email protected]85231d72012-08-31 09:45:29315 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25316 }
[email protected]c357acb42011-06-09 20:52:42317}
318
[email protected]c5dbef02011-05-13 05:06:09319ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
320 Delegate* delegate)
321 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27322 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35323}
324
[email protected]32dda362009-06-05 19:07:01325ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01326}
327
[email protected]c5dbef02011-05-13 05:06:09328void ExtensionFunctionDispatcher::Dispatch(
329 const ExtensionHostMsg_Request_Params& params,
330 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47331 UIThreadResponseCallbackWrapperMap::const_iterator
332 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
333 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
334 if (iter == ui_thread_response_callback_wrappers_.end()) {
335 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
336 render_view_host);
337 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
338 } else {
339 callback_wrapper = iter->second;
340 }
341
342 DispatchWithCallback(params, render_view_host,
343 callback_wrapper->CreateCallback(params.request_id));
344}
345
346void ExtensionFunctionDispatcher::DispatchWithCallback(
347 const ExtensionHostMsg_Request_Params& params,
348 RenderViewHost* render_view_host,
349 const ExtensionFunction::ResponseCallback& callback) {
350 // TODO(yzshen): There is some shared logic between this method and
351 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09352 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28353 ExtensionProcessManager* process_manager =
354 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46355 extensions::ProcessMap* process_map = service->process_map();
356 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09357 return;
358
[email protected]615d88f2011-12-13 01:47:44359 const Extension* extension = service->extensions()->GetByID(
360 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09361 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55362 extension = service->extensions()->GetHostedAppByURL(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}