Bug 1105827 - Part 7: Add helpers to convert between PermissionName and permission type. r=baku

This commit is contained in:
Birunthan Mohanathas 2015-07-31 15:49:59 -07:00
parent a1f31ea066
commit d6ccefe440
3 changed files with 44 additions and 7 deletions

View File

@ -9,6 +9,39 @@
namespace mozilla {
namespace dom {
const char* kPermissionTypes[] = {
"geo",
"desktop-notification",
"push",
"midi"
};
// `-1` for the last null entry.
const size_t kPermissionNameCount =
ArrayLength(PermissionNameValues::strings) - 1;
static_assert(ArrayLength(kPermissionTypes) == kPermissionNameCount,
"kPermissionTypes and PermissionName count should match");
const char*
PermissionNameToType(PermissionName aName)
{
MOZ_ASSERT((size_t)aName < ArrayLength(kPermissionTypes));
return kPermissionTypes[static_cast<size_t>(aName)];
}
Maybe<PermissionName>
TypeToPermissionName(const char* aType)
{
for (size_t i = 0; i < ArrayLength(kPermissionTypes); ++i) {
if (!strcmp(aType, kPermissionTypes[i])) {
return Some(static_cast<PermissionName>(i));
}
}
return Nothing();
}
PermissionState
ActionToPermissionState(uint32_t aAction)
{

View File

@ -8,10 +8,15 @@
#define mozilla_dom_PermissionUtils_h_
#include "mozilla/dom/PermissionsBinding.h"
#include "mozilla/dom/PermissionStatusBinding.h"
#include "mozilla/Maybe.h"
namespace mozilla {
namespace dom {
const char* PermissionNameToType(PermissionName aName);
Maybe<PermissionName> TypeToPermissionName(const char* aType);
PermissionState ActionToPermissionState(uint32_t aAction);
} // namespace dom

View File

@ -43,11 +43,10 @@ Permissions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
namespace {
nsresult
CheckPermission(const char* aName,
CheckPermission(PermissionName aName,
nsPIDOMWindow* aWindow,
PermissionState& aResult)
{
MOZ_ASSERT(aName);
MOZ_ASSERT(aWindow);
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
@ -56,7 +55,9 @@ CheckPermission(const char* aName,
}
uint32_t action = nsIPermissionManager::DENY_ACTION;
nsresult rv = permMgr->TestPermissionFromWindow(aWindow, aName, &action);
nsresult rv = permMgr->TestPermissionFromWindow(aWindow,
PermissionNameToType(aName),
&action);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_FAILURE;
}
@ -81,7 +82,7 @@ CheckPushPermission(JSContext* aCx,
return NS_ERROR_NOT_IMPLEMENTED;
}
return CheckPermission("push", aWindow, aResult);
return CheckPermission(permission.mName, aWindow, aResult);
}
nsresult
@ -98,10 +99,8 @@ CheckPermission(JSContext* aCx,
switch (permission.mName) {
case PermissionName::Geolocation:
return CheckPermission("geo", aWindow, aResult);
case PermissionName::Notifications:
return CheckPermission("desktop-notification", aWindow, aResult);
return CheckPermission(permission.mName, aWindow, aResult);
case PermissionName::Push:
return CheckPushPermission(aCx, aPermission, aWindow, aResult);