From d6ccefe440b3084947578247bbaf70eef18e8651 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Fri, 31 Jul 2015 15:49:59 -0700 Subject: [PATCH] Bug 1105827 - Part 7: Add helpers to convert between PermissionName and permission type. r=baku --- dom/permission/PermissionUtils.cpp | 33 ++++++++++++++++++++++++++++++ dom/permission/PermissionUtils.h | 5 +++++ dom/permission/Permissions.cpp | 13 ++++++------ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/dom/permission/PermissionUtils.cpp b/dom/permission/PermissionUtils.cpp index 70a478b4305d..0095b3dd877d 100644 --- a/dom/permission/PermissionUtils.cpp +++ b/dom/permission/PermissionUtils.cpp @@ -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(aName)]; +} + +Maybe +TypeToPermissionName(const char* aType) +{ + for (size_t i = 0; i < ArrayLength(kPermissionTypes); ++i) { + if (!strcmp(aType, kPermissionTypes[i])) { + return Some(static_cast(i)); + } + } + + return Nothing(); +} + PermissionState ActionToPermissionState(uint32_t aAction) { diff --git a/dom/permission/PermissionUtils.h b/dom/permission/PermissionUtils.h index ebf6a43c435b..34bf7573d876 100644 --- a/dom/permission/PermissionUtils.h +++ b/dom/permission/PermissionUtils.h @@ -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 TypeToPermissionName(const char* aType); + PermissionState ActionToPermissionState(uint32_t aAction); } // namespace dom diff --git a/dom/permission/Permissions.cpp b/dom/permission/Permissions.cpp index 36e046dd587e..acb7be81abc1 100644 --- a/dom/permission/Permissions.cpp +++ b/dom/permission/Permissions.cpp @@ -43,11 +43,10 @@ Permissions::WrapObject(JSContext* aCx, JS::Handle aGivenProto) namespace { nsresult -CheckPermission(const char* aName, +CheckPermission(PermissionName aName, nsPIDOMWindow* aWindow, PermissionState& aResult) { - MOZ_ASSERT(aName); MOZ_ASSERT(aWindow); nsCOMPtr 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);