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"
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"
22 namespace mozilla::dom
{
24 /*******************************************************************************
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();
45 mDatabase
->ClearActor();
48 MOZ_ALWAYS_TRUE(PBackgroundLSDatabaseChild::SendDeleteMe());
52 void LSDatabaseChild::ActorDestroy(ActorDestroyReason aWhy
) {
53 AssertIsOnOwningThread();
56 mDatabase
->ClearActor();
63 mozilla::ipc::IPCResult
LSDatabaseChild::RecvRequestAllowToClose() {
64 AssertIsOnOwningThread();
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.
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
) {
93 /*******************************************************************************
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();
114 mObserver
->ClearActor();
117 MOZ_ALWAYS_TRUE(PBackgroundLSObserverChild::SendDeleteMe());
121 void LSObserverChild::ActorDestroy(ActorDestroyReason aWhy
) {
122 AssertIsOnOwningThread();
125 mObserver
->ClearActor();
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();
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);
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();
187 void LSRequestChild::ActorDestroy(ActorDestroyReason aWhy
) {
188 AssertIsOnOwningThread();
191 MOZ_ASSERT(aWhy
!= Deletion
);
193 mCallback
->OnResponse(NS_ERROR_FAILURE
);
199 mozilla::ipc::IPCResult
LSRequestChild::Recv__delete__(
200 const LSRequestResponse
& aResponse
) {
201 AssertIsOnOwningThread();
202 MOZ_ASSERT(mCallback
);
204 mCallback
->OnResponse(aResponse
);
211 mozilla::ipc::IPCResult
LSRequestChild::RecvReady() {
212 AssertIsOnOwningThread();
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());
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();
254 MOZ_ASSERT(aWhy
!= Deletion
);
256 mCallback
->OnResponse(NS_ERROR_FAILURE
);
262 mozilla::ipc::IPCResult
LSSimpleRequestChild::Recv__delete__(
263 const LSSimpleRequestResponse
& aResponse
) {
264 AssertIsOnOwningThread();
265 MOZ_ASSERT(mCallback
);
267 mCallback
->OnResponse(aResponse
);
274 /*******************************************************************************
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();
295 mSnapshot
->ClearActor();
298 MOZ_ALWAYS_TRUE(PBackgroundLSSnapshotChild::SendDeleteMe());
302 void LSSnapshotChild::ActorDestroy(ActorDestroyReason aWhy
) {
303 AssertIsOnOwningThread();
306 mSnapshot
->ClearActor();
313 mozilla::ipc::IPCResult
LSSnapshotChild::RecvMarkDirty() {
314 AssertIsOnOwningThread();
320 mSnapshot
->MarkDirty();
325 } // namespace mozilla::dom