1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/src/storage/DOMStorageIPC.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,201 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsDOMStorageIPC_h___ 1.10 +#define nsDOMStorageIPC_h___ 1.11 + 1.12 +#include "mozilla/dom/PStorageChild.h" 1.13 +#include "mozilla/dom/PStorageParent.h" 1.14 +#include "DOMStorageDBThread.h" 1.15 +#include "DOMStorageCache.h" 1.16 +#include "DOMStorageObserver.h" 1.17 +#include "mozilla/Mutex.h" 1.18 + 1.19 +namespace mozilla { 1.20 +namespace dom { 1.21 + 1.22 +class DOMLocalStorageManager; 1.23 + 1.24 +// Child side of the IPC protocol, exposes as DB interface but 1.25 +// is responsible to send all requests to the parent process 1.26 +// and expects asynchronous answers. Those are then transparently 1.27 +// forwarded back to consumers on the child process. 1.28 +class DOMStorageDBChild MOZ_FINAL : public DOMStorageDBBridge 1.29 + , public PStorageChild 1.30 +{ 1.31 +public: 1.32 + DOMStorageDBChild(DOMLocalStorageManager* aManager); 1.33 + virtual ~DOMStorageDBChild(); 1.34 + 1.35 + NS_IMETHOD_(MozExternalRefCountType) AddRef(void); 1.36 + NS_IMETHOD_(MozExternalRefCountType) Release(void); 1.37 + 1.38 + void AddIPDLReference(); 1.39 + void ReleaseIPDLReference(); 1.40 + 1.41 + virtual nsresult Init(); 1.42 + virtual nsresult Shutdown(); 1.43 + 1.44 + virtual void AsyncPreload(DOMStorageCacheBridge* aCache, bool aPriority = false); 1.45 + virtual void AsyncGetUsage(DOMStorageUsageBridge* aUsage); 1.46 + 1.47 + virtual void SyncPreload(DOMStorageCacheBridge* aCache, bool aForceSync = false); 1.48 + 1.49 + virtual nsresult AsyncAddItem(DOMStorageCacheBridge* aCache, const nsAString& aKey, const nsAString& aValue); 1.50 + virtual nsresult AsyncUpdateItem(DOMStorageCacheBridge* aCache, const nsAString& aKey, const nsAString& aValue); 1.51 + virtual nsresult AsyncRemoveItem(DOMStorageCacheBridge* aCache, const nsAString& aKey); 1.52 + virtual nsresult AsyncClear(DOMStorageCacheBridge* aCache); 1.53 + 1.54 + virtual void AsyncClearAll() 1.55 + { 1.56 + if (mScopesHavingData) { 1.57 + mScopesHavingData->Clear(); /* NO-OP on the child process otherwise */ 1.58 + } 1.59 + } 1.60 + 1.61 + virtual void AsyncClearMatchingScope(const nsACString& aScope) 1.62 + { /* NO-OP on the child process */ } 1.63 + 1.64 + virtual void AsyncFlush() 1.65 + { SendAsyncFlush(); } 1.66 + 1.67 + virtual bool ShouldPreloadScope(const nsACString& aScope); 1.68 + virtual void GetScopesHavingData(InfallibleTArray<nsCString>* aScopes) 1.69 + { NS_NOTREACHED("Not implemented for child process"); } 1.70 + 1.71 +private: 1.72 + bool RecvObserve(const nsCString& aTopic, 1.73 + const nsCString& aScopePrefix); 1.74 + bool RecvLoadItem(const nsCString& aScope, 1.75 + const nsString& aKey, 1.76 + const nsString& aValue); 1.77 + bool RecvLoadDone(const nsCString& aScope, 1.78 + const nsresult& aRv); 1.79 + bool RecvScopesHavingData(const InfallibleTArray<nsCString>& aScopes); 1.80 + bool RecvLoadUsage(const nsCString& aScope, 1.81 + const int64_t& aUsage); 1.82 + bool RecvError(const nsresult& aRv); 1.83 + 1.84 + nsTHashtable<nsCStringHashKey>& ScopesHavingData(); 1.85 + 1.86 + ThreadSafeAutoRefCnt mRefCnt; 1.87 + NS_DECL_OWNINGTHREAD 1.88 + 1.89 + // Held to get caches to forward answers to. 1.90 + nsRefPtr<DOMLocalStorageManager> mManager; 1.91 + 1.92 + // Scopes having data hash, for optimization purposes only 1.93 + nsAutoPtr<nsTHashtable<nsCStringHashKey> > mScopesHavingData; 1.94 + 1.95 + // List of caches waiting for preload. This ensures the contract that 1.96 + // AsyncPreload call references the cache for time of the preload. 1.97 + nsTHashtable<nsRefPtrHashKey<DOMStorageCacheBridge> > mLoadingCaches; 1.98 + 1.99 + // Status of the remote database 1.100 + nsresult mStatus; 1.101 + 1.102 + bool mIPCOpen; 1.103 +}; 1.104 + 1.105 + 1.106 +// Receives async requests from child processes and is responsible 1.107 +// to send back responses from the DB thread. Exposes as a fake 1.108 +// DOMStorageCache consumer. 1.109 +// Also responsible for forwardning all chrome operation notifications 1.110 +// such as cookie cleaning etc to the child process. 1.111 +class DOMStorageDBParent MOZ_FINAL : public PStorageParent 1.112 + , public DOMStorageObserverSink 1.113 +{ 1.114 +public: 1.115 + DOMStorageDBParent(); 1.116 + virtual ~DOMStorageDBParent(); 1.117 + 1.118 + virtual mozilla::ipc::IProtocol* 1.119 + CloneProtocol(Channel* aChannel, 1.120 + mozilla::ipc::ProtocolCloneContext* aCtx) MOZ_OVERRIDE; 1.121 + 1.122 + NS_IMETHOD_(MozExternalRefCountType) AddRef(void); 1.123 + NS_IMETHOD_(MozExternalRefCountType) Release(void); 1.124 + 1.125 + void AddIPDLReference(); 1.126 + void ReleaseIPDLReference(); 1.127 + 1.128 + bool IPCOpen() { return mIPCOpen; } 1.129 + 1.130 +public: 1.131 + // Fake cache class receiving async callbacks from DB thread, sending 1.132 + // them back to appropriate cache object on the child process. 1.133 + class CacheParentBridge : public DOMStorageCacheBridge { 1.134 + public: 1.135 + CacheParentBridge(DOMStorageDBParent* aParentDB, const nsACString& aScope) 1.136 + : mParent(aParentDB), mScope(aScope), mLoaded(false), mLoadedCount(0) {} 1.137 + virtual ~CacheParentBridge() {} 1.138 + 1.139 + // DOMStorageCacheBridge 1.140 + virtual const nsCString& Scope() const 1.141 + { return mScope; } 1.142 + virtual bool Loaded() 1.143 + { return mLoaded; } 1.144 + virtual uint32_t LoadedCount() 1.145 + { return mLoadedCount; } 1.146 + 1.147 + virtual bool LoadItem(const nsAString& aKey, const nsString& aValue); 1.148 + virtual void LoadDone(nsresult aRv); 1.149 + virtual void LoadWait(); 1.150 + 1.151 + private: 1.152 + nsRefPtr<DOMStorageDBParent> mParent; 1.153 + nsCString mScope; 1.154 + bool mLoaded; 1.155 + uint32_t mLoadedCount; 1.156 + }; 1.157 + 1.158 + // Fake usage class receiving async callbacks from DB thread 1.159 + class UsageParentBridge : public DOMStorageUsageBridge 1.160 + { 1.161 + public: 1.162 + UsageParentBridge(DOMStorageDBParent* aParentDB, const nsACString& aScope) 1.163 + : mParent(aParentDB), mScope(aScope) {} 1.164 + virtual ~UsageParentBridge() {} 1.165 + 1.166 + // DOMStorageUsageBridge 1.167 + virtual const nsCString& Scope() { return mScope; } 1.168 + virtual void LoadUsage(const int64_t usage); 1.169 + 1.170 + private: 1.171 + nsRefPtr<DOMStorageDBParent> mParent; 1.172 + nsCString mScope; 1.173 + }; 1.174 + 1.175 +private: 1.176 + // IPC 1.177 + bool RecvAsyncPreload(const nsCString& aScope, const bool& aPriority); 1.178 + bool RecvPreload(const nsCString& aScope, const uint32_t& aAlreadyLoadedCount, 1.179 + InfallibleTArray<nsString>* aKeys, InfallibleTArray<nsString>* aValues, 1.180 + nsresult* aRv); 1.181 + bool RecvAsyncGetUsage(const nsCString& aScope); 1.182 + bool RecvAsyncAddItem(const nsCString& aScope, const nsString& aKey, const nsString& aValue); 1.183 + bool RecvAsyncUpdateItem(const nsCString& aScope, const nsString& aKey, const nsString& aValue); 1.184 + bool RecvAsyncRemoveItem(const nsCString& aScope, const nsString& aKey); 1.185 + bool RecvAsyncClear(const nsCString& aScope); 1.186 + bool RecvAsyncFlush(); 1.187 + 1.188 + // DOMStorageObserverSink 1.189 + virtual nsresult Observe(const char* aTopic, const nsACString& aScopePrefix); 1.190 + 1.191 +private: 1.192 + CacheParentBridge* NewCache(const nsACString& aScope); 1.193 + 1.194 + ThreadSafeAutoRefCnt mRefCnt; 1.195 + NS_DECL_OWNINGTHREAD 1.196 + 1.197 + // True when IPC channel is open and Send*() methods are OK to use. 1.198 + bool mIPCOpen; 1.199 +}; 1.200 + 1.201 +} // ::dom 1.202 +} // ::mozilla 1.203 + 1.204 +#endif