blob: d6da6d080303f9ff6af3c9dbfe5ffbcc337e9b84 [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]d13950e2009-12-04 01:43:0223#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]83820d42011-11-12 22:03:1124#include "chrome/browser/extensions/process_map.h"
[email protected]ed2b1002011-05-25 14:12:1025#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3326#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4227#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]83820d42011-11-12 22:03:1128#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2329#include "chrome/common/extensions/extension_messages.h"
[email protected]615d88f2011-12-13 01:47:4430#include "chrome/common/extensions/extension_set.h"
[email protected]9c45b7182009-08-04 16:44:4331#include "chrome/common/url_constants.h"
[email protected]4b64d712013-01-17 17:53:1732#include "content/public/browser/browser_thread.h"
[email protected]c333e792012-01-06 16:57:3933#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3334#include "content/public/browser/render_view_host.h"
[email protected]35548ab2013-05-15 08:59:4735#include "content/public/browser/render_view_host_observer.h"
36#include "content/public/browser/user_metrics.h"
37#include "content/public/common/result_codes.h"
[email protected]f82d57b52011-04-27 19:13:1738#include "ipc/ipc_message.h"
39#include "ipc/ipc_message_macros.h"
[email protected]d0fcff72013-07-23 02:45:4340#include "webkit/common/resource_type.h"
[email protected]61b55b62011-03-24 09:03:1041
[email protected]1c321ee2012-05-21 03:02:3442using extensions::Extension;
[email protected]83820d42011-11-12 22:03:1143using extensions::ExtensionAPI;
[email protected]b5b26b72013-08-02 00:25:1144using extensions::Feature;
[email protected]eaabba22012-03-07 15:02:1145using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1146
[email protected]5bc248a2012-04-04 23:38:1147namespace {
48
[email protected]e5a440c2013-06-04 21:55:1249void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1750 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4151 scoped_ptr<base::ListValue> args,
[email protected]4b64d712013-01-17 17:53:1752 Profile* profile) {
53 // The ActivityLog can only be accessed from the main (UI) thread. If we're
54 // running on the wrong thread, re-dispatch from the main thread.
55 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
56 BrowserThread::PostTask(BrowserThread::UI,
57 FROM_HERE,
58 base::Bind(&LogSuccess,
[email protected]e5a440c2013-06-04 21:55:1259 extension_id,
[email protected]4b64d712013-01-17 17:53:1760 api_name,
[email protected]c02087b512013-02-04 03:09:2061 base::Passed(&args),
[email protected]4b64d712013-01-17 17:53:1762 profile));
63 } else {
64 extensions::ActivityLog* activity_log =
65 extensions::ActivityLog::GetInstance(profile);
[email protected]05602fa32013-08-01 06:30:4366 scoped_refptr<extensions::Action> action =
67 new extensions::Action(extension_id,
68 base::Time::Now(),
69 extensions::Action::ACTION_API_CALL,
70 api_name);
71 action->set_args(args.Pass());
72 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:4273 }
74}
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();
90 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
91 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
148 : public content::RenderViewHostObserver {
149 public:
150 UIThreadResponseCallbackWrapper(
151 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
152 RenderViewHost* render_view_host)
153 : content::RenderViewHostObserver(render_view_host),
154 dispatcher_(dispatcher),
155 weak_ptr_factory_(this) {
156 }
157
158 virtual ~UIThreadResponseCallbackWrapper() {
159 }
160
161 // content::RenderViewHostObserver overrides.
162 virtual void RenderViewHostDestroyed(
163 RenderViewHost* render_view_host) OVERRIDE {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[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
170 // This call will delete |this|.
171 content::RenderViewHostObserver::RenderViewHostDestroyed(render_view_host);
172 }
173
174 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
175 return base::Bind(
176 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
177 weak_ptr_factory_.GetWeakPtr(),
178 request_id);
179 }
180
181 private:
182 void OnExtensionFunctionCompleted(int request_id,
183 ExtensionFunction::ResponseType type,
184 const base::ListValue& results,
185 const std::string& error) {
186 CommonResponseCallback(
187 render_view_host(), render_view_host()->GetRoutingID(),
188 render_view_host()->GetProcess()->GetHandle(), request_id, type,
189 results, error);
190 }
191
192 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
193 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
194
195 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
196};
197
[email protected]44f4b132012-07-17 20:36:57198extensions::WindowController*
199ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08200 const {
201 return NULL;
202}
203
204content::WebContents*
[email protected]44f4b132012-07-17 20:36:57205ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08206 return NULL;
207}
[email protected]5bc248a2012-04-04 23:38:11208
[email protected]1ce88e82013-06-28 05:17:10209content::WebContents*
210ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
211 return GetAssociatedWebContents();
212}
213
[email protected]bfdffe2b2009-04-24 22:05:35214void ExtensionFunctionDispatcher::GetAllFunctionNames(
215 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35216 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35217}
218
[email protected]b83e4602009-05-15 22:58:33219bool ExtensionFunctionDispatcher::OverrideFunction(
220 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35221 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
222 factory);
[email protected]b83e4602009-05-15 22:58:33223}
224
225void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35226 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33227}
228
[email protected]c357acb42011-06-09 20:52:42229// static
230void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25231 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18232 void* profile,
[email protected]c357acb42011-06-09 20:52:42233 int render_process_id,
234 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53235 int routing_id,
[email protected]c357acb42011-06-09 20:52:42236 const ExtensionHostMsg_Request_Params& params) {
237 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11238 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]4b64d712013-01-17 17:53:17239 Profile* profile_cast = static_cast<Profile*>(profile);
[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]35548ab2013-05-15 08:59:47249 profile, callback));
[email protected]4b64d712013-01-17 17:53:17250 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
251
[email protected]ecc854a2013-08-22 10:12:42252 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42253 return;
254
255 IOThreadExtensionFunction* function_io =
256 function->AsIOThreadExtensionFunction();
257 if (!function_io) {
258 NOTREACHED();
259 return;
260 }
[email protected]44295a12013-06-05 08:45:46261 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42262 function_io->set_extension_info_map(extension_info_map);
263 function->set_include_incognito(
264 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25265
[email protected]ecc854a2013-08-22 10:12:42266 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07267 return;
[email protected]d2fe22ff2012-10-03 00:40:07268
[email protected]36296912012-03-20 11:08:49269 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29270 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09271 function.get(),
[email protected]85231d72012-08-31 09:45:29272 &params.arguments,
273 base::TimeTicks::Now());
274 if (violation_error.empty()) {
[email protected]e5a440c2013-06-04 21:55:12275 LogSuccess(extension->id(),
[email protected]4b64d712013-01-17 17:53:17276 params.name,
277 args.Pass(),
278 profile_cast);
[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]c5dbef02011-05-13 05:06:09285ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
286 Delegate* delegate)
287 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27288 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35289}
290
[email protected]32dda362009-06-05 19:07:01291ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01292}
293
[email protected]c5dbef02011-05-13 05:06:09294void ExtensionFunctionDispatcher::Dispatch(
295 const ExtensionHostMsg_Request_Params& params,
296 RenderViewHost* render_view_host) {
[email protected]35548ab2013-05-15 08:59:47297 UIThreadResponseCallbackWrapperMap::const_iterator
298 iter = ui_thread_response_callback_wrappers_.find(render_view_host);
299 UIThreadResponseCallbackWrapper* callback_wrapper = NULL;
300 if (iter == ui_thread_response_callback_wrappers_.end()) {
301 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(),
302 render_view_host);
303 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper;
304 } else {
305 callback_wrapper = iter->second;
306 }
307
308 DispatchWithCallback(params, render_view_host,
309 callback_wrapper->CreateCallback(params.request_id));
310}
311
312void ExtensionFunctionDispatcher::DispatchWithCallback(
313 const ExtensionHostMsg_Request_Params& params,
314 RenderViewHost* render_view_host,
315 const ExtensionFunction::ResponseCallback& callback) {
316 // TODO(yzshen): There is some shared logic between this method and
317 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]c5dbef02011-05-13 05:06:09318 ExtensionService* service = profile()->GetExtensionService();
[email protected]efb4b082012-10-17 22:28:28319 ExtensionProcessManager* process_manager =
320 extensions::ExtensionSystem::Get(profile())->process_manager();
[email protected]6f371442011-11-09 06:45:46321 extensions::ProcessMap* process_map = service->process_map();
322 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09323 return;
324
[email protected]615d88f2011-12-13 01:47:44325 const Extension* extension = service->extensions()->GetByID(
326 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09327 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55328 extension = service->extensions()->GetHostedAppByURL(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09329
[email protected]8add5412011-10-01 21:02:14330 scoped_refptr<ExtensionFunction> function(
[email protected]74e21e72012-07-09 21:20:53331 CreateExtensionFunction(params, extension,
[email protected]9f76c1e2012-03-05 15:15:58332 render_view_host->GetProcess()->GetID(),
[email protected]6f371442011-11-09 06:45:46333 *(service->process_map()),
[email protected]5bc248a2012-04-04 23:38:11334 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]35548ab2013-05-15 08:59:47335 profile(), callback));
[email protected]4b64d712013-01-17 17:53:17336 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
337
[email protected]ecc854a2013-08-22 10:12:42338 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17339 return;
[email protected]f82d57b52011-04-27 19:13:17340
[email protected]a2aef2e2011-05-26 22:48:12341 UIThreadExtensionFunction* function_ui =
342 function->AsUIThreadExtensionFunction();
343 if (!function_ui) {
344 NOTREACHED();
345 return;
346 }
[email protected]35548ab2013-05-15 08:59:47347 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12348 function_ui->set_dispatcher(AsWeakPtr());
349 function_ui->set_profile(profile_);
[email protected]a7ff4b72013-10-17 20:56:02350 function->set_include_incognito(extension_util::CanCrossIncognito(extension,
351 service));
[email protected]cb0ce1e022010-03-10 19:54:41352
[email protected]ecc854a2013-08-22 10:12:42353 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07354 return;
[email protected]d2fe22ff2012-10-03 00:40:07355
[email protected]d13950e2009-12-04 01:43:02356 ExtensionsQuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29357 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09358 function.get(),
[email protected]85231d72012-08-31 09:45:29359 &params.arguments,
360 base::TimeTicks::Now());
361 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26362 // See crbug.com/39178.
363 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]e5a440c2013-06-04 21:55:12364 LogSuccess(extension->id(), params.name, args.Pass(), profile());
[email protected]d13950e2009-12-04 01:43:02365 function->Run();
366 } else {
[email protected]85231d72012-08-31 09:45:29367 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02368 }
[email protected]720ad1312012-02-27 23:07:36369
[email protected]efb4b082012-10-17 22:28:28370 // Note: do not access |this| after this point. We may have been deleted
371 // if function->Run() ended up closing the tab that owns us.
372
[email protected]5734e882012-05-04 22:17:56373 // Check if extension was uninstalled by management.uninstall.
374 if (!service->extensions()->GetByID(params.extension_id))
375 return;
376
[email protected]720ad1312012-02-27 23:07:36377 // We only adjust the keepalive count for UIThreadExtensionFunction for
378 // now, largely for simplicity's sake. This is OK because currently, only
379 // the webRequest API uses IOThreadExtensionFunction, and that API is not
380 // compatible with lazy background pages.
[email protected]efb4b082012-10-17 22:28:28381 process_manager->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36382}
383
384void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
385 const Extension* extension) {
[email protected]be93bba02012-10-24 16:44:03386 extensions::ExtensionSystem::Get(profile())->process_manager()->
387 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35388}
389
[email protected]c357acb42011-06-09 20:52:42390// static
[email protected]d2fe22ff2012-10-03 00:40:07391bool ExtensionFunctionDispatcher::CheckPermissions(
392 ExtensionFunction* function,
393 const Extension* extension,
394 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47395 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07396 if (!function->HasPermission()) {
397 LOG(ERROR) << "Extension " << extension->id() << " does not have "
398 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47399 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07400 return false;
401 }
402 return true;
403}
404
[email protected]f33542112013-02-04 16:52:38405namespace {
406
407// Only COMPONENT hosted apps may call extension APIs, and they are limited
408// to just the permissions they explicitly request. They should not have access
409// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
410// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11411// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
412// it altogether).
[email protected]f33542112013-02-04 16:52:38413bool AllowHostedAppAPICall(const Extension& extension,
414 const GURL& source_url,
415 const std::string& function_name) {
416 if (extension.location() != extensions::Manifest::COMPONENT)
417 return false;
418
419 if (!extension.web_extent().MatchesURL(source_url))
420 return false;
421
[email protected]b5b26b72013-08-02 00:25:11422 Feature::Availability availability =
423 ExtensionAPI::GetSharedInstance()->IsAvailable(
424 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
425 source_url);
426 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38427}
428
429} // namespace
430
431
[email protected]d2fe22ff2012-10-03 00:40:07432// static
[email protected]c357acb42011-06-09 20:52:42433ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
434 const ExtensionHostMsg_Request_Params& params,
435 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46436 int requesting_process_id,
437 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11438 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18439 void* profile,
[email protected]35548ab2013-05-15 08:59:47440 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42441 if (!extension) {
[email protected]6f371442011-11-09 06:45:46442 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47443 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46444 return NULL;
445 }
446
[email protected]f33542112013-02-04 16:52:38447 // Most hosted apps can't call APIs.
448 bool allowed = true;
449 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47450 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38451
452 // Privileged APIs can only be called from the process the extension
453 // is running in.
454 if (allowed && api->IsPrivileged(params.name))
455 allowed = process_map.Contains(extension->id(), requesting_process_id);
456
457 if (!allowed) {
458 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
459 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46460 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47461 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42462 return NULL;
463 }
464
[email protected]c357acb42011-06-09 20:52:42465 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35466 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14467 if (!function) {
468 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47469 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14470 return NULL;
471 }
472
[email protected]c357acb42011-06-09 20:52:42473 function->SetArgs(&params.arguments);
474 function->set_source_url(params.source_url);
475 function->set_request_id(params.request_id);
476 function->set_has_callback(params.has_callback);
477 function->set_user_gesture(params.user_gesture);
478 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32479 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47480 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16481
[email protected]c357acb42011-06-09 20:52:42482 return function;
483}
484
485// static
[email protected]c5dbef02011-05-13 05:06:09486void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47487 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01488 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47489 callback.Run(ExtensionFunction::FAILED, empty_list,
490 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35491}