File: ClientDirectoryLockHandle.h

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (122 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- 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/. */

#ifndef DOM_QUOTA_CLIENTDIRECTORYLOCKHANDLE_H_
#define DOM_QUOTA_CLIENTDIRECTORYLOCKHANDLE_H_

#include "mozilla/RefPtr.h"
#include "mozilla/dom/quota/ConditionalCompilation.h"
#include "nsISupportsImpl.h"

namespace mozilla::dom::quota {

class ClientDirectoryLock;

/**
 * @class ClientDirectoryLockHandle
 * @brief RAII-style wrapper for managing a ClientDirectoryLock.
 *
 * ClientDirectoryLockHandle is a RAII-style wrapper that manages a
 * ClientDirectoryLock created by QuotaManager::OpenClientDirectory.
 *
 * This class ensures that the associated directory lock remains acquired
 * while the handle is in scope and automatically drops it when destroyed.
 *
 * ## Usage:
 * - See QuotaManager::OpenClientDirectory for details on obtaining a
 *   ClientDirectoryLockHandle.
 * - The handle should be retained for as long as access to the directory is
 *   needed.
 *
 * ## Threading:
 * - Must be used only on the thread that created it, except that it may be
 *   safely destroyed from another thread after being moved (see also
 *   Destruction).
 * - `AssertIsOnOwningThread()` is primarily used internally to verify correct
 *   threading, but clients can use it for additional thread-safety checks if
 *   needed.
 *
 * ## Destruction:
 * - If the lock has already been dropped (e.g., due to move), the destructor
 *   does nothing.
 * - The destructor automatically drops the lock if it is still held.
 * - Thus, it is safe to destroy a handle from any thread as long as the handle
 *   was moved beforehand on the owning thread.
 *
 * ## Key Features:
 * - Move-only: Prevents accidental copies.
 * - Implicit boolean conversion to check if the handle holds a valid
 *   `ClientDirectoryLock`.
 * - Easy access to the underlying ClientDirectoryLock using `operator*` and
 *   `operator->`.
 * - Moved-from handles are placed in a well-defined inert state and can be
 *   safely inspected using `IsInert()` for diagnostic purposes.
 */
class ClientDirectoryLockHandle final {
 public:
  ClientDirectoryLockHandle();

  explicit ClientDirectoryLockHandle(
      RefPtr<ClientDirectoryLock> aClientDirectoryLock);

  ClientDirectoryLockHandle(const ClientDirectoryLockHandle&) = delete;

  ClientDirectoryLockHandle(ClientDirectoryLockHandle&& aOther) noexcept;

  ~ClientDirectoryLockHandle();

  void AssertIsOnOwningThread() const;

  ClientDirectoryLockHandle& operator=(const ClientDirectoryLockHandle&) =
      delete;

  ClientDirectoryLockHandle& operator=(
      ClientDirectoryLockHandle&& aOther) noexcept;

  explicit operator bool() const;

  ClientDirectoryLock* get() const;

  ClientDirectoryLock& operator*() const;

  ClientDirectoryLock* operator->() const;

  bool IsRegistered() const;

  void SetRegistered(bool aRegistered);

  /**
   * Returns true if this handle is in an inert state — either it was
   * default-constructed and never assigned a lock, or it was explicitly
   * cleared (via move).
   *
   * This method is primarily intended for use in destructors of objects that
   * own a ClientDirectoryLockHandle, to assert that the lock has been properly
   * dropped and cleared before destruction.
   *
   * It is safe to call this method at any time on the owning thread. It may
   * also be called from other threads during destruction, under the assumption
   * that no other thread is concurrently accessing or modifying the handle.
   *
   * This method should not be used for control flow or runtime decision
   * making.
   */
  DIAGNOSTICONLY(bool IsInert() const);

 private:
  NS_DECL_OWNINGTHREAD

  // If new members are added or existing ones are changed, make sure to update
  // the move constructor and move assignment operator accordingly to preserve
  // correct state during moves.
  RefPtr<ClientDirectoryLock> mClientDirectoryLock;

  bool mRegistered = false;
};

}  // namespace mozilla::dom::quota

#endif  // DOM_QUOTA_CLIENTDIRECTORYLOCKHANDLE_H_