-
Notifications
You must be signed in to change notification settings - Fork 54
/
StorageIPC.h
379 lines (296 loc) · 13.9 KB
/
StorageIPC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* -*- 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_StorageIPC_h
#define mozilla_dom_StorageIPC_h
#include "mozilla/dom/PBackgroundLocalStorageCacheChild.h"
#include "mozilla/dom/PBackgroundLocalStorageCacheParent.h"
#include "mozilla/dom/PBackgroundStorageChild.h"
#include "mozilla/dom/PBackgroundStorageParent.h"
#include "StorageDBThread.h"
#include "LocalStorageCache.h"
#include "StorageObserver.h"
#include "mozilla/Mutex.h"
#include "nsAutoPtr.h"
namespace mozilla {
class OriginAttributesPattern;
namespace ipc {
class BackgroundChildImpl;
class PrincipalInfo;
} // namespace ipc
namespace dom {
class LocalStorageManager;
class PBackgroundStorageParent;
class LocalStorageCacheChild final : public PBackgroundLocalStorageCacheChild {
friend class mozilla::ipc::BackgroundChildImpl;
friend class LocalStorageCache;
friend class LocalStorageManager;
// LocalStorageCache effectively owns this instance, although IPC handles its
// allocation/deallocation. When the LocalStorageCache destructor runs, it
// will invoke SendDeleteMeInternal() which will trigger both instances to
// drop their mutual references and cause IPC to destroy the actor after the
// DeleteMe round-trip.
LocalStorageCache* MOZ_NON_OWNING_REF mCache;
NS_DECL_OWNINGTHREAD
public:
void AssertIsOnOwningThread() const {
NS_ASSERT_OWNINGTHREAD(LocalStorageCacheChild);
}
private:
// Only created by LocalStorageManager.
explicit LocalStorageCacheChild(LocalStorageCache* aCache);
// Only destroyed by mozilla::ipc::BackgroundChildImpl.
~LocalStorageCacheChild();
// Only called by LocalStorageCache.
void SendDeleteMeInternal();
// IPDL methods are only called by IPDL.
void ActorDestroy(ActorDestroyReason aWhy) override;
mozilla::ipc::IPCResult RecvObserve(const PrincipalInfo& aPrincipalInfo,
const uint32_t& aPrivateBrowsingId,
const nsString& aDocumentURI,
const nsString& aKey,
const nsString& aOldValue,
const nsString& aNewValue) override;
};
// 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.
class StorageDBChild final : public PBackgroundStorageChild {
class ShutdownObserver;
virtual ~StorageDBChild();
public:
explicit StorageDBChild(LocalStorageManager* aManager);
static StorageDBChild* Get();
static StorageDBChild* GetOrCreate();
NS_INLINE_DECL_REFCOUNTING(StorageDBChild);
void AddIPDLReference();
void ReleaseIPDLReference();
virtual nsresult Init();
virtual nsresult Shutdown();
virtual void AsyncPreload(LocalStorageCacheBridge* aCache,
bool aPriority = false);
virtual void AsyncGetUsage(StorageUsageBridge* aUsage);
virtual void SyncPreload(LocalStorageCacheBridge* aCache,
bool aForceSync = false);
virtual nsresult AsyncAddItem(LocalStorageCacheBridge* aCache,
const nsAString& aKey, const nsAString& aValue);
virtual nsresult AsyncUpdateItem(LocalStorageCacheBridge* aCache,
const nsAString& aKey,
const nsAString& aValue);
virtual nsresult AsyncRemoveItem(LocalStorageCacheBridge* aCache,
const nsAString& aKey);
virtual nsresult AsyncClear(LocalStorageCacheBridge* aCache);
virtual void AsyncClearAll() {
if (mOriginsHavingData) {
mOriginsHavingData->Clear(); /* NO-OP on the child process otherwise */
}
}
virtual void AsyncClearMatchingOrigin(const nsACString& aOriginNoSuffix) {
MOZ_CRASH("Shouldn't be called!");
}
virtual void AsyncClearMatchingOriginAttributes(
const OriginAttributesPattern& aPattern) {
MOZ_CRASH("Shouldn't be called!");
}
virtual void AsyncFlush() { MOZ_CRASH("Shouldn't be called!"); }
virtual bool ShouldPreloadOrigin(const nsACString& aOriginNoSuffix);
private:
mozilla::ipc::IPCResult RecvObserve(const nsCString& aTopic,
const nsString& aOriginAttributesPattern,
const nsCString& aOriginScope) override;
mozilla::ipc::IPCResult RecvLoadItem(const nsCString& aOriginSuffix,
const nsCString& aOriginNoSuffix,
const nsString& aKey,
const nsString& aValue) override;
mozilla::ipc::IPCResult RecvLoadDone(const nsCString& aOriginSuffix,
const nsCString& aOriginNoSuffix,
const nsresult& aRv) override;
mozilla::ipc::IPCResult RecvOriginsHavingData(
nsTArray<nsCString>&& aOrigins) override;
mozilla::ipc::IPCResult RecvLoadUsage(const nsCString& aOriginNoSuffix,
const int64_t& aUsage) override;
mozilla::ipc::IPCResult RecvError(const nsresult& aRv) override;
nsTHashtable<nsCStringHashKey>& OriginsHavingData();
// Held to get caches to forward answers to.
RefPtr<LocalStorageManager> mManager;
// Origins having data hash, for optimization purposes only
nsAutoPtr<nsTHashtable<nsCStringHashKey>> mOriginsHavingData;
// List of caches waiting for preload. This ensures the contract that
// AsyncPreload call references the cache for time of the preload.
nsTHashtable<nsRefPtrHashKey<LocalStorageCacheBridge>> mLoadingCaches;
// Status of the remote database
nsresult mStatus;
bool mIPCOpen;
};
class LocalStorageCacheParent final
: public PBackgroundLocalStorageCacheParent {
const PrincipalInfo mPrincipalInfo;
const nsCString mOriginKey;
uint32_t mPrivateBrowsingId;
bool mActorDestroyed;
public:
// Created in AllocPBackgroundLocalStorageCacheParent.
LocalStorageCacheParent(const PrincipalInfo& aPrincipalInfo,
const nsACString& aOriginKey,
uint32_t aPrivateBrowsingId);
NS_INLINE_DECL_REFCOUNTING(mozilla::dom::LocalStorageCacheParent)
private:
// Reference counted.
~LocalStorageCacheParent();
// IPDL methods are only called by IPDL.
void ActorDestroy(ActorDestroyReason aWhy) override;
mozilla::ipc::IPCResult RecvDeleteMe() override;
mozilla::ipc::IPCResult RecvNotify(const nsString& aDocumentURI,
const nsString& aKey,
const nsString& aOldValue,
const nsString& aNewValue) override;
};
// Receives async requests from child processes and is responsible
// to send back responses from the DB thread. Exposes as a fake
// LocalStorageCache consumer.
// Also responsible for forwardning all chrome operation notifications
// such as cookie cleaning etc to the child process.
class StorageDBParent final : public PBackgroundStorageParent {
class ObserverSink;
virtual ~StorageDBParent();
public:
explicit StorageDBParent(const nsString& aProfilePath);
void Init();
NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
NS_IMETHOD_(MozExternalRefCountType) Release(void);
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.
class CacheParentBridge : public LocalStorageCacheBridge {
public:
CacheParentBridge(StorageDBParent* aParentDB,
const nsACString& aOriginSuffix,
const nsACString& aOriginNoSuffix)
: mOwningEventTarget(GetCurrentThreadSerialEventTarget()),
mParent(aParentDB),
mOriginSuffix(aOriginSuffix),
mOriginNoSuffix(aOriginNoSuffix),
mLoaded(false),
mLoadedCount(0) {}
virtual ~CacheParentBridge() {}
// LocalStorageCacheBridge
virtual const nsCString Origin() const override;
virtual const nsCString& OriginNoSuffix() const override {
return mOriginNoSuffix;
}
virtual const nsCString& OriginSuffix() const override {
return mOriginSuffix;
}
virtual bool Loaded() override { return mLoaded; }
virtual uint32_t LoadedCount() override { return mLoadedCount; }
virtual bool LoadItem(const nsAString& aKey,
const nsString& aValue) override;
virtual void LoadDone(nsresult aRv) override;
virtual void LoadWait() override;
NS_IMETHOD_(void)
Release(void) override;
private:
void Destroy();
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
RefPtr<StorageDBParent> mParent;
nsCString mOriginSuffix, mOriginNoSuffix;
bool mLoaded;
uint32_t mLoadedCount;
};
// Fake usage class receiving async callbacks from DB thread
class UsageParentBridge : public StorageUsageBridge {
public:
UsageParentBridge(StorageDBParent* aParentDB,
const nsACString& aOriginScope)
: mOwningEventTarget(GetCurrentThreadSerialEventTarget()),
mParent(aParentDB),
mOriginScope(aOriginScope) {}
virtual ~UsageParentBridge() {}
// StorageUsageBridge
virtual const nsCString& OriginScope() override { return mOriginScope; }
virtual void LoadUsage(const int64_t usage) override;
NS_IMETHOD_(MozExternalRefCountType)
Release(void) override;
private:
void Destroy();
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
RefPtr<StorageDBParent> mParent;
nsCString mOriginScope;
};
private:
// IPC
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
mozilla::ipc::IPCResult RecvDeleteMe() override;
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,
nsresult* aRv) override;
mozilla::ipc::IPCResult RecvAsyncGetUsage(
const nsCString& aOriginNoSuffix) override;
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;
mozilla::ipc::IPCResult RecvAsyncFlush() override;
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);
private:
CacheParentBridge* NewCache(const nsACString& aOriginSuffix,
const nsACString& aOriginNoSuffix);
RefPtr<ObserverSink> mObserverSink;
// 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;
ThreadSafeAutoRefCnt mRefCnt;
NS_DECL_OWNINGTHREAD
// True when IPC channel is open and Send*() methods are OK to use.
bool mIPCOpen;
};
PBackgroundLocalStorageCacheParent* AllocPBackgroundLocalStorageCacheParent(
const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
const nsCString& aOriginKey, const uint32_t& aPrivateBrowsingId);
mozilla::ipc::IPCResult RecvPBackgroundLocalStorageCacheConstructor(
mozilla::ipc::PBackgroundParent* aBackgroundActor,
PBackgroundLocalStorageCacheParent* aActor,
const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
const nsCString& aOriginKey, const uint32_t& aPrivateBrowsingId);
bool DeallocPBackgroundLocalStorageCacheParent(
PBackgroundLocalStorageCacheParent* aActor);
PBackgroundStorageParent* AllocPBackgroundStorageParent(
const nsString& aProfilePath);
mozilla::ipc::IPCResult RecvPBackgroundStorageConstructor(
PBackgroundStorageParent* aActor, const nsString& aProfilePath);
bool DeallocPBackgroundStorageParent(PBackgroundStorageParent* aActor);
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_StorageIPC_h