blob: fb3b50d66b28fedf401c357d85df84a44ec02079 [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]04783d22014-04-11 13:26:2112#include "base/metrics/sparse_histogram.h"
[email protected]d09a4ce1c2013-07-24 17:37:0213#include "base/process/process.h"
[email protected]bfdffe2b2009-04-24 22:05:3514#include "base/values.h"
[email protected]17d40f02010-07-01 01:18:0615#include "build/build_config.h"
[email protected]4b64d712013-01-17 17:53:1716#include "content/public/browser/browser_thread.h"
[email protected]6dd625e2013-12-20 17:03:0717#include "content/public/browser/render_frame_host.h"
[email protected]c333e792012-01-06 16:57:3918#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3319#include "content/public/browser/render_view_host.h"
[email protected]d6ec84a2013-11-01 13:07:3820#include "content/public/browser/user_metrics.h"
[email protected]ab2a7e3c2013-10-22 03:41:3621#include "content/public/browser/web_contents.h"
22#include "content/public/browser/web_contents_observer.h"
[email protected]35548ab2013-05-15 08:59:4723#include "content/public/common/result_codes.h"
[email protected]b32260f2014-02-06 10:03:4124#include "extensions/browser/api_activity_monitor.h"
[email protected]21c6c432014-03-05 18:47:3125#include "extensions/browser/extension_function_registry.h"
[email protected]1a0436892014-04-01 00:38:2526#include "extensions/browser/extension_message_filter.h"
[email protected]aab23102014-02-05 18:57:5527#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2428#include "extensions/browser/extension_system.h"
[email protected]b32260f2014-02-06 10:03:4129#include "extensions/browser/extensions_browser_client.h"
[email protected]aab23102014-02-05 18:57:5530#include "extensions/browser/process_manager.h"
[email protected]50de9aa22013-11-14 06:30:3431#include "extensions/browser/process_map.h"
[email protected]38427a12013-11-09 17:34:2032#include "extensions/browser/quota_service.h"
[email protected]d6ec84a2013-11-01 13:07:3833#include "extensions/common/extension_api.h"
[email protected]fb820c02014-03-13 15:07:0834#include "extensions/common/extension_messages.h"
[email protected]289c44b2013-12-17 03:26:5735#include "extensions/common/extension_set.h"
[email protected]f82d57b52011-04-27 19:13:1736#include "ipc/ipc_message.h"
37#include "ipc/ipc_message_macros.h"
[email protected]61b55b62011-03-24 09:03:1038
[email protected]b32260f2014-02-06 10:03:4139using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1140using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1141
[email protected]1a0436892014-04-01 00:38:2542namespace extensions {
[email protected]5bc248a2012-04-04 23:38:1143namespace {
44
[email protected]b32260f2014-02-06 10:03:4145// Notifies the ApiActivityMonitor that an extension API function has been
46// called. May be called from any thread.
47void NotifyApiFunctionCalled(const std::string& extension_id,
48 const std::string& api_name,
49 scoped_ptr<base::ListValue> args,
50 content::BrowserContext* browser_context) {
[email protected]eafbd05a2014-02-06 19:29:0151 // The ApiActivityMonitor can only be accessed from the main (UI) thread. If
[email protected]b32260f2014-02-06 10:03:4152 // we're running on the wrong thread, re-dispatch from the main thread.
[email protected]4b64d712013-01-17 17:53:1753 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
54 BrowserThread::PostTask(BrowserThread::UI,
55 FROM_HERE,
[email protected]b32260f2014-02-06 10:03:4156 base::Bind(&NotifyApiFunctionCalled,
[email protected]e5a440c2013-06-04 21:55:1257 extension_id,
[email protected]4b64d712013-01-17 17:53:1758 api_name,
[email protected]c02087b512013-02-04 03:09:2059 base::Passed(&args),
[email protected]86376022013-12-03 18:18:0560 browser_context));
[email protected]b32260f2014-02-06 10:03:4161 return;
[email protected]efd75992011-12-15 22:42:4262 }
[email protected]eafbd05a2014-02-06 19:29:0163 // The BrowserContext may become invalid after the task above is posted.
64 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
65 return;
66
[email protected]1a0436892014-04-01 00:38:2567 ApiActivityMonitor* monitor =
[email protected]eafbd05a2014-02-06 19:29:0168 ExtensionsBrowserClient::Get()->GetApiActivityMonitor(browser_context);
[email protected]b32260f2014-02-06 10:03:4169 if (monitor)
70 monitor->OnApiFunctionCalled(extension_id, api_name, args.Pass());
[email protected]efd75992011-12-15 22:42:4271}
72
[email protected]5bc248a2012-04-04 23:38:1173// Separate copy of ExtensionAPI used for IO thread extension functions. We need
74// this because ExtensionAPI has mutable data. It should be possible to remove
75// this once all the extension APIs are updated to the feature system.
76struct Static {
[email protected]1a0436892014-04-01 00:38:2577 Static() : api(ExtensionAPI::CreateWithDefaultConfiguration()) {}
78 scoped_ptr<ExtensionAPI> api;
[email protected]5bc248a2012-04-04 23:38:1179};
80base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
81
[email protected]35548ab2013-05-15 08:59:4782// Kills the specified process because it sends us a malformed message.
83void KillBadMessageSender(base::ProcessHandle process) {
84 NOTREACHED();
[email protected]e6e30ac2014-01-13 21:24:3985 content::RecordAction(base::UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]35548ab2013-05-15 08:59:4786 if (process)
87 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
88}
89
90void CommonResponseCallback(IPC::Sender* ipc_sender,
91 int routing_id,
92 base::ProcessHandle peer_process,
93 int request_id,
94 ExtensionFunction::ResponseType type,
95 const base::ListValue& results,
96 const std::string& error) {
97 DCHECK(ipc_sender);
98
99 if (type == ExtensionFunction::BAD_MESSAGE) {
100 // The renderer has done validation before sending extension api requests.
101 // Therefore, we should never receive a request that is invalid in a way
102 // that JSON validation in the renderer should have caught. It could be an
103 // attacker trying to exploit the browser, so we crash the renderer instead.
104 LOG(ERROR) <<
105 "Terminating renderer because of malformed extension message.";
106 if (content::RenderProcessHost::run_renderer_in_process()) {
107 // In single process mode it is better if we don't suicide but just crash.
108 CHECK(false);
109 } else {
110 KillBadMessageSender(peer_process);
111 }
112
113 return;
114 }
115
116 ipc_sender->Send(new ExtensionMsg_Response(
117 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
118 error));
119}
120
121void IOThreadResponseCallback(
[email protected]1a0436892014-04-01 00:38:25122 const base::WeakPtr<ExtensionMessageFilter>& ipc_sender,
[email protected]35548ab2013-05-15 08:59:47123 int routing_id,
124 int request_id,
125 ExtensionFunction::ResponseType type,
126 const base::ListValue& results,
127 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45128 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47129 return;
130
[email protected]e8dad9b2013-06-04 04:43:45131 CommonResponseCallback(ipc_sender.get(),
132 routing_id,
[email protected]950be552013-07-10 19:13:02133 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45134 request_id,
135 type,
136 results,
137 error);
[email protected]35548ab2013-05-15 08:59:47138}
139
[email protected]5bc248a2012-04-04 23:38:11140} // namespace
141
[email protected]35548ab2013-05-15 08:59:47142class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
[email protected]ab2a7e3c2013-10-22 03:41:36143 : public content::WebContentsObserver {
[email protected]35548ab2013-05-15 08:59:47144 public:
145 UIThreadResponseCallbackWrapper(
146 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
147 RenderViewHost* render_view_host)
[email protected]ab2a7e3c2013-10-22 03:41:36148 : content::WebContentsObserver(
149 content::WebContents::FromRenderViewHost(render_view_host)),
[email protected]35548ab2013-05-15 08:59:47150 dispatcher_(dispatcher),
[email protected]ab2a7e3c2013-10-22 03:41:36151 render_view_host_(render_view_host),
[email protected]35548ab2013-05-15 08:59:47152 weak_ptr_factory_(this) {
153 }
154
155 virtual ~UIThreadResponseCallbackWrapper() {
156 }
157
[email protected]ab2a7e3c2013-10-22 03:41:36158 // content::WebContentsObserver overrides.
159 virtual void RenderViewDeleted(
[email protected]35548ab2013-05-15 08:59:47160 RenderViewHost* render_view_host) OVERRIDE {
[email protected]54ee8192014-03-29 17:37:24161 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]ab2a7e3c2013-10-22 03:41:36162 if (render_view_host != render_view_host_)
163 return;
164
[email protected]e8dad9b2013-06-04 04:43:45165 if (dispatcher_.get()) {
166 dispatcher_->ui_thread_response_callback_wrappers_
167 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47168 }
169
[email protected]ab2a7e3c2013-10-22 03:41:36170 delete this;
[email protected]35548ab2013-05-15 08:59:47171 }
172
173 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
174 return base::Bind(
175 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
176 weak_ptr_factory_.GetWeakPtr(),
177 request_id);
178 }
179
180 private:
181 void OnExtensionFunctionCompleted(int request_id,
182 ExtensionFunction::ResponseType type,
183 const base::ListValue& results,
184 const std::string& error) {
185 CommonResponseCallback(
[email protected]ab2a7e3c2013-10-22 03:41:36186 render_view_host_, render_view_host_->GetRoutingID(),
187 render_view_host_->GetProcess()->GetHandle(), request_id, type,
[email protected]35548ab2013-05-15 08:59:47188 results, error);
189 }
190
191 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
[email protected]ab2a7e3c2013-10-22 03:41:36192 content::RenderViewHost* render_view_host_;
[email protected]35548ab2013-05-15 08:59:47193 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
194
195 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
196};
197
[email protected]1a0436892014-04-01 00:38:25198WindowController*
199ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const {
[email protected]d72d3a62012-05-10 03:45:08200 return NULL;
201}
202
203content::WebContents*
[email protected]44f4b132012-07-17 20:36:57204ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08205 return NULL;
206}
[email protected]5bc248a2012-04-04 23:38:11207
[email protected]1ce88e82013-06-28 05:17:10208content::WebContents*
209ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
210 return GetAssociatedWebContents();
211}
212
[email protected]bfdffe2b2009-04-24 22:05:35213void ExtensionFunctionDispatcher::GetAllFunctionNames(
214 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35215 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35216}
217
[email protected]b83e4602009-05-15 22:58:33218bool ExtensionFunctionDispatcher::OverrideFunction(
219 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35220 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
221 factory);
[email protected]b83e4602009-05-15 22:58:33222}
223
[email protected]c357acb42011-06-09 20:52:42224// static
225void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]1a0436892014-04-01 00:38:25226 InfoMap* extension_info_map,
227 void* profile_id,
[email protected]c357acb42011-06-09 20:52:42228 int render_process_id,
[email protected]1a0436892014-04-01 00:38:25229 base::WeakPtr<ExtensionMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53230 int routing_id,
[email protected]c357acb42011-06-09 20:52:42231 const ExtensionHostMsg_Request_Params& params) {
232 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11233 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]35548ab2013-05-15 08:59:47234
235 ExtensionFunction::ResponseCallback callback(
236 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
237 params.request_id));
238
[email protected]6f371442011-11-09 06:45:46239 scoped_refptr<ExtensionFunction> function(
[email protected]1a0436892014-04-01 00:38:25240 CreateExtensionFunction(params,
241 extension,
242 render_process_id,
[email protected]5bc248a2012-04-04 23:38:11243 extension_info_map->process_map(),
244 g_global_io_data.Get().api.get(),
[email protected]1a0436892014-04-01 00:38:25245 profile_id,
246 callback));
[email protected]ecc854a2013-08-22 10:12:42247 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42248 return;
249
250 IOThreadExtensionFunction* function_io =
251 function->AsIOThreadExtensionFunction();
252 if (!function_io) {
253 NOTREACHED();
254 return;
255 }
[email protected]44295a12013-06-05 08:45:46256 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42257 function_io->set_extension_info_map(extension_info_map);
258 function->set_include_incognito(
259 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25260
[email protected]ecc854a2013-08-22 10:12:42261 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07262 return;
[email protected]d2fe22ff2012-10-03 00:40:07263
[email protected]1a0436892014-04-01 00:38:25264 QuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29265 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09266 function.get(),
[email protected]85231d72012-08-31 09:45:29267 &params.arguments,
268 base::TimeTicks::Now());
269 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53270 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
[email protected]1a0436892014-04-01 00:38:25271 NotifyApiFunctionCalled(extension->id(),
272 params.name,
273 args.Pass(),
274 static_cast<content::BrowserContext*>(profile_id));
[email protected]04783d22014-04-11 13:26:21275 UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.FunctionCalls",
276 function->histogram_value());
[email protected]a0c91a9f2014-05-03 03:41:43277 function->Run()->Execute();
[email protected]fd50e7b2011-11-03 09:20:25278 } else {
[email protected]85231d72012-08-31 09:45:29279 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25280 }
[email protected]c357acb42011-06-09 20:52:42281}
282
[email protected]96e6a1032013-11-28 06:58:03283ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
284 content::BrowserContext* browser_context,
285 Delegate* delegate)
[email protected]86376022013-12-03 18:18:05286 : browser_context_(browser_context),
[email protected]96e6a1032013-11-28 06:58:03287 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35288}
289
[email protected]32dda362009-06-05 19:07:01290ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01291}
292
[email protected]c5dbef02011-05-13 05:06:09293void ExtensionFunctionDispatcher::Dispatch(
294 const ExtensionHostMsg_Request_Params& params,
295 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47296 UIThreadResponseCallbackWrapperMap::const_iterator
297 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
298 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
299 if (iter == ui_thread_response_callback_wrappers_.end()) {
300 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
301 render_view_host);
302 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
303 } else {
304 callback_wrapper = iter->second;
305 }
306
[email protected]6dd625e2013-12-20 17:03:07307 DispatchWithCallbackInternal(
308 params, render_view_host, NULL,
309 callback_wrapper->CreateCallback(params.request_id));
[email protected]35548ab2013-05-15 08:59:47310}
311
312void ExtensionFunctionDispatcher::DispatchWithCallback(
313 const ExtensionHostMsg_Request_Params& params,
[email protected]6dd625e2013-12-20 17:03:07314 content::RenderFrameHost* render_frame_host,
[email protected]35548ab2013-05-15 08:59:47315 const ExtensionFunction::ResponseCallback& callback) {
[email protected]6dd625e2013-12-20 17:03:07316 DispatchWithCallbackInternal(params, NULL, render_frame_host, callback);
317}
318
319void ExtensionFunctionDispatcher::DispatchWithCallbackInternal(
320 const ExtensionHostMsg_Request_Params& params,
321 RenderViewHost* render_view_host,
322 content::RenderFrameHost* render_frame_host,
323 const ExtensionFunction::ResponseCallback& callback) {
324 DCHECK(render_view_host || render_frame_host);
[email protected]35548ab2013-05-15 08:59:47325 // TODO(yzshen): There is some shared logic between this method and
326 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]1a0436892014-04-01 00:38:25327 ProcessMap* process_map = ProcessMap::Get(browser_context_);
[email protected]86376022013-12-03 18:18:05328 if (!process_map)
[email protected]c5dbef02011-05-13 05:06:09329 return;
330
[email protected]1a0436892014-04-01 00:38:25331 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
[email protected]aab23102014-02-05 18:57:55332 const Extension* extension = registry->enabled_extensions().GetByID(
[email protected]615d88f2011-12-13 01:47:44333 params.extension_id);
[email protected]aab23102014-02-05 18:57:55334 if (!extension) {
335 extension =
336 registry->enabled_extensions().GetHostedAppByURL(params.source_url);
337 }
[email protected]c5dbef02011-05-13 05:06:09338
[email protected]6dd625e2013-12-20 17:03:07339 int process_id = render_view_host ? render_view_host->GetProcess()->GetID() :
340 render_frame_host->GetProcess()->GetID();
[email protected]8add5412011-10-01 21:02:14341 scoped_refptr<ExtensionFunction> function(
[email protected]86376022013-12-03 18:18:05342 CreateExtensionFunction(params,
343 extension,
[email protected]6dd625e2013-12-20 17:03:07344 process_id,
[email protected]86376022013-12-03 18:18:05345 *process_map,
[email protected]1a0436892014-04-01 00:38:25346 ExtensionAPI::GetSharedInstance(),
[email protected]86376022013-12-03 18:18:05347 browser_context_,
348 callback));
[email protected]ecc854a2013-08-22 10:12:42349 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17350 return;
[email protected]f82d57b52011-04-27 19:13:17351
[email protected]a2aef2e2011-05-26 22:48:12352 UIThreadExtensionFunction* function_ui =
353 function->AsUIThreadExtensionFunction();
354 if (!function_ui) {
355 NOTREACHED();
356 return;
357 }
[email protected]6dd625e2013-12-20 17:03:07358 if (render_view_host) {
359 function_ui->SetRenderViewHost(render_view_host);
360 } else {
361 function_ui->SetRenderFrameHost(render_frame_host);
362 }
[email protected]a2aef2e2011-05-26 22:48:12363 function_ui->set_dispatcher(AsWeakPtr());
[email protected]659be682014-02-28 15:06:45364 function_ui->set_browser_context(browser_context_);
[email protected]1d5cf4142014-01-24 18:25:22365 function->set_include_incognito(
[email protected]944ad022014-02-07 23:00:23366 ExtensionsBrowserClient::Get()->CanExtensionCrossIncognito(
367 extension, browser_context_));
[email protected]cb0ce1e022010-03-10 19:54:41368
[email protected]ecc854a2013-08-22 10:12:42369 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07370 return;
[email protected]d2fe22ff2012-10-03 00:40:07371
[email protected]aab23102014-02-05 18:57:55372 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context_);
[email protected]1a0436892014-04-01 00:38:25373 QuotaService* quota = extension_system->quota_service();
[email protected]85231d72012-08-31 09:45:29374 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09375 function.get(),
[email protected]85231d72012-08-31 09:45:29376 &params.arguments,
377 base::TimeTicks::Now());
378 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53379 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
380
[email protected]b32260f2014-02-06 10:03:41381 NotifyApiFunctionCalled(
382 extension->id(), params.name, args.Pass(), browser_context_);
[email protected]04783d22014-04-11 13:26:21383 UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.FunctionCalls",
384 function->histogram_value());
[email protected]a0c91a9f2014-05-03 03:41:43385 function->Run()->Execute();
[email protected]d13950e2009-12-04 01:43:02386 } else {
[email protected]85231d72012-08-31 09:45:29387 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02388 }
[email protected]720ad1312012-02-27 23:07:36389
[email protected]efb4b082012-10-17 22:28:28390 // Note: do not access |this| after this point. We may have been deleted
391 // if function->Run() ended up closing the tab that owns us.
392
[email protected]5734e882012-05-04 22:17:56393 // Check if extension was uninstalled by management.uninstall.
[email protected]aab23102014-02-05 18:57:55394 if (!registry->enabled_extensions().GetByID(params.extension_id))
[email protected]5734e882012-05-04 22:17:56395 return;
396
[email protected]720ad1312012-02-27 23:07:36397 // We only adjust the keepalive count for UIThreadExtensionFunction for
398 // now, largely for simplicity's sake. This is OK because currently, only
399 // the webRequest API uses IOThreadExtensionFunction, and that API is not
400 // compatible with lazy background pages.
[email protected]86376022013-12-03 18:18:05401 extension_system->process_manager()->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36402}
403
404void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
405 const Extension* extension) {
[email protected]59b0e602014-01-30 00:41:24406 ExtensionSystem::Get(browser_context_)->process_manager()->
[email protected]be93bba02012-10-24 16:44:03407 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35408}
409
[email protected]c357acb42011-06-09 20:52:42410// static
[email protected]d2fe22ff2012-10-03 00:40:07411bool ExtensionFunctionDispatcher::CheckPermissions(
412 ExtensionFunction* function,
413 const Extension* extension,
414 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47415 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07416 if (!function->HasPermission()) {
417 LOG(ERROR) << "Extension " << extension->id() << " does not have "
418 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47419 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07420 return false;
421 }
422 return true;
423}
424
[email protected]f33542112013-02-04 16:52:38425namespace {
426
427// Only COMPONENT hosted apps may call extension APIs, and they are limited
428// to just the permissions they explicitly request. They should not have access
429// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
430// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11431// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
432// it altogether).
[email protected]f33542112013-02-04 16:52:38433bool AllowHostedAppAPICall(const Extension& extension,
434 const GURL& source_url,
435 const std::string& function_name) {
[email protected]1a0436892014-04-01 00:38:25436 if (extension.location() != Manifest::COMPONENT)
[email protected]f33542112013-02-04 16:52:38437 return false;
438
439 if (!extension.web_extent().MatchesURL(source_url))
440 return false;
441
[email protected]713a6e82013-12-19 23:06:05442 // Note: Not BLESSED_WEB_PAGE_CONTEXT here because these component hosted app
443 // entities have traditionally been treated as blessed extensions, for better
444 // or worse.
[email protected]b5b26b72013-08-02 00:25:11445 Feature::Availability availability =
446 ExtensionAPI::GetSharedInstance()->IsAvailable(
447 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
448 source_url);
449 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38450}
451
452} // namespace
453
454
[email protected]d2fe22ff2012-10-03 00:40:07455// static
[email protected]c357acb42011-06-09 20:52:42456ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
457 const ExtensionHostMsg_Request_Params& params,
458 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46459 int requesting_process_id,
[email protected]1a0436892014-04-01 00:38:25460 const ProcessMap& process_map,
461 ExtensionAPI* api,
462 void* profile_id,
[email protected]35548ab2013-05-15 08:59:47463 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42464 if (!extension) {
[email protected]6f371442011-11-09 06:45:46465 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47466 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46467 return NULL;
468 }
469
[email protected]f33542112013-02-04 16:52:38470 // Most hosted apps can't call APIs.
471 bool allowed = true;
472 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47473 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38474
475 // Privileged APIs can only be called from the process the extension
476 // is running in.
477 if (allowed && api->IsPrivileged(params.name))
478 allowed = process_map.Contains(extension->id(), requesting_process_id);
479
480 if (!allowed) {
481 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
482 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46483 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47484 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42485 return NULL;
486 }
487
[email protected]c357acb42011-06-09 20:52:42488 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35489 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14490 if (!function) {
491 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47492 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14493 return NULL;
494 }
495
[email protected]c357acb42011-06-09 20:52:42496 function->SetArgs(&params.arguments);
497 function->set_source_url(params.source_url);
498 function->set_request_id(params.request_id);
499 function->set_has_callback(params.has_callback);
500 function->set_user_gesture(params.user_gesture);
501 function->set_extension(extension);
[email protected]1a0436892014-04-01 00:38:25502 function->set_profile_id(profile_id);
[email protected]35548ab2013-05-15 08:59:47503 function->set_response_callback(callback);
[email protected]eb7ef5f2014-02-06 09:59:19504 function->set_source_tab_id(params.source_tab_id);
[email protected]3d0e2262012-08-02 15:32:16505
[email protected]c357acb42011-06-09 20:52:42506 return function;
507}
508
509// static
[email protected]c5dbef02011-05-13 05:06:09510void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47511 const ExtensionFunction::ResponseCallback& callback) {
[email protected]023b3d12013-12-23 18:46:49512 base::ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47513 callback.Run(ExtensionFunction::FAILED, empty_list,
514 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35515}
[email protected]1a0436892014-04-01 00:38:25516
517} // namespace extensions