forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1725734 - Basic Web Locks implementation r=smaug,asuth
Differential Revision: https://phabricator.services.mozilla.com/D122656
- Loading branch information
Showing
36 changed files
with
671 additions
and
1,118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim:set ts=2 sw=2 sts=2 et cindent: */ | ||
/* 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/Lock.h" | ||
#include "mozilla/dom/LockBinding.h" | ||
#include "mozilla/dom/LockManager.h" | ||
#include "mozilla/dom/Promise.h" | ||
|
||
namespace mozilla::dom { | ||
|
||
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Lock, mOwner, mLockManager, | ||
mWaitingPromise, mReleasedPromise) | ||
NS_IMPL_CYCLE_COLLECTING_ADDREF(Lock) | ||
NS_IMPL_CYCLE_COLLECTING_RELEASE(Lock) | ||
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Lock) | ||
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY | ||
NS_INTERFACE_MAP_ENTRY(nsISupports) | ||
NS_INTERFACE_MAP_END | ||
|
||
Lock::Lock(nsIGlobalObject* aGlobal, const RefPtr<LockManager>& aLockManager, | ||
const nsString& aName, LockMode aMode, | ||
const RefPtr<Promise>& aReleasedPromise, ErrorResult& aRv) | ||
: mOwner(aGlobal), | ||
mLockManager(aLockManager), | ||
mName(aName), | ||
mMode(aMode), | ||
mWaitingPromise(Promise::Create(aGlobal, aRv)), | ||
mReleasedPromise(aReleasedPromise) { | ||
MOZ_ASSERT(aLockManager); | ||
MOZ_ASSERT(aReleasedPromise); | ||
} | ||
|
||
JSObject* Lock::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { | ||
return Lock_Binding::Wrap(aCx, this, aGivenProto); | ||
} | ||
|
||
void Lock::GetName(nsString& aRetVal) const { aRetVal = mName; } | ||
|
||
LockMode Lock::Mode() const { return mMode; } | ||
|
||
Promise& Lock::GetWaitingPromise() { | ||
MOZ_ASSERT(mWaitingPromise); | ||
return *mWaitingPromise; | ||
} | ||
|
||
void Lock::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) { | ||
RefPtr<Lock> pin = this; | ||
// decrements the refcount, may delete this | ||
mLockManager->ReleaseHeldLock(this); | ||
mReleasedPromise->MaybeResolve(aValue); | ||
} | ||
|
||
void Lock::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) { | ||
RefPtr<Lock> pin = this; | ||
// decrements the refcount, may delete this | ||
mLockManager->ReleaseHeldLock(this); | ||
mReleasedPromise->MaybeReject(aValue); | ||
} | ||
|
||
} // namespace mozilla::dom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim:set ts=2 sw=2 sts=2 et cindent: */ | ||
/* 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_Lock_h | ||
#define mozilla_dom_Lock_h | ||
|
||
#include "js/TypeDecls.h" | ||
#include "mozilla/Attributes.h" | ||
#include "mozilla/ErrorResult.h" | ||
#include "mozilla/dom/BindingDeclarations.h" | ||
#include "mozilla/dom/LockManagerBinding.h" | ||
#include "mozilla/dom/PromiseNativeHandler.h" | ||
#include "nsCycleCollectionParticipant.h" | ||
#include "nsWrapperCache.h" | ||
|
||
namespace mozilla::dom { | ||
|
||
class LockManager; | ||
|
||
class Lock final : public PromiseNativeHandler, public nsWrapperCache { | ||
friend class LockManager; | ||
|
||
public: | ||
NS_DECL_CYCLE_COLLECTING_ISUPPORTS | ||
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Lock) | ||
|
||
Lock(nsIGlobalObject* aGlobal, const RefPtr<LockManager>& aLockManager, | ||
const nsString& aName, LockMode aMode, | ||
const RefPtr<Promise>& aReleasedPromise, ErrorResult& aRv); | ||
|
||
bool operator==(const Lock& aOther) const { | ||
// This operator is needed to remove released locks from the queue. | ||
// This assumes each lock has a unique promise | ||
MOZ_ASSERT(mReleasedPromise && aOther.mReleasedPromise, | ||
"Promises are null when locks are unreleased??"); | ||
return mReleasedPromise == aOther.mReleasedPromise; | ||
} | ||
|
||
protected: | ||
~Lock() = default; | ||
|
||
public: | ||
nsIGlobalObject* GetParentObject() const { return mOwner; }; | ||
|
||
JSObject* WrapObject(JSContext* aCx, | ||
JS::Handle<JSObject*> aGivenProto) override; | ||
|
||
void GetName(nsString& aRetVal) const; | ||
|
||
LockMode Mode() const; | ||
|
||
Promise& GetWaitingPromise(); | ||
|
||
// PromiseNativeHandler | ||
virtual void ResolvedCallback(JSContext* aCx, | ||
JS::Handle<JS::Value> aValue) override; | ||
virtual void RejectedCallback(JSContext* aCx, | ||
JS::Handle<JS::Value> aValue) override; | ||
|
||
private: | ||
nsCOMPtr<nsIGlobalObject> mOwner; | ||
RefPtr<LockManager> mLockManager; | ||
|
||
nsString mName; | ||
LockMode mMode; | ||
RefPtr<Promise> mWaitingPromise; | ||
RefPtr<Promise> mReleasedPromise; | ||
}; | ||
|
||
} // namespace mozilla::dom | ||
|
||
#endif // mozilla_dom_Lock_h |
Oops, something went wrong.