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/PermissionStatus.h"
|
2020-04-10 11:36:46 +03:00
|
|
|
#include "mozilla/PermissionDelegateHandler.h"
|
2015-08-11 20:04:35 +03:00
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2020-04-10 11:36:37 +03:00
|
|
|
#include "mozilla/Permission.h"
|
2015-08-22 05:53:29 +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 "PermissionObserver.h"
|
2015-08-22 05:53:29 +03:00
|
|
|
#include "PermissionUtils.h"
|
2015-07-28 16:33:46 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2015-07-28 16:33:46 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<PermissionStatus> PermissionStatus::Create(
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aWindow, PermissionName aName, ErrorResult& aRv) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PermissionStatus> status = new PermissionStatus(aWindow, aName);
|
2015-08-01 00:56:59 +03:00
|
|
|
aRv = status->Init();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2015-08-22 05:53:29 +03:00
|
|
|
}
|
|
|
|
|
2015-08-01 00:56:59 +03:00
|
|
|
return status.forget();
|
2015-08-22 05:53:29 +03:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
PermissionStatus::PermissionStatus(nsPIDOMWindowInner* aWindow,
|
2015-08-22 05:53:29 +03:00
|
|
|
PermissionName aName)
|
2015-07-28 16:33:46 +03:00
|
|
|
: DOMEventTargetHelper(aWindow),
|
2015-08-22 05:53:29 +03:00
|
|
|
mName(aName),
|
|
|
|
mState(PermissionState::Denied) {
|
2021-12-14 19:54:50 +03:00
|
|
|
KeepAliveIfHasListenersFor(nsGkAtoms::onchange);
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
nsresult PermissionStatus::Init() {
|
|
|
|
mObserver = PermissionObserver::GetInstance();
|
|
|
|
if (NS_WARN_IF(!mObserver)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mObserver->AddSink(this);
|
|
|
|
|
|
|
|
nsresult rv = UpdateState();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
PermissionStatus::~PermissionStatus() {
|
2015-08-22 05:53:29 +03:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->RemoveSink(this);
|
|
|
|
}
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* PermissionStatus::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PermissionStatus_Binding::Wrap(aCx, this, aGivenProto);
|
2015-07-28 16:33:46 +03:00
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
nsresult PermissionStatus::UpdateState() {
|
2019-09-21 11:38:26 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
|
|
|
|
if (NS_WARN_IF(!window)) {
|
2015-08-22 05:53:29 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-09-21 11:38:26 +03:00
|
|
|
RefPtr<Document> document = window->GetExtantDoc();
|
|
|
|
if (NS_WARN_IF(!document)) {
|
2015-08-22 05:53:29 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t action = nsIPermissionManager::DENY_ACTION;
|
2019-09-21 11:38:26 +03:00
|
|
|
|
|
|
|
PermissionDelegateHandler* permissionHandler =
|
|
|
|
document->GetPermissionDelegateHandler();
|
2019-11-07 17:29:02 +03:00
|
|
|
if (NS_WARN_IF(!permissionHandler)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-09-21 11:38:26 +03:00
|
|
|
nsresult rv = permissionHandler->GetPermissionForPermissionsAPI(
|
|
|
|
PermissionNameToType(mName), &action);
|
2015-08-22 05:53:29 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = ActionToPermissionState(action);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
already_AddRefed<nsIPrincipal> PermissionStatus::GetPrincipal() const {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
|
2015-08-22 05:53:29 +03:00
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = window->GetExtantDoc();
|
2015-08-22 05:53:29 +03:00
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:53:11 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::ClonePrincipalForPermission(doc->NodePrincipal());
|
2016-11-08 13:53:11 +03:00
|
|
|
NS_ENSURE_TRUE(principal, nullptr);
|
|
|
|
|
|
|
|
return principal.forget();
|
2015-08-22 05:53:29 +03:00
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
void PermissionStatus::PermissionChanged() {
|
2022-02-21 08:11:57 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
|
|
|
|
if (NS_WARN_IF(!window) || !window->IsFullyActive()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-22 05:53:29 +03:00
|
|
|
auto oldState = mState;
|
|
|
|
UpdateState();
|
|
|
|
if (mState != oldState) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> eventDispatcher =
|
2018-06-25 19:23:50 +03:00
|
|
|
new AsyncEventDispatcher(this, u"change"_ns, CanBubble::eNo);
|
2015-08-22 05:53:29 +03:00
|
|
|
eventDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 01:19:12 +03:00
|
|
|
void PermissionStatus::DisconnectFromOwner() {
|
2021-12-14 19:54:50 +03:00
|
|
|
IgnoreKeepAliveIfHasListenersFor(nsGkAtoms::onchange);
|
2018-08-01 01:19:12 +03:00
|
|
|
|
|
|
|
if (mObserver) {
|
|
|
|
mObserver->RemoveSink(this);
|
|
|
|
mObserver = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|