2015-03-02 16:20:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_cache_Context_h
|
|
|
|
#define mozilla_dom_cache_Context_h
|
|
|
|
|
2020-05-11 15:09:02 +03:00
|
|
|
#include "mozilla/dom/SafeRefPtr.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/Types.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2015-03-16 17:10:36 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
2015-03-16 17:10:36 +03:00
|
|
|
#include "nsTObserverArray.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
class nsIEventTarget;
|
2015-03-16 17:10:36 +03:00
|
|
|
class nsIThread;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-06-30 15:59:53 +03:00
|
|
|
|
|
|
|
namespace quota {
|
|
|
|
|
|
|
|
class DirectoryLock;
|
|
|
|
|
|
|
|
} // namespace quota
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
namespace cache {
|
|
|
|
|
|
|
|
class Action;
|
|
|
|
class Manager;
|
|
|
|
|
|
|
|
// The Context class is RAII-style class for managing IO operations within the
|
|
|
|
// Cache.
|
|
|
|
//
|
|
|
|
// When a Context is created it performs the complicated steps necessary to
|
|
|
|
// initialize the QuotaManager. Action objects dispatched on the Context are
|
|
|
|
// delayed until this initialization is complete. They are then allow to
|
|
|
|
// execute on any specified thread. Once all references to the Context are
|
|
|
|
// gone, then the steps necessary to release the QuotaManager are performed.
|
2015-03-16 17:10:36 +03:00
|
|
|
// After initialization the Context holds a self reference, so it will stay
|
|
|
|
// alive until one of three conditions occur:
|
|
|
|
//
|
|
|
|
// 1) The Manager will call Context::AllowToClose() when all of the actors
|
|
|
|
// have removed themselves as listener. This means an idle context with
|
|
|
|
// no active DOM objects will close gracefully.
|
2015-06-30 15:59:27 +03:00
|
|
|
// 2) The QuotaManager aborts all operations so it can delete the files.
|
2020-12-09 11:13:44 +03:00
|
|
|
// In this case the QuotaManager calls Client::AbortOperationsForLocks()
|
|
|
|
// which in turn cancels all existing Action objects and then marks the
|
|
|
|
// Manager as invalid.
|
2015-03-16 17:10:36 +03:00
|
|
|
// 3) Browser shutdown occurs and the Manager calls Context::CancelAll().
|
|
|
|
//
|
|
|
|
// In either case, though, the Action objects must be destroyed first to
|
|
|
|
// allow the Context to be destroyed.
|
2015-03-02 16:20:00 +03:00
|
|
|
//
|
|
|
|
// While the Context performs operations asynchronously on threads, all of
|
|
|
|
// methods in its public interface must be called on the same thread
|
|
|
|
// originally used to create the Context.
|
|
|
|
//
|
|
|
|
// As an invariant, all Context objects must be destroyed before permitting
|
|
|
|
// the "profile-before-change" shutdown event to complete. This is ensured
|
|
|
|
// via the code in ShutdownObserver.cpp.
|
2020-05-11 15:09:55 +03:00
|
|
|
class Context final : public SafeRefCounted<Context> {
|
2021-08-06 15:10:13 +03:00
|
|
|
using DirectoryLock = mozilla::dom::quota::DirectoryLock;
|
2015-06-30 15:59:27 +03:00
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
public:
|
2015-03-16 17:10:36 +03:00
|
|
|
// Define a class allowing other threads to hold the Context alive. This also
|
|
|
|
// allows these other threads to safely close or cancel the Context.
|
2020-05-11 15:13:25 +03:00
|
|
|
class ThreadsafeHandle final : public AtomicSafeRefCounted<ThreadsafeHandle> {
|
2015-03-16 17:10:36 +03:00
|
|
|
friend class Context;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-16 17:10:36 +03:00
|
|
|
public:
|
2020-05-11 15:09:55 +03:00
|
|
|
explicit ThreadsafeHandle(SafeRefPtr<Context> aContext);
|
2015-03-16 17:10:36 +03:00
|
|
|
~ThreadsafeHandle();
|
|
|
|
|
2020-05-11 15:13:25 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(cache::Context::ThreadsafeHandle)
|
2015-03-16 17:10:36 +03:00
|
|
|
|
2020-05-11 15:13:25 +03:00
|
|
|
void AllowToClose();
|
|
|
|
void InvalidateAndAllowToClose();
|
|
|
|
|
|
|
|
private:
|
2015-03-16 17:10:36 +03:00
|
|
|
void AllowToCloseOnOwningThread();
|
|
|
|
void InvalidateAndAllowToCloseOnOwningThread();
|
|
|
|
|
2020-05-11 15:09:55 +03:00
|
|
|
void ContextDestroyed(Context& aContext);
|
2015-03-16 17:10:36 +03:00
|
|
|
|
|
|
|
// Cleared to allow the Context to close. Only safe to access on
|
|
|
|
// owning thread.
|
2020-05-11 15:09:55 +03:00
|
|
|
SafeRefPtr<Context> mStrongRef;
|
2015-03-16 17:10:36 +03:00
|
|
|
|
|
|
|
// Used to support cancelation even while the Context is already allowed
|
|
|
|
// to close. Cleared by ~Context() calling ContextDestroyed(). Only
|
|
|
|
// safe to access on owning thread.
|
|
|
|
Context* mWeakRef;
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
|
2015-03-16 17:10:36 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Different objects hold references to the Context while some work is being
|
|
|
|
// performed asynchronously. These objects must implement the Activity
|
|
|
|
// interface and register themselves with the AddActivity(). When they are
|
|
|
|
// destroyed they must call RemoveActivity(). This allows the Context to
|
|
|
|
// cancel any outstanding Activity work when the Context is cancelled.
|
|
|
|
class Activity {
|
|
|
|
public:
|
|
|
|
virtual void Cancel() = 0;
|
|
|
|
virtual bool MatchesCacheId(CacheId aCacheId) const = 0;
|
|
|
|
};
|
|
|
|
|
2015-03-24 05:23:45 +03:00
|
|
|
// Create a Context attached to the given Manager. The given Action
|
|
|
|
// will run on the QuotaManager IO thread. Note, this Action must
|
|
|
|
// be execute synchronously.
|
2020-05-11 15:09:55 +03:00
|
|
|
static SafeRefPtr<Context> Create(SafeRefPtr<Manager> aManager,
|
|
|
|
nsISerialEventTarget* aTarget,
|
2020-07-01 17:20:12 +03:00
|
|
|
SafeRefPtr<Action> aInitAction,
|
2020-05-11 15:09:55 +03:00
|
|
|
Maybe<Context&> aOldContext);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// Execute given action on the target once the quota manager has been
|
|
|
|
// initialized.
|
|
|
|
//
|
|
|
|
// Only callable from the thread that created the Context.
|
2020-07-01 17:20:12 +03:00
|
|
|
void Dispatch(SafeRefPtr<Action> aAction);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2020-12-09 11:13:44 +03:00
|
|
|
Maybe<DirectoryLock&> MaybeDirectoryLockRef() const;
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// Cancel any Actions running or waiting to run. This should allow the
|
|
|
|
// Context to be released and Listener::RemoveContext() will be called
|
2015-03-16 20:51:33 +03:00
|
|
|
// when complete.
|
2015-03-02 16:20:00 +03:00
|
|
|
//
|
|
|
|
// Only callable from the thread that created the Context.
|
|
|
|
void CancelAll();
|
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
// True if CancelAll() has been called.
|
|
|
|
bool IsCanceled() const;
|
|
|
|
|
2015-03-16 17:10:36 +03:00
|
|
|
// Like CancelAll(), but also marks the Manager as "invalid".
|
|
|
|
void Invalidate();
|
|
|
|
|
|
|
|
// Remove any self references and allow the Context to be released when
|
|
|
|
// there are no more Actions to process.
|
|
|
|
void AllowToClose();
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// Cancel any Actions running or waiting to run that operate on the given
|
|
|
|
// cache ID.
|
|
|
|
//
|
|
|
|
// Only callable from the thread that created the Context.
|
|
|
|
void CancelForCacheId(CacheId aCacheId);
|
|
|
|
|
2020-12-16 02:25:30 +03:00
|
|
|
void AddActivity(Activity& aActivity);
|
|
|
|
void RemoveActivity(Activity& aActivity);
|
2015-03-16 17:10:36 +03:00
|
|
|
|
2015-06-26 08:22:46 +03:00
|
|
|
// Tell the Context that some state information has been orphaned in the
|
|
|
|
// data store and won't be cleaned up. The Context will leave the marker
|
|
|
|
// in place to trigger cleanup the next times its opened.
|
|
|
|
void NoteOrphanedData();
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
private:
|
2015-05-07 15:16:51 +03:00
|
|
|
class Data;
|
2015-03-02 16:20:00 +03:00
|
|
|
class QuotaInitRunnable;
|
|
|
|
class ActionRunnable;
|
|
|
|
|
|
|
|
enum State {
|
2015-04-20 21:14:57 +03:00
|
|
|
STATE_CONTEXT_PREINIT,
|
2015-03-02 16:20:00 +03:00
|
|
|
STATE_CONTEXT_INIT,
|
|
|
|
STATE_CONTEXT_READY,
|
|
|
|
STATE_CONTEXT_CANCELED
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PendingAction {
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
2020-07-01 17:20:12 +03:00
|
|
|
SafeRefPtr<Action> mAction;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
2020-05-11 15:09:55 +03:00
|
|
|
void Init(Maybe<Context&> aOldContext);
|
2015-04-20 21:14:57 +03:00
|
|
|
void Start();
|
2020-07-01 17:20:12 +03:00
|
|
|
void DispatchAction(SafeRefPtr<Action> aAction, bool aDoomData = false);
|
2021-11-24 23:50:37 +03:00
|
|
|
void OnQuotaInit(nsresult aRv,
|
|
|
|
const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata,
|
2015-11-22 12:44:16 +03:00
|
|
|
already_AddRefed<DirectoryLock> aDirectoryLock);
|
|
|
|
|
2020-05-11 15:13:25 +03:00
|
|
|
SafeRefPtr<ThreadsafeHandle> CreateThreadsafeHandle();
|
2015-03-16 17:10:36 +03:00
|
|
|
|
2020-05-11 15:09:55 +03:00
|
|
|
void SetNextContext(SafeRefPtr<Context> aNextContext);
|
2015-04-20 21:14:57 +03:00
|
|
|
|
2015-05-07 15:16:51 +03:00
|
|
|
void DoomTargetData();
|
|
|
|
|
2020-05-11 15:09:02 +03:00
|
|
|
SafeRefPtr<Manager> mManager;
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mTarget;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Data> mData;
|
2015-03-02 16:20:00 +03:00
|
|
|
State mState;
|
2015-06-26 08:22:46 +03:00
|
|
|
bool mOrphanedData;
|
2021-11-24 23:50:37 +03:00
|
|
|
Maybe<CacheDirectoryMetadata> mDirectoryMetadata;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<QuotaInitRunnable> mInitRunnable;
|
2020-07-01 17:20:12 +03:00
|
|
|
SafeRefPtr<Action> mInitAction;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsTArray<PendingAction> mPendingActions;
|
|
|
|
|
2015-03-16 17:10:36 +03:00
|
|
|
// Weak refs since activites must remove themselves from this list before
|
|
|
|
// being destroyed by calling RemoveActivity().
|
2020-12-16 02:25:30 +03:00
|
|
|
nsTObserverArray<NotNull<Activity*>> mActivityList;
|
2015-03-16 17:10:36 +03:00
|
|
|
|
|
|
|
// The ThreadsafeHandle may have a strong ref back to us. This creates
|
|
|
|
// a ref-cycle that keeps the Context alive. The ref-cycle is broken
|
|
|
|
// when ThreadsafeHandle::AllowToClose() is called.
|
2020-05-11 15:13:25 +03:00
|
|
|
SafeRefPtr<ThreadsafeHandle> mThreadsafeHandle;
|
2015-03-16 17:10:36 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
RefPtr<DirectoryLock> mDirectoryLock;
|
2020-05-11 15:09:55 +03:00
|
|
|
SafeRefPtr<Context> mNextContext;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
public:
|
2020-05-11 15:09:55 +03:00
|
|
|
// XXX Consider adding a private guard parameter.
|
|
|
|
Context(SafeRefPtr<Manager> aManager, nsISerialEventTarget* aTarget,
|
2020-07-01 17:20:12 +03:00
|
|
|
SafeRefPtr<Action> aInitAction);
|
2020-05-11 15:09:55 +03:00
|
|
|
~Context();
|
|
|
|
|
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(cache::Context)
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_cache_Context_h
|