2015-07-28 16:33:46 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/Permissions.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/PermissionsBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-07-28 16:33:46 +03:00
|
|
|
#include "mozilla/Services.h"
|
2015-07-28 16:33:46 +03:00
|
|
|
#include "nsIPermissionManager.h"
|
2015-08-01 01:49:55 +03:00
|
|
|
#include "PermissionUtils.h"
|
2015-07-28 16:33:46 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Permissions)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Permissions)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Permissions)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Permissions, mWindow)
|
|
|
|
|
|
|
|
Permissions::Permissions(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Permissions::~Permissions()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
Permissions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return PermissionsBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
nsresult
|
2015-08-11 20:04:35 +03:00
|
|
|
CheckPermission(const char* aName,
|
2015-08-11 20:04:35 +03:00
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
PermissionState& aResult)
|
|
|
|
{
|
2015-08-11 20:04:35 +03:00
|
|
|
MOZ_ASSERT(aName);
|
2015-08-11 20:04:35 +03:00
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
if (NS_WARN_IF(!permMgr)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t action = nsIPermissionManager::DENY_ACTION;
|
2015-08-11 20:04:35 +03:00
|
|
|
nsresult rv = permMgr->TestPermissionFromWindow(aWindow, aName, &action);
|
2015-08-11 20:04:35 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult = ActionToPermissionState(action);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CheckPushPermission(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
PermissionState& aResult)
|
2015-07-28 16:33:46 +03:00
|
|
|
{
|
|
|
|
PushPermissionDescriptor permission;
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
|
|
|
|
if (NS_WARN_IF(!permission.Init(aCx, value))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permission.mUserVisible) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:04:35 +03:00
|
|
|
return CheckPermission("push", aWindow, aResult);
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-08-11 20:04:35 +03:00
|
|
|
CheckPermission(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
PermissionState& aResult)
|
2015-07-28 16:33:46 +03:00
|
|
|
{
|
|
|
|
PermissionDescriptor permission;
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
|
|
|
|
if (NS_WARN_IF(!permission.Init(aCx, value))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (permission.mName) {
|
|
|
|
case PermissionName::Geolocation:
|
2015-08-11 20:04:35 +03:00
|
|
|
return CheckPermission("geo", aWindow, aResult);
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
case PermissionName::Notifications:
|
2015-08-11 20:04:35 +03:00
|
|
|
return CheckPermission("desktop-notification", aWindow, aResult);
|
2015-07-28 16:33:46 +03:00
|
|
|
|
|
|
|
case PermissionName::Push:
|
2015-08-11 20:04:35 +03:00
|
|
|
return CheckPushPermission(aCx, aPermission, aWindow, aResult);
|
2015-07-28 16:33:46 +03:00
|
|
|
|
|
|
|
case PermissionName::Midi:
|
|
|
|
default:
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
already_AddRefed<Promise>
|
2015-07-28 16:33:46 +03:00
|
|
|
Permissions::Query(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
ErrorResult& aRv)
|
2015-07-28 16:33:46 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow);
|
|
|
|
if (!global) {
|
2015-07-28 16:33:46 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2015-07-28 16:33:46 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:04:35 +03:00
|
|
|
PermissionState state = PermissionState::Denied;
|
|
|
|
nsresult rv = CheckPermission(aCx, aPermission, mWindow, state);
|
2015-07-28 16:33:46 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
promise->MaybeReject(rv);
|
|
|
|
} else {
|
2015-08-11 20:04:35 +03:00
|
|
|
promise->MaybeResolve(new PermissionStatus(mWindow, state));
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
2015-07-28 16:33:46 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|