Blame dom/cache/Action.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#ifndef mozilla_dom_cache_Action_h
Packit f0b94e
#define mozilla_dom_cache_Action_h
Packit f0b94e
Packit f0b94e
#include "mozilla/Atomics.h"
Packit f0b94e
#include "mozilla/dom/cache/Types.h"
Packit f0b94e
#include "nsISupportsImpl.h"
Packit f0b94e
Packit f0b94e
class mozIStorageConnection;
Packit f0b94e
Packit f0b94e
namespace mozilla {
Packit f0b94e
namespace dom {
Packit f0b94e
namespace cache {
Packit f0b94e
Packit f0b94e
class Action {
Packit f0b94e
 public:
Packit f0b94e
  class Resolver {
Packit f0b94e
   public:
Packit f0b94e
    // Note: Action must drop Resolver ref after calling Resolve()!
Packit f0b94e
    // Note: Must be called on the same thread used to execute
Packit f0b94e
    //       Action::RunOnTarget().
Packit f0b94e
    virtual void Resolve(nsresult aRv) = 0;
Packit f0b94e
Packit f0b94e
    NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
Packit f0b94e
  };
Packit f0b94e
Packit f0b94e
  // Class containing data that can be opportunistically shared between
Packit f0b94e
  // multiple Actions running on the same thread/Context.  In theory
Packit f0b94e
  // this could be abstracted to a generic key/value map, but for now
Packit f0b94e
  // just explicitly provide accessors for the data we need.
Packit f0b94e
  class Data {
Packit f0b94e
   public:
Packit f0b94e
    virtual mozIStorageConnection* GetConnection() const = 0;
Packit f0b94e
Packit f0b94e
    virtual void SetConnection(mozIStorageConnection* aConn) = 0;
Packit f0b94e
  };
Packit f0b94e
Packit f0b94e
  // Execute operations on the target thread.  Once complete call
Packit f0b94e
  // Resolver::Resolve().  This can be done sync or async.
Packit f0b94e
  // Note: Action should hold Resolver ref until its ready to call Resolve().
Packit f0b94e
  // Note: The "target" thread is determined when the Action is scheduled on
Packit f0b94e
  //       Context.  The Action should not assume any particular thread is used.
Packit f0b94e
  virtual void RunOnTarget(Resolver* aResolver, const QuotaInfo& aQuotaInfo,
Packit f0b94e
                           Data* aOptionalData) = 0;
Packit f0b94e
Packit f0b94e
  // Called on initiating thread when the Action is canceled.  The Action is
Packit f0b94e
  // responsible for calling Resolver::Resolve() as normal; either with a
Packit f0b94e
  // normal error code or NS_ERROR_ABORT.  If CancelOnInitiatingThread() is
Packit f0b94e
  // called after Resolve() has already occurred, then the cancel can be
Packit f0b94e
  // ignored.
Packit f0b94e
  //
Packit f0b94e
  // Cancellation is a best effort to stop processing as soon as possible, but
Packit f0b94e
  // does not guarantee the Action will not run.
Packit f0b94e
  //
Packit f0b94e
  // CancelOnInitiatingThread() may be called more than once.  Subsequent
Packit f0b94e
  // calls should have no effect.
Packit f0b94e
  //
Packit f0b94e
  // Default implementation sets an internal cancellation flag that can be
Packit f0b94e
  // queried with IsCanceled().
Packit f0b94e
  virtual void CancelOnInitiatingThread();
Packit f0b94e
Packit f0b94e
  // Executed on the initiating thread and is passed the nsresult given to
Packit f0b94e
  // Resolver::Resolve().
Packit f0b94e
  virtual void CompleteOnInitiatingThread(nsresult aRv) {}
Packit f0b94e
Packit f0b94e
  // Executed on the initiating thread.  If this Action will operate on the
Packit f0b94e
  // given cache ID then override this to return true.
Packit f0b94e
  virtual bool MatchesCacheId(CacheId aCacheId) const { return false; }
Packit f0b94e
Packit f0b94e
  NS_INLINE_DECL_REFCOUNTING(cache::Action)
Packit f0b94e
Packit f0b94e
 protected:
Packit f0b94e
  Action();
Packit f0b94e
Packit f0b94e
  // virtual because deleted through base class pointer
Packit f0b94e
  virtual ~Action();
Packit f0b94e
Packit f0b94e
  // Check if this Action has been canceled.  May be called from any thread,
Packit f0b94e
  // but typically used from the target thread.
Packit f0b94e
  bool IsCanceled() const;
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  // Accessible from any thread.
Packit f0b94e
  Atomic<bool> mCanceled;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
}  // namespace cache
Packit f0b94e
}  // namespace dom
Packit f0b94e
}  // namespace mozilla
Packit f0b94e
Packit f0b94e
#endif  // mozilla_dom_cache_Action_h