lock.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_LOCKS_LOCK_H_
  5. #define THIRD_PARTY_BLINK_RENDERER_MODULES_LOCKS_LOCK_H_
  6. #include "third_party/blink/public/mojom/feature_observer/feature_observer.mojom-blink.h"
  7. #include "third_party/blink/public/mojom/locks/lock_manager.mojom-blink.h"
  8. #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
  9. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  10. #include "third_party/blink/renderer/platform/heap/handle.h"
  11. #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_remote.h"
  12. #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
  13. #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  14. #include "third_party/blink/renderer/platform/wtf/vector.h"
  15. namespace blink {
  16. class LockManager;
  17. class ScriptPromise;
  18. class ScriptPromiseResolver;
  19. class ScriptState;
  20. class Lock final : public ScriptWrappable,
  21. public ExecutionContextLifecycleObserver {
  22. DEFINE_WRAPPERTYPEINFO();
  23. public:
  24. Lock(ScriptState*,
  25. const String& name,
  26. mojom::blink::LockMode,
  27. mojo::PendingAssociatedRemote<mojom::blink::LockHandle>,
  28. mojo::PendingRemote<mojom::blink::ObservedFeature>,
  29. LockManager*);
  30. ~Lock() override;
  31. void Trace(Visitor*) const override;
  32. // Lock.idl
  33. String name() const { return name_; }
  34. String mode() const;
  35. // ExecutionContextLifecycleObserver
  36. void ContextDestroyed() override;
  37. // The lock is held until the passed promise resolves. When it is released,
  38. // the passed resolver is invoked with the promise's result.
  39. void HoldUntil(ScriptPromise, ScriptPromiseResolver*);
  40. static mojom::blink::LockMode StringToMode(const String&);
  41. static String ModeToString(mojom::blink::LockMode);
  42. private:
  43. class ThenFunction;
  44. void ReleaseIfHeld();
  45. void OnConnectionError();
  46. Member<ScriptPromiseResolver> resolver_;
  47. const String name_;
  48. const mojom::blink::LockMode mode_;
  49. // An opaque handle; this one end of a mojo pipe. When this is closed,
  50. // the lock is released by the back end.
  51. HeapMojoAssociatedRemote<mojom::blink::LockHandle> handle_;
  52. HeapMojoRemote<mojom::blink::ObservedFeature> lock_lifetime_;
  53. // LockManager::OnLockReleased() is called when this lock is released, to
  54. // stop artificially keeping this instance alive. It is necessary in the
  55. // case where the resolver's promise could potentially be GC'd.
  56. Member<LockManager> manager_;
  57. };
  58. } // namespace blink
  59. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_LOCKS_LOCK_H_