|
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/. */ |
|
5 |
|
6 #ifndef nsDOMStorageIPC_h___ |
|
7 #define nsDOMStorageIPC_h___ |
|
8 |
|
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" |
|
15 |
|
16 namespace mozilla { |
|
17 namespace dom { |
|
18 |
|
19 class DOMLocalStorageManager; |
|
20 |
|
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 |
|
27 { |
|
28 public: |
|
29 DOMStorageDBChild(DOMLocalStorageManager* aManager); |
|
30 virtual ~DOMStorageDBChild(); |
|
31 |
|
32 NS_IMETHOD_(MozExternalRefCountType) AddRef(void); |
|
33 NS_IMETHOD_(MozExternalRefCountType) Release(void); |
|
34 |
|
35 void AddIPDLReference(); |
|
36 void ReleaseIPDLReference(); |
|
37 |
|
38 virtual nsresult Init(); |
|
39 virtual nsresult Shutdown(); |
|
40 |
|
41 virtual void AsyncPreload(DOMStorageCacheBridge* aCache, bool aPriority = false); |
|
42 virtual void AsyncGetUsage(DOMStorageUsageBridge* aUsage); |
|
43 |
|
44 virtual void SyncPreload(DOMStorageCacheBridge* aCache, bool aForceSync = false); |
|
45 |
|
46 virtual nsresult AsyncAddItem(DOMStorageCacheBridge* aCache, const nsAString& aKey, const nsAString& aValue); |
|
47 virtual nsresult AsyncUpdateItem(DOMStorageCacheBridge* aCache, const nsAString& aKey, const nsAString& aValue); |
|
48 virtual nsresult AsyncRemoveItem(DOMStorageCacheBridge* aCache, const nsAString& aKey); |
|
49 virtual nsresult AsyncClear(DOMStorageCacheBridge* aCache); |
|
50 |
|
51 virtual void AsyncClearAll() |
|
52 { |
|
53 if (mScopesHavingData) { |
|
54 mScopesHavingData->Clear(); /* NO-OP on the child process otherwise */ |
|
55 } |
|
56 } |
|
57 |
|
58 virtual void AsyncClearMatchingScope(const nsACString& aScope) |
|
59 { /* NO-OP on the child process */ } |
|
60 |
|
61 virtual void AsyncFlush() |
|
62 { SendAsyncFlush(); } |
|
63 |
|
64 virtual bool ShouldPreloadScope(const nsACString& aScope); |
|
65 virtual void GetScopesHavingData(InfallibleTArray<nsCString>* aScopes) |
|
66 { NS_NOTREACHED("Not implemented for child process"); } |
|
67 |
|
68 private: |
|
69 bool RecvObserve(const nsCString& aTopic, |
|
70 const nsCString& aScopePrefix); |
|
71 bool RecvLoadItem(const nsCString& aScope, |
|
72 const nsString& aKey, |
|
73 const nsString& aValue); |
|
74 bool RecvLoadDone(const nsCString& aScope, |
|
75 const nsresult& aRv); |
|
76 bool RecvScopesHavingData(const InfallibleTArray<nsCString>& aScopes); |
|
77 bool RecvLoadUsage(const nsCString& aScope, |
|
78 const int64_t& aUsage); |
|
79 bool RecvError(const nsresult& aRv); |
|
80 |
|
81 nsTHashtable<nsCStringHashKey>& ScopesHavingData(); |
|
82 |
|
83 ThreadSafeAutoRefCnt mRefCnt; |
|
84 NS_DECL_OWNINGTHREAD |
|
85 |
|
86 // Held to get caches to forward answers to. |
|
87 nsRefPtr<DOMLocalStorageManager> mManager; |
|
88 |
|
89 // Scopes having data hash, for optimization purposes only |
|
90 nsAutoPtr<nsTHashtable<nsCStringHashKey> > mScopesHavingData; |
|
91 |
|
92 // List of caches waiting for preload. This ensures the contract that |
|
93 // AsyncPreload call references the cache for time of the preload. |
|
94 nsTHashtable<nsRefPtrHashKey<DOMStorageCacheBridge> > mLoadingCaches; |
|
95 |
|
96 // Status of the remote database |
|
97 nsresult mStatus; |
|
98 |
|
99 bool mIPCOpen; |
|
100 }; |
|
101 |
|
102 |
|
103 // Receives async requests from child processes and is responsible |
|
104 // to send back responses from the DB thread. Exposes as a fake |
|
105 // DOMStorageCache consumer. |
|
106 // Also responsible for forwardning all chrome operation notifications |
|
107 // such as cookie cleaning etc to the child process. |
|
108 class DOMStorageDBParent MOZ_FINAL : public PStorageParent |
|
109 , public DOMStorageObserverSink |
|
110 { |
|
111 public: |
|
112 DOMStorageDBParent(); |
|
113 virtual ~DOMStorageDBParent(); |
|
114 |
|
115 virtual mozilla::ipc::IProtocol* |
|
116 CloneProtocol(Channel* aChannel, |
|
117 mozilla::ipc::ProtocolCloneContext* aCtx) MOZ_OVERRIDE; |
|
118 |
|
119 NS_IMETHOD_(MozExternalRefCountType) AddRef(void); |
|
120 NS_IMETHOD_(MozExternalRefCountType) Release(void); |
|
121 |
|
122 void AddIPDLReference(); |
|
123 void ReleaseIPDLReference(); |
|
124 |
|
125 bool IPCOpen() { return mIPCOpen; } |
|
126 |
|
127 public: |
|
128 // Fake cache class receiving async callbacks from DB thread, sending |
|
129 // them back to appropriate cache object on the child process. |
|
130 class CacheParentBridge : public DOMStorageCacheBridge { |
|
131 public: |
|
132 CacheParentBridge(DOMStorageDBParent* aParentDB, const nsACString& aScope) |
|
133 : mParent(aParentDB), mScope(aScope), mLoaded(false), mLoadedCount(0) {} |
|
134 virtual ~CacheParentBridge() {} |
|
135 |
|
136 // DOMStorageCacheBridge |
|
137 virtual const nsCString& Scope() const |
|
138 { return mScope; } |
|
139 virtual bool Loaded() |
|
140 { return mLoaded; } |
|
141 virtual uint32_t LoadedCount() |
|
142 { return mLoadedCount; } |
|
143 |
|
144 virtual bool LoadItem(const nsAString& aKey, const nsString& aValue); |
|
145 virtual void LoadDone(nsresult aRv); |
|
146 virtual void LoadWait(); |
|
147 |
|
148 private: |
|
149 nsRefPtr<DOMStorageDBParent> mParent; |
|
150 nsCString mScope; |
|
151 bool mLoaded; |
|
152 uint32_t mLoadedCount; |
|
153 }; |
|
154 |
|
155 // Fake usage class receiving async callbacks from DB thread |
|
156 class UsageParentBridge : public DOMStorageUsageBridge |
|
157 { |
|
158 public: |
|
159 UsageParentBridge(DOMStorageDBParent* aParentDB, const nsACString& aScope) |
|
160 : mParent(aParentDB), mScope(aScope) {} |
|
161 virtual ~UsageParentBridge() {} |
|
162 |
|
163 // DOMStorageUsageBridge |
|
164 virtual const nsCString& Scope() { return mScope; } |
|
165 virtual void LoadUsage(const int64_t usage); |
|
166 |
|
167 private: |
|
168 nsRefPtr<DOMStorageDBParent> mParent; |
|
169 nsCString mScope; |
|
170 }; |
|
171 |
|
172 private: |
|
173 // IPC |
|
174 bool RecvAsyncPreload(const nsCString& aScope, const bool& aPriority); |
|
175 bool RecvPreload(const nsCString& aScope, const uint32_t& aAlreadyLoadedCount, |
|
176 InfallibleTArray<nsString>* aKeys, InfallibleTArray<nsString>* aValues, |
|
177 nsresult* aRv); |
|
178 bool RecvAsyncGetUsage(const nsCString& aScope); |
|
179 bool RecvAsyncAddItem(const nsCString& aScope, const nsString& aKey, const nsString& aValue); |
|
180 bool RecvAsyncUpdateItem(const nsCString& aScope, const nsString& aKey, const nsString& aValue); |
|
181 bool RecvAsyncRemoveItem(const nsCString& aScope, const nsString& aKey); |
|
182 bool RecvAsyncClear(const nsCString& aScope); |
|
183 bool RecvAsyncFlush(); |
|
184 |
|
185 // DOMStorageObserverSink |
|
186 virtual nsresult Observe(const char* aTopic, const nsACString& aScopePrefix); |
|
187 |
|
188 private: |
|
189 CacheParentBridge* NewCache(const nsACString& aScope); |
|
190 |
|
191 ThreadSafeAutoRefCnt mRefCnt; |
|
192 NS_DECL_OWNINGTHREAD |
|
193 |
|
194 // True when IPC channel is open and Send*() methods are OK to use. |
|
195 bool mIPCOpen; |
|
196 }; |
|
197 |
|
198 } // ::dom |
|
199 } // ::mozilla |
|
200 |
|
201 #endif |