blob: 733d79aa664de00c29abc1cabc37ddda3adc003b [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]c333e792012-01-06 16:57:3928#include "content/public/browser/render_process_host.h"
[email protected]9c1662b2012-03-06 15:44:3329#include "content/public/browser/render_view_host.h"
[email protected]d6ec84a2013-11-01 13:07:3830#include "content/public/browser/user_metrics.h"
[email protected]ab2a7e3c2013-10-22 03:41:3631#include "content/public/browser/web_contents.h"
32#include "content/public/browser/web_contents_observer.h"
[email protected]35548ab2013-05-15 08:59:4733#include "content/public/common/result_codes.h"
[email protected]50de9aa22013-11-14 06:30:3434#include "extensions/browser/process_map.h"
[email protected]38427a12013-11-09 17:34:2035#include "extensions/browser/quota_service.h"
[email protected]d6ec84a2013-11-01 13:07:3836#include "extensions/common/extension_api.h"
[email protected]289c44b2013-12-17 03:26:5737#include "extensions/common/extension_set.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]86376022013-12-03 18:18:0544using extensions::ExtensionSystem;
[email protected]b5b26b72013-08-02 00:25:1145using extensions::Feature;
[email protected]eaabba22012-03-07 15:02:1146using content::RenderViewHost;
[email protected]83820d42011-11-12 22:03:1147
[email protected]5bc248a2012-04-04 23:38:1148namespace {
49
[email protected]e5a440c2013-06-04 21:55:1250void LogSuccess(const std::string& extension_id,
[email protected]4b64d712013-01-17 17:53:1751 const std::string& api_name,
[email protected]aeca23f2013-06-21 22:34:4152 scoped_ptr<base::ListValue> args,
[email protected]86376022013-12-03 18:18:0553 content::BrowserContext* browser_context) {
[email protected]4b64d712013-01-17 17:53:1754 // The ActivityLog can only be accessed from the main (UI) thread. If we're
55 // running on the wrong thread, re-dispatch from the main thread.
56 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
57 BrowserThread::PostTask(BrowserThread::UI,
58 FROM_HERE,
59 base::Bind(&LogSuccess,
[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]4b64d712013-01-17 17:53:1764 } else {
65 extensions::ActivityLog* activity_log =
[email protected]86376022013-12-03 18:18:0566 extensions::ActivityLog::GetInstance(browser_context);
[email protected]05602fa32013-08-01 06:30:4367 scoped_refptr<extensions::Action> action =
68 new extensions::Action(extension_id,
69 base::Time::Now(),
70 extensions::Action::ACTION_API_CALL,
71 api_name);
72 action->set_args(args.Pass());
73 activity_log->LogAction(action);
[email protected]efd75992011-12-15 22:42:4274 }
75}
76
[email protected]5bc248a2012-04-04 23:38:1177// Separate copy of ExtensionAPI used for IO thread extension functions. We need
78// this because ExtensionAPI has mutable data. It should be possible to remove
79// this once all the extension APIs are updated to the feature system.
80struct Static {
81 Static()
82 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) {
83 }
84 scoped_ptr<extensions::ExtensionAPI> api;
85};
86base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER;
87
[email protected]35548ab2013-05-15 08:59:4788// Kills the specified process because it sends us a malformed message.
89void KillBadMessageSender(base::ProcessHandle process) {
90 NOTREACHED();
91 content::RecordAction(content::UserMetricsAction("BadMessageTerminate_EFD"));
92 if (process)
93 base::KillProcess(process, content::RESULT_CODE_KILLED_BAD_MESSAGE, false);
94}
95
96void CommonResponseCallback(IPC::Sender* ipc_sender,
97 int routing_id,
98 base::ProcessHandle peer_process,
99 int request_id,
100 ExtensionFunction::ResponseType type,
101 const base::ListValue& results,
102 const std::string& error) {
103 DCHECK(ipc_sender);
104
105 if (type == ExtensionFunction::BAD_MESSAGE) {
106 // The renderer has done validation before sending extension api requests.
107 // Therefore, we should never receive a request that is invalid in a way
108 // that JSON validation in the renderer should have caught. It could be an
109 // attacker trying to exploit the browser, so we crash the renderer instead.
110 LOG(ERROR) <<
111 "Terminating renderer because of malformed extension message.";
112 if (content::RenderProcessHost::run_renderer_in_process()) {
113 // In single process mode it is better if we don't suicide but just crash.
114 CHECK(false);
115 } else {
116 KillBadMessageSender(peer_process);
117 }
118
119 return;
120 }
121
122 ipc_sender->Send(new ExtensionMsg_Response(
123 routing_id, request_id, type == ExtensionFunction::SUCCEEDED, results,
124 error));
125}
126
127void IOThreadResponseCallback(
128 const base::WeakPtr<ChromeRenderMessageFilter>& ipc_sender,
129 int routing_id,
130 int request_id,
131 ExtensionFunction::ResponseType type,
132 const base::ListValue& results,
133 const std::string& error) {
[email protected]e8dad9b2013-06-04 04:43:45134 if (!ipc_sender.get())
[email protected]35548ab2013-05-15 08:59:47135 return;
136
[email protected]e8dad9b2013-06-04 04:43:45137 CommonResponseCallback(ipc_sender.get(),
138 routing_id,
[email protected]950be552013-07-10 19:13:02139 ipc_sender->PeerHandle(),
[email protected]e8dad9b2013-06-04 04:43:45140 request_id,
141 type,
142 results,
143 error);
[email protected]35548ab2013-05-15 08:59:47144}
145
[email protected]5bc248a2012-04-04 23:38:11146} // namespace
147
[email protected]35548ab2013-05-15 08:59:47148class ExtensionFunctionDispatcher::UIThreadResponseCallbackWrapper
[email protected]ab2a7e3c2013-10-22 03:41:36149 : public content::WebContentsObserver {
[email protected]35548ab2013-05-15 08:59:47150 public:
151 UIThreadResponseCallbackWrapper(
152 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher,
153 RenderViewHost* render_view_host)
[email protected]ab2a7e3c2013-10-22 03:41:36154 : content::WebContentsObserver(
155 content::WebContents::FromRenderViewHost(render_view_host)),
[email protected]35548ab2013-05-15 08:59:47156 dispatcher_(dispatcher),
[email protected]ab2a7e3c2013-10-22 03:41:36157 render_view_host_(render_view_host),
[email protected]35548ab2013-05-15 08:59:47158 weak_ptr_factory_(this) {
159 }
160
161 virtual ~UIThreadResponseCallbackWrapper() {
162 }
163
[email protected]ab2a7e3c2013-10-22 03:41:36164 // content::WebContentsObserver overrides.
165 virtual void RenderViewDeleted(
[email protected]35548ab2013-05-15 08:59:47166 RenderViewHost* render_view_host) OVERRIDE {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]ab2a7e3c2013-10-22 03:41:36168 if (render_view_host != render_view_host_)
169 return;
170
[email protected]e8dad9b2013-06-04 04:43:45171 if (dispatcher_.get()) {
172 dispatcher_->ui_thread_response_callback_wrappers_
173 .erase(render_view_host);
[email protected]35548ab2013-05-15 08:59:47174 }
175
[email protected]ab2a7e3c2013-10-22 03:41:36176 delete this;
[email protected]35548ab2013-05-15 08:59:47177 }
178
179 ExtensionFunction::ResponseCallback CreateCallback(int request_id) {
180 return base::Bind(
181 &UIThreadResponseCallbackWrapper::OnExtensionFunctionCompleted,
182 weak_ptr_factory_.GetWeakPtr(),
183 request_id);
184 }
185
186 private:
187 void OnExtensionFunctionCompleted(int request_id,
188 ExtensionFunction::ResponseType type,
189 const base::ListValue& results,
190 const std::string& error) {
191 CommonResponseCallback(
[email protected]ab2a7e3c2013-10-22 03:41:36192 render_view_host_, render_view_host_->GetRoutingID(),
193 render_view_host_->GetProcess()->GetHandle(), request_id, type,
[email protected]35548ab2013-05-15 08:59:47194 results, error);
195 }
196
197 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_;
[email protected]ab2a7e3c2013-10-22 03:41:36198 content::RenderViewHost* render_view_host_;
[email protected]35548ab2013-05-15 08:59:47199 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_;
200
201 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper);
202};
203
[email protected]44f4b132012-07-17 20:36:57204extensions::WindowController*
205ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController()
[email protected]d72d3a62012-05-10 03:45:08206 const {
207 return NULL;
208}
209
210content::WebContents*
[email protected]44f4b132012-07-17 20:36:57211ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const {
[email protected]d72d3a62012-05-10 03:45:08212 return NULL;
213}
[email protected]5bc248a2012-04-04 23:38:11214
[email protected]1ce88e82013-06-28 05:17:10215content::WebContents*
216ExtensionFunctionDispatcher::Delegate::GetVisibleWebContents() const {
217 return GetAssociatedWebContents();
218}
219
[email protected]bfdffe2b2009-04-24 22:05:35220void ExtensionFunctionDispatcher::GetAllFunctionNames(
221 std::vector<std::string>* names) {
[email protected]ae33d322012-03-19 22:24:35222 ExtensionFunctionRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35223}
224
[email protected]b83e4602009-05-15 22:58:33225bool ExtensionFunctionDispatcher::OverrideFunction(
226 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]ae33d322012-03-19 22:24:35227 return ExtensionFunctionRegistry::GetInstance()->OverrideFunction(name,
228 factory);
[email protected]b83e4602009-05-15 22:58:33229}
230
231void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]ae33d322012-03-19 22:24:35232 ExtensionFunctionRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33233}
234
[email protected]c357acb42011-06-09 20:52:42235// static
236void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]38427a12013-11-09 17:34:20237 extensions::InfoMap* extension_info_map,
[email protected]86376022013-12-03 18:18:05238 void* browser_context,
[email protected]c357acb42011-06-09 20:52:42239 int render_process_id,
240 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
[email protected]74e21e72012-07-09 21:20:53241 int routing_id,
[email protected]c357acb42011-06-09 20:52:42242 const ExtensionHostMsg_Request_Params& params) {
243 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11244 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]35548ab2013-05-15 08:59:47245
246 ExtensionFunction::ResponseCallback callback(
247 base::Bind(&IOThreadResponseCallback, ipc_sender, routing_id,
248 params.request_id));
249
[email protected]6f371442011-11-09 06:45:46250 scoped_refptr<ExtensionFunction> function(
251 CreateExtensionFunction(params, extension, render_process_id,
[email protected]5bc248a2012-04-04 23:38:11252 extension_info_map->process_map(),
253 g_global_io_data.Get().api.get(),
[email protected]86376022013-12-03 18:18:05254 browser_context, callback));
[email protected]4b64d712013-01-17 17:53:17255 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
256
[email protected]ecc854a2013-08-22 10:12:42257 if (!function.get())
[email protected]c357acb42011-06-09 20:52:42258 return;
259
260 IOThreadExtensionFunction* function_io =
261 function->AsIOThreadExtensionFunction();
262 if (!function_io) {
263 NOTREACHED();
264 return;
265 }
[email protected]44295a12013-06-05 08:45:46266 function_io->set_ipc_sender(ipc_sender, routing_id);
[email protected]c357acb42011-06-09 20:52:42267 function_io->set_extension_info_map(extension_info_map);
268 function->set_include_incognito(
269 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25270
[email protected]ecc854a2013-08-22 10:12:42271 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07272 return;
[email protected]d2fe22ff2012-10-03 00:40:07273
[email protected]38427a12013-11-09 17:34:20274 extensions::QuotaService* quota = extension_info_map->GetQuotaService();
[email protected]85231d72012-08-31 09:45:29275 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09276 function.get(),
[email protected]85231d72012-08-31 09:45:29277 &params.arguments,
278 base::TimeTicks::Now());
279 if (violation_error.empty()) {
[email protected]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
314 DispatchWithCallback(params, render_view_host,
315 callback_wrapper->CreateCallback(params.request_id));
316}
317
318void ExtensionFunctionDispatcher::DispatchWithCallback(
319 const ExtensionHostMsg_Request_Params& params,
320 RenderViewHost* render_view_host,
321 const ExtensionFunction::ResponseCallback& callback) {
322 // TODO(yzshen): There is some shared logic between this method and
323 // DispatchOnIOThread(). It is nice to deduplicate.
[email protected]86376022013-12-03 18:18:05324 ExtensionSystem* extension_system =
325 ExtensionSystem::GetForBrowserContext(browser_context_);
326 ExtensionService* service = extension_system->extension_service();
[email protected]6f371442011-11-09 06:45:46327 extensions::ProcessMap* process_map = service->process_map();
[email protected]86376022013-12-03 18:18:05328 if (!process_map)
[email protected]c5dbef02011-05-13 05:06:09329 return;
330
[email protected]615d88f2011-12-13 01:47:44331 const Extension* extension = service->extensions()->GetByID(
332 params.extension_id);
[email protected]c5dbef02011-05-13 05:06:09333 if (!extension)
[email protected]be9915fb2013-07-18 09:28:55334 extension = service->extensions()->GetHostedAppByURL(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09335
[email protected]8add5412011-10-01 21:02:14336 scoped_refptr<ExtensionFunction> function(
[email protected]86376022013-12-03 18:18:05337 CreateExtensionFunction(params,
338 extension,
[email protected]9f76c1e2012-03-05 15:15:58339 render_view_host->GetProcess()->GetID(),
[email protected]86376022013-12-03 18:18:05340 *process_map,
[email protected]5bc248a2012-04-04 23:38:11341 extensions::ExtensionAPI::GetSharedInstance(),
[email protected]86376022013-12-03 18:18:05342 browser_context_,
343 callback));
[email protected]4b64d712013-01-17 17:53:17344 scoped_ptr<ListValue> args(params.arguments.DeepCopy());
345
[email protected]ecc854a2013-08-22 10:12:42346 if (!function.get())
[email protected]f82d57b52011-04-27 19:13:17347 return;
[email protected]f82d57b52011-04-27 19:13:17348
[email protected]a2aef2e2011-05-26 22:48:12349 UIThreadExtensionFunction* function_ui =
350 function->AsUIThreadExtensionFunction();
351 if (!function_ui) {
352 NOTREACHED();
353 return;
354 }
[email protected]35548ab2013-05-15 08:59:47355 function_ui->SetRenderViewHost(render_view_host);
[email protected]a2aef2e2011-05-26 22:48:12356 function_ui->set_dispatcher(AsWeakPtr());
[email protected]86376022013-12-03 18:18:05357 function_ui->set_context(browser_context_);
[email protected]a7ff4b72013-10-17 20:56:02358 function->set_include_incognito(extension_util::CanCrossIncognito(extension,
359 service));
[email protected]cb0ce1e022010-03-10 19:54:41360
[email protected]ecc854a2013-08-22 10:12:42361 if (!CheckPermissions(function.get(), extension, params, callback))
[email protected]d2fe22ff2012-10-03 00:40:07362 return;
[email protected]d2fe22ff2012-10-03 00:40:07363
[email protected]38427a12013-11-09 17:34:20364 extensions::QuotaService* quota = service->quota_service();
[email protected]85231d72012-08-31 09:45:29365 std::string violation_error = quota->Assess(extension->id(),
[email protected]dc24976f2013-06-02 21:15:09366 function.get(),
[email protected]85231d72012-08-31 09:45:29367 &params.arguments,
368 base::TimeTicks::Now());
369 if (violation_error.empty()) {
[email protected]d070ec62010-07-27 21:28:26370 // See crbug.com/39178.
371 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]86376022013-12-03 18:18:05372 LogSuccess(extension->id(), params.name, args.Pass(), browser_context_);
[email protected]d13950e2009-12-04 01:43:02373 function->Run();
374 } else {
[email protected]85231d72012-08-31 09:45:29375 function->OnQuotaExceeded(violation_error);
[email protected]d13950e2009-12-04 01:43:02376 }
[email protected]720ad1312012-02-27 23:07:36377
[email protected]efb4b082012-10-17 22:28:28378 // Note: do not access |this| after this point. We may have been deleted
379 // if function->Run() ended up closing the tab that owns us.
380
[email protected]5734e882012-05-04 22:17:56381 // Check if extension was uninstalled by management.uninstall.
382 if (!service->extensions()->GetByID(params.extension_id))
383 return;
384
[email protected]720ad1312012-02-27 23:07:36385 // We only adjust the keepalive count for UIThreadExtensionFunction for
386 // now, largely for simplicity's sake. This is OK because currently, only
387 // the webRequest API uses IOThreadExtensionFunction, and that API is not
388 // compatible with lazy background pages.
[email protected]86376022013-12-03 18:18:05389 extension_system->process_manager()->IncrementLazyKeepaliveCount(extension);
[email protected]720ad1312012-02-27 23:07:36390}
391
392void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted(
393 const Extension* extension) {
[email protected]86376022013-12-03 18:18:05394 ExtensionSystem::GetForBrowserContext(browser_context_)->process_manager()->
[email protected]be93bba02012-10-24 16:44:03395 DecrementLazyKeepaliveCount(extension);
[email protected]bfdffe2b2009-04-24 22:05:35396}
397
[email protected]c357acb42011-06-09 20:52:42398// static
[email protected]d2fe22ff2012-10-03 00:40:07399bool ExtensionFunctionDispatcher::CheckPermissions(
400 ExtensionFunction* function,
401 const Extension* extension,
402 const ExtensionHostMsg_Request_Params& params,
[email protected]35548ab2013-05-15 08:59:47403 const ExtensionFunction::ResponseCallback& callback) {
[email protected]d2fe22ff2012-10-03 00:40:07404 if (!function->HasPermission()) {
405 LOG(ERROR) << "Extension " << extension->id() << " does not have "
406 << "permission to function: " << params.name;
[email protected]35548ab2013-05-15 08:59:47407 SendAccessDenied(callback);
[email protected]d2fe22ff2012-10-03 00:40:07408 return false;
409 }
410 return true;
411}
412
[email protected]f33542112013-02-04 16:52:38413namespace {
414
415// Only COMPONENT hosted apps may call extension APIs, and they are limited
416// to just the permissions they explicitly request. They should not have access
417// to extension APIs like eg chrome.runtime, chrome.windows, etc. that normally
418// are available without permission.
[email protected]b5b26b72013-08-02 00:25:11419// TODO(mpcomplete): move this to ExtensionFunction::HasPermission (or remove
420// it altogether).
[email protected]f33542112013-02-04 16:52:38421bool AllowHostedAppAPICall(const Extension& extension,
422 const GURL& source_url,
423 const std::string& function_name) {
424 if (extension.location() != extensions::Manifest::COMPONENT)
425 return false;
426
427 if (!extension.web_extent().MatchesURL(source_url))
428 return false;
429
[email protected]713a6e82013-12-19 23:06:05430 // Note: Not BLESSED_WEB_PAGE_CONTEXT here because these component hosted app
431 // entities have traditionally been treated as blessed extensions, for better
432 // or worse.
[email protected]b5b26b72013-08-02 00:25:11433 Feature::Availability availability =
434 ExtensionAPI::GetSharedInstance()->IsAvailable(
435 function_name, &extension, Feature::BLESSED_EXTENSION_CONTEXT,
436 source_url);
437 return availability.is_available();
[email protected]f33542112013-02-04 16:52:38438}
439
440} // namespace
441
442
[email protected]d2fe22ff2012-10-03 00:40:07443// static
[email protected]c357acb42011-06-09 20:52:42444ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
445 const ExtensionHostMsg_Request_Params& params,
446 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46447 int requesting_process_id,
448 const extensions::ProcessMap& process_map,
[email protected]5bc248a2012-04-04 23:38:11449 extensions::ExtensionAPI* api,
[email protected]673514522011-07-13 18:17:18450 void* profile,
[email protected]35548ab2013-05-15 08:59:47451 const ExtensionFunction::ResponseCallback& callback) {
[email protected]c357acb42011-06-09 20:52:42452 if (!extension) {
[email protected]6f371442011-11-09 06:45:46453 LOG(ERROR) << "Specified extension does not exist.";
[email protected]35548ab2013-05-15 08:59:47454 SendAccessDenied(callback);
[email protected]6f371442011-11-09 06:45:46455 return NULL;
456 }
457
[email protected]f33542112013-02-04 16:52:38458 // Most hosted apps can't call APIs.
459 bool allowed = true;
460 if (extension->is_hosted_app())
[email protected]35548ab2013-05-15 08:59:47461 allowed = AllowHostedAppAPICall(*extension, params.source_url, params.name);
[email protected]f33542112013-02-04 16:52:38462
463 // Privileged APIs can only be called from the process the extension
464 // is running in.
465 if (allowed && api->IsPrivileged(params.name))
466 allowed = process_map.Contains(extension->id(), requesting_process_id);
467
468 if (!allowed) {
469 LOG(ERROR) << "Extension API call disallowed - name:" << params.name
470 << " pid:" << requesting_process_id
[email protected]6f371442011-11-09 06:45:46471 << " from URL " << params.source_url.spec();
[email protected]35548ab2013-05-15 08:59:47472 SendAccessDenied(callback);
[email protected]c357acb42011-06-09 20:52:42473 return NULL;
474 }
475
[email protected]c357acb42011-06-09 20:52:42476 ExtensionFunction* function =
[email protected]ae33d322012-03-19 22:24:35477 ExtensionFunctionRegistry::GetInstance()->NewFunction(params.name);
[email protected]42681ec82013-04-09 12:40:14478 if (!function) {
479 LOG(ERROR) << "Unknown Extension API - " << params.name;
[email protected]35548ab2013-05-15 08:59:47480 SendAccessDenied(callback);
[email protected]42681ec82013-04-09 12:40:14481 return NULL;
482 }
483
[email protected]c357acb42011-06-09 20:52:42484 function->SetArgs(&params.arguments);
485 function->set_source_url(params.source_url);
486 function->set_request_id(params.request_id);
487 function->set_has_callback(params.has_callback);
488 function->set_user_gesture(params.user_gesture);
489 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32490 function->set_profile_id(profile);
[email protected]35548ab2013-05-15 08:59:47491 function->set_response_callback(callback);
[email protected]3d0e2262012-08-02 15:32:16492
[email protected]c357acb42011-06-09 20:52:42493 return function;
494}
495
496// static
[email protected]c5dbef02011-05-13 05:06:09497void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]35548ab2013-05-15 08:59:47498 const ExtensionFunction::ResponseCallback& callback) {
[email protected]602542d2012-04-20 02:48:01499 ListValue empty_list;
[email protected]35548ab2013-05-15 08:59:47500 callback.Run(ExtensionFunction::FAILED, empty_list,
501 "Access to extension API denied.");
[email protected]bfdffe2b2009-04-24 22:05:35502}