Action.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_cache_Action_h
  6. #define mozilla_dom_cache_Action_h
  7. #include "mozilla/Atomics.h"
  8. #include "mozilla/dom/cache/Types.h"
  9. #include "nsISupportsImpl.h"
  10. class mozIStorageConnection;
  11. namespace mozilla {
  12. namespace dom {
  13. namespace cache {
  14. class Action
  15. {
  16. public:
  17. class Resolver
  18. {
  19. public:
  20. // Note: Action must drop Resolver ref after calling Resolve()!
  21. // Note: Must be called on the same thread used to execute
  22. // Action::RunOnTarget().
  23. virtual void Resolve(nsresult aRv) = 0;
  24. NS_IMETHOD_(MozExternalRefCountType)
  25. AddRef(void) = 0;
  26. NS_IMETHOD_(MozExternalRefCountType)
  27. Release(void) = 0;
  28. };
  29. // Class containing data that can be opportunistically shared between
  30. // multiple Actions running on the same thread/Context. In theory
  31. // this could be abstracted to a generic key/value map, but for now
  32. // just explicitly provide accessors for the data we need.
  33. class Data
  34. {
  35. public:
  36. virtual mozIStorageConnection*
  37. GetConnection() const = 0;
  38. virtual void
  39. SetConnection(mozIStorageConnection* aConn) = 0;
  40. };
  41. // Execute operations on the target thread. Once complete call
  42. // Resolver::Resolve(). This can be done sync or async.
  43. // Note: Action should hold Resolver ref until its ready to call Resolve().
  44. // Note: The "target" thread is determined when the Action is scheduled on
  45. // Context. The Action should not assume any particular thread is used.
  46. virtual void RunOnTarget(Resolver* aResolver, const QuotaInfo& aQuotaInfo,
  47. Data* aOptionalData) = 0;
  48. // Called on initiating thread when the Action is canceled. The Action is
  49. // responsible for calling Resolver::Resolve() as normal; either with a
  50. // normal error code or NS_ERROR_ABORT. If CancelOnInitiatingThread() is
  51. // called after Resolve() has already occurred, then the cancel can be
  52. // ignored.
  53. //
  54. // Cancellation is a best effort to stop processing as soon as possible, but
  55. // does not guarantee the Action will not run.
  56. //
  57. // CancelOnInitiatingThread() may be called more than once. Subsequent
  58. // calls should have no effect.
  59. //
  60. // Default implementation sets an internal cancellation flag that can be
  61. // queried with IsCanceled().
  62. virtual void CancelOnInitiatingThread();
  63. // Executed on the initiating thread and is passed the nsresult given to
  64. // Resolver::Resolve().
  65. virtual void CompleteOnInitiatingThread(nsresult aRv) { }
  66. // Executed on the initiating thread. If this Action will operate on the
  67. // given cache ID then override this to return true.
  68. virtual bool MatchesCacheId(CacheId aCacheId) const { return false; }
  69. NS_INLINE_DECL_REFCOUNTING(cache::Action)
  70. protected:
  71. Action();
  72. // virtual because deleted through base class pointer
  73. virtual ~Action();
  74. // Check if this Action has been canceled. May be called from any thread,
  75. // but typically used from the target thread.
  76. bool IsCanceled() const;
  77. private:
  78. // Accessible from any thread.
  79. Atomic<bool> mCanceled;
  80. };
  81. } // namespace cache
  82. } // namespace dom
  83. } // namespace mozilla
  84. #endif // mozilla_dom_cache_Action_h