Bug 1197461 - Implement Permissions.revoke. r=poiru,baku
authorMarco Castelluccio <mcastelluccio@mozilla.com>
Wed, 02 Mar 2016 21:09:38 +0000
changeset 324756 e978b825517d6d06d1071d179d5f0d89a069733c
parent 324755 c3e97c1f281977eb73c9c99490b6d600878c7fdd
child 324757 59b8d765123ce48d13db01a00a3d3fc628ad67e8
push id1128
push userjlund@mozilla.com
push dateWed, 01 Jun 2016 01:31:59 +0000
treeherdermozilla-release@fe0d30de989d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspoiru, baku
bugs1197461
milestone47.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 1197461 - Implement Permissions.revoke. r=poiru,baku
dom/permission/Permissions.cpp
dom/permission/Permissions.h
dom/webidl/Permissions.webidl
--- a/dom/permission/Permissions.cpp
+++ b/dom/permission/Permissions.cpp
@@ -116,10 +116,63 @@ Permissions::Query(JSContext* aCx,
     promise->MaybeReject(aRv);
   } else {
     MOZ_ASSERT(status);
     promise->MaybeResolve(status);
   }
   return promise.forget();
 }
 
+already_AddRefed<Promise>
+Permissions::Revoke(JSContext* aCx,
+                    JS::Handle<JSObject*> aPermission,
+                    ErrorResult& aRv)
+{
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow);
+  if (!global) {
+    aRv.Throw(NS_ERROR_UNEXPECTED);
+    return nullptr;
+  }
+
+  PermissionDescriptor permission;
+  JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aPermission));
+  if (NS_WARN_IF(!permission.Init(aCx, value))) {
+    aRv.Throw(NS_ERROR_UNEXPECTED);
+    return nullptr;
+  }
+
+  RefPtr<Promise> promise = Promise::Create(global, aRv);
+  if (NS_WARN_IF(aRv.Failed())) {
+    return nullptr;
+  }
+
+  nsCOMPtr<nsIDocument> document = mWindow->GetExtantDoc();
+  if (!document) {
+    promise->MaybeReject(NS_ERROR_UNEXPECTED);
+    return promise.forget();
+  }
+
+  nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
+  if (NS_WARN_IF(!permMgr)) {
+    promise->MaybeReject(NS_ERROR_FAILURE);
+    return promise.forget();
+  }
+
+  nsresult rv = permMgr->RemoveFromPrincipal(document->NodePrincipal(), PermissionNameToType(permission.mName));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    promise->MaybeReject(rv);
+    return promise.forget();
+  }
+
+  RefPtr<PermissionStatus> status =
+    CreatePermissionStatus(aCx, aPermission, mWindow, aRv);
+  if (NS_WARN_IF(aRv.Failed())) {
+    MOZ_ASSERT(!status);
+    promise->MaybeReject(aRv);
+  } else {
+    MOZ_ASSERT(status);
+    promise->MaybeResolve(status);
+  }
+  return promise.forget();
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/permission/Permissions.h
+++ b/dom/permission/Permissions.h
@@ -33,16 +33,20 @@ public:
 
   JSObject* WrapObject(JSContext* aCx,
                        JS::Handle<JSObject*> aGivenProto) override;
 
   already_AddRefed<Promise> Query(JSContext* aCx,
                                   JS::Handle<JSObject*> aPermission,
                                   ErrorResult& aRv);
 
+  already_AddRefed<Promise> Revoke(JSContext* aCx,
+                                   JS::Handle<JSObject*> aPermission,
+                                   ErrorResult& aRv);
+
 private:
   ~Permissions();
 
   nsCOMPtr<nsPIDOMWindowInner> mWindow;
 };
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/webidl/Permissions.webidl
+++ b/dom/webidl/Permissions.webidl
@@ -21,9 +21,11 @@ dictionary PermissionDescriptor {
 dictionary PushPermissionDescriptor : PermissionDescriptor {
   boolean userVisible = false;
 };
 
 [Exposed=(Window)]
 interface Permissions {
   [Throws]
   Promise<PermissionStatus> query(object permission);
+  [Throws]
+  Promise<PermissionStatus> revoke(object permission);
 };