1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef nsDOMStorageIPC_h___
7 #define nsDOMStorageIPC_h___
9 #include "mozilla/dom/PStorageChild.h"
10 #include "mozilla/dom/PStorageParent.h"
11 #include "DOMStorageDBThread.h"
12 #include "DOMStorageCache.h"
13 #include "DOMStorageObserver.h"
14 #include "mozilla/Mutex.h"
19 class DOMLocalStorageManager
;
21 // Child side of the IPC protocol, exposes as DB interface but
22 // is responsible to send all requests to the parent process
23 // and expects asynchronous answers. Those are then transparently
24 // forwarded back to consumers on the child process.
25 class DOMStorageDBChild MOZ_FINAL
: public DOMStorageDBBridge
26 , public PStorageChild
28 virtual ~DOMStorageDBChild();
31 explicit DOMStorageDBChild(DOMLocalStorageManager
* aManager
);
33 NS_IMETHOD_(MozExternalRefCountType
) AddRef(void);
34 NS_IMETHOD_(MozExternalRefCountType
) Release(void);
36 void AddIPDLReference();
37 void ReleaseIPDLReference();
39 virtual nsresult
Init();
40 virtual nsresult
Shutdown();
42 virtual void AsyncPreload(DOMStorageCacheBridge
* aCache
, bool aPriority
= false);
43 virtual void AsyncGetUsage(DOMStorageUsageBridge
* aUsage
);
45 virtual void SyncPreload(DOMStorageCacheBridge
* aCache
, bool aForceSync
= false);
47 virtual nsresult
AsyncAddItem(DOMStorageCacheBridge
* aCache
, const nsAString
& aKey
, const nsAString
& aValue
);
48 virtual nsresult
AsyncUpdateItem(DOMStorageCacheBridge
* aCache
, const nsAString
& aKey
, const nsAString
& aValue
);
49 virtual nsresult
AsyncRemoveItem(DOMStorageCacheBridge
* aCache
, const nsAString
& aKey
);
50 virtual nsresult
AsyncClear(DOMStorageCacheBridge
* aCache
);
52 virtual void AsyncClearAll()
54 if (mScopesHavingData
) {
55 mScopesHavingData
->Clear(); /* NO-OP on the child process otherwise */
59 virtual void AsyncClearMatchingScope(const nsACString
& aScope
)
60 { /* NO-OP on the child process */ }
62 virtual void AsyncFlush()
65 virtual bool ShouldPreloadScope(const nsACString
& aScope
);
66 virtual void GetScopesHavingData(InfallibleTArray
<nsCString
>* aScopes
)
67 { NS_NOTREACHED("Not implemented for child process"); }
70 bool RecvObserve(const nsCString
& aTopic
,
71 const nsCString
& aScopePrefix
);
72 bool RecvLoadItem(const nsCString
& aScope
,
74 const nsString
& aValue
);
75 bool RecvLoadDone(const nsCString
& aScope
,
77 bool RecvScopesHavingData(const InfallibleTArray
<nsCString
>& aScopes
);
78 bool RecvLoadUsage(const nsCString
& aScope
,
79 const int64_t& aUsage
);
80 bool RecvError(const nsresult
& aRv
);
82 nsTHashtable
<nsCStringHashKey
>& ScopesHavingData();
84 ThreadSafeAutoRefCnt mRefCnt
;
87 // Held to get caches to forward answers to.
88 nsRefPtr
<DOMLocalStorageManager
> mManager
;
90 // Scopes having data hash, for optimization purposes only
91 nsAutoPtr
<nsTHashtable
<nsCStringHashKey
> > mScopesHavingData
;
93 // List of caches waiting for preload. This ensures the contract that
94 // AsyncPreload call references the cache for time of the preload.
95 nsTHashtable
<nsRefPtrHashKey
<DOMStorageCacheBridge
> > mLoadingCaches
;
97 // Status of the remote database
104 // Receives async requests from child processes and is responsible
105 // to send back responses from the DB thread. Exposes as a fake
106 // DOMStorageCache consumer.
107 // Also responsible for forwardning all chrome operation notifications
108 // such as cookie cleaning etc to the child process.
109 class DOMStorageDBParent MOZ_FINAL
: public PStorageParent
110 , public DOMStorageObserverSink
112 virtual ~DOMStorageDBParent();
115 DOMStorageDBParent();
117 virtual mozilla::ipc::IProtocol
*
118 CloneProtocol(Channel
* aChannel
,
119 mozilla::ipc::ProtocolCloneContext
* aCtx
) MOZ_OVERRIDE
;
121 NS_IMETHOD_(MozExternalRefCountType
) AddRef(void);
122 NS_IMETHOD_(MozExternalRefCountType
) Release(void);
124 void AddIPDLReference();
125 void ReleaseIPDLReference();
127 bool IPCOpen() { return mIPCOpen
; }
130 // Fake cache class receiving async callbacks from DB thread, sending
131 // them back to appropriate cache object on the child process.
132 class CacheParentBridge
: public DOMStorageCacheBridge
{
134 CacheParentBridge(DOMStorageDBParent
* aParentDB
, const nsACString
& aScope
)
135 : mParent(aParentDB
), mScope(aScope
), mLoaded(false), mLoadedCount(0) {}
136 virtual ~CacheParentBridge() {}
138 // DOMStorageCacheBridge
139 virtual const nsCString
& Scope() const
141 virtual bool Loaded()
143 virtual uint32_t LoadedCount()
144 { return mLoadedCount
; }
146 virtual bool LoadItem(const nsAString
& aKey
, const nsString
& aValue
);
147 virtual void LoadDone(nsresult aRv
);
148 virtual void LoadWait();
151 nsRefPtr
<DOMStorageDBParent
> mParent
;
154 uint32_t mLoadedCount
;
157 // Fake usage class receiving async callbacks from DB thread
158 class UsageParentBridge
: public DOMStorageUsageBridge
161 UsageParentBridge(DOMStorageDBParent
* aParentDB
, const nsACString
& aScope
)
162 : mParent(aParentDB
), mScope(aScope
) {}
163 virtual ~UsageParentBridge() {}
165 // DOMStorageUsageBridge
166 virtual const nsCString
& Scope() { return mScope
; }
167 virtual void LoadUsage(const int64_t usage
);
170 nsRefPtr
<DOMStorageDBParent
> mParent
;
176 virtual void ActorDestroy(ActorDestroyReason aWhy
) MOZ_OVERRIDE
;
177 bool RecvAsyncPreload(const nsCString
& aScope
, const bool& aPriority
) MOZ_OVERRIDE
;
178 bool RecvPreload(const nsCString
& aScope
, const uint32_t& aAlreadyLoadedCount
,
179 InfallibleTArray
<nsString
>* aKeys
, InfallibleTArray
<nsString
>* aValues
,
180 nsresult
* aRv
) MOZ_OVERRIDE
;
181 bool RecvAsyncGetUsage(const nsCString
& aScope
) MOZ_OVERRIDE
;
182 bool RecvAsyncAddItem(const nsCString
& aScope
, const nsString
& aKey
, const nsString
& aValue
) MOZ_OVERRIDE
;
183 bool RecvAsyncUpdateItem(const nsCString
& aScope
, const nsString
& aKey
, const nsString
& aValue
) MOZ_OVERRIDE
;
184 bool RecvAsyncRemoveItem(const nsCString
& aScope
, const nsString
& aKey
) MOZ_OVERRIDE
;
185 bool RecvAsyncClear(const nsCString
& aScope
) MOZ_OVERRIDE
;
186 bool RecvAsyncFlush() MOZ_OVERRIDE
;
188 // DOMStorageObserverSink
189 virtual nsresult
Observe(const char* aTopic
, const nsACString
& aScopePrefix
) MOZ_OVERRIDE
;
192 CacheParentBridge
* NewCache(const nsACString
& aScope
);
194 ThreadSafeAutoRefCnt mRefCnt
;
197 // True when IPC channel is open and Send*() methods are OK to use.