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"
|
|
|
|
|
2016-03-03 00:09:48 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2015-07-28 16:33:46 +03:00
|
|
|
#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-22 05:53:29 +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)
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
Permissions::Permissions(nsPIDOMWindowInner* aWindow) : mWindow(aWindow) {}
|
2015-07-28 16:33:46 +03:00
|
|
|
|
2020-03-09 17:48:10 +03:00
|
|
|
Permissions::~Permissions() = default;
|
2015-07-28 16:33:46 +03:00
|
|
|
|
|
|
|
JSObject* Permissions::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return Permissions_Binding::Wrap(aCx, this, aGivenProto);
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
namespace {
|
|
|
|
|
2015-08-01 00:56:59 +03:00
|
|
|
already_AddRefed<PermissionStatus> CreatePermissionStatus(
|
2015-08-22 05:53:29 +03:00
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aPermission,
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aWindow, ErrorResult& aRv) {
|
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))) {
|
2016-03-23 18:02:57 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2015-08-01 00:56:59 +03:00
|
|
|
return nullptr;
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (permission.mName) {
|
|
|
|
case PermissionName::Geolocation:
|
|
|
|
case PermissionName::Notifications:
|
|
|
|
case PermissionName::Push:
|
2016-12-16 13:00:53 +03:00
|
|
|
case PermissionName::Persistent_storage:
|
2016-04-22 21:22:42 +03:00
|
|
|
return PermissionStatus::Create(aWindow, permission.mName, aRv);
|
2015-07-28 16:33:46 +03:00
|
|
|
|
|
|
|
default:
|
2015-12-18 16:02:50 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled type");
|
2015-08-01 00:56:59 +03:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
already_AddRefed<Promise> Permissions::Query(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
ErrorResult& aRv) {
|
2018-02-23 01:28:39 +03:00
|
|
|
if (!mWindow) {
|
2015-07-28 16:33:46 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2015-07-28 16:33:46 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:02:57 +03:00
|
|
|
RefPtr<PermissionStatus> status =
|
|
|
|
CreatePermissionStatus(aCx, aPermission, mWindow, aRv);
|
2015-07-28 16:33:46 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2016-03-23 18:02:57 +03:00
|
|
|
MOZ_ASSERT(!status);
|
2015-07-28 16:33:46 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:02:57 +03:00
|
|
|
MOZ_ASSERT(status);
|
2018-02-23 01:28:39 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mWindow->AsGlobal(), aRv);
|
2015-08-01 00:56:59 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2016-03-23 18:02:57 +03:00
|
|
|
return nullptr;
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
2016-03-23 18:02:57 +03:00
|
|
|
|
|
|
|
promise->MaybeResolve(status);
|
2015-07-28 16:33:46 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
nsresult Permissions::RemovePermission(nsIPrincipal* aPrincipal,
|
|
|
|
const nsACString& aPermissionType) {
|
2016-03-03 00:09:48 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
if (NS_WARN_IF(!permMgr)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return permMgr->RemoveFromPrincipal(aPrincipal, aPermissionType);
|
|
|
|
}
|
|
|
|
|
2016-03-03 00:09:38 +03:00
|
|
|
already_AddRefed<Promise> Permissions::Revoke(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
ErrorResult& aRv) {
|
2018-02-23 01:28:39 +03:00
|
|
|
if (!mWindow) {
|
2016-03-03 00:09:38 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionDescriptor permission;
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
|
|
|
|
if (NS_WARN_IF(!permission.Init(aCx, value))) {
|
2016-03-23 18:02:57 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-03-03 00:09:38 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-23 01:28:39 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mWindow->AsGlobal(), aRv);
|
2016-03-03 00:09:38 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> document = mWindow->GetExtantDoc();
|
2016-03-03 00:09:38 +03:00
|
|
|
if (!document) {
|
|
|
|
promise->MaybeReject(NS_ERROR_UNEXPECTED);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
if (NS_WARN_IF(!permMgr)) {
|
|
|
|
promise->MaybeReject(NS_ERROR_FAILURE);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2019-02-22 01:54:28 +03:00
|
|
|
const nsLiteralCString& permissionType =
|
|
|
|
PermissionNameToType(permission.mName);
|
2016-03-03 00:09:48 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
rv = RemovePermission(document->NodePrincipal(), permissionType);
|
|
|
|
} else {
|
|
|
|
// Permissions can't be removed from the content process. Send a message
|
|
|
|
// to the parent; `ContentParent::RecvRemovePermission` will call
|
|
|
|
// `RemovePermission`.
|
|
|
|
ContentChild::GetSingleton()->SendRemovePermission(
|
2019-02-22 01:54:28 +03:00
|
|
|
IPC::Principal(document->NodePrincipal()), permissionType, &rv);
|
2016-03-03 00:09:48 +03:00
|
|
|
}
|
|
|
|
|
2016-03-03 00:09:38 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
promise->MaybeReject(rv);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PermissionStatus> status =
|
|
|
|
CreatePermissionStatus(aCx, aPermission, mWindow, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
MOZ_ASSERT(!status);
|
2016-03-23 18:02:57 +03:00
|
|
|
return nullptr;
|
2016-03-03 00:09:38 +03:00
|
|
|
}
|
2016-03-23 18:02:57 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(status);
|
|
|
|
promise->MaybeResolve(status);
|
2016-03-03 00:09:38 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|