2015-07-28 13:33:46 +00: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"
|
|
|
|
|
|
|
|
#include "mozilla/dom/PermissionsBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-07-28 13:33:46 +00:00
|
|
|
#include "mozilla/Services.h"
|
2015-07-28 13:33:46 +00:00
|
|
|
#include "nsIPermissionManager.h"
|
2015-08-22 02:53:29 +00:00
|
|
|
#include "PermissionUtils.h"
|
2015-07-28 13:33:46 +00: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)
|
|
|
|
|
|
|
|
Permissions::Permissions(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Permissions::~Permissions()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
Permissions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return PermissionsBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2015-07-28 13:33:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-07-31 21:56:59 +00:00
|
|
|
already_AddRefed<PermissionStatus>
|
2015-08-22 02:53:29 +00:00
|
|
|
CreatePushPermissionStatus(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
nsPIDOMWindow* aWindow,
|
2015-07-31 21:56:59 +00:00
|
|
|
ErrorResult& aRv)
|
2015-07-28 13:33:46 +00:00
|
|
|
{
|
|
|
|
PushPermissionDescriptor permission;
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
|
|
|
|
if (NS_WARN_IF(!permission.Init(aCx, value))) {
|
2015-07-31 21:56:59 +00:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2015-07-28 13:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (permission.mUserVisible) {
|
2015-12-18 13:02:50 +00:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
2015-07-31 21:56:59 +00:00
|
|
|
return nullptr;
|
2015-07-28 13:33:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 21:56:59 +00:00
|
|
|
return PermissionStatus::Create(aWindow, permission.mName, aRv);
|
2015-07-28 13:33:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 21:56:59 +00:00
|
|
|
already_AddRefed<PermissionStatus>
|
2015-08-22 02:53:29 +00:00
|
|
|
CreatePermissionStatus(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
nsPIDOMWindow* aWindow,
|
2015-07-31 21:56:59 +00:00
|
|
|
ErrorResult& aRv)
|
2015-07-28 13:33:46 +00:00
|
|
|
{
|
|
|
|
PermissionDescriptor permission;
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
|
|
|
|
if (NS_WARN_IF(!permission.Init(aCx, value))) {
|
2015-07-31 21:56:59 +00:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2015-07-28 13:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (permission.mName) {
|
|
|
|
case PermissionName::Geolocation:
|
|
|
|
case PermissionName::Notifications:
|
2015-07-31 21:56:59 +00:00
|
|
|
return PermissionStatus::Create(aWindow, permission.mName, aRv);
|
2015-07-28 13:33:46 +00:00
|
|
|
|
|
|
|
case PermissionName::Push:
|
2015-07-31 21:56:59 +00:00
|
|
|
return CreatePushPermissionStatus(aCx, aPermission, aWindow, aRv);
|
2015-07-28 13:33:46 +00:00
|
|
|
|
|
|
|
default:
|
2015-12-18 13:02:50 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled type");
|
2015-07-31 21:56:59 +00:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
2015-07-28 13:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-07-28 13:33:46 +00:00
|
|
|
already_AddRefed<Promise>
|
2015-07-28 13:33:46 +00:00
|
|
|
Permissions::Query(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPermission,
|
|
|
|
ErrorResult& aRv)
|
2015-07-28 13:33:46 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow);
|
|
|
|
if (!global) {
|
2015-07-28 13:33:46 +00:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2015-07-28 13:33:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
2015-07-28 13:33:46 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<PermissionStatus> status =
|
2015-07-31 21:56:59 +00:00
|
|
|
CreatePermissionStatus(aCx, aPermission, mWindow, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-08-22 02:53:29 +00:00
|
|
|
MOZ_ASSERT(!status);
|
2015-07-31 21:56:59 +00:00
|
|
|
promise->MaybeReject(aRv);
|
2015-07-28 13:33:46 +00:00
|
|
|
} else {
|
2015-08-22 02:53:29 +00:00
|
|
|
MOZ_ASSERT(status);
|
|
|
|
promise->MaybeResolve(status);
|
2015-07-28 13:33:46 +00:00
|
|
|
}
|
2015-07-28 13:33:46 +00:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|