Bug 1105827 - Part 8: Move permission checking into PermissionStatus. r=baku
☠☠ backed out by 2ec72d95fe87 ☠ ☠
authorBirunthan Mohanathas <birunthan@mohanathas.com>
Fri, 31 Jul 2015 15:53:10 -0700
changeset 291563 fc34b9b8e87464bb423d0f2d7843448d7e970732
parent 291562 92d2843b5938ad6c2f29212780382f13edabc241
child 291564 7ee1772ffc10ba685e52e9c4c4d6a3dc419dec8d
push id962
push userjlund@mozilla.com
push dateFri, 04 Dec 2015 23:28:54 +0000
treeherdermozilla-release@23a2d286e80f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1105827
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1105827 - Part 8: Move permission checking into PermissionStatus. r=baku This is in prepartion of a subsequent change where we will update the state within PermissionStatus.
dom/permission/PermissionStatus.cpp
dom/permission/PermissionStatus.h
dom/permission/Permissions.cpp
--- a/dom/permission/PermissionStatus.cpp
+++ b/dom/permission/PermissionStatus.cpp
@@ -1,33 +1,79 @@
 /* -*- 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"
+
 #include "mozilla/Services.h"
-
+#include "mozilla/UniquePtr.h"
 #include "nsIPermissionManager.h"
+#include "PermissionUtils.h"
 
 namespace mozilla {
 namespace dom {
 
+/* static */ nsresult
+PermissionStatus::Create(nsPIDOMWindow* aWindow,
+                         PermissionName aName,
+                         PermissionStatus** aStatus)
+{
+  MOZ_ASSERT(aStatus);
+  *aStatus = nullptr;
+
+  UniquePtr<PermissionStatus> status(new PermissionStatus(aWindow, aName));
+  nsresult rv = status->UpdateState();
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  *aStatus = status.release();
+  return NS_OK;
+}
+
 PermissionStatus::PermissionStatus(nsPIDOMWindow* aWindow,
-                                   PermissionState aState)
+                                   PermissionName aName)
   : DOMEventTargetHelper(aWindow)
-  , mState(aState)
+  , mName(aName)
+  , mState(PermissionState::Denied)
 {
 }
 
 PermissionStatus::~PermissionStatus()
 {
 }
 
 JSObject*
 PermissionStatus::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return PermissionStatusBinding::Wrap(aCx, this, aGivenProto);
 }
 
+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;
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/permission/PermissionStatus.h
+++ b/dom/permission/PermissionStatus.h
@@ -2,37 +2,45 @@
 /* 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/. */
 
 #ifndef mozilla_dom_PermissionStatus_h_
 #define mozilla_dom_PermissionStatus_h_
 
+#include "mozilla/dom/PermissionsBinding.h"
 #include "mozilla/dom/PermissionStatusBinding.h"
 #include "mozilla/DOMEventTargetHelper.h"
 
 namespace mozilla {
 namespace dom {
 
 class PermissionStatus final
   : public DOMEventTargetHelper
 {
 public:
-  explicit PermissionStatus(nsPIDOMWindow* aWindow, PermissionState aState);
+  ~PermissionStatus();
+
+  static nsresult Create(nsPIDOMWindow* aWindow,
+                         PermissionName aName,
+                         PermissionStatus** aStatus);
 
   JSObject* WrapObject(JSContext* aCx,
                        JS::Handle<JSObject*> aGivenProto) override;
 
   PermissionState State() const { return mState; }
 
   IMPL_EVENT_HANDLER(change)
 
 private:
-  ~PermissionStatus();
+  PermissionStatus(nsPIDOMWindow* aWindow, PermissionName aName);
 
+  nsresult UpdateState();
+
+  PermissionName mName;
   PermissionState mState;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_permissionstatus_h_
--- a/dom/permission/Permissions.cpp
+++ b/dom/permission/Permissions.cpp
@@ -38,77 +38,53 @@ JSObject*
 Permissions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return PermissionsBinding::Wrap(aCx, this, aGivenProto);
 }
 
 namespace {
 
 nsresult
-CheckPermission(PermissionName aName,
-                nsPIDOMWindow* aWindow,
-                PermissionState& aResult)
-{
-  MOZ_ASSERT(aWindow);
-
-  nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
-  if (NS_WARN_IF(!permMgr)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  uint32_t action = nsIPermissionManager::DENY_ACTION;
-  nsresult rv = permMgr->TestPermissionFromWindow(aWindow,
-                                                  PermissionNameToType(aName),
-                                                  &action);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return NS_ERROR_FAILURE;
-  }
-
-  aResult = ActionToPermissionState(action);
-  return NS_OK;
-}
-
-nsresult
-CheckPushPermission(JSContext* aCx,
-                    JS::Handle<JSObject*> aPermission,
-                    nsPIDOMWindow* aWindow,
-                    PermissionState& aResult)
+CreatePushPermissionStatus(JSContext* aCx,
+                           JS::Handle<JSObject*> aPermission,
+                           nsPIDOMWindow* aWindow,
+                           PermissionStatus** aResult)
 {
   PushPermissionDescriptor permission;
   JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
   if (NS_WARN_IF(!permission.Init(aCx, value))) {
     return NS_ERROR_UNEXPECTED;
   }
 
   if (permission.mUserVisible) {
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
-  return CheckPermission(permission.mName, aWindow, aResult);
+  return PermissionStatus::Create(aWindow, permission.mName, aResult);
 }
 
 nsresult
-CheckPermission(JSContext* aCx,
-                JS::Handle<JSObject*> aPermission,
-                nsPIDOMWindow* aWindow,
-                PermissionState& aResult)
+CreatePermissionStatus(JSContext* aCx,
+                       JS::Handle<JSObject*> aPermission,
+                       nsPIDOMWindow* aWindow,
+                       PermissionStatus** aResult)
 {
   PermissionDescriptor permission;
   JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
   if (NS_WARN_IF(!permission.Init(aCx, value))) {
     return NS_ERROR_UNEXPECTED;
   }
 
   switch (permission.mName) {
     case PermissionName::Geolocation:
     case PermissionName::Notifications:
-      return CheckPermission(permission.mName, aWindow, aResult);
+      return PermissionStatus::Create(aWindow, permission.mName, aResult);
 
     case PermissionName::Push:
-      return CheckPushPermission(aCx, aPermission, aWindow, aResult);
+      return CreatePushPermissionStatus(aCx, aPermission, aWindow, aResult);
 
     case PermissionName::Midi:
     default:
       return NS_ERROR_NOT_IMPLEMENTED;
   }
 }
 
 } // namespace
@@ -124,20 +100,22 @@ Permissions::Query(JSContext* aCx,
     return nullptr;
   }
 
   nsRefPtr<Promise> promise = Promise::Create(global, aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     return nullptr;
   }
 
-  PermissionState state = PermissionState::Denied;
-  nsresult rv = CheckPermission(aCx, aPermission, mWindow, state);
+  PermissionStatus* status = nullptr;
+  nsresult rv = CreatePermissionStatus(aCx, aPermission, mWindow, &status);
   if (NS_WARN_IF(NS_FAILED(rv))) {
+    MOZ_ASSERT(!status);
     promise->MaybeReject(rv);
   } else {
-    promise->MaybeResolve(new PermissionStatus(mWindow, state));
+    MOZ_ASSERT(status);
+    promise->MaybeResolve(status);
   }
   return promise.forget();
 }
 
 } // namespace dom
 } // namespace mozilla