Line data Source code
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 "mozilla/Atomics.h"
11 : #include "mozilla/dom/cache/Types.h"
12 : #include "nsISupportsImpl.h"
13 :
14 : class mozIStorageConnection;
15 :
16 : namespace mozilla {
17 : namespace dom {
18 : namespace cache {
19 :
20 : class Action
21 : {
22 : public:
23 0 : class Resolver
24 : {
25 : public:
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 : NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
32 : };
33 :
34 : // Class containing data that can be opportunistically shared between
35 : // multiple Actions running on the same thread/Context. In theory
36 : // this could be abstracted to a generic key/value map, but for now
37 : // just explicitly provide accessors for the data we need.
38 0 : class Data
39 : {
40 : public:
41 : virtual mozIStorageConnection*
42 : GetConnection() const = 0;
43 :
44 : virtual void
45 : SetConnection(mozIStorageConnection* aConn) = 0;
46 : };
47 :
48 : // Execute operations on the target thread. Once complete call
49 : // Resolver::Resolve(). This can be done sync or async.
50 : // Note: Action should hold Resolver ref until its ready to call Resolve().
51 : // Note: The "target" thread is determined when the Action is scheduled on
52 : // Context. The Action should not assume any particular thread is used.
53 : virtual void RunOnTarget(Resolver* aResolver, const QuotaInfo& aQuotaInfo,
54 : Data* aOptionalData) = 0;
55 :
56 : // Called on initiating thread when the Action is canceled. The Action is
57 : // responsible for calling Resolver::Resolve() as normal; either with a
58 : // normal error code or NS_ERROR_ABORT. If CancelOnInitiatingThread() is
59 : // called after Resolve() has already occurred, then the cancel can be
60 : // ignored.
61 : //
62 : // Cancellation is a best effort to stop processing as soon as possible, but
63 : // does not guarantee the Action will not run.
64 : //
65 : // CancelOnInitiatingThread() may be called more than once. Subsequent
66 : // calls should have no effect.
67 : //
68 : // Default implementation sets an internal cancellation flag that can be
69 : // queried with IsCanceled().
70 : virtual void CancelOnInitiatingThread();
71 :
72 : // Executed on the initiating thread and is passed the nsresult given to
73 : // Resolver::Resolve().
74 0 : virtual void CompleteOnInitiatingThread(nsresult aRv) { }
75 :
76 : // Executed on the initiating thread. If this Action will operate on the
77 : // given cache ID then override this to return true.
78 0 : virtual bool MatchesCacheId(CacheId aCacheId) const { return false; }
79 :
80 0 : NS_INLINE_DECL_REFCOUNTING(cache::Action)
81 :
82 : protected:
83 : Action();
84 :
85 : // virtual because deleted through base class pointer
86 : virtual ~Action();
87 :
88 : // Check if this Action has been canceled. May be called from any thread,
89 : // but typically used from the target thread.
90 : bool IsCanceled() const;
91 :
92 : private:
93 : // Accessible from any thread.
94 : Atomic<bool> mCanceled;
95 : };
96 :
97 : } // namespace cache
98 : } // namespace dom
99 : } // namespace mozilla
100 :
101 : #endif // mozilla_dom_cache_Action_h
|