2015-05-03 22:32:37 +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: */
|
2013-04-15 16:38:48 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
#ifndef mozilla_dom_StorageIPC_h
|
|
|
|
#define mozilla_dom_StorageIPC_h
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-08-08 23:56:49 +03:00
|
|
|
#include "mozilla/dom/PBackgroundStorageChild.h"
|
|
|
|
#include "mozilla/dom/PBackgroundStorageParent.h"
|
2017-01-04 16:53:01 +03:00
|
|
|
#include "StorageDBThread.h"
|
2017-05-17 08:01:14 +03:00
|
|
|
#include "LocalStorageCache.h"
|
2017-01-04 16:53:01 +03:00
|
|
|
#include "StorageObserver.h"
|
2013-04-15 16:38:48 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2016-01-05 15:25:00 +03:00
|
|
|
|
|
|
|
class OriginAttributesPattern;
|
|
|
|
|
2013-04-15 16:38:48 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
class LocalStorageManager;
|
2017-08-08 23:56:49 +03:00
|
|
|
class PBackgroundStorageParent;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
// Child side of the IPC protocol, exposes as DB interface but
|
|
|
|
// is responsible to send all requests to the parent process
|
|
|
|
// and expects asynchronous answers. Those are then transparently
|
|
|
|
// forwarded back to consumers on the child process.
|
2017-07-26 13:19:13 +03:00
|
|
|
class StorageDBChild final
|
|
|
|
: public PBackgroundStorageChild
|
2013-04-15 16:38:48 +04:00
|
|
|
{
|
2017-07-26 13:19:13 +03:00
|
|
|
class ShutdownObserver;
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
virtual ~StorageDBChild();
|
2014-06-23 23:56:07 +04:00
|
|
|
|
2013-04-15 16:38:48 +04:00
|
|
|
public:
|
2017-05-17 08:01:14 +03:00
|
|
|
explicit StorageDBChild(LocalStorageManager* aManager);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-07-26 13:19:13 +03:00
|
|
|
static StorageDBChild*
|
|
|
|
Get();
|
|
|
|
|
|
|
|
static StorageDBChild*
|
|
|
|
GetOrCreate();
|
|
|
|
|
2017-08-09 00:02:57 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(StorageDBChild);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
void AddIPDLReference();
|
|
|
|
void ReleaseIPDLReference();
|
|
|
|
|
|
|
|
virtual nsresult Init();
|
|
|
|
virtual nsresult Shutdown();
|
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual void AsyncPreload(LocalStorageCacheBridge* aCache,
|
|
|
|
bool aPriority = false);
|
2017-01-04 16:53:01 +03:00
|
|
|
virtual void AsyncGetUsage(StorageUsageBridge* aUsage);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual void SyncPreload(LocalStorageCacheBridge* aCache,
|
|
|
|
bool aForceSync = false);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual nsresult AsyncAddItem(LocalStorageCacheBridge* aCache,
|
2017-01-04 16:53:01 +03:00
|
|
|
const nsAString& aKey, const nsAString& aValue);
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual nsresult AsyncUpdateItem(LocalStorageCacheBridge* aCache,
|
2017-01-04 16:53:01 +03:00
|
|
|
const nsAString& aKey,
|
|
|
|
const nsAString& aValue);
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual nsresult AsyncRemoveItem(LocalStorageCacheBridge* aCache,
|
2017-01-04 16:53:01 +03:00
|
|
|
const nsAString& aKey);
|
2017-05-17 08:01:14 +03:00
|
|
|
virtual nsresult AsyncClear(LocalStorageCacheBridge* aCache);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
virtual void AsyncClearAll()
|
2013-09-02 12:41:57 +04:00
|
|
|
{
|
2016-01-05 15:25:00 +03:00
|
|
|
if (mOriginsHavingData) {
|
|
|
|
mOriginsHavingData->Clear(); /* NO-OP on the child process otherwise */
|
2013-09-02 12:41:57 +04:00
|
|
|
}
|
|
|
|
}
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2016-01-05 15:25:00 +03:00
|
|
|
virtual void AsyncClearMatchingOrigin(const nsACString& aOriginNoSuffix)
|
2017-07-26 13:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Shouldn't be called!");
|
|
|
|
}
|
2016-01-05 15:25:00 +03:00
|
|
|
|
|
|
|
virtual void AsyncClearMatchingOriginAttributes(const OriginAttributesPattern& aPattern)
|
2017-07-26 13:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Shouldn't be called!");
|
|
|
|
}
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
virtual void AsyncFlush()
|
2017-07-26 13:19:13 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("Shouldn't be called!");
|
|
|
|
}
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2016-01-05 15:25:00 +03:00
|
|
|
virtual bool ShouldPreloadOrigin(const nsACString& aOriginNoSuffix);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
private:
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult RecvObserve(const nsCString& aTopic,
|
|
|
|
const nsString& aOriginAttributesPattern,
|
2017-11-06 06:37:28 +03:00
|
|
|
const nsCString& aOriginScope) override;
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult RecvLoadItem(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const nsString& aKey,
|
2017-11-06 06:37:28 +03:00
|
|
|
const nsString& aValue) override;
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult RecvLoadDone(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
2017-11-06 06:37:28 +03:00
|
|
|
const nsresult& aRv) override;
|
|
|
|
mozilla::ipc::IPCResult RecvOriginsHavingData(nsTArray<nsCString>&& aOrigins) override;
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult RecvLoadUsage(const nsCString& aOriginNoSuffix,
|
2017-11-06 06:37:28 +03:00
|
|
|
const int64_t& aUsage) override;
|
|
|
|
mozilla::ipc::IPCResult RecvError(const nsresult& aRv) override;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2016-01-05 15:25:00 +03:00
|
|
|
nsTHashtable<nsCStringHashKey>& OriginsHavingData();
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
// Held to get caches to forward answers to.
|
2017-05-17 08:01:14 +03:00
|
|
|
RefPtr<LocalStorageManager> mManager;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2016-01-05 15:25:00 +03:00
|
|
|
// Origins having data hash, for optimization purposes only
|
|
|
|
nsAutoPtr<nsTHashtable<nsCStringHashKey>> mOriginsHavingData;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
// List of caches waiting for preload. This ensures the contract that
|
|
|
|
// AsyncPreload call references the cache for time of the preload.
|
2017-05-17 08:01:14 +03:00
|
|
|
nsTHashtable<nsRefPtrHashKey<LocalStorageCacheBridge>> mLoadingCaches;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
// Status of the remote database
|
|
|
|
nsresult mStatus;
|
|
|
|
|
|
|
|
bool mIPCOpen;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Receives async requests from child processes and is responsible
|
|
|
|
// to send back responses from the DB thread. Exposes as a fake
|
2017-05-17 08:01:14 +03:00
|
|
|
// LocalStorageCache consumer.
|
2013-04-15 16:38:48 +04:00
|
|
|
// Also responsible for forwardning all chrome operation notifications
|
|
|
|
// such as cookie cleaning etc to the child process.
|
2017-08-08 23:56:49 +03:00
|
|
|
class StorageDBParent final : public PBackgroundStorageParent
|
2013-04-15 16:38:48 +04:00
|
|
|
{
|
2017-08-09 00:01:33 +03:00
|
|
|
class ObserverSink;
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
virtual ~StorageDBParent();
|
2014-06-23 23:56:07 +04:00
|
|
|
|
2013-04-15 16:38:48 +04:00
|
|
|
public:
|
2017-08-09 00:01:52 +03:00
|
|
|
explicit StorageDBParent(const nsString& aProfilePath);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-08-09 00:01:33 +03:00
|
|
|
void
|
|
|
|
Init();
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release(void);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
void AddIPDLReference();
|
|
|
|
void ReleaseIPDLReference();
|
|
|
|
|
|
|
|
bool IPCOpen() { return mIPCOpen; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Fake cache class receiving async callbacks from DB thread, sending
|
|
|
|
// them back to appropriate cache object on the child process.
|
2017-05-17 08:01:14 +03:00
|
|
|
class CacheParentBridge : public LocalStorageCacheBridge {
|
2013-04-15 16:38:48 +04:00
|
|
|
public:
|
2017-01-04 16:53:01 +03:00
|
|
|
CacheParentBridge(StorageDBParent* aParentDB,
|
2016-01-05 15:25:00 +03:00
|
|
|
const nsACString& aOriginSuffix,
|
|
|
|
const nsACString& aOriginNoSuffix)
|
2017-07-26 13:19:13 +03:00
|
|
|
: mOwningEventTarget(GetCurrentThreadSerialEventTarget())
|
|
|
|
, mParent(aParentDB)
|
2016-01-05 15:25:00 +03:00
|
|
|
, mOriginSuffix(aOriginSuffix), mOriginNoSuffix(aOriginNoSuffix)
|
|
|
|
, mLoaded(false), mLoadedCount(0) {}
|
2013-04-15 16:38:48 +04:00
|
|
|
virtual ~CacheParentBridge() {}
|
|
|
|
|
2017-05-17 08:01:14 +03:00
|
|
|
// LocalStorageCacheBridge
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual const nsCString Origin() const override;
|
|
|
|
virtual const nsCString& OriginNoSuffix() const override
|
2016-01-05 15:25:00 +03:00
|
|
|
{ return mOriginNoSuffix; }
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual const nsCString& OriginSuffix() const override
|
2016-01-05 15:25:00 +03:00
|
|
|
{ return mOriginSuffix; }
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual bool Loaded() override
|
2013-04-15 16:38:48 +04:00
|
|
|
{ return mLoaded; }
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual uint32_t LoadedCount() override
|
2013-04-15 16:38:48 +04:00
|
|
|
{ return mLoadedCount; }
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual bool LoadItem(const nsAString& aKey, const nsString& aValue) override;
|
|
|
|
virtual void LoadDone(nsresult aRv) override;
|
|
|
|
virtual void LoadWait() override;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-07-26 13:19:13 +03:00
|
|
|
NS_IMETHOD_(void)
|
2017-11-06 06:37:28 +03:00
|
|
|
Release(void) override;
|
2017-07-26 13:19:13 +03:00
|
|
|
|
2013-04-15 16:38:48 +04:00
|
|
|
private:
|
2017-07-26 13:19:13 +03:00
|
|
|
void
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
|
2017-01-04 16:53:01 +03:00
|
|
|
RefPtr<StorageDBParent> mParent;
|
2016-01-05 15:25:00 +03:00
|
|
|
nsCString mOriginSuffix, mOriginNoSuffix;
|
2013-04-15 16:38:48 +04:00
|
|
|
bool mLoaded;
|
|
|
|
uint32_t mLoadedCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fake usage class receiving async callbacks from DB thread
|
2017-01-04 16:53:01 +03:00
|
|
|
class UsageParentBridge : public StorageUsageBridge
|
2013-04-15 16:38:48 +04:00
|
|
|
{
|
|
|
|
public:
|
2017-01-04 16:53:01 +03:00
|
|
|
UsageParentBridge(StorageDBParent* aParentDB,
|
|
|
|
const nsACString& aOriginScope)
|
2017-07-26 13:19:13 +03:00
|
|
|
: mOwningEventTarget(GetCurrentThreadSerialEventTarget())
|
|
|
|
, mParent(aParentDB)
|
|
|
|
, mOriginScope(aOriginScope) {}
|
2013-04-15 16:38:48 +04:00
|
|
|
virtual ~UsageParentBridge() {}
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
// StorageUsageBridge
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual const nsCString& OriginScope() override { return mOriginScope; }
|
|
|
|
virtual void LoadUsage(const int64_t usage) override;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-07-26 13:19:13 +03:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType)
|
2017-11-06 06:37:28 +03:00
|
|
|
Release(void) override;
|
2017-07-26 13:19:13 +03:00
|
|
|
|
2013-04-15 16:38:48 +04:00
|
|
|
private:
|
2017-07-26 13:19:13 +03:00
|
|
|
void
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
|
2017-01-04 16:53:01 +03:00
|
|
|
RefPtr<StorageDBParent> mParent;
|
2016-01-05 15:25:00 +03:00
|
|
|
nsCString mOriginScope;
|
2013-04-15 16:38:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
// IPC
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
|
2017-08-09 00:02:57 +03:00
|
|
|
mozilla::ipc::IPCResult RecvDeleteMe() override;
|
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
mozilla::ipc::IPCResult RecvAsyncPreload(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const bool& aPriority) override;
|
|
|
|
mozilla::ipc::IPCResult RecvPreload(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const uint32_t& aAlreadyLoadedCount,
|
|
|
|
InfallibleTArray<nsString>* aKeys,
|
|
|
|
InfallibleTArray<nsString>* aValues,
|
2016-11-15 06:26:00 +03:00
|
|
|
nsresult* aRv) override;
|
|
|
|
mozilla::ipc::IPCResult RecvAsyncGetUsage(const nsCString& aOriginNoSuffix) override;
|
2017-01-04 16:53:01 +03:00
|
|
|
mozilla::ipc::IPCResult RecvAsyncAddItem(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const nsString& aKey,
|
|
|
|
const nsString& aValue) override;
|
|
|
|
mozilla::ipc::IPCResult RecvAsyncUpdateItem(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const nsString& aKey,
|
|
|
|
const nsString& aValue) override;
|
|
|
|
mozilla::ipc::IPCResult RecvAsyncRemoveItem(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const nsString& aKey) override;
|
|
|
|
mozilla::ipc::IPCResult RecvAsyncClear(const nsCString& aOriginSuffix,
|
|
|
|
const nsCString& aOriginNoSuffix) override;
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult RecvAsyncFlush() override;
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-08-09 00:01:33 +03:00
|
|
|
mozilla::ipc::IPCResult RecvStartup() override;
|
|
|
|
mozilla::ipc::IPCResult RecvClearAll() override;
|
|
|
|
mozilla::ipc::IPCResult RecvClearMatchingOrigin(
|
|
|
|
const nsCString& aOriginNoSuffix) override;
|
|
|
|
mozilla::ipc::IPCResult RecvClearMatchingOriginAttributes(
|
|
|
|
const OriginAttributesPattern& aPattern) override;
|
|
|
|
|
|
|
|
void Observe(const nsCString& aTopic,
|
|
|
|
const nsString& aOriginAttrPattern,
|
|
|
|
const nsCString& aOriginScope);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
|
|
|
private:
|
2017-01-04 16:53:01 +03:00
|
|
|
CacheParentBridge* NewCache(const nsACString& aOriginSuffix,
|
|
|
|
const nsACString& aOriginNoSuffix);
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-08-09 00:01:33 +03:00
|
|
|
RefPtr<ObserverSink> mObserverSink;
|
|
|
|
|
2017-08-09 00:01:52 +03:00
|
|
|
// A hack to deal with deadlock between the parent process main thread and
|
|
|
|
// background thread when invoking StorageDBThread::GetOrCreate because it
|
|
|
|
// cannot safely perform a synchronous dispatch back to the main thread
|
|
|
|
// (because we are already synchronously doing things on the stack).
|
|
|
|
// Populated for the same process actors, empty for other process actors.
|
|
|
|
nsString mProfilePath;
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
ThreadSafeAutoRefCnt mRefCnt;
|
|
|
|
NS_DECL_OWNINGTHREAD
|
2015-01-07 07:51:13 +03:00
|
|
|
|
|
|
|
// True when IPC channel is open and Send*() methods are OK to use.
|
2013-04-15 16:38:48 +04:00
|
|
|
bool mIPCOpen;
|
|
|
|
};
|
|
|
|
|
2017-08-08 23:56:49 +03:00
|
|
|
PBackgroundStorageParent*
|
2017-08-09 00:01:52 +03:00
|
|
|
AllocPBackgroundStorageParent(const nsString& aProfilePath);
|
2017-08-08 23:56:49 +03:00
|
|
|
|
|
|
|
mozilla::ipc::IPCResult
|
2017-08-09 00:01:52 +03:00
|
|
|
RecvPBackgroundStorageConstructor(PBackgroundStorageParent* aActor,
|
|
|
|
const nsString& aProfilePath);
|
2017-08-08 23:56:49 +03:00
|
|
|
|
|
|
|
bool
|
|
|
|
DeallocPBackgroundStorageParent(PBackgroundStorageParent* aActor);
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-04-15 16:38:48 +04:00
|
|
|
|
2017-01-04 16:53:01 +03:00
|
|
|
#endif // mozilla_dom_StorageIPC_h
|