Backed out changeset fc34b9b8e874 (bug 1105827)
authorBirunthan Mohanathas <birunthan@mohanathas.com>
Tue, 11 Aug 2015 10:04:35 -0700
changeset 284596 2ec72d95fe874f4c72375ca61eb008ede8f09c09
parent 284595 7b6f60b570c212adbefff135e9a8308280174359
child 284597 019492cdccf98e6edea0dc23ac41817b07c562a8
push id4313
push useralwu@mozilla.com
push dateWed, 12 Aug 2015 06:36:08 +0000
bugs1105827
milestone43.0a1
backs outfc34b9b8e87464bb423d0f2d7843448d7e970732
Backed out changeset fc34b9b8e874 (bug 1105827)
dom/permission/PermissionStatus.cpp
dom/permission/PermissionStatus.h
dom/permission/Permissions.cpp
--- a/dom/permission/PermissionStatus.cpp
+++ b/dom/permission/PermissionStatus.cpp
@@ -1,79 +1,33 @@
 /* -*- 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/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,
-                                   PermissionName aName)
+                                   PermissionState aState)
   : DOMEventTargetHelper(aWindow)
-  , mName(aName)
-  , mState(PermissionState::Denied)
+  , mState(aState)
 {
 }
 
 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,45 +2,37 @@
 /* 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:
-  ~PermissionStatus();
-
-  static nsresult Create(nsPIDOMWindow* aWindow,
-                         PermissionName aName,
-                         PermissionStatus** aStatus);
+  explicit PermissionStatus(nsPIDOMWindow* aWindow, PermissionState aState);
 
   JSObject* WrapObject(JSContext* aCx,
                        JS::Handle<JSObject*> aGivenProto) override;
 
   PermissionState State() const { return mState; }
 
   IMPL_EVENT_HANDLER(change)
 
 private:
-  PermissionStatus(nsPIDOMWindow* aWindow, PermissionName aName);
+  ~PermissionStatus();
 
-  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,53 +38,77 @@ JSObject*
 Permissions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return PermissionsBinding::Wrap(aCx, this, aGivenProto);
 }
 
 namespace {
 
 nsresult
-CreatePushPermissionStatus(JSContext* aCx,
-                           JS::Handle<JSObject*> aPermission,
-                           nsPIDOMWindow* aWindow,
-                           PermissionStatus** aResult)
+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)
 {
   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 PermissionStatus::Create(aWindow, permission.mName, aResult);
+  return CheckPermission(permission.mName, aWindow, aResult);
 }
 
 nsresult
-CreatePermissionStatus(JSContext* aCx,
-                       JS::Handle<JSObject*> aPermission,
-                       nsPIDOMWindow* aWindow,
-                       PermissionStatus** aResult)
+CheckPermission(JSContext* aCx,
+                JS::Handle<JSObject*> aPermission,
+                nsPIDOMWindow* aWindow,
+                PermissionState& 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 PermissionStatus::Create(aWindow, permission.mName, aResult);
+      return CheckPermission(permission.mName, aWindow, aResult);
 
     case PermissionName::Push:
-      return CreatePushPermissionStatus(aCx, aPermission, aWindow, aResult);
+      return CheckPushPermission(aCx, aPermission, aWindow, aResult);
 
     case PermissionName::Midi:
     default:
       return NS_ERROR_NOT_IMPLEMENTED;
   }
 }
 
 } // namespace
@@ -100,22 +124,20 @@ Permissions::Query(JSContext* aCx,
     return nullptr;
   }
 
   nsRefPtr<Promise> promise = Promise::Create(global, aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     return nullptr;
   }
 
-  PermissionStatus* status = nullptr;
-  nsresult rv = CreatePermissionStatus(aCx, aPermission, mWindow, &status);
+  PermissionState state = PermissionState::Denied;
+  nsresult rv = CheckPermission(aCx, aPermission, mWindow, state);
   if (NS_WARN_IF(NS_FAILED(rv))) {
-    MOZ_ASSERT(!status);
     promise->MaybeReject(rv);
   } else {
-    MOZ_ASSERT(status);
-    promise->MaybeResolve(status);
+    promise->MaybeResolve(new PermissionStatus(mWindow, state));
   }
   return promise.forget();
 }
 
 } // namespace dom
 } // namespace mozilla