blob: 1de6920401c9647ea5337fcd5a23f9096c159495 [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]a7ff4b72013-10-17 20:56:0219#include "chrome/browser/extensions/extension_util.h"
[email protected]8f9d4eb2011-02-05 01:39:1020#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]ed2b1002011-05-25 14:12:1021#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]c357acb42011-06-09 20:52:4222#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]44c49c92011-03-28 16:17:2323#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4324#include "chrome/common/url_constants.h"
[email protected]4b64d712013-01-17 17:53:1725#include "content/public/browser/browser_thread.h"
[email protected]6dd625e2013-12-20 17:03:0726#include "content/public/browser/render_frame_host.h"
[email protected]c333e792012-01-06 16:57:3927#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3328#include "content/public/browser/render_view_host.h"
[email protected]d6ec84a2013-11-01 13:07:3829#include "content/public/browser/user_metrics.h"
[email protected]ab2a7e3c2013-10-22 03:41:3630#include "content/public/browser/web_contents.h"
31#include "content/public/browser/web_contents_observer.h"
[email protected]35548ab2013-05-15 08:59:4732#include "content/public/common/result_codes.h"
[email protected]aab23102014-02-05 18:57:5533#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2434#include "extensions/browser/extension_system.h"
[email protected]aab23102014-02-05 18:57:5535#include "extensions/browser/process_manager.h"
[email protected]50de9aa22013-11-14 06:30:3436#include "extensions/browser/process_map.h"
[email protected]38427a12013-11-09 17:34:2037#include "extensions/browser/quota_service.h"
[email protected]d6ec84a2013-11-01 13:07:3838#include "extensions/common/extension_api.h"
[email protected]289c44b2013-12-17 03:26:5739#include "extensions/common/extension_set.h"
[email protected]f82d57b52011-04-27 19:13:1740#include "ipc/ipc_message.h"
41#include "ipc/ipc_message_macros.h"
[email protected]d0fcff72013-07-23 02:45:4342#include "webkit/common/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1043
[email protected]1c321ee2012-05-21 03:02:3444using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1145using extensions::ExtensionAPI;
[email protected]86376022013-12-03 18:18:0546using extensions::ExtensionSystem;
[email protected]b5b26b72013-08-02 00:25:1147using extensions::Feature;
[email protected]eaabba22012-03-07 15:02:1148using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1149
[email protected]5bc248a2012-04-04 23:38:1150namespace {
51
[email protected]e5a440c2013-06-04 21:55:1252void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1753 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4154 scoped_ptr<base::ListValue> args,
[email protected]86376022013-12-03 18:18:0555 content::BrowserContext* browser_context) {
[email protected]4b64d712013-01-17 17:53:1756 // The ActivityLog can only be accessed from the main (UI) thread. If we're
57 // running on the wrong thread, re-dispatch from the main thread.
58 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
59 BrowserThread::PostTask(BrowserThread::UI,
60 FROM_HERE,
61 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1262 extension_id,
[email protected]4b64d712013-01-17 17:53:1763 api_name,
[email protected]c02087b512013-02-04 03:09:2064 base::Passed(&args),
[email protected]86376022013-12-03 18:18:0565 browser_context));
[email protected]4b64d712013-01-17 17:53:1766 } else {
67 extensions::ActivityLog* activity_log =
[email protected]86376022013-12-03 18:18:0568 extensions::ActivityLog::GetInstance(browser_context);
[email protected]05602fa32013-08-01 06:30:4369 scoped_refptr<extensions::Action> action =
70 new extensions::Action(extension_id,
71 base::Time::Now(),
72 extensions::Action::ACTION_API_CALL,
73 api_name);
74 action->set_args(args.Pass());
75 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:4276 }
77}
78
[email protected]5bc248a2012-04-04 23:38:1179// Separate copy of ExtensionAPI used for IO thread extension functions. We need
80// this because ExtensionAPI has mutable data. It should be possible to remove
81// this once all the extension APIs are updated to the feature system.
82struct Static {
83 Static()
84 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
85 }
86 scoped_ptr<extensions::ExtensionAPI> api;
87};
88base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
89
[email protected]35548ab2013-05-15 08:59:4790// Kills the specified process because it sends us a malformed message.
91void KillBadMessageSender(base::ProcessHandle process) {
92 NOTREACHED();
[email protected]e6e30ac2014-01-13 21:24:3993 content::RecordAction(base::UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]35548ab2013-05-15 08:59:4794 if (process)
95 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
96}
97
98void CommonResponseCallback(IPC::Sender* ipc_sender,
99 int routing_id,
100 base::ProcessHandle peer_process,
101 int request_id,
102 ExtensionFunction::ResponseType type,
103 const base::ListValue& results,
104 const std::string& error) {
105 DCHECK(ipc_sender);
106
107 if (type == ExtensionFunction::BAD_MESSAGE) {
108 // The renderer has done validation before sending extension api requests.
109 // Therefore, we should never receive a request that is invalid in a way
110 // that JSON validation in the renderer should have caught. It could be an
111 // attacker trying to exploit the browser, so we crash the renderer instead.
112 LOG(ERROR) <<
113 "Terminating renderer because of malformed extension message.";
114 if (content::RenderProcessHost::run_renderer_in_process()) {
115 // In single process mode it is better if we don't suicide but just crash.
116 CHECK(false);
117 } else {
118 KillBadMessageSender(peer_process);
119 }
120
121 return;
122 }
123
124 ipc_sender->Send(new ExtensionMsg_Response(
125 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
126 error));
127}
128
129void IOThreadResponseCallback(
130 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
131 int routing_id,
132 int request_id,
133 ExtensionFunction::ResponseType type,
134 const base::ListValue& results,
135 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45136 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47137 return;
138
[email protected]e8dad9b2013-06-04 04:43:45139 CommonResponseCallback(ipc_sender.get(),
140 routing_id,
[email protected]950be552013-07-10 19:13:02141 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45142 request_id,
143 type,
144 results,
145 error);
[email protected]35548ab2013-05-15 08:59:47146}
147
[email protected]5bc248a2012-04-04 23:38:11148} // namespace
149
[email protected]35548ab2013-05-15 08:59:47150class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
[email protected]ab2a7e3c2013-10-22 03:41:36151 : public content::WebContentsObserver {
[email protected]35548ab2013-05-15 08:59:47152 public:
153 UIThreadResponseCallbackWrapper(
154 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
155 RenderViewHost* render_view_host)
[email protected]ab2a7e3c2013-10-22 03:41:36156 : content::WebContentsObserver(
157 content::WebContents::FromRenderViewHost(render_view_host)),
[email protected]35548ab2013-05-15 08:59:47158 dispatcher_(dispatcher),
[email protected]ab2a7e3c2013-10-22 03:41:36159 render_view_host_(render_view_host),
[email protected]35548ab2013-05-15 08:59:47160 weak_ptr_factory_(this) {
161 }
162
163 virtual ~UIThreadResponseCallbackWrapper() {
164 }
165
[email protected]ab2a7e3c2013-10-22 03:41:36166 // content::WebContentsObserver overrides.
167 virtual void RenderViewDeleted(
[email protected]35548ab2013-05-15 08:59:47168 RenderViewHost* render_view_host) OVERRIDE {
169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]ab2a7e3c2013-10-22 03:41:36170 if (render_view_host != render_view_host_)
171 return;
172
[email protected]e8dad9b2013-06-04 04:43:45173 if (dispatcher_.get()) {
174 dispatcher_->ui_thread_response_callback_wrappers_
175 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47176 }
177
[email protected]ab2a7e3c2013-10-22 03:41:36178 delete this;
[email protected]35548ab2013-05-15 08:59:47179 }
180
181 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
182 return base::Bind(
183 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
184 weak_ptr_factory_.GetWeakPtr(),
185 request_id);
186 }
187
188 private:
189 void OnExtensionFunctionCompleted(int request_id,
190 ExtensionFunction::ResponseType type,
191 const base::ListValue& results,
192 const std::string& error) {
193 CommonResponseCallback(
[email protected]ab2a7e3c2013-10-22 03:41:36194 render_view_host_, render_view_host_->GetRoutingID(),
195 render_view_host_->GetProcess()->GetHandle(), request_id, type,
[email protected]35548ab2013-05-15 08:59:47196 results, error);
197 }
198
199 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
[email protected]ab2a7e3c2013-10-22 03:41:36200 content::RenderViewHost* render_view_host_;
[email protected]35548ab2013-05-15 08:59:47201 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
202
203 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
204};
205
[email protected]44f4b132012-07-17 20:36:57206extensions::WindowController*
207ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08208 const {
209 return NULL;
210}
211
212content::WebContents*
[email protected]44f4b132012-07-17 20:36:57213ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08214 return NULL;
215}
[email protected]5bc248a2012-04-04 23:38:11216
[email protected]1ce88e82013-06-28 05:17:10217content::WebContents*
218ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
219 return GetAssociatedWebContents();
220}
221
[email protected]bfdffe2b2009-04-24 22:05:35222void ExtensionFunctionDispatcher::GetAllFunctionNames(
223 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35224 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35225}
226
[email protected]b83e4602009-05-15 22:58:33227bool ExtensionFunctionDispatcher::OverrideFunction(
228 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35229 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
230 factory);
[email protected]b83e4602009-05-15 22:58:33231}
232
233void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35234 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33235}
236
[email protected]c357acb42011-06-09 20:52:42237// static
238void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]38427a12013-11-09 17:34:20239 extensions::InfoMap* extension_info_map,
[email protected]86376022013-12-03 18:18:05240 void* browser_context,
[email protected]c357acb42011-06-09 20:52:42241 int render_process_id,
242 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53243 int routing_id,
[email protected]c357acb42011-06-09 20:52:42244 const ExtensionHostMsg_Request_Params& params) {
245 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11246 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]35548ab2013-05-15 08:59:47247
248 ExtensionFunction::ResponseCallback callback(
249 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
250 params.request_id));
251
[email protected]6f371442011-11-09 06:45:46252 scoped_refptr<ExtensionFunction> function(
253 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11254 extension_info_map->process_map(),
255 g_global_io_data.Get().api.get(),
[email protected]86376022013-12-03 18:18:05256 browser_context, callback));
[email protected]ecc854a2013-08-22 10:12:42257 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42258 return;
259
260 IOThreadExtensionFunction* function_io =
261 function->AsIOThreadExtensionFunction();
262 if (!function_io) {
263 NOTREACHED();
264 return;
265 }
[email protected]44295a12013-06-05 08:45:46266 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42267 function_io->set_extension_info_map(extension_info_map);
268 function->set_include_incognito(
269 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25270
[email protected]ecc854a2013-08-22 10:12:42271 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07272 return;
[email protected]d2fe22ff2012-10-03 00:40:07273
[email protected]38427a12013-11-09 17:34:20274 extensions::QuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29275 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09276 function.get(),
[email protected]85231d72012-08-31 09:45:29277 &params.arguments,
278 base::TimeTicks::Now());
279 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53280 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
[email protected]e5a440c2013-06-04 21:55:12281 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17282 params.name,
283 args.Pass(),
[email protected]86376022013-12-03 18:18:05284 static_cast<content::BrowserContext*>(browser_context));
[email protected]fd50e7b2011-11-03 09:20:25285 function->Run();
286 } else {
[email protected]85231d72012-08-31 09:45:29287 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25288 }
[email protected]c357acb42011-06-09 20:52:42289}
290
[email protected]96e6a1032013-11-28 06:58:03291ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
292 content::BrowserContext* browser_context,
293 Delegate* delegate)
[email protected]86376022013-12-03 18:18:05294 : browser_context_(browser_context),
[email protected]96e6a1032013-11-28 06:58:03295 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35296}
297
[email protected]32dda362009-06-05 19:07:01298ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01299}
300
[email protected]c5dbef02011-05-13 05:06:09301void ExtensionFunctionDispatcher::Dispatch(
302 const ExtensionHostMsg_Request_Params& params,
303 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47304 UIThreadResponseCallbackWrapperMap::const_iterator
305 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
306 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
307 if (iter == ui_thread_response_callback_wrappers_.end()) {
308 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
309 render_view_host);
310 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
311 } else {
312 callback_wrapper = iter->second;
313 }
314
[email protected]6dd625e2013-12-20 17:03:07315 DispatchWithCallbackInternal(
316 params, render_view_host, NULL,
317 callback_wrapper->CreateCallback(params.request_id));
[email protected]35548ab2013-05-15 08:59:47318}
319
320void ExtensionFunctionDispatcher::DispatchWithCallback(
321 const ExtensionHostMsg_Request_Params& params,
[email protected]6dd625e2013-12-20 17:03:07322 content::RenderFrameHost* render_frame_host,
[email protected]35548ab2013-05-15 08:59:47323 const ExtensionFunction::ResponseCallback& callback) {
[email protected]6dd625e2013-12-20 17:03:07324 DispatchWithCallbackInternal(params, NULL, render_frame_host, callback);
325}
326
327void ExtensionFunctionDispatcher::DispatchWithCallbackInternal(
328 const ExtensionHostMsg_Request_Params& params,
329 RenderViewHost* render_view_host,
330 content::RenderFrameHost* render_frame_host,
331 const ExtensionFunction::ResponseCallback& callback) {
332 DCHECK(render_view_host || render_frame_host);
[email protected]35548ab2013-05-15 08:59:47333 // TODO(yzshen): There is some shared logic between this method and
334 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]fafdc842014-01-17 18:09:08335 extensions::ProcessMap* process_map =
336 extensions::ProcessMap::Get(browser_context_);
[email protected]86376022013-12-03 18:18:05337 if (!process_map)
[email protected]c5dbef02011-05-13 05:06:09338 return;
339
[email protected]aab23102014-02-05 18:57:55340 extensions::ExtensionRegistry* registry =
341 extensions::ExtensionRegistry::Get(browser_context_);
342 const Extension* extension = registry->enabled_extensions().GetByID(
[email protected]615d88f2011-12-13 01:47:44343 params.extension_id);
[email protected]aab23102014-02-05 18:57:55344 if (!extension) {
345 extension =
346 registry->enabled_extensions().GetHostedAppByURL(params.source_url);
347 }
[email protected]c5dbef02011-05-13 05:06:09348
[email protected]6dd625e2013-12-20 17:03:07349 int process_id = render_view_host ? render_view_host->GetProcess()->GetID() :
350 render_frame_host->GetProcess()->GetID();
[email protected]8add5412011-10-01 21:02:14351 scoped_refptr<ExtensionFunction> function(
[email protected]86376022013-12-03 18:18:05352 CreateExtensionFunction(params,
353 extension,
[email protected]6dd625e2013-12-20 17:03:07354 process_id,
[email protected]86376022013-12-03 18:18:05355 *process_map,
[email protected]5bc248a2012-04-04 23:38:11356 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]86376022013-12-03 18:18:05357 browser_context_,
358 callback));
[email protected]ecc854a2013-08-22 10:12:42359 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17360 return;
[email protected]f82d57b52011-04-27 19:13:17361
[email protected]a2aef2e2011-05-26 22:48:12362 UIThreadExtensionFunction* function_ui =
363 function->AsUIThreadExtensionFunction();
364 if (!function_ui) {
365 NOTREACHED();
366 return;
367 }
[email protected]6dd625e2013-12-20 17:03:07368 if (render_view_host) {
369 function_ui->SetRenderViewHost(render_view_host);
370 } else {
371 function_ui->SetRenderFrameHost(render_frame_host);
372 }
[email protected]a2aef2e2011-05-26 22:48:12373 function_ui->set_dispatcher(AsWeakPtr());
[email protected]86376022013-12-03 18:18:05374 function_ui->set_context(browser_context_);
[email protected]1d5cf4142014-01-24 18:25:22375 function->set_include_incognito(
376 extensions::util::CanCrossIncognito(extension, browser_context_));
[email protected]cb0ce1e022010-03-10 19:54:41377
[email protected]ecc854a2013-08-22 10:12:42378 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07379 return;
[email protected]d2fe22ff2012-10-03 00:40:07380
[email protected]aab23102014-02-05 18:57:55381 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context_);
382 extensions::QuotaService* quota = extension_system->quota_service();
[email protected]85231d72012-08-31 09:45:29383 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09384 function.get(),
[email protected]85231d72012-08-31 09:45:29385 &params.arguments,
386 base::TimeTicks::Now());
387 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53388 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
389
[email protected]d070ec62010-07-27 21:28:26390 // See crbug.com/39178.
391 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]86376022013-12-03 18:18:05392 LogSuccess(extension->id(), params.name, args.Pass(), browser_context_);
[email protected]d13950e2009-12-04 01:43:02393 function->Run();
394 } else {
[email protected]85231d72012-08-31 09:45:29395 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02396 }
[email protected]720ad1312012-02-27 23:07:36397
[email protected]efb4b082012-10-17 22:28:28398 // Note: do not access |this| after this point. We may have been deleted
399 // if function->Run() ended up closing the tab that owns us.
400
[email protected]5734e882012-05-04 22:17:56401 // Check if extension was uninstalled by management.uninstall.
[email protected]aab23102014-02-05 18:57:55402 if (!registry->enabled_extensions().GetByID(params.extension_id))
[email protected]5734e882012-05-04 22:17:56403 return;
404
[email protected]720ad1312012-02-27 23:07:36405 // We only adjust the keepalive count for UIThreadExtensionFunction for
406 // now, largely for simplicity's sake. This is OK because currently, only
407 // the webRequest API uses IOThreadExtensionFunction, and that API is not
408 // compatible with lazy background pages.
[email protected]86376022013-12-03 18:18:05409 extension_system->process_manager()->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36410}
411
412void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
413 const Extension* extension) {
[email protected]59b0e602014-01-30 00:41:24414 ExtensionSystem::Get(browser_context_)->process_manager()->
[email protected]be93bba02012-10-24 16:44:03415 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35416}
417
[email protected]c357acb42011-06-09 20:52:42418// static
[email protected]d2fe22ff2012-10-03 00:40:07419bool ExtensionFunctionDispatcher::CheckPermissions(
420 ExtensionFunction* function,
421 const Extension* extension,
422 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47423 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07424 if (!function->HasPermission()) {
425 LOG(ERROR) << "Extension " << extension->id() << " does not have "
426 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47427 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07428 return false;
429 }
430 return true;
431}
432
[email protected]f33542112013-02-04 16:52:38433namespace {
434
435// Only COMPONENT hosted apps may call extension APIs, and they are limited
436// to just the permissions they explicitly request. They should not have access
437// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
438// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11439// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
440// it altogether).
[email protected]f33542112013-02-04 16:52:38441bool AllowHostedAppAPICall(const Extension& extension,
442 const GURL& source_url,
443 const std::string& function_name) {
444 if (extension.location() != extensions::Manifest::COMPONENT)
445 return false;
446
447 if (!extension.web_extent().MatchesURL(source_url))
448 return false;
449
[email protected]713a6e82013-12-19 23:06:05450 // Note: Not BLESSED_WEB_PAGE_CONTEXT here because these component hosted app
451 // entities have traditionally been treated as blessed extensions, for better
452 // or worse.
[email protected]b5b26b72013-08-02 00:25:11453 Feature::Availability availability =
454 ExtensionAPI::GetSharedInstance()->IsAvailable(
455 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
456 source_url);
457 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38458}
459
460} // namespace
461
462
[email protected]d2fe22ff2012-10-03 00:40:07463// static
[email protected]c357acb42011-06-09 20:52:42464ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
465 const ExtensionHostMsg_Request_Params& params,
466 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46467 int requesting_process_id,
468 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11469 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18470 void* profile,
[email protected]35548ab2013-05-15 08:59:47471 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42472 if (!extension) {
[email protected]6f371442011-11-09 06:45:46473 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47474 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46475 return NULL;
476 }
477
[email protected]f33542112013-02-04 16:52:38478 // Most hosted apps can't call APIs.
479 bool allowed = true;
480 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47481 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38482
483 // Privileged APIs can only be called from the process the extension
484 // is running in.
485 if (allowed && api->IsPrivileged(params.name))
486 allowed = process_map.Contains(extension->id(), requesting_process_id);
487
488 if (!allowed) {
489 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
490 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46491 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47492 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42493 return NULL;
494 }
495
[email protected]c357acb42011-06-09 20:52:42496 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35497 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14498 if (!function) {
499 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47500 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14501 return NULL;
502 }
503
[email protected]c357acb42011-06-09 20:52:42504 function->SetArgs(&params.arguments);
505 function->set_source_url(params.source_url);
506 function->set_request_id(params.request_id);
507 function->set_has_callback(params.has_callback);
508 function->set_user_gesture(params.user_gesture);
509 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32510 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47511 function->set_response_callback(callback);
[email protected]eb7ef5f2014-02-06 09:59:19512 function->set_source_tab_id(params.source_tab_id);
[email protected]3d0e2262012-08-02 15:32:16513
[email protected]c357acb42011-06-09 20:52:42514 return function;
515}
516
517// static
[email protected]c5dbef02011-05-13 05:06:09518void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47519 const ExtensionFunction::ResponseCallback& callback) {
[email protected]023b3d12013-12-23 18:46:49520 base::ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47521 callback.Run(ExtensionFunction::FAILED, empty_list,
522 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35523}