Files
tubestation/dom/permission/PermissionObserver.cpp
Andrea Marchesini 45eb7e06ce Bug 1193373 - Support Permissions API in Worker Context, r=manuel,webidl,asuth,smaug
This commit exposes the Permissions API to DOM Workers. It achieves this goal
by introducing a thread-safe bridge between `PermissionStatus` and the
`PermissionObserver`: the `PermissionStatusSink` object.

Actors:
- The `PermissionObserver` is a main-thread-only singleton that monitors
  permission change events and propagates the notification to the right sink
  objects.
- The `PermissionStatus` is the DOM object exposed to the global. It's not
  thread-safe.
- The `PermissionStatusSink` is the new bridge introduced by this commit.

The `PermissionStatusSink` lifetime:
- This object is kept alive on the current thread by the `PermissionStatus` and
  on the main thread by the `PermissionObserver`.
- The `PermissionStatus` creates the object on its creation thread. When
  `PermissionStatus` object is released (or disconnected from the owner, it
  disentangles itself from the `PermissionStatusSink`. The disentangle
  operation triggers the un-registration procedure from the
  `PermissionObserver` on the main thread.
- A weak `WorkerRef` is used to monitor the worker's lifetime.

Permission change notification:
- When the  `PermissionObserver` is notified for a permission-change event, it
  notifies all the `PermissionStatusSink`. This happens on the main thread (see
  `MaybeUpdatedByOnMainThread` and `MaybeUpdatedByNotifyOnlyOnMainThread`).
- Using `MozPromise`, the `PermissionStatusSink` computes the permission action
  (`PermissionChangedOnMainThread`) on the main thread, then informs the
  parent `PermissionStatus` object on its creation thread.
- The `PermissionStatus` object converts the action to the DOM
  `PermissionState` and dispatches an event.

Differential Revision: https://phabricator.services.mozilla.com/D224594
2024-10-11 06:53:48 +00:00

141 lines
4.1 KiB
C++

/* -*- 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 "PermissionObserver.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsIPermission.h"
#include "PermissionStatusSink.h"
#include "PermissionUtils.h"
namespace mozilla::dom {
namespace {
PermissionObserver* gInstance = nullptr;
} // namespace
NS_IMPL_ISUPPORTS(PermissionObserver, nsIObserver, nsISupportsWeakReference)
PermissionObserver::PermissionObserver() {
MOZ_ASSERT_DEBUG_OR_FUZZING(NS_IsMainThread());
MOZ_ASSERT(!gInstance);
}
PermissionObserver::~PermissionObserver() {
MOZ_ASSERT_DEBUG_OR_FUZZING(NS_IsMainThread());
MOZ_ASSERT(mSinks.IsEmpty());
MOZ_ASSERT(gInstance == this);
gInstance = nullptr;
}
/* static */
already_AddRefed<PermissionObserver> PermissionObserver::GetInstance() {
MOZ_ASSERT_DEBUG_OR_FUZZING(NS_IsMainThread());
RefPtr<PermissionObserver> instance = gInstance;
if (!instance) {
instance = new PermissionObserver();
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (NS_WARN_IF(!obs)) {
return nullptr;
}
nsresult rv = obs->AddObserver(instance, "perm-changed", true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
rv = obs->AddObserver(instance, "perm-changed-notify-only", true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
gInstance = instance;
}
return instance.forget();
}
void PermissionObserver::AddSink(PermissionStatusSink* aSink) {
MOZ_ASSERT_DEBUG_OR_FUZZING(NS_IsMainThread());
MOZ_ASSERT(aSink);
MOZ_ASSERT(!mSinks.Contains(aSink));
mSinks.AppendElement(aSink);
}
void PermissionObserver::RemoveSink(PermissionStatusSink* aSink) {
MOZ_ASSERT_DEBUG_OR_FUZZING(NS_IsMainThread());
MOZ_ASSERT(aSink);
MOZ_ASSERT(mSinks.Contains(aSink));
mSinks.RemoveElement(aSink);
}
NS_IMETHODIMP
PermissionObserver::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
MOZ_ASSERT_DEBUG_OR_FUZZING(NS_IsMainThread());
MOZ_ASSERT(!strcmp(aTopic, "perm-changed") ||
!strcmp(aTopic, "perm-changed-notify-only"));
if (mSinks.IsEmpty()) {
return NS_OK;
}
nsCOMPtr<nsIPermission> perm = nullptr;
nsCOMPtr<nsPIDOMWindowInner> innerWindow = nullptr;
nsAutoCString type;
if (!strcmp(aTopic, "perm-changed")) {
perm = do_QueryInterface(aSubject);
if (!perm) {
return NS_OK;
}
perm->GetType(type);
} else if (!strcmp(aTopic, "perm-changed-notify-only")) {
innerWindow = do_QueryInterface(aSubject);
if (!innerWindow) {
return NS_OK;
}
type = NS_ConvertUTF16toUTF8(aData);
}
Maybe<PermissionName> permission = TypeToPermissionName(type);
if (permission) {
for (PermissionStatusSink* sink : mSinks) {
if (sink->Name() != permission.value()) {
continue;
}
// Check for permissions that are changed for this sink's principal
// via the "perm-changed" notification. These permissions affect
// the window the sink (PermissionStatus) is held in directly.
if (perm && sink->MaybeUpdatedByOnMainThread(perm)) {
sink->PermissionChangedOnMainThread();
}
// Check for permissions that are changed for this sink's principal
// via the "perm-changed-notify-only" notification. These permissions
// affect the window the sink (PermissionStatus) is held in indirectly- if
// the window is same-party with the secondary key of a permission. For
// example, a "3rdPartyFrameStorage^https://example.com" permission would
// return true on these checks where sink is in a window that is same-site
// with https://example.com.
if (innerWindow &&
sink->MaybeUpdatedByNotifyOnlyOnMainThread(innerWindow)) {
sink->PermissionChangedOnMainThread();
}
}
}
return NS_OK;
}
} // namespace mozilla::dom