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"
|
2015-08-11 20:04:35 +03:00
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.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
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-08-01 00:56:59 +03:00
|
|
|
/* static */ already_AddRefed<PermissionStatus>
|
2015-08-22 05:53:29 +03:00
|
|
|
PermissionStatus::Create(nsPIDOMWindow* aWindow,
|
|
|
|
PermissionName aName,
|
2015-08-01 00:56:59 +03:00
|
|
|
ErrorResult& aRv)
|
2015-08-22 05:53:29 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
PermissionStatus::PermissionStatus(nsPIDOMWindow* 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)
|
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)
|
|
|
|
{
|
|
|
|
return PermissionStatusBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
nsresult
|
|
|
|
PermissionStatus::UpdateState()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
if (NS_WARN_IF(!permMgr)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(GetOwner());
|
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t action = nsIPermissionManager::DENY_ACTION;
|
|
|
|
nsresult rv = permMgr->TestPermissionFromWindow(window,
|
|
|
|
PermissionNameToType(mName),
|
|
|
|
&action);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = ActionToPermissionState(action);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:53:29 +03:00
|
|
|
void
|
|
|
|
PermissionStatus::PermissionChanged()
|
|
|
|
{
|
|
|
|
auto oldState = mState;
|
|
|
|
UpdateState();
|
|
|
|
if (mState != oldState) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> eventDispatcher =
|
2015-08-22 05:53:29 +03:00
|
|
|
new AsyncEventDispatcher(this, NS_LITERAL_STRING("change"), false);
|
|
|
|
eventDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:33:46 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|