Bug 1105827 - Part 11: Use nsRefPtr to store PermissionStatus. r=baku

This commit is contained in:
Birunthan Mohanathas 2015-07-31 14:56:59 -07:00
Родитель e08489fa66
Коммит e53e26f42e
3 изменённых файлов: 31 добавлений и 32 удалений

Просмотреть файл

@ -8,7 +8,6 @@
#include "mozilla/AsyncEventDispatcher.h" #include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Services.h" #include "mozilla/Services.h"
#include "mozilla/UniquePtr.h"
#include "nsIPermissionManager.h" #include "nsIPermissionManager.h"
#include "PermissionObserver.h" #include "PermissionObserver.h"
#include "PermissionUtils.h" #include "PermissionUtils.h"
@ -16,22 +15,18 @@
namespace mozilla { namespace mozilla {
namespace dom { namespace dom {
/* static */ nsresult /* static */ already_AddRefed<PermissionStatus>
PermissionStatus::Create(nsPIDOMWindow* aWindow, PermissionStatus::Create(nsPIDOMWindow* aWindow,
PermissionName aName, PermissionName aName,
PermissionStatus** aStatus) ErrorResult& aRv)
{ {
MOZ_ASSERT(aStatus); nsRefPtr<PermissionStatus> status = new PermissionStatus(aWindow, aName);
*aStatus = nullptr; aRv = status->Init();
if (NS_WARN_IF(aRv.Failed())) {
UniquePtr<PermissionStatus> status(new PermissionStatus(aWindow, aName)); return nullptr;
nsresult rv = status->Init();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
} }
*aStatus = status.release(); return status.forget();
return NS_OK;
} }
PermissionStatus::PermissionStatus(nsPIDOMWindow* aWindow, PermissionStatus::PermissionStatus(nsPIDOMWindow* aWindow,

Просмотреть файл

@ -22,11 +22,9 @@ class PermissionStatus final
friend class PermissionObserver; friend class PermissionObserver;
public: public:
~PermissionStatus(); static already_AddRefed<PermissionStatus> Create(nsPIDOMWindow* aWindow,
PermissionName aName,
static nsresult Create(nsPIDOMWindow* aWindow, ErrorResult& aRv);
PermissionName aName,
PermissionStatus** aStatus);
JSObject* WrapObject(JSContext* aCx, JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override; JS::Handle<JSObject*> aGivenProto) override;
@ -36,6 +34,8 @@ public:
IMPL_EVENT_HANDLER(change) IMPL_EVENT_HANDLER(change)
private: private:
~PermissionStatus();
PermissionStatus(nsPIDOMWindow* aWindow, PermissionName aName); PermissionStatus(nsPIDOMWindow* aWindow, PermissionName aName);
nsresult Init(); nsresult Init();

Просмотреть файл

@ -42,48 +42,52 @@ Permissions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
namespace { namespace {
nsresult already_AddRefed<PermissionStatus>
CreatePushPermissionStatus(JSContext* aCx, CreatePushPermissionStatus(JSContext* aCx,
JS::Handle<JSObject*> aPermission, JS::Handle<JSObject*> aPermission,
nsPIDOMWindow* aWindow, nsPIDOMWindow* aWindow,
PermissionStatus** aResult) ErrorResult& aRv)
{ {
PushPermissionDescriptor permission; PushPermissionDescriptor permission;
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission)); JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
if (NS_WARN_IF(!permission.Init(aCx, value))) { if (NS_WARN_IF(!permission.Init(aCx, value))) {
return NS_ERROR_UNEXPECTED; aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
} }
if (permission.mUserVisible) { if (permission.mUserVisible) {
return NS_ERROR_NOT_IMPLEMENTED; aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
} }
return PermissionStatus::Create(aWindow, permission.mName, aResult); return PermissionStatus::Create(aWindow, permission.mName, aRv);
} }
nsresult already_AddRefed<PermissionStatus>
CreatePermissionStatus(JSContext* aCx, CreatePermissionStatus(JSContext* aCx,
JS::Handle<JSObject*> aPermission, JS::Handle<JSObject*> aPermission,
nsPIDOMWindow* aWindow, nsPIDOMWindow* aWindow,
PermissionStatus** aResult) ErrorResult& aRv)
{ {
PermissionDescriptor permission; PermissionDescriptor permission;
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission)); JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
if (NS_WARN_IF(!permission.Init(aCx, value))) { if (NS_WARN_IF(!permission.Init(aCx, value))) {
return NS_ERROR_UNEXPECTED; aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
} }
switch (permission.mName) { switch (permission.mName) {
case PermissionName::Geolocation: case PermissionName::Geolocation:
case PermissionName::Notifications: case PermissionName::Notifications:
return PermissionStatus::Create(aWindow, permission.mName, aResult); return PermissionStatus::Create(aWindow, permission.mName, aRv);
case PermissionName::Push: case PermissionName::Push:
return CreatePushPermissionStatus(aCx, aPermission, aWindow, aResult); return CreatePushPermissionStatus(aCx, aPermission, aWindow, aRv);
case PermissionName::Midi: case PermissionName::Midi:
default: default:
return NS_ERROR_NOT_IMPLEMENTED; aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
return nullptr;
} }
} }
@ -105,11 +109,11 @@ Permissions::Query(JSContext* aCx,
return nullptr; return nullptr;
} }
PermissionStatus* status = nullptr; nsRefPtr<PermissionStatus> status =
nsresult rv = CreatePermissionStatus(aCx, aPermission, mWindow, &status); CreatePermissionStatus(aCx, aPermission, mWindow, aRv);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(aRv.Failed())) {
MOZ_ASSERT(!status); MOZ_ASSERT(!status);
promise->MaybeReject(rv); promise->MaybeReject(aRv);
} else { } else {
MOZ_ASSERT(status); MOZ_ASSERT(status);
promise->MaybeResolve(status); promise->MaybeResolve(status);