mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1105827 - Part 9: Add PermissionObserver to watch for perm-changed notifications. r=baku
This commit is contained in:
parent
fee7b4c5f0
commit
de9f7bfbaf
131
dom/permission/PermissionObserver.cpp
Normal file
131
dom/permission/PermissionObserver.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
/* -*- 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/PermissionStatus.h"
|
||||
#include "mozilla/Services.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
#include "nsIObserverService.h"
|
||||
#include "nsIPermission.h"
|
||||
#include "PermissionUtils.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
namespace {
|
||||
PermissionObserver* gInstance = nullptr;
|
||||
} // namespace
|
||||
|
||||
NS_IMPL_ISUPPORTS(PermissionObserver,
|
||||
nsIObserver,
|
||||
nsISupportsWeakReference)
|
||||
|
||||
PermissionObserver::PermissionObserver()
|
||||
{
|
||||
MOZ_ASSERT(!gInstance);
|
||||
}
|
||||
|
||||
PermissionObserver::~PermissionObserver()
|
||||
{
|
||||
MOZ_ASSERT(mSinks.IsEmpty());
|
||||
MOZ_ASSERT(gInstance == this);
|
||||
|
||||
gInstance = nullptr;
|
||||
}
|
||||
|
||||
/* static */ already_AddRefed<PermissionObserver>
|
||||
PermissionObserver::GetInstance()
|
||||
{
|
||||
nsRefPtr<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;
|
||||
}
|
||||
|
||||
gInstance = instance;
|
||||
}
|
||||
|
||||
return instance.forget();
|
||||
}
|
||||
|
||||
void
|
||||
PermissionObserver::AddSink(PermissionStatus* aSink)
|
||||
{
|
||||
MOZ_ASSERT(aSink);
|
||||
MOZ_ASSERT(!mSinks.Contains(aSink));
|
||||
|
||||
mSinks.AppendElement(aSink);
|
||||
}
|
||||
|
||||
void
|
||||
PermissionObserver::RemoveSink(PermissionStatus* aSink)
|
||||
{
|
||||
MOZ_ASSERT(aSink);
|
||||
MOZ_ASSERT(mSinks.Contains(aSink));
|
||||
|
||||
mSinks.RemoveElement(aSink);
|
||||
}
|
||||
|
||||
void
|
||||
PermissionObserver::Notify(PermissionName aName, nsIPrincipal& aPrincipal)
|
||||
{
|
||||
for (auto* sink : mSinks) {
|
||||
if (sink->mName != aName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nsIPrincipal* sinkPrincipal = sink->GetPrincipal();
|
||||
if (NS_WARN_IF(!sinkPrincipal) || !aPrincipal.Equals(sinkPrincipal)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sink->PermissionChanged();
|
||||
}
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
PermissionObserver::Observe(nsISupports* aSubject,
|
||||
const char* aTopic,
|
||||
const char16_t* aData)
|
||||
{
|
||||
MOZ_ASSERT(!strcmp(aTopic, "perm-changed"));
|
||||
|
||||
if (mSinks.IsEmpty()) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIPermission> perm = do_QueryInterface(aSubject);
|
||||
if (!perm) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIPrincipal> principal;
|
||||
perm->GetPrincipal(getter_AddRefs(principal));
|
||||
if (!principal) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsAutoCString type;
|
||||
perm->GetType(type);
|
||||
Maybe<PermissionName> permission = TypeToPermissionName(type.get());
|
||||
if (permission) {
|
||||
Notify(permission.value(), *principal);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
49
dom/permission/PermissionObserver.h
Normal file
49
dom/permission/PermissionObserver.h
Normal file
@ -0,0 +1,49 @@
|
||||
/* -*- 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/. */
|
||||
|
||||
#ifndef mozilla_dom_PermissionObserver_h_
|
||||
#define mozilla_dom_PermissionObserver_h_
|
||||
|
||||
#include "mozilla/dom/PermissionsBinding.h"
|
||||
|
||||
#include "nsIObserver.h"
|
||||
#include "nsIPrincipal.h"
|
||||
#include "nsTArray.h"
|
||||
#include "nsWeakReference.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
class PermissionStatus;
|
||||
|
||||
// Singleton that watches for perm-changed notifications in order to notify
|
||||
// PermissionStatus objects.
|
||||
class PermissionObserver final
|
||||
: public nsIObserver
|
||||
, public nsSupportsWeakReference
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIOBSERVER
|
||||
|
||||
static already_AddRefed<PermissionObserver> GetInstance();
|
||||
|
||||
void AddSink(PermissionStatus* aObs);
|
||||
void RemoveSink(PermissionStatus* aObs);
|
||||
|
||||
private:
|
||||
PermissionObserver();
|
||||
virtual ~PermissionObserver();
|
||||
|
||||
void Notify(PermissionName aName, nsIPrincipal& aPrincipal);
|
||||
|
||||
nsTArray<PermissionStatus*> mSinks;
|
||||
};
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
||||
|
||||
#endif
|
@ -9,6 +9,7 @@
|
||||
#include "mozilla/Services.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
#include "nsIPermissionManager.h"
|
||||
#include "PermissionObserver.h"
|
||||
#include "PermissionUtils.h"
|
||||
|
||||
namespace mozilla {
|
||||
@ -23,7 +24,7 @@ PermissionStatus::Create(nsPIDOMWindow* aWindow,
|
||||
*aStatus = nullptr;
|
||||
|
||||
UniquePtr<PermissionStatus> status(new PermissionStatus(aWindow, aName));
|
||||
nsresult rv = status->UpdateState();
|
||||
nsresult rv = status->Init();
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
@ -40,8 +41,29 @@ PermissionStatus::PermissionStatus(nsPIDOMWindow* aWindow,
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
PermissionStatus::~PermissionStatus()
|
||||
{
|
||||
if (mObserver) {
|
||||
mObserver->RemoveSink(this);
|
||||
}
|
||||
}
|
||||
|
||||
JSObject*
|
||||
@ -75,5 +97,21 @@ PermissionStatus::UpdateState()
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsIPrincipal*
|
||||
PermissionStatus::GetPrincipal() const
|
||||
{
|
||||
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(GetOwner());
|
||||
if (NS_WARN_IF(!window)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
nsIDocument* doc = window->GetExtantDoc();
|
||||
if (NS_WARN_IF(!doc)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return doc->NodePrincipal();
|
||||
}
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
||||
|
@ -14,9 +14,13 @@
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
class PermissionObserver;
|
||||
|
||||
class PermissionStatus final
|
||||
: public DOMEventTargetHelper
|
||||
{
|
||||
friend class PermissionObserver;
|
||||
|
||||
public:
|
||||
~PermissionStatus();
|
||||
|
||||
@ -34,10 +38,18 @@ public:
|
||||
private:
|
||||
PermissionStatus(nsPIDOMWindow* aWindow, PermissionName aName);
|
||||
|
||||
nsresult Init();
|
||||
|
||||
nsresult UpdateState();
|
||||
|
||||
nsIPrincipal* GetPrincipal() const;
|
||||
|
||||
void PermissionChanged() {}
|
||||
|
||||
PermissionName mName;
|
||||
PermissionState mState;
|
||||
|
||||
nsRefPtr<PermissionObserver> mObserver;
|
||||
};
|
||||
|
||||
} // namespace dom
|
||||
|
@ -10,6 +10,7 @@ EXPORTS.mozilla.dom += [
|
||||
]
|
||||
|
||||
UNIFIED_SOURCES += [
|
||||
'PermissionObserver.cpp',
|
||||
'Permissions.cpp',
|
||||
'PermissionStatus.cpp',
|
||||
'PermissionUtils.cpp',
|
||||
|
Loading…
Reference in New Issue
Block a user