blob: 5e0cffe2b8dd9d586070266c036c979a2a73a5f7 [file] [log] [blame]
[email protected]0b9de032014-03-15 05:47:011// Copyright 2014 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
[email protected]0b9de032014-03-15 05:47:015#include "extensions/browser/extension_function_dispatcher.h"
[email protected]bfdffe2b2009-04-24 22:05:356
[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]4b64d712013-01-17 17:53:1715#include "content/public/browser/browser_thread.h"
[email protected]6dd625e2013-12-20 17:03:0716#include "content/public/browser/render_frame_host.h"
[email protected]c333e792012-01-06 16:57:3917#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3318#include "content/public/browser/render_view_host.h"
[email protected]d6ec84a2013-11-01 13:07:3819#include "content/public/browser/user_metrics.h"
[email protected]ab2a7e3c2013-10-22 03:41:3620#include "content/public/browser/web_contents.h"
21#include "content/public/browser/web_contents_observer.h"
[email protected]35548ab2013-05-15 08:59:4722#include "content/public/common/result_codes.h"
[email protected]b32260f2014-02-06 10:03:4123#include "extensions/browser/api_activity_monitor.h"
[email protected]21c6c432014-03-05 18:47:3124#include "extensions/browser/extension_function_registry.h"
[email protected]1a0436892014-04-01 00:38:2525#include "extensions/browser/extension_message_filter.h"
[email protected]aab23102014-02-05 18:57:5526#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2427#include "extensions/browser/extension_system.h"
[email protected]b32260f2014-02-06 10:03:4128#include "extensions/browser/extensions_browser_client.h"
[email protected]aab23102014-02-05 18:57:5529#include "extensions/browser/process_manager.h"
[email protected]50de9aa22013-11-14 06:30:3430#include "extensions/browser/process_map.h"
[email protected]38427a12013-11-09 17:34:2031#include "extensions/browser/quota_service.h"
[email protected]d6ec84a2013-11-01 13:07:3832#include "extensions/common/extension_api.h"
[email protected]fb820c02014-03-13 15:07:0833#include "extensions/common/extension_messages.h"
[email protected]289c44b2013-12-17 03:26:5734#include "extensions/common/extension_set.h"
[email protected]f82d57b52011-04-27 19:13:1735#include "ipc/ipc_message.h"
36#include "ipc/ipc_message_macros.h"
[email protected]61b55b62011-03-24 09:03:1037
[email protected]b32260f2014-02-06 10:03:4138using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1139using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1140
[email protected]1a0436892014-04-01 00:38:2541namespace extensions {
[email protected]5bc248a2012-04-04 23:38:1142namespace {
43
[email protected]b32260f2014-02-06 10:03:4144// Notifies the ApiActivityMonitor that an extension API function has been
45// called. May be called from any thread.
46void NotifyApiFunctionCalled(const std::string& extension_id,
47 const std::string& api_name,
48 scoped_ptr<base::ListValue> args,
49 content::BrowserContext* browser_context) {
[email protected]eafbd05a2014-02-06 19:29:0150 // The ApiActivityMonitor can only be accessed from the main (UI) thread. If
[email protected]b32260f2014-02-06 10:03:4151 // we're running on the wrong thread, re-dispatch from the main thread.
[email protected]4b64d712013-01-17 17:53:1752 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
53 BrowserThread::PostTask(BrowserThread::UI,
54 FROM_HERE,
[email protected]b32260f2014-02-06 10:03:4155 base::Bind(&NotifyApiFunctionCalled,
[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]86376022013-12-03 18:18:0559 browser_context));
[email protected]b32260f2014-02-06 10:03:4160 return;
[email protected]efd75992011-12-15 22:42:4261 }
[email protected]eafbd05a2014-02-06 19:29:0162 // The BrowserContext may become invalid after the task above is posted.
63 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
64 return;
65
[email protected]1a0436892014-04-01 00:38:2566 ApiActivityMonitor* monitor =
[email protected]eafbd05a2014-02-06 19:29:0167 ExtensionsBrowserClient::Get()->GetApiActivityMonitor(browser_context);
[email protected]b32260f2014-02-06 10:03:4168 if (monitor)
69 monitor->OnApiFunctionCalled(extension_id, api_name, args.Pass());
[email protected]efd75992011-12-15 22:42:4270}
71
[email protected]5bc248a2012-04-04 23:38:1172// Separate copy of ExtensionAPI used for IO thread extension functions. We need
73// this because ExtensionAPI has mutable data. It should be possible to remove
74// this once all the extension APIs are updated to the feature system.
75struct Static {
[email protected]1a0436892014-04-01 00:38:2576 Static() : api(ExtensionAPI::CreateWithDefaultConfiguration()) {}
77 scoped_ptr<ExtensionAPI> api;
[email protected]5bc248a2012-04-04 23:38:1178};
79base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
80
[email protected]35548ab2013-05-15 08:59:4781// Kills the specified process because it sends us a malformed message.
82void KillBadMessageSender(base::ProcessHandle process) {
83 NOTREACHED();
[email protected]e6e30ac2014-01-13 21:24:3984 content::RecordAction(base::UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]35548ab2013-05-15 08:59:4785 if (process)
86 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
87}
88
89void CommonResponseCallback(IPC::Sender* ipc_sender,
90 int routing_id,
91 base::ProcessHandle peer_process,
92 int request_id,
93 ExtensionFunction::ResponseType type,
94 const base::ListValue& results,
95 const std::string& error) {
96 DCHECK(ipc_sender);
97
98 if (type == ExtensionFunction::BAD_MESSAGE) {
99 // The renderer has done validation before sending extension api requests.
100 // Therefore, we should never receive a request that is invalid in a way
101 // that JSON validation in the renderer should have caught. It could be an
102 // attacker trying to exploit the browser, so we crash the renderer instead.
103 LOG(ERROR) <<
104 "Terminating renderer because of malformed extension message.";
105 if (content::RenderProcessHost::run_renderer_in_process()) {
106 // In single process mode it is better if we don't suicide but just crash.
107 CHECK(false);
108 } else {
109 KillBadMessageSender(peer_process);
110 }
111
112 return;
113 }
114
115 ipc_sender->Send(new ExtensionMsg_Response(
116 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
117 error));
118}
119
120void IOThreadResponseCallback(
[email protected]1a0436892014-04-01 00:38:25121 const base::WeakPtr<ExtensionMessageFilter>& ipc_sender,
[email protected]35548ab2013-05-15 08:59:47122 int routing_id,
123 int request_id,
124 ExtensionFunction::ResponseType type,
125 const base::ListValue& results,
126 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45127 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47128 return;
129
[email protected]e8dad9b2013-06-04 04:43:45130 CommonResponseCallback(ipc_sender.get(),
131 routing_id,
[email protected]950be552013-07-10 19:13:02132 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45133 request_id,
134 type,
135 results,
136 error);
[email protected]35548ab2013-05-15 08:59:47137}
138
[email protected]5bc248a2012-04-04 23:38:11139} // namespace
140
[email protected]35548ab2013-05-15 08:59:47141class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
[email protected]ab2a7e3c2013-10-22 03:41:36142 : public content::WebContentsObserver {
[email protected]35548ab2013-05-15 08:59:47143 public:
144 UIThreadResponseCallbackWrapper(
145 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
146 RenderViewHost* render_view_host)
[email protected]ab2a7e3c2013-10-22 03:41:36147 : content::WebContentsObserver(
148 content::WebContents::FromRenderViewHost(render_view_host)),
[email protected]35548ab2013-05-15 08:59:47149 dispatcher_(dispatcher),
[email protected]ab2a7e3c2013-10-22 03:41:36150 render_view_host_(render_view_host),
[email protected]35548ab2013-05-15 08:59:47151 weak_ptr_factory_(this) {
152 }
153
154 virtual ~UIThreadResponseCallbackWrapper() {
155 }
156
[email protected]ab2a7e3c2013-10-22 03:41:36157 // content::WebContentsObserver overrides.
158 virtual void RenderViewDeleted(
[email protected]35548ab2013-05-15 08:59:47159 RenderViewHost* render_view_host) OVERRIDE {
[email protected]54ee8192014-03-29 17:37:24160 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]ab2a7e3c2013-10-22 03:41:36161 if (render_view_host != render_view_host_)
162 return;
163
[email protected]e8dad9b2013-06-04 04:43:45164 if (dispatcher_.get()) {
165 dispatcher_->ui_thread_response_callback_wrappers_
166 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47167 }
168
[email protected]ab2a7e3c2013-10-22 03:41:36169 delete this;
[email protected]35548ab2013-05-15 08:59:47170 }
171
172 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
173 return base::Bind(
174 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
175 weak_ptr_factory_.GetWeakPtr(),
176 request_id);
177 }
178
179 private:
180 void OnExtensionFunctionCompleted(int request_id,
181 ExtensionFunction::ResponseType type,
182 const base::ListValue& results,
183 const std::string& error) {
184 CommonResponseCallback(
[email protected]ab2a7e3c2013-10-22 03:41:36185 render_view_host_, render_view_host_->GetRoutingID(),
186 render_view_host_->GetProcess()->GetHandle(), request_id, type,
[email protected]35548ab2013-05-15 08:59:47187 results, error);
188 }
189
190 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
[email protected]ab2a7e3c2013-10-22 03:41:36191 content::RenderViewHost* render_view_host_;
[email protected]35548ab2013-05-15 08:59:47192 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
193
194 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
195};
196
[email protected]1a0436892014-04-01 00:38:25197WindowController*
198ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const {
[email protected]d72d3a62012-05-10 03:45:08199 return NULL;
200}
201
202content::WebContents*
[email protected]44f4b132012-07-17 20:36:57203ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08204 return NULL;
205}
[email protected]5bc248a2012-04-04 23:38:11206
[email protected]1ce88e82013-06-28 05:17:10207content::WebContents*
208ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
209 return GetAssociatedWebContents();
210}
211
[email protected]bfdffe2b2009-04-24 22:05:35212void ExtensionFunctionDispatcher::GetAllFunctionNames(
213 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35214 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35215}
216
[email protected]b83e4602009-05-15 22:58:33217bool ExtensionFunctionDispatcher::OverrideFunction(
218 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35219 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
220 factory);
[email protected]b83e4602009-05-15 22:58:33221}
222
[email protected]c357acb42011-06-09 20:52:42223// static
224void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]1a0436892014-04-01 00:38:25225 InfoMap* extension_info_map,
226 void* profile_id,
[email protected]c357acb42011-06-09 20:52:42227 int render_process_id,
[email protected]1a0436892014-04-01 00:38:25228 base::WeakPtr<ExtensionMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53229 int routing_id,
[email protected]c357acb42011-06-09 20:52:42230 const ExtensionHostMsg_Request_Params& params) {
231 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11232 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]35548ab2013-05-15 08:59:47233
234 ExtensionFunction::ResponseCallback callback(
235 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
236 params.request_id));
237
[email protected]6f371442011-11-09 06:45:46238 scoped_refptr<ExtensionFunction> function(
[email protected]1a0436892014-04-01 00:38:25239 CreateExtensionFunction(params,
240 extension,
241 render_process_id,
[email protected]5bc248a2012-04-04 23:38:11242 extension_info_map->process_map(),
243 g_global_io_data.Get().api.get(),
[email protected]1a0436892014-04-01 00:38:25244 profile_id,
245 callback));
[email protected]ecc854a2013-08-22 10:12:42246 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42247 return;
248
249 IOThreadExtensionFunction* function_io =
250 function->AsIOThreadExtensionFunction();
251 if (!function_io) {
252 NOTREACHED();
253 return;
254 }
[email protected]44295a12013-06-05 08:45:46255 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42256 function_io->set_extension_info_map(extension_info_map);
257 function->set_include_incognito(
258 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25259
[email protected]ecc854a2013-08-22 10:12:42260 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07261 return;
[email protected]d2fe22ff2012-10-03 00:40:07262
[email protected]1a0436892014-04-01 00:38:25263 QuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29264 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09265 function.get(),
[email protected]85231d72012-08-31 09:45:29266 &params.arguments,
267 base::TimeTicks::Now());
268 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53269 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
[email protected]1a0436892014-04-01 00:38:25270 NotifyApiFunctionCalled(extension->id(),
271 params.name,
272 args.Pass(),
273 static_cast<content::BrowserContext*>(profile_id));
[email protected]fd50e7b2011-11-03 09:20:25274 function->Run();
275 } else {
[email protected]85231d72012-08-31 09:45:29276 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25277 }
[email protected]c357acb42011-06-09 20:52:42278}
279
[email protected]96e6a1032013-11-28 06:58:03280ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
281 content::BrowserContext* browser_context,
282 Delegate* delegate)
[email protected]86376022013-12-03 18:18:05283 : browser_context_(browser_context),
[email protected]96e6a1032013-11-28 06:58:03284 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35285}
286
[email protected]32dda362009-06-05 19:07:01287ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01288}
289
[email protected]c5dbef02011-05-13 05:06:09290void ExtensionFunctionDispatcher::Dispatch(
291 const ExtensionHostMsg_Request_Params& params,
292 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47293 UIThreadResponseCallbackWrapperMap::const_iterator
294 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
295 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
296 if (iter == ui_thread_response_callback_wrappers_.end()) {
297 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
298 render_view_host);
299 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
300 } else {
301 callback_wrapper = iter->second;
302 }
303
[email protected]6dd625e2013-12-20 17:03:07304 DispatchWithCallbackInternal(
305 params, render_view_host, NULL,
306 callback_wrapper->CreateCallback(params.request_id));
[email protected]35548ab2013-05-15 08:59:47307}
308
309void ExtensionFunctionDispatcher::DispatchWithCallback(
310 const ExtensionHostMsg_Request_Params& params,
[email protected]6dd625e2013-12-20 17:03:07311 content::RenderFrameHost* render_frame_host,
[email protected]35548ab2013-05-15 08:59:47312 const ExtensionFunction::ResponseCallback& callback) {
[email protected]6dd625e2013-12-20 17:03:07313 DispatchWithCallbackInternal(params, NULL, render_frame_host, callback);
314}
315
316void ExtensionFunctionDispatcher::DispatchWithCallbackInternal(
317 const ExtensionHostMsg_Request_Params& params,
318 RenderViewHost* render_view_host,
319 content::RenderFrameHost* render_frame_host,
320 const ExtensionFunction::ResponseCallback& callback) {
321 DCHECK(render_view_host || render_frame_host);
[email protected]35548ab2013-05-15 08:59:47322 // TODO(yzshen): There is some shared logic between this method and
323 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]1a0436892014-04-01 00:38:25324 ProcessMap* process_map = ProcessMap::Get(browser_context_);
[email protected]86376022013-12-03 18:18:05325 if (!process_map)
[email protected]c5dbef02011-05-13 05:06:09326 return;
327
[email protected]1a0436892014-04-01 00:38:25328 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
[email protected]aab23102014-02-05 18:57:55329 const Extension* extension = registry->enabled_extensions().GetByID(
[email protected]615d88f2011-12-13 01:47:44330 params.extension_id);
[email protected]aab23102014-02-05 18:57:55331 if (!extension) {
332 extension =
333 registry->enabled_extensions().GetHostedAppByURL(params.source_url);
334 }
[email protected]c5dbef02011-05-13 05:06:09335
[email protected]6dd625e2013-12-20 17:03:07336 int process_id = render_view_host ? render_view_host->GetProcess()->GetID() :
337 render_frame_host->GetProcess()->GetID();
[email protected]8add5412011-10-01 21:02:14338 scoped_refptr<ExtensionFunction> function(
[email protected]86376022013-12-03 18:18:05339 CreateExtensionFunction(params,
340 extension,
[email protected]6dd625e2013-12-20 17:03:07341 process_id,
[email protected]86376022013-12-03 18:18:05342 *process_map,
[email protected]1a0436892014-04-01 00:38:25343 ExtensionAPI::GetSharedInstance(),
[email protected]86376022013-12-03 18:18:05344 browser_context_,
345 callback));
[email protected]ecc854a2013-08-22 10:12:42346 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17347 return;
[email protected]f82d57b52011-04-27 19:13:17348
[email protected]a2aef2e2011-05-26 22:48:12349 UIThreadExtensionFunction* function_ui =
350 function->AsUIThreadExtensionFunction();
351 if (!function_ui) {
352 NOTREACHED();
353 return;
354 }
[email protected]6dd625e2013-12-20 17:03:07355 if (render_view_host) {
356 function_ui->SetRenderViewHost(render_view_host);
357 } else {
358 function_ui->SetRenderFrameHost(render_frame_host);
359 }
[email protected]a2aef2e2011-05-26 22:48:12360 function_ui->set_dispatcher(AsWeakPtr());
[email protected]659be682014-02-28 15:06:45361 function_ui->set_browser_context(browser_context_);
[email protected]1d5cf4142014-01-24 18:25:22362 function->set_include_incognito(
[email protected]944ad022014-02-07 23:00:23363 ExtensionsBrowserClient::Get()->CanExtensionCrossIncognito(
364 extension, browser_context_));
[email protected]cb0ce1e022010-03-10 19:54:41365
[email protected]ecc854a2013-08-22 10:12:42366 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07367 return;
[email protected]d2fe22ff2012-10-03 00:40:07368
[email protected]aab23102014-02-05 18:57:55369 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context_);
[email protected]1a0436892014-04-01 00:38:25370 QuotaService* quota = extension_system->quota_service();
[email protected]85231d72012-08-31 09:45:29371 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09372 function.get(),
[email protected]85231d72012-08-31 09:45:29373 &params.arguments,
374 base::TimeTicks::Now());
375 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53376 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
377
[email protected]d070ec62010-07-27 21:28:26378 // See crbug.com/39178.
[email protected]0b9de032014-03-15 05:47:01379 ExtensionsBrowserClient::Get()->PermitExternalProtocolHandler();
[email protected]b32260f2014-02-06 10:03:41380 NotifyApiFunctionCalled(
381 extension->id(), params.name, args.Pass(), browser_context_);
[email protected]d13950e2009-12-04 01:43:02382 function->Run();
383 } else {
[email protected]85231d72012-08-31 09:45:29384 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02385 }
[email protected]720ad1312012-02-27 23:07:36386
[email protected]efb4b082012-10-17 22:28:28387 // Note: do not access |this| after this point. We may have been deleted
388 // if function->Run() ended up closing the tab that owns us.
389
[email protected]5734e882012-05-04 22:17:56390 // Check if extension was uninstalled by management.uninstall.
[email protected]aab23102014-02-05 18:57:55391 if (!registry->enabled_extensions().GetByID(params.extension_id))
[email protected]5734e882012-05-04 22:17:56392 return;
393
[email protected]720ad1312012-02-27 23:07:36394 // We only adjust the keepalive count for UIThreadExtensionFunction for
395 // now, largely for simplicity's sake. This is OK because currently, only
396 // the webRequest API uses IOThreadExtensionFunction, and that API is not
397 // compatible with lazy background pages.
[email protected]86376022013-12-03 18:18:05398 extension_system->process_manager()->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36399}
400
401void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
402 const Extension* extension) {
[email protected]59b0e602014-01-30 00:41:24403 ExtensionSystem::Get(browser_context_)->process_manager()->
[email protected]be93bba02012-10-24 16:44:03404 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35405}
406
[email protected]c357acb42011-06-09 20:52:42407// static
[email protected]d2fe22ff2012-10-03 00:40:07408bool ExtensionFunctionDispatcher::CheckPermissions(
409 ExtensionFunction* function,
410 const Extension* extension,
411 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47412 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07413 if (!function->HasPermission()) {
414 LOG(ERROR) << "Extension " << extension->id() << " does not have "
415 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47416 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07417 return false;
418 }
419 return true;
420}
421
[email protected]f33542112013-02-04 16:52:38422namespace {
423
424// Only COMPONENT hosted apps may call extension APIs, and they are limited
425// to just the permissions they explicitly request. They should not have access
426// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
427// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11428// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
429// it altogether).
[email protected]f33542112013-02-04 16:52:38430bool AllowHostedAppAPICall(const Extension& extension,
431 const GURL& source_url,
432 const std::string& function_name) {
[email protected]1a0436892014-04-01 00:38:25433 if (extension.location() != Manifest::COMPONENT)
[email protected]f33542112013-02-04 16:52:38434 return false;
435
436 if (!extension.web_extent().MatchesURL(source_url))
437 return false;
438
[email protected]713a6e82013-12-19 23:06:05439 // Note: Not BLESSED_WEB_PAGE_CONTEXT here because these component hosted app
440 // entities have traditionally been treated as blessed extensions, for better
441 // or worse.
[email protected]b5b26b72013-08-02 00:25:11442 Feature::Availability availability =
443 ExtensionAPI::GetSharedInstance()->IsAvailable(
444 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
445 source_url);
446 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38447}
448
449} // namespace
450
451
[email protected]d2fe22ff2012-10-03 00:40:07452// static
[email protected]c357acb42011-06-09 20:52:42453ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
454 const ExtensionHostMsg_Request_Params& params,
455 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46456 int requesting_process_id,
[email protected]1a0436892014-04-01 00:38:25457 const ProcessMap& process_map,
458 ExtensionAPI* api,
459 void* profile_id,
[email protected]35548ab2013-05-15 08:59:47460 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42461 if (!extension) {
[email protected]6f371442011-11-09 06:45:46462 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47463 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46464 return NULL;
465 }
466
[email protected]f33542112013-02-04 16:52:38467 // Most hosted apps can't call APIs.
468 bool allowed = true;
469 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47470 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38471
472 // Privileged APIs can only be called from the process the extension
473 // is running in.
474 if (allowed && api->IsPrivileged(params.name))
475 allowed = process_map.Contains(extension->id(), requesting_process_id);
476
477 if (!allowed) {
478 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
479 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46480 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47481 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42482 return NULL;
483 }
484
[email protected]c357acb42011-06-09 20:52:42485 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35486 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14487 if (!function) {
488 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47489 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14490 return NULL;
491 }
492
[email protected]c357acb42011-06-09 20:52:42493 function->SetArgs(&params.arguments);
494 function->set_source_url(params.source_url);
495 function->set_request_id(params.request_id);
496 function->set_has_callback(params.has_callback);
497 function->set_user_gesture(params.user_gesture);
498 function->set_extension(extension);
[email protected]1a0436892014-04-01 00:38:25499 function->set_profile_id(profile_id);
[email protected]35548ab2013-05-15 08:59:47500 function->set_response_callback(callback);
[email protected]eb7ef5f2014-02-06 09:59:19501 function->set_source_tab_id(params.source_tab_id);
[email protected]3d0e2262012-08-02 15:32:16502
[email protected]c357acb42011-06-09 20:52:42503 return function;
504}
505
506// static
[email protected]c5dbef02011-05-13 05:06:09507void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47508 const ExtensionFunction::ResponseCallback& callback) {
[email protected]023b3d12013-12-23 18:46:49509 base::ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47510 callback.Run(ExtensionFunction::FAILED, empty_list,
511 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35512}
[email protected]1a0436892014-04-01 00:38:25513
514} // namespace extensions