blob: e5ab1e4e73fa1e670a20de8358284f5c080be743 [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]c357acb42011-06-09 20:52:4215#include "chrome/browser/renderer_host/chrome_render_message_filter.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]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]1c321ee2012-05-21 03:02:3438using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1139using extensions::ExtensionAPI;
[email protected]eafbd05a2014-02-06 19:29:0140using extensions::ExtensionsBrowserClient;
[email protected]86376022013-12-03 18:18:0541using extensions::ExtensionSystem;
[email protected]b5b26b72013-08-02 00:25:1142using extensions::Feature;
[email protected]b32260f2014-02-06 10:03:4143using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1144using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1145
[email protected]5bc248a2012-04-04 23:38:1146namespace {
47
[email protected]b32260f2014-02-06 10:03:4148// Notifies the ApiActivityMonitor that an extension API function has been
49// called. May be called from any thread.
50void NotifyApiFunctionCalled(const std::string& extension_id,
51 const std::string& api_name,
52 scoped_ptr<base::ListValue> args,
53 content::BrowserContext* browser_context) {
[email protected]eafbd05a2014-02-06 19:29:0154 // The ApiActivityMonitor can only be accessed from the main (UI) thread. If
[email protected]b32260f2014-02-06 10:03:4155 // we're running on the wrong thread, re-dispatch from the main thread.
[email protected]4b64d712013-01-17 17:53:1756 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
57 BrowserThread::PostTask(BrowserThread::UI,
58 FROM_HERE,
[email protected]b32260f2014-02-06 10:03:4159 base::Bind(&NotifyApiFunctionCalled,
[email protected]e5a440c2013-06-04 21:55:1260 extension_id,
[email protected]4b64d712013-01-17 17:53:1761 api_name,
[email protected]c02087b512013-02-04 03:09:2062 base::Passed(&args),
[email protected]86376022013-12-03 18:18:0563 browser_context));
[email protected]b32260f2014-02-06 10:03:4164 return;
[email protected]efd75992011-12-15 22:42:4265 }
[email protected]eafbd05a2014-02-06 19:29:0166 // The BrowserContext may become invalid after the task above is posted.
67 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
68 return;
69
[email protected]b32260f2014-02-06 10:03:4170 extensions::ApiActivityMonitor* monitor =
[email protected]eafbd05a2014-02-06 19:29:0171 ExtensionsBrowserClient::Get()->GetApiActivityMonitor(browser_context);
[email protected]b32260f2014-02-06 10:03:4172 if (monitor)
73 monitor->OnApiFunctionCalled(extension_id, api_name, args.Pass());
[email protected]efd75992011-12-15 22:42:4274}
75
[email protected]5bc248a2012-04-04 23:38:1176// Separate copy of ExtensionAPI used for IO thread extension functions. We need
77// this because ExtensionAPI has mutable data. It should be possible to remove
78// this once all the extension APIs are updated to the feature system.
79struct Static {
80 Static()
81 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
82 }
83 scoped_ptr<extensions::ExtensionAPI> api;
84};
85base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
86
[email protected]35548ab2013-05-15 08:59:4787// Kills the specified process because it sends us a malformed message.
88void KillBadMessageSender(base::ProcessHandle process) {
89 NOTREACHED();
[email protected]e6e30ac2014-01-13 21:24:3990 content::RecordAction(base::UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]35548ab2013-05-15 08:59:4791 if (process)
92 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
93}
94
95void CommonResponseCallback(IPC::Sender* ipc_sender,
96 int routing_id,
97 base::ProcessHandle peer_process,
98 int request_id,
99 ExtensionFunction::ResponseType type,
100 const base::ListValue& results,
101 const std::string& error) {
102 DCHECK(ipc_sender);
103
104 if (type == ExtensionFunction::BAD_MESSAGE) {
105 // The renderer has done validation before sending extension api requests.
106 // Therefore, we should never receive a request that is invalid in a way
107 // that JSON validation in the renderer should have caught. It could be an
108 // attacker trying to exploit the browser, so we crash the renderer instead.
109 LOG(ERROR) <<
110 "Terminating renderer because of malformed extension message.";
111 if (content::RenderProcessHost::run_renderer_in_process()) {
112 // In single process mode it is better if we don't suicide but just crash.
113 CHECK(false);
114 } else {
115 KillBadMessageSender(peer_process);
116 }
117
118 return;
119 }
120
121 ipc_sender->Send(new ExtensionMsg_Response(
122 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
123 error));
124}
125
126void IOThreadResponseCallback(
127 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
128 int routing_id,
129 int request_id,
130 ExtensionFunction::ResponseType type,
131 const base::ListValue& results,
132 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45133 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47134 return;
135
[email protected]e8dad9b2013-06-04 04:43:45136 CommonResponseCallback(ipc_sender.get(),
137 routing_id,
[email protected]950be552013-07-10 19:13:02138 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45139 request_id,
140 type,
141 results,
142 error);
[email protected]35548ab2013-05-15 08:59:47143}
144
[email protected]5bc248a2012-04-04 23:38:11145} // namespace
146
[email protected]35548ab2013-05-15 08:59:47147class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
[email protected]ab2a7e3c2013-10-22 03:41:36148 : public content::WebContentsObserver {
[email protected]35548ab2013-05-15 08:59:47149 public:
150 UIThreadResponseCallbackWrapper(
151 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
152 RenderViewHost* render_view_host)
[email protected]ab2a7e3c2013-10-22 03:41:36153 : content::WebContentsObserver(
154 content::WebContents::FromRenderViewHost(render_view_host)),
[email protected]35548ab2013-05-15 08:59:47155 dispatcher_(dispatcher),
[email protected]ab2a7e3c2013-10-22 03:41:36156 render_view_host_(render_view_host),
[email protected]35548ab2013-05-15 08:59:47157 weak_ptr_factory_(this) {
158 }
159
160 virtual ~UIThreadResponseCallbackWrapper() {
161 }
162
[email protected]ab2a7e3c2013-10-22 03:41:36163 // content::WebContentsObserver overrides.
164 virtual void RenderViewDeleted(
[email protected]35548ab2013-05-15 08:59:47165 RenderViewHost* render_view_host) OVERRIDE {
[email protected]54ee8192014-03-29 17:37:24166 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]ab2a7e3c2013-10-22 03:41:36167 if (render_view_host != render_view_host_)
168 return;
169
[email protected]e8dad9b2013-06-04 04:43:45170 if (dispatcher_.get()) {
171 dispatcher_->ui_thread_response_callback_wrappers_
172 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47173 }
174
[email protected]ab2a7e3c2013-10-22 03:41:36175 delete this;
[email protected]35548ab2013-05-15 08:59:47176 }
177
178 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
179 return base::Bind(
180 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
181 weak_ptr_factory_.GetWeakPtr(),
182 request_id);
183 }
184
185 private:
186 void OnExtensionFunctionCompleted(int request_id,
187 ExtensionFunction::ResponseType type,
188 const base::ListValue& results,
189 const std::string& error) {
190 CommonResponseCallback(
[email protected]ab2a7e3c2013-10-22 03:41:36191 render_view_host_, render_view_host_->GetRoutingID(),
192 render_view_host_->GetProcess()->GetHandle(), request_id, type,
[email protected]35548ab2013-05-15 08:59:47193 results, error);
194 }
195
196 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
[email protected]ab2a7e3c2013-10-22 03:41:36197 content::RenderViewHost* render_view_host_;
[email protected]35548ab2013-05-15 08:59:47198 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
199
200 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
201};
202
[email protected]44f4b132012-07-17 20:36:57203extensions::WindowController*
204ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08205 const {
206 return NULL;
207}
208
209content::WebContents*
[email protected]44f4b132012-07-17 20:36:57210ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08211 return NULL;
212}
[email protected]5bc248a2012-04-04 23:38:11213
[email protected]1ce88e82013-06-28 05:17:10214content::WebContents*
215ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
216 return GetAssociatedWebContents();
217}
218
[email protected]bfdffe2b2009-04-24 22:05:35219void ExtensionFunctionDispatcher::GetAllFunctionNames(
220 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35221 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35222}
223
[email protected]b83e4602009-05-15 22:58:33224bool ExtensionFunctionDispatcher::OverrideFunction(
225 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35226 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
227 factory);
[email protected]b83e4602009-05-15 22:58:33228}
229
[email protected]c357acb42011-06-09 20:52:42230// static
231void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]38427a12013-11-09 17:34:20232 extensions::InfoMap* extension_info_map,
[email protected]86376022013-12-03 18:18:05233 void* browser_context,
[email protected]c357acb42011-06-09 20:52:42234 int render_process_id,
235 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53236 int routing_id,
[email protected]c357acb42011-06-09 20:52:42237 const ExtensionHostMsg_Request_Params& params) {
238 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11239 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]35548ab2013-05-15 08:59:47240
241 ExtensionFunction::ResponseCallback callback(
242 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
243 params.request_id));
244
[email protected]6f371442011-11-09 06:45:46245 scoped_refptr<ExtensionFunction> function(
246 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11247 extension_info_map->process_map(),
248 g_global_io_data.Get().api.get(),
[email protected]86376022013-12-03 18:18:05249 browser_context, callback));
[email protected]ecc854a2013-08-22 10:12:42250 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42251 return;
252
253 IOThreadExtensionFunction* function_io =
254 function->AsIOThreadExtensionFunction();
255 if (!function_io) {
256 NOTREACHED();
257 return;
258 }
[email protected]44295a12013-06-05 08:45:46259 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42260 function_io->set_extension_info_map(extension_info_map);
261 function->set_include_incognito(
262 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25263
[email protected]ecc854a2013-08-22 10:12:42264 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07265 return;
[email protected]d2fe22ff2012-10-03 00:40:07266
[email protected]38427a12013-11-09 17:34:20267 extensions::QuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29268 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09269 function.get(),
[email protected]85231d72012-08-31 09:45:29270 &params.arguments,
271 base::TimeTicks::Now());
272 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53273 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
[email protected]b32260f2014-02-06 10:03:41274 NotifyApiFunctionCalled(
275 extension->id(),
276 params.name,
277 args.Pass(),
278 static_cast<content::BrowserContext*>(browser_context));
[email protected]fd50e7b2011-11-03 09:20:25279 function->Run();
280 } else {
[email protected]85231d72012-08-31 09:45:29281 function->OnQuotaExceeded(violation_error);
[email protected]fd50e7b2011-11-03 09:20:25282 }
[email protected]c357acb42011-06-09 20:52:42283}
284
[email protected]96e6a1032013-11-28 06:58:03285ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
286 content::BrowserContext* browser_context,
287 Delegate* delegate)
[email protected]86376022013-12-03 18:18:05288 : browser_context_(browser_context),
[email protected]96e6a1032013-11-28 06:58:03289 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35290}
291
[email protected]32dda362009-06-05 19:07:01292ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01293}
294
[email protected]c5dbef02011-05-13 05:06:09295void ExtensionFunctionDispatcher::Dispatch(
296 const ExtensionHostMsg_Request_Params& params,
297 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47298 UIThreadResponseCallbackWrapperMap::const_iterator
299 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
300 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
301 if (iter == ui_thread_response_callback_wrappers_.end()) {
302 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
303 render_view_host);
304 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
305 } else {
306 callback_wrapper = iter->second;
307 }
308
[email protected]6dd625e2013-12-20 17:03:07309 DispatchWithCallbackInternal(
310 params, render_view_host, NULL,
311 callback_wrapper->CreateCallback(params.request_id));
[email protected]35548ab2013-05-15 08:59:47312}
313
314void ExtensionFunctionDispatcher::DispatchWithCallback(
315 const ExtensionHostMsg_Request_Params& params,
[email protected]6dd625e2013-12-20 17:03:07316 content::RenderFrameHost* render_frame_host,
[email protected]35548ab2013-05-15 08:59:47317 const ExtensionFunction::ResponseCallback& callback) {
[email protected]6dd625e2013-12-20 17:03:07318 DispatchWithCallbackInternal(params, NULL, render_frame_host, callback);
319}
320
321void ExtensionFunctionDispatcher::DispatchWithCallbackInternal(
322 const ExtensionHostMsg_Request_Params& params,
323 RenderViewHost* render_view_host,
324 content::RenderFrameHost* render_frame_host,
325 const ExtensionFunction::ResponseCallback& callback) {
326 DCHECK(render_view_host || render_frame_host);
[email protected]35548ab2013-05-15 08:59:47327 // TODO(yzshen): There is some shared logic between this method and
328 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]fafdc842014-01-17 18:09:08329 extensions::ProcessMap* process_map =
330 extensions::ProcessMap::Get(browser_context_);
[email protected]86376022013-12-03 18:18:05331 if (!process_map)
[email protected]c5dbef02011-05-13 05:06:09332 return;
333
[email protected]aab23102014-02-05 18:57:55334 extensions::ExtensionRegistry* registry =
335 extensions::ExtensionRegistry::Get(browser_context_);
336 const Extension* extension = registry->enabled_extensions().GetByID(
[email protected]615d88f2011-12-13 01:47:44337 params.extension_id);
[email protected]aab23102014-02-05 18:57:55338 if (!extension) {
339 extension =
340 registry->enabled_extensions().GetHostedAppByURL(params.source_url);
341 }
[email protected]c5dbef02011-05-13 05:06:09342
[email protected]6dd625e2013-12-20 17:03:07343 int process_id = render_view_host ? render_view_host->GetProcess()->GetID() :
344 render_frame_host->GetProcess()->GetID();
[email protected]8add5412011-10-01 21:02:14345 scoped_refptr<ExtensionFunction> function(
[email protected]86376022013-12-03 18:18:05346 CreateExtensionFunction(params,
347 extension,
[email protected]6dd625e2013-12-20 17:03:07348 process_id,
[email protected]86376022013-12-03 18:18:05349 *process_map,
[email protected]5bc248a2012-04-04 23:38:11350 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]86376022013-12-03 18:18:05351 browser_context_,
352 callback));
[email protected]ecc854a2013-08-22 10:12:42353 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17354 return;
[email protected]f82d57b52011-04-27 19:13:17355
[email protected]a2aef2e2011-05-26 22:48:12356 UIThreadExtensionFunction* function_ui =
357 function->AsUIThreadExtensionFunction();
358 if (!function_ui) {
359 NOTREACHED();
360 return;
361 }
[email protected]6dd625e2013-12-20 17:03:07362 if (render_view_host) {
363 function_ui->SetRenderViewHost(render_view_host);
364 } else {
365 function_ui->SetRenderFrameHost(render_frame_host);
366 }
[email protected]a2aef2e2011-05-26 22:48:12367 function_ui->set_dispatcher(AsWeakPtr());
[email protected]659be682014-02-28 15:06:45368 function_ui->set_browser_context(browser_context_);
[email protected]1d5cf4142014-01-24 18:25:22369 function->set_include_incognito(
[email protected]944ad022014-02-07 23:00:23370 ExtensionsBrowserClient::Get()->CanExtensionCrossIncognito(
371 extension, browser_context_));
[email protected]cb0ce1e022010-03-10 19:54:41372
[email protected]ecc854a2013-08-22 10:12:42373 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07374 return;
[email protected]d2fe22ff2012-10-03 00:40:07375
[email protected]aab23102014-02-05 18:57:55376 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context_);
377 extensions::QuotaService* quota = extension_system->quota_service();
[email protected]85231d72012-08-31 09:45:29378 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09379 function.get(),
[email protected]85231d72012-08-31 09:45:29380 &params.arguments,
381 base::TimeTicks::Now());
382 if (violation_error.empty()) {
[email protected]061a3c22014-01-22 01:48:53383 scoped_ptr<base::ListValue> args(params.arguments.DeepCopy());
384
[email protected]d070ec62010-07-27 21:28:26385 // See crbug.com/39178.
[email protected]0b9de032014-03-15 05:47:01386 ExtensionsBrowserClient::Get()->PermitExternalProtocolHandler();
[email protected]b32260f2014-02-06 10:03:41387 NotifyApiFunctionCalled(
388 extension->id(), params.name, args.Pass(), browser_context_);
[email protected]d13950e2009-12-04 01:43:02389 function->Run();
390 } else {
[email protected]85231d72012-08-31 09:45:29391 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02392 }
[email protected]720ad1312012-02-27 23:07:36393
[email protected]efb4b082012-10-17 22:28:28394 // Note: do not access |this| after this point. We may have been deleted
395 // if function->Run() ended up closing the tab that owns us.
396
[email protected]5734e882012-05-04 22:17:56397 // Check if extension was uninstalled by management.uninstall.
[email protected]aab23102014-02-05 18:57:55398 if (!registry->enabled_extensions().GetByID(params.extension_id))
[email protected]5734e882012-05-04 22:17:56399 return;
400
[email protected]720ad1312012-02-27 23:07:36401 // We only adjust the keepalive count for UIThreadExtensionFunction for
402 // now, largely for simplicity's sake. This is OK because currently, only
403 // the webRequest API uses IOThreadExtensionFunction, and that API is not
404 // compatible with lazy background pages.
[email protected]86376022013-12-03 18:18:05405 extension_system->process_manager()->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36406}
407
408void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
409 const Extension* extension) {
[email protected]59b0e602014-01-30 00:41:24410 ExtensionSystem::Get(browser_context_)->process_manager()->
[email protected]be93bba02012-10-24 16:44:03411 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35412}
413
[email protected]c357acb42011-06-09 20:52:42414// static
[email protected]d2fe22ff2012-10-03 00:40:07415bool ExtensionFunctionDispatcher::CheckPermissions(
416 ExtensionFunction* function,
417 const Extension* extension,
418 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47419 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07420 if (!function->HasPermission()) {
421 LOG(ERROR) << "Extension " << extension->id() << " does not have "
422 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47423 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07424 return false;
425 }
426 return true;
427}
428
[email protected]f33542112013-02-04 16:52:38429namespace {
430
431// Only COMPONENT hosted apps may call extension APIs, and they are limited
432// to just the permissions they explicitly request. They should not have access
433// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
434// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11435// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
436// it altogether).
[email protected]f33542112013-02-04 16:52:38437bool AllowHostedAppAPICall(const Extension& extension,
438 const GURL& source_url,
439 const std::string& function_name) {
440 if (extension.location() != extensions::Manifest::COMPONENT)
441 return false;
442
443 if (!extension.web_extent().MatchesURL(source_url))
444 return false;
445
[email protected]713a6e82013-12-19 23:06:05446 // Note: Not BLESSED_WEB_PAGE_CONTEXT here because these component hosted app
447 // entities have traditionally been treated as blessed extensions, for better
448 // or worse.
[email protected]b5b26b72013-08-02 00:25:11449 Feature::Availability availability =
450 ExtensionAPI::GetSharedInstance()->IsAvailable(
451 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
452 source_url);
453 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38454}
455
456} // namespace
457
458
[email protected]d2fe22ff2012-10-03 00:40:07459// static
[email protected]c357acb42011-06-09 20:52:42460ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
461 const ExtensionHostMsg_Request_Params& params,
462 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46463 int requesting_process_id,
464 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11465 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18466 void* profile,
[email protected]35548ab2013-05-15 08:59:47467 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42468 if (!extension) {
[email protected]6f371442011-11-09 06:45:46469 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47470 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46471 return NULL;
472 }
473
[email protected]f33542112013-02-04 16:52:38474 // Most hosted apps can't call APIs.
475 bool allowed = true;
476 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47477 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38478
479 // Privileged APIs can only be called from the process the extension
480 // is running in.
481 if (allowed && api->IsPrivileged(params.name))
482 allowed = process_map.Contains(extension->id(), requesting_process_id);
483
484 if (!allowed) {
485 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
486 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46487 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47488 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42489 return NULL;
490 }
491
[email protected]c357acb42011-06-09 20:52:42492 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35493 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14494 if (!function) {
495 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47496 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14497 return NULL;
498 }
499
[email protected]c357acb42011-06-09 20:52:42500 function->SetArgs(&params.arguments);
501 function->set_source_url(params.source_url);
502 function->set_request_id(params.request_id);
503 function->set_has_callback(params.has_callback);
504 function->set_user_gesture(params.user_gesture);
505 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32506 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47507 function->set_response_callback(callback);
[email protected]eb7ef5f2014-02-06 09:59:19508 function->set_source_tab_id(params.source_tab_id);
[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}