Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / localstorage / ActorsChild.cpp
blobea7c589a663b1d8577887b3db33724996fb37bc0
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ActorsChild.h"
9 #include "ErrorList.h"
10 #include "LSDatabase.h"
11 #include "LSObserver.h"
12 #include "LSSnapshot.h"
13 #include "LocalStorageCommon.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Result.h"
16 #include "mozilla/dom/LSValue.h"
17 #include "mozilla/dom/Storage.h"
18 #include "mozilla/dom/quota/QuotaCommon.h"
19 #include "mozilla/ipc/BackgroundUtils.h"
20 #include "nsCOMPtr.h"
22 namespace mozilla::dom {
24 /*******************************************************************************
25 * LSDatabaseChild
26 ******************************************************************************/
28 LSDatabaseChild::LSDatabaseChild(LSDatabase* aDatabase) : mDatabase(aDatabase) {
29 AssertIsOnOwningThread();
30 MOZ_ASSERT(aDatabase);
32 MOZ_COUNT_CTOR(LSDatabaseChild);
35 LSDatabaseChild::~LSDatabaseChild() {
36 AssertIsOnOwningThread();
38 MOZ_COUNT_DTOR(LSDatabaseChild);
41 void LSDatabaseChild::SendDeleteMeInternal() {
42 AssertIsOnOwningThread();
44 if (mDatabase) {
45 mDatabase->ClearActor();
46 mDatabase = nullptr;
48 MOZ_ALWAYS_TRUE(PBackgroundLSDatabaseChild::SendDeleteMe());
52 void LSDatabaseChild::ActorDestroy(ActorDestroyReason aWhy) {
53 AssertIsOnOwningThread();
55 if (mDatabase) {
56 mDatabase->ClearActor();
57 #ifdef DEBUG
58 mDatabase = nullptr;
59 #endif
63 mozilla::ipc::IPCResult LSDatabaseChild::RecvRequestAllowToClose() {
64 AssertIsOnOwningThread();
66 if (mDatabase) {
67 mDatabase->RequestAllowToClose();
69 // TODO: A new datastore will be prepared at first LocalStorage API
70 // synchronous call. It would be better to start preparing a new
71 // datastore right here, but asynchronously.
72 // However, we probably shouldn't do that if we are shutting down.
75 return IPC_OK();
78 PBackgroundLSSnapshotChild* LSDatabaseChild::AllocPBackgroundLSSnapshotChild(
79 const nsAString& aDocumentURI, const nsAString& aKey,
80 const bool& aIncreasePeakUsage, const int64_t& aMinSize,
81 LSSnapshotInitInfo* aInitInfo) {
82 MOZ_CRASH("PBackgroundLSSnapshotChild actor should be manually constructed!");
85 bool LSDatabaseChild::DeallocPBackgroundLSSnapshotChild(
86 PBackgroundLSSnapshotChild* aActor) {
87 MOZ_ASSERT(aActor);
89 delete aActor;
90 return true;
93 /*******************************************************************************
94 * LSObserverChild
95 ******************************************************************************/
97 LSObserverChild::LSObserverChild(LSObserver* aObserver) : mObserver(aObserver) {
98 AssertIsOnOwningThread();
99 MOZ_ASSERT(aObserver);
101 MOZ_COUNT_CTOR(LSObserverChild);
104 LSObserverChild::~LSObserverChild() {
105 AssertIsOnOwningThread();
107 MOZ_COUNT_DTOR(LSObserverChild);
110 void LSObserverChild::SendDeleteMeInternal() {
111 AssertIsOnOwningThread();
113 if (mObserver) {
114 mObserver->ClearActor();
115 mObserver = nullptr;
117 MOZ_ALWAYS_TRUE(PBackgroundLSObserverChild::SendDeleteMe());
121 void LSObserverChild::ActorDestroy(ActorDestroyReason aWhy) {
122 AssertIsOnOwningThread();
124 if (mObserver) {
125 mObserver->ClearActor();
126 #ifdef DEBUG
127 mObserver = nullptr;
128 #endif
132 mozilla::ipc::IPCResult LSObserverChild::RecvObserve(
133 const PrincipalInfo& aPrincipalInfo, const uint32_t& aPrivateBrowsingId,
134 const nsAString& aDocumentURI, const nsAString& aKey,
135 const LSValue& aOldValue, const LSValue& aNewValue) {
136 AssertIsOnOwningThread();
138 if (!mObserver) {
139 return IPC_OK();
142 QM_TRY_INSPECT(const auto& principal,
143 PrincipalInfoToPrincipal(aPrincipalInfo),
144 IPC_FAIL(this, "PrincipalInfoToPrincipal failed!"));
146 Storage::NotifyChange(/* aStorage */ nullptr, principal, aKey,
147 aOldValue.AsString(), aNewValue.AsString(),
148 /* aStorageType */ kLocalStorageType, aDocumentURI,
149 /* aIsPrivate */ !!aPrivateBrowsingId,
150 /* aImmediateDispatch */ true);
152 return IPC_OK();
155 /*******************************************************************************
156 * LocalStorageRequestChild
157 ******************************************************************************/
159 LSRequestChild::LSRequestChild() : mFinishing(false) {
160 AssertIsOnOwningThread();
162 MOZ_COUNT_CTOR(LSRequestChild);
165 LSRequestChild::~LSRequestChild() {
166 AssertIsOnOwningThread();
167 MOZ_ASSERT(!mCallback);
169 MOZ_COUNT_DTOR(LSRequestChild);
172 void LSRequestChild::SetCallback(LSRequestChildCallback* aCallback) {
173 AssertIsOnOwningThread();
174 MOZ_ASSERT(aCallback);
175 MOZ_ASSERT(!mCallback);
176 MOZ_ASSERT(Manager());
178 mCallback = aCallback;
181 bool LSRequestChild::Finishing() const {
182 AssertIsOnOwningThread();
184 return mFinishing;
187 void LSRequestChild::ActorDestroy(ActorDestroyReason aWhy) {
188 AssertIsOnOwningThread();
190 if (mCallback) {
191 MOZ_ASSERT(aWhy != Deletion);
193 mCallback->OnResponse(NS_ERROR_FAILURE);
195 mCallback = nullptr;
199 mozilla::ipc::IPCResult LSRequestChild::Recv__delete__(
200 const LSRequestResponse& aResponse) {
201 AssertIsOnOwningThread();
202 MOZ_ASSERT(mCallback);
204 mCallback->OnResponse(aResponse);
206 mCallback = nullptr;
208 return IPC_OK();
211 mozilla::ipc::IPCResult LSRequestChild::RecvReady() {
212 AssertIsOnOwningThread();
214 mFinishing = true;
216 // We only expect this to return false if the channel has been closed, but
217 // PBackground's channel never gets shutdown.
218 MOZ_ALWAYS_TRUE(SendFinish());
220 return IPC_OK();
223 /*******************************************************************************
224 * LSSimpleRequestChild
225 ******************************************************************************/
227 LSSimpleRequestChild::LSSimpleRequestChild() {
228 AssertIsOnOwningThread();
230 MOZ_COUNT_CTOR(LSSimpleRequestChild);
233 LSSimpleRequestChild::~LSSimpleRequestChild() {
234 AssertIsOnOwningThread();
235 MOZ_ASSERT(!mCallback);
237 MOZ_COUNT_DTOR(LSSimpleRequestChild);
240 void LSSimpleRequestChild::SetCallback(
241 LSSimpleRequestChildCallback* aCallback) {
242 AssertIsOnOwningThread();
243 MOZ_ASSERT(aCallback);
244 MOZ_ASSERT(!mCallback);
245 MOZ_ASSERT(Manager());
247 mCallback = aCallback;
250 void LSSimpleRequestChild::ActorDestroy(ActorDestroyReason aWhy) {
251 AssertIsOnOwningThread();
253 if (mCallback) {
254 MOZ_ASSERT(aWhy != Deletion);
256 mCallback->OnResponse(NS_ERROR_FAILURE);
258 mCallback = nullptr;
262 mozilla::ipc::IPCResult LSSimpleRequestChild::Recv__delete__(
263 const LSSimpleRequestResponse& aResponse) {
264 AssertIsOnOwningThread();
265 MOZ_ASSERT(mCallback);
267 mCallback->OnResponse(aResponse);
269 mCallback = nullptr;
271 return IPC_OK();
274 /*******************************************************************************
275 * LSSnapshotChild
276 ******************************************************************************/
278 LSSnapshotChild::LSSnapshotChild(LSSnapshot* aSnapshot) : mSnapshot(aSnapshot) {
279 AssertIsOnOwningThread();
280 MOZ_ASSERT(aSnapshot);
282 MOZ_COUNT_CTOR(LSSnapshotChild);
285 LSSnapshotChild::~LSSnapshotChild() {
286 AssertIsOnOwningThread();
288 MOZ_COUNT_DTOR(LSSnapshotChild);
291 void LSSnapshotChild::SendDeleteMeInternal() {
292 AssertIsOnOwningThread();
294 if (mSnapshot) {
295 mSnapshot->ClearActor();
296 mSnapshot = nullptr;
298 MOZ_ALWAYS_TRUE(PBackgroundLSSnapshotChild::SendDeleteMe());
302 void LSSnapshotChild::ActorDestroy(ActorDestroyReason aWhy) {
303 AssertIsOnOwningThread();
305 if (mSnapshot) {
306 mSnapshot->ClearActor();
307 #ifdef DEBUG
308 mSnapshot = nullptr;
309 #endif
313 mozilla::ipc::IPCResult LSSnapshotChild::RecvMarkDirty() {
314 AssertIsOnOwningThread();
316 if (!mSnapshot) {
317 return IPC_OK();
320 mSnapshot->MarkDirty();
322 return IPC_OK();
325 } // namespace mozilla::dom