blob: f628d3d568ad245a43238f150ca901a2b1fe31fa [file] [log] [blame]
[email protected]64c820732012-01-05 20:50:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bfdffe2b2009-04-24 22:05:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_function_dispatcher.h"
6
[email protected]4b64d712013-01-17 17:53:177#include "base/bind.h"
[email protected]ffbec692012-02-26 20:26:428#include "base/json/json_string_value_serializer.h"
[email protected]5bc248a2012-04-04 23:38:119#include "base/lazy_instance.h"
[email protected]35548ab2013-05-15 08:59:4710#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
[email protected]d09a4ce1c2013-07-24 17:37:0212#include "base/process/process.h"
[email protected]bfdffe2b2009-04-24 22:05:3513#include "base/values.h"
[email protected]17d40f02010-07-01 01:18:0614#include "build/build_config.h"
[email protected]05602fa32013-08-01 06:30:4315#include "chrome/browser/extensions/activity_log/activity_action_constants.h"
[email protected]17263b22013-05-16 03:29:2216#include "chrome/browser/extensions/activity_log/activity_log.h"
[email protected]05602fa32013-08-01 06:30:4317#include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
[email protected]ae33d322012-03-19 22:24:3518#include "chrome/browser/extensions/extension_function_registry.h"
[email protected]2f69b382011-02-19 00:34:2519#include "chrome/browser/extensions/extension_service.h"
[email protected]efb4b082012-10-17 22:28:2820#include "chrome/browser/extensions/extension_system.h"
[email protected]a7ff4b72013-10-17 20:56:0221#include "chrome/browser/extensions/extension_util.h"
[email protected]8f9d4eb2011-02-05 01:39:1022#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]ed2b1002011-05-25 14:12:1023#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]c357acb42011-06-09 20:52:4224#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]44c49c92011-03-28 16:17:2325#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4326#include "chrome/common/url_constants.h"
[email protected]4b64d712013-01-17 17:53:1727#include "content/public/browser/browser_thread.h"
[email protected]6dd625e2013-12-20 17:03:0728#include "content/public/browser/render_frame_host.h"
[email protected]c333e792012-01-06 16:57:3929#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3330#include "content/public/browser/render_view_host.h"
[email protected]d6ec84a2013-11-01 13:07:3831#include "content/public/browser/user_metrics.h"
[email protected]ab2a7e3c2013-10-22 03:41:3632#include "content/public/browser/web_contents.h"
33#include "content/public/browser/web_contents_observer.h"
[email protected]35548ab2013-05-15 08:59:4734#include "content/public/common/result_codes.h"
[email protected]50de9aa22013-11-14 06:30:3435#include "extensions/browser/process_map.h"
[email protected]38427a12013-11-09 17:34:2036#include "extensions/browser/quota_service.h"
[email protected]d6ec84a2013-11-01 13:07:3837#include "extensions/common/extension_api.h"
[email protected]289c44b2013-12-17 03:26:5738#include "extensions/common/extension_set.h"
[email protected]f82d57b52011-04-27 19:13:1739#include "ipc/ipc_message.h"
40#include "ipc/ipc_message_macros.h"
[email protected]d0fcff72013-07-23 02:45:4341#include "webkit/common/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1042
[email protected]1c321ee2012-05-21 03:02:3443using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1144using extensions::ExtensionAPI;
[email protected]86376022013-12-03 18:18:0545using extensions::ExtensionSystem;
[email protected]b5b26b72013-08-02 00:25:1146using extensions::Feature;
[email protected]eaabba22012-03-07 15:02:1147using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1148
[email protected]5bc248a2012-04-04 23:38:1149namespace {
50
[email protected]e5a440c2013-06-04 21:55:1251void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1752 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4153 scoped_ptr<base::ListValue> args,
[email protected]86376022013-12-03 18:18:0554 content::BrowserContext* browser_context) {
[email protected]4b64d712013-01-17 17:53:1755 // The ActivityLog can only be accessed from the main (UI) thread. If we're
56 // running on the wrong thread, re-dispatch from the main thread.
57 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
58 BrowserThread::PostTask(BrowserThread::UI,
59 FROM_HERE,
60 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1261 extension_id,
[email protected]4b64d712013-01-17 17:53:1762 api_name,
[email protected]c02087b512013-02-04 03:09:2063 base::Passed(&args),
[email protected]86376022013-12-03 18:18:0564 browser_context));
[email protected]4b64d712013-01-17 17:53:1765 } else {
66 extensions::ActivityLog* activity_log =
[email protected]86376022013-12-03 18:18:0567 extensions::ActivityLog::GetInstance(browser_context);
[email protected]05602fa32013-08-01 06:30:4368 scoped_refptr<extensions::Action> action =
69 new extensions::Action(extension_id,
70 base::Time::Now(),
71 extensions::Action::ACTION_API_CALL,
72 api_name);
73 action->set_args(args.Pass());
74 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:4275 }
76}
77
[email protected]5bc248a2012-04-04 23:38:1178// Separate copy of ExtensionAPI used for IO thread extension functions. We need
79// this because ExtensionAPI has mutable data. It should be possible to remove
80// this once all the extension APIs are updated to the feature system.
81struct Static {
82 Static()
83 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
84 }
85 scoped_ptr<extensions::ExtensionAPI> api;
86};
87base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
88
[email protected]35548ab2013-05-15 08:59:4789// Kills the specified process because it sends us a malformed message.
90void KillBadMessageSender(base::ProcessHandle process) {
91 NOTREACHED();
[email protected]e6e30ac2014-01-13 21:24:3992 content::RecordAction(base::UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]35548ab2013-05-15 08:59:4793 if (process)
94 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
95}
96
97void CommonResponseCallback(IPC::Sender* ipc_sender,
98 int routing_id,
99 base::ProcessHandle peer_process,
100 int request_id,
101 ExtensionFunction::ResponseType type,
102 const base::ListValue& results,
103 const std::string& error) {
104 DCHECK(ipc_sender);
105
106 if (type == ExtensionFunction::BAD_MESSAGE) {
107 // The renderer has done validation before sending extension api requests.
108 // Therefore, we should never receive a request that is invalid in a way
109 // that JSON validation in the renderer should have caught. It could be an
110 // attacker trying to exploit the browser, so we crash the renderer instead.
111 LOG(ERROR) <<
112 "Terminating renderer because of malformed extension message.";
113 if (content::RenderProcessHost::run_renderer_in_process()) {
114 // In single process mode it is better if we don't suicide but just crash.
115 CHECK(false);
116 } else {
117 KillBadMessageSender(peer_process);
118 }
119
120 return;
121 }
122
123 ipc_sender->Send(new ExtensionMsg_Response(
124 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
125 error));
126}
127
128void IOThreadResponseCallback(
129 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
130 int routing_id,
131 int request_id,
132 ExtensionFunction::ResponseType type,
133 const base::ListValue& results,
134 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45135 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47136 return;
137
[email protected]e8dad9b2013-06-04 04:43:45138 CommonResponseCallback(ipc_sender.get(),
139 routing_id,
[email protected]950be552013-07-10 19:13:02140 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45141 request_id,
142 type,
143 results,
144 error);
[email protected]35548ab2013-05-15 08:59:47145}
146
[email protected]5bc248a2012-04-04 23:38:11147} // namespace
148
[email protected]35548ab2013-05-15 08:59:47149class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
[email protected]ab2a7e3c2013-10-22 03:41:36150 : public content::WebContentsObserver {
[email protected]35548ab2013-05-15 08:59:47151 public:
152 UIThreadResponseCallbackWrapper(
153 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
154 RenderViewHost* render_view_host)
[email protected]ab2a7e3c2013-10-22 03:41:36155 : content::WebContentsObserver(
156 content::WebContents::FromRenderViewHost(render_view_host)),
[email protected]35548ab2013-05-15 08:59:47157 dispatcher_(dispatcher),
[email protected]ab2a7e3c2013-10-22 03:41:36158 render_view_host_(render_view_host),
[email protected]35548ab2013-05-15 08:59:47159 weak_ptr_factory_(this) {
160 }
161
162 virtual ~UIThreadResponseCallbackWrapper() {
163 }
164
[email protected]ab2a7e3c2013-10-22 03:41:36165 // content::WebContentsObserver overrides.
166 virtual void RenderViewDeleted(
[email protected]35548ab2013-05-15 08:59:47167 RenderViewHost* render_view_host) OVERRIDE {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]ab2a7e3c2013-10-22 03:41:36169 if (render_view_host != render_view_host_)
170 return;
171
[email protected]e8dad9b2013-06-04 04:43:45172 if (dispatcher_.get()) {
173 dispatcher_->ui_thread_response_callback_wrappers_
174 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47175 }
176
[email protected]ab2a7e3c2013-10-22 03:41:36177 delete this;
[email protected]35548ab2013-05-15 08:59:47178 }
179
180 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
181 return base::Bind(
182 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
183 weak_ptr_factory_.GetWeakPtr(),
184 request_id);
185 }
186
187 private:
188 void OnExtensionFunctionCompleted(int request_id,
189 ExtensionFunction::ResponseType type,
190 const base::ListValue& results,
191 const std::string& error) {
192 CommonResponseCallback(
[email protected]ab2a7e3c2013-10-22 03:41:36193 render_view_host_, render_view_host_->GetRoutingID(),
194 render_view_host_->GetProcess()->GetHandle(), request_id, type,
[email protected]35548ab2013-05-15 08:59:47195 results, error);
196 }
197
198 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
[email protected]ab2a7e3c2013-10-22 03:41:36199 content::RenderViewHost* render_view_host_;
[email protected]35548ab2013-05-15 08:59:47200 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
201
202 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
203};
204
[email protected]44f4b132012-07-17 20:36:57205extensions::WindowController*
206ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08207 const {
208 return NULL;
209}
210
211content::WebContents*
[email protected]44f4b132012-07-17 20:36:57212ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08213 return NULL;
214}
[email protected]5bc248a2012-04-04 23:38:11215
[email protected]1ce88e82013-06-28 05:17:10216content::WebContents*
217ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
218 return GetAssociatedWebContents();
219}
220
[email protected]bfdffe2b2009-04-24 22:05:35221void ExtensionFunctionDispatcher::GetAllFunctionNames(
222 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35223 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35224}
225
[email protected]b83e4602009-05-15 22:58:33226bool ExtensionFunctionDispatcher::OverrideFunction(
227 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35228 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
229 factory);
[email protected]b83e4602009-05-15 22:58:33230}
231
232void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35233 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33234}
235
[email protected]c357acb42011-06-09 20:52:42236// static
237void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]38427a12013-11-09 17:34:20238 extensions::InfoMap* extension_info_map,
[email protected]86376022013-12-03 18:18:05239 void* browser_context,
[email protected]c357acb42011-06-09 20:52:42240 int render_process_id,
241 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53242 int routing_id,
[email protected]c357acb42011-06-09 20:52:42243 const ExtensionHostMsg_Request_Params& params) {
244 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11245 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]35548ab2013-05-15 08:59:47246
247 ExtensionFunction::ResponseCallback callback(
248 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
249 params.request_id));
250
[email protected]6f371442011-11-09 06:45:46251 scoped_refptr<ExtensionFunction> function(
252 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11253 extension_info_map->process_map(),
254 g_global_io_data.Get().api.get(),
[email protected]86376022013-12-03 18:18:05255 browser_context, callback));
[email protected]ecc854a2013-08-22 10:12:42256 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42257 return;
258
259 IOThreadExtensionFunction* function_io =
260 function->AsIOThreadExtensionFunction();
261 if (!function_io) {
262 NOTREACHED();
263 return;
264 }
[email protected]44295a12013-06-05 08:45:46265 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42266 function_io->set_extension_info_map(extension_info_map);
267 function->set_include_incognito(
268 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25269
[email protected]ecc854a2013-08-22 10:12:42270 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07271 return;
[email protected]d2fe22ff2012-10-03 00:40:07272
[email protected]38427a12013-11-09 17:34:20273 extensions::QuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29274 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09275 function.get(),
[email protected]85231d72012-08-31 09:45:29276 &params.arguments,
277 base::TimeTicks::Now());
278 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53279 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
[email protected]e5a440c2013-06-04 21:55:12280 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17281 params.name,
282 args.Pass(),
[email protected]86376022013-12-03 18:18:05283 static_cast<content::BrowserContext*>(browser_context));
[email protected]fd50e7b2011-11-03 09:20:25284 function->Run();
285 } else {
[email protected]85231d72012-08-31 09:45:29286 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25287 }
[email protected]c357acb42011-06-09 20:52:42288}
289
[email protected]96e6a1032013-11-28 06:58:03290ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
291 content::BrowserContext* browser_context,
292 Delegate* delegate)
[email protected]86376022013-12-03 18:18:05293 : browser_context_(browser_context),
[email protected]96e6a1032013-11-28 06:58:03294 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35295}
296
[email protected]32dda362009-06-05 19:07:01297ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01298}
299
[email protected]c5dbef02011-05-13 05:06:09300void ExtensionFunctionDispatcher::Dispatch(
301 const ExtensionHostMsg_Request_Params& params,
302 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47303 UIThreadResponseCallbackWrapperMap::const_iterator
304 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
305 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
306 if (iter == ui_thread_response_callback_wrappers_.end()) {
307 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
308 render_view_host);
309 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
310 } else {
311 callback_wrapper = iter->second;
312 }
313
[email protected]6dd625e2013-12-20 17:03:07314 DispatchWithCallbackInternal(
315 params, render_view_host, NULL,
316 callback_wrapper->CreateCallback(params.request_id));
[email protected]35548ab2013-05-15 08:59:47317}
318
319void ExtensionFunctionDispatcher::DispatchWithCallback(
320 const ExtensionHostMsg_Request_Params& params,
[email protected]6dd625e2013-12-20 17:03:07321 content::RenderFrameHost* render_frame_host,
[email protected]35548ab2013-05-15 08:59:47322 const ExtensionFunction::ResponseCallback& callback) {
[email protected]6dd625e2013-12-20 17:03:07323 DispatchWithCallbackInternal(params, NULL, render_frame_host, callback);
324}
325
326void ExtensionFunctionDispatcher::DispatchWithCallbackInternal(
327 const ExtensionHostMsg_Request_Params& params,
328 RenderViewHost* render_view_host,
329 content::RenderFrameHost* render_frame_host,
330 const ExtensionFunction::ResponseCallback& callback) {
331 DCHECK(render_view_host || render_frame_host);
[email protected]35548ab2013-05-15 08:59:47332 // TODO(yzshen): There is some shared logic between this method and
333 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]86376022013-12-03 18:18:05334 ExtensionSystem* extension_system =
335 ExtensionSystem::GetForBrowserContext(browser_context_);
336 ExtensionService* service = extension_system->extension_service();
[email protected]fafdc842014-01-17 18:09:08337 extensions::ProcessMap* process_map =
338 extensions::ProcessMap::Get(browser_context_);
[email protected]86376022013-12-03 18:18:05339 if (!process_map)
[email protected]c5dbef02011-05-13 05:06:09340 return;
341
[email protected]615d88f2011-12-13 01:47:44342 const Extension* extension = service->extensions()->GetByID(
343 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09344 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55345 extension = service->extensions()->GetHostedAppByURL(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09346
[email protected]6dd625e2013-12-20 17:03:07347 int process_id = render_view_host ? render_view_host->GetProcess()->GetID() :
348 render_frame_host->GetProcess()->GetID();
[email protected]8add5412011-10-01 21:02:14349 scoped_refptr<ExtensionFunction> function(
[email protected]86376022013-12-03 18:18:05350 CreateExtensionFunction(params,
351 extension,
[email protected]6dd625e2013-12-20 17:03:07352 process_id,
[email protected]86376022013-12-03 18:18:05353 *process_map,
[email protected]5bc248a2012-04-04 23:38:11354 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]86376022013-12-03 18:18:05355 browser_context_,
356 callback));
[email protected]ecc854a2013-08-22 10:12:42357 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17358 return;
[email protected]f82d57b52011-04-27 19:13:17359
[email protected]a2aef2e2011-05-26 22:48:12360 UIThreadExtensionFunction* function_ui =
361 function->AsUIThreadExtensionFunction();
362 if (!function_ui) {
363 NOTREACHED();
364 return;
365 }
[email protected]6dd625e2013-12-20 17:03:07366 if (render_view_host) {
367 function_ui->SetRenderViewHost(render_view_host);
368 } else {
369 function_ui->SetRenderFrameHost(render_frame_host);
370 }
[email protected]a2aef2e2011-05-26 22:48:12371 function_ui->set_dispatcher(AsWeakPtr());
[email protected]86376022013-12-03 18:18:05372 function_ui->set_context(browser_context_);
[email protected]a7ff4b72013-10-17 20:56:02373 function->set_include_incognito(extension_util::CanCrossIncognito(extension,
374 service));
[email protected]cb0ce1e022010-03-10 19:54:41375
[email protected]ecc854a2013-08-22 10:12:42376 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07377 return;
[email protected]d2fe22ff2012-10-03 00:40:07378
[email protected]38427a12013-11-09 17:34:20379 extensions::QuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29380 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09381 function.get(),
[email protected]85231d72012-08-31 09:45:29382 &params.arguments,
383 base::TimeTicks::Now());
384 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53385 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
386
[email protected]d070ec62010-07-27 21:28:26387 // See crbug.com/39178.
388 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]86376022013-12-03 18:18:05389 LogSuccess(extension->id(), params.name, args.Pass(), browser_context_);
[email protected]d13950e2009-12-04 01:43:02390 function->Run();
391 } else {
[email protected]85231d72012-08-31 09:45:29392 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02393 }
[email protected]720ad1312012-02-27 23:07:36394
[email protected]efb4b082012-10-17 22:28:28395 // Note: do not access |this| after this point. We may have been deleted
396 // if function->Run() ended up closing the tab that owns us.
397
[email protected]5734e882012-05-04 22:17:56398 // Check if extension was uninstalled by management.uninstall.
399 if (!service->extensions()->GetByID(params.extension_id))
400 return;
401
[email protected]720ad1312012-02-27 23:07:36402 // We only adjust the keepalive count for UIThreadExtensionFunction for
403 // now, largely for simplicity's sake. This is OK because currently, only
404 // the webRequest API uses IOThreadExtensionFunction, and that API is not
405 // compatible with lazy background pages.
[email protected]86376022013-12-03 18:18:05406 extension_system->process_manager()->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36407}
408
409void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
410 const Extension* extension) {
[email protected]86376022013-12-03 18:18:05411 ExtensionSystem::GetForBrowserContext(browser_context_)->process_manager()->
[email protected]be93bba02012-10-24 16:44:03412 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35413}
414
[email protected]c357acb42011-06-09 20:52:42415// static
[email protected]d2fe22ff2012-10-03 00:40:07416bool ExtensionFunctionDispatcher::CheckPermissions(
417 ExtensionFunction* function,
418 const Extension* extension,
419 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47420 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07421 if (!function->HasPermission()) {
422 LOG(ERROR) << "Extension " << extension->id() << " does not have "
423 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47424 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07425 return false;
426 }
427 return true;
428}
429
[email protected]f33542112013-02-04 16:52:38430namespace {
431
432// Only COMPONENT hosted apps may call extension APIs, and they are limited
433// to just the permissions they explicitly request. They should not have access
434// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
435// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11436// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
437// it altogether).
[email protected]f33542112013-02-04 16:52:38438bool AllowHostedAppAPICall(const Extension& extension,
439 const GURL& source_url,
440 const std::string& function_name) {
441 if (extension.location() != extensions::Manifest::COMPONENT)
442 return false;
443
444 if (!extension.web_extent().MatchesURL(source_url))
445 return false;
446
[email protected]713a6e82013-12-19 23:06:05447 // Note: Not BLESSED_WEB_PAGE_CONTEXT here because these component hosted app
448 // entities have traditionally been treated as blessed extensions, for better
449 // or worse.
[email protected]b5b26b72013-08-02 00:25:11450 Feature::Availability availability =
451 ExtensionAPI::GetSharedInstance()->IsAvailable(
452 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
453 source_url);
454 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38455}
456
457} // namespace
458
459
[email protected]d2fe22ff2012-10-03 00:40:07460// static
[email protected]c357acb42011-06-09 20:52:42461ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
462 const ExtensionHostMsg_Request_Params& params,
463 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46464 int requesting_process_id,
465 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11466 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18467 void* profile,
[email protected]35548ab2013-05-15 08:59:47468 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42469 if (!extension) {
[email protected]6f371442011-11-09 06:45:46470 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47471 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46472 return NULL;
473 }
474
[email protected]f33542112013-02-04 16:52:38475 // Most hosted apps can't call APIs.
476 bool allowed = true;
477 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47478 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38479
480 // Privileged APIs can only be called from the process the extension
481 // is running in.
482 if (allowed && api->IsPrivileged(params.name))
483 allowed = process_map.Contains(extension->id(), requesting_process_id);
484
485 if (!allowed) {
486 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
487 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46488 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47489 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42490 return NULL;
491 }
492
[email protected]c357acb42011-06-09 20:52:42493 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35494 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14495 if (!function) {
496 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47497 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14498 return NULL;
499 }
500
[email protected]c357acb42011-06-09 20:52:42501 function->SetArgs(&params.arguments);
502 function->set_source_url(params.source_url);
503 function->set_request_id(params.request_id);
504 function->set_has_callback(params.has_callback);
505 function->set_user_gesture(params.user_gesture);
506 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32507 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47508 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16509
[email protected]c357acb42011-06-09 20:52:42510 return function;
511}
512
513// static
[email protected]c5dbef02011-05-13 05:06:09514void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47515 const ExtensionFunction::ResponseCallback& callback) {
[email protected]023b3d12013-12-23 18:46:49516 base::ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47517 callback.Run(ExtensionFunction::FAILED, empty_list,
518 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35519}