Action.h (3737B)
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/. */ 6 7 #ifndef mozilla_dom_cache_Action_h 8 #define mozilla_dom_cache_Action_h 9 10 #include "CacheCipherKeyManager.h" 11 #include "mozilla/Atomics.h" 12 #include "mozilla/dom/SafeRefPtr.h" 13 #include "mozilla/dom/cache/Types.h" 14 #include "nsISupportsImpl.h" 15 16 class mozIStorageConnection; 17 18 namespace mozilla::dom::cache { 19 20 class Action : public SafeRefCounted<Action> { 21 public: 22 class Resolver { 23 public: 24 Resolver& operator=(const Resolver& aRHS) = delete; 25 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; 30 31 inline SafeRefPtr<Action::Resolver> SafeRefPtrFromThis() { 32 return SafeRefPtr<Action::Resolver>{this, AcquireStrongRefFromRawPtr{}}; 33 } 34 35 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 36 }; 37 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. 42 class Data { 43 public: 44 virtual mozIStorageConnection* GetConnection() const = 0; 45 46 virtual void SetConnection(mozIStorageConnection* aConn) = 0; 47 }; 48 49 // virtual because deleted through base class pointer 50 virtual ~Action(); 51 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; 61 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 66 // ignored. 67 // 68 // Cancellation is a best effort to stop processing as soon as possible, but 69 // does not guarantee the Action will not run. 70 // 71 // CancelOnInitiatingThread() may be called more than once. Subsequent 72 // calls should have no effect. 73 // 74 // Default implementation sets an internal cancellation flag that can be 75 // queried with IsCanceled(). 76 virtual void CancelOnInitiatingThread(); 77 78 // Executed on the initiating thread and is passed the nsresult given to 79 // Resolver::Resolve(). 80 virtual void CompleteOnInitiatingThread(nsresult aRv) {} 81 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; } 85 86 NS_DECL_OWNINGTHREAD 87 MOZ_DECLARE_REFCOUNTED_TYPENAME(cache::Action) 88 89 protected: 90 Action(); 91 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; 95 96 private: 97 // Accessible from any thread. 98 Atomic<bool> mCanceled; 99 }; 100 101 } // namespace mozilla::dom::cache 102 103 #endif // mozilla_dom_cache_Action_h