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 "CacheCipherKeyManager.h"
11 #include "mozilla/Atomics.h"
12 #include "mozilla/dom/cache/Types.h"
13 #include "mozilla/dom/SafeRefPtr.h"
14 #include "nsISupportsImpl.h"
16 class mozIStorageConnection
;
18 namespace mozilla::dom::cache
{
20 class Action
: public SafeRefCounted
<Action
> {
24 Resolver
& operator=(const Resolver
& aRHS
) = delete;
26 // Note: Action must drop Resolver ref after calling Resolve()!
27 // Note: Must be called on the same thread used to execute
28 // Action::RunOnTarget().
29 virtual void Resolve(nsresult aRv
) = 0;
31 inline SafeRefPtr
<Action::Resolver
> SafeRefPtrFromThis() {
32 return SafeRefPtr
<Action::Resolver
>{this, AcquireStrongRefFromRawPtr
{}};
35 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
38 // Class containing data that can be opportunistically shared between
39 // multiple Actions running on the same thread/Context. In theory
40 // this could be abstracted to a generic key/value map, but for now
41 // just explicitly provide accessors for the data we need.
44 virtual mozIStorageConnection
* GetConnection() const = 0;
46 virtual void SetConnection(mozIStorageConnection
* aConn
) = 0;
49 // virtual because deleted through base class pointer
52 // Execute operations on the target thread. Once complete call
53 // Resolver::Resolve(). This can be done sync or async.
54 // Note: Action should hold Resolver ref until its ready to call Resolve().
55 // Note: The "target" thread is determined when the Action is scheduled on
56 // Context. The Action should not assume any particular thread is used.
57 virtual void RunOnTarget(
58 SafeRefPtr
<Resolver
> aResolver
,
59 const Maybe
<CacheDirectoryMetadata
>& aDirectoryMetadata
,
60 Data
* aOptionalData
, const Maybe
<CipherKey
>& aMaybeCipherKey
) = 0;
62 // Called on initiating thread when the Action is canceled. The Action is
63 // responsible for calling Resolver::Resolve() as normal; either with a
64 // normal error code or NS_ERROR_ABORT. If CancelOnInitiatingThread() is
65 // called after Resolve() has already occurred, then the cancel can be
68 // Cancellation is a best effort to stop processing as soon as possible, but
69 // does not guarantee the Action will not run.
71 // CancelOnInitiatingThread() may be called more than once. Subsequent
72 // calls should have no effect.
74 // Default implementation sets an internal cancellation flag that can be
75 // queried with IsCanceled().
76 virtual void CancelOnInitiatingThread();
78 // Executed on the initiating thread and is passed the nsresult given to
79 // Resolver::Resolve().
80 virtual void CompleteOnInitiatingThread(nsresult aRv
) {}
82 // Executed on the initiating thread. If this Action will operate on the
83 // given cache ID then override this to return true.
84 virtual bool MatchesCacheId(CacheId aCacheId
) const { return false; }
87 MOZ_DECLARE_REFCOUNTED_TYPENAME(cache::Action
)
92 // Check if this Action has been canceled. May be called from any thread,
93 // but typically used from the target thread.
94 bool IsCanceled() const;
97 // Accessible from any thread.
98 Atomic
<bool> mCanceled
;
101 } // namespace mozilla::dom::cache
103 #endif // mozilla_dom_cache_Action_h