Bug 1826136 [wpt PR 39338] - Update wpt metadata, a=testonly
[gecko.git] / dom / cache / Action.h
blob1219c58e249c93aa15e9c169ffc43eb2d43b16dd
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_cache_Action_h
8 #define mozilla_dom_cache_Action_h
10 #include "mozilla/Atomics.h"
11 #include "mozilla/dom/cache/Types.h"
12 #include "mozilla/dom/SafeRefPtr.h"
13 #include "nsISupportsImpl.h"
15 class mozIStorageConnection;
17 namespace mozilla::dom::cache {
19 class Action : public SafeRefCounted<Action> {
20 public:
21 class Resolver {
22 public:
23 Resolver& operator=(const Resolver& aRHS) = delete;
25 // Note: Action must drop Resolver ref after calling Resolve()!
26 // Note: Must be called on the same thread used to execute
27 // Action::RunOnTarget().
28 virtual void Resolve(nsresult aRv) = 0;
30 inline SafeRefPtr<Action::Resolver> SafeRefPtrFromThis() {
31 return SafeRefPtr<Action::Resolver>{this, AcquireStrongRefFromRawPtr{}};
34 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
37 // Class containing data that can be opportunistically shared between
38 // multiple Actions running on the same thread/Context. In theory
39 // this could be abstracted to a generic key/value map, but for now
40 // just explicitly provide accessors for the data we need.
41 class Data {
42 public:
43 virtual mozIStorageConnection* GetConnection() const = 0;
45 virtual void SetConnection(mozIStorageConnection* aConn) = 0;
48 // virtual because deleted through base class pointer
49 virtual ~Action();
51 // Execute operations on the target thread. Once complete call
52 // Resolver::Resolve(). This can be done sync or async.
53 // Note: Action should hold Resolver ref until its ready to call Resolve().
54 // Note: The "target" thread is determined when the Action is scheduled on
55 // Context. The Action should not assume any particular thread is used.
56 virtual void RunOnTarget(
57 SafeRefPtr<Resolver> aResolver,
58 const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata,
59 Data* aOptionalData) = 0;
61 // Called on initiating thread when the Action is canceled. The Action is
62 // responsible for calling Resolver::Resolve() as normal; either with a
63 // normal error code or NS_ERROR_ABORT. If CancelOnInitiatingThread() is
64 // called after Resolve() has already occurred, then the cancel can be
65 // ignored.
67 // Cancellation is a best effort to stop processing as soon as possible, but
68 // does not guarantee the Action will not run.
70 // CancelOnInitiatingThread() may be called more than once. Subsequent
71 // calls should have no effect.
73 // Default implementation sets an internal cancellation flag that can be
74 // queried with IsCanceled().
75 virtual void CancelOnInitiatingThread();
77 // Executed on the initiating thread and is passed the nsresult given to
78 // Resolver::Resolve().
79 virtual void CompleteOnInitiatingThread(nsresult aRv) {}
81 // Executed on the initiating thread. If this Action will operate on the
82 // given cache ID then override this to return true.
83 virtual bool MatchesCacheId(CacheId aCacheId) const { return false; }
85 NS_DECL_OWNINGTHREAD
86 MOZ_DECLARE_REFCOUNTED_TYPENAME(cache::Action)
88 protected:
89 Action();
91 // Check if this Action has been canceled. May be called from any thread,
92 // but typically used from the target thread.
93 bool IsCanceled() const;
95 private:
96 // Accessible from any thread.
97 Atomic<bool> mCanceled;
100 } // namespace mozilla::dom::cache
102 #endif // mozilla_dom_cache_Action_h