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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ServiceWorkerCloneData.h"
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/dom/DOMTypes.h"
12 #include "mozilla/dom/StructuredCloneHolder.h"
13 #include "nsISerialEventTarget.h"
14 #include "nsProxyRelease.h"
15 #include "nsThreadUtils.h"
17 namespace mozilla::dom
{
19 ServiceWorkerCloneData::~ServiceWorkerCloneData() {
20 RefPtr
<ipc::SharedJSAllocatedData
> sharedData
= TakeSharedData();
22 NS_ProxyRelease(__func__
, mEventTarget
, sharedData
.forget());
26 ServiceWorkerCloneData::ServiceWorkerCloneData()
27 : ipc::StructuredCloneData(
28 StructuredCloneHolder::StructuredCloneScope::UnknownDestination
,
29 StructuredCloneHolder::TransferringSupported
),
30 mEventTarget(GetCurrentSerialEventTarget()),
31 mIsErrorMessageData(false) {
32 MOZ_DIAGNOSTIC_ASSERT(mEventTarget
);
35 bool ServiceWorkerCloneData::BuildClonedMessageData(
36 ClonedOrErrorMessageData
& aClonedData
) {
37 if (IsErrorMessageData()) {
38 aClonedData
= ErrorMessageData();
42 MOZ_DIAGNOSTIC_ASSERT(
44 StructuredCloneHolder::StructuredCloneScope::DifferentProcess
);
46 ClonedMessageData messageData
;
47 if (!StructuredCloneData::BuildClonedMessageData(messageData
)) {
51 aClonedData
= std::move(messageData
);
56 void ServiceWorkerCloneData::CopyFromClonedMessageData(
57 const ClonedOrErrorMessageData
& aClonedData
) {
58 if (aClonedData
.type() == ClonedOrErrorMessageData::TErrorMessageData
) {
59 mIsErrorMessageData
= true;
63 MOZ_DIAGNOSTIC_ASSERT(aClonedData
.type() ==
64 ClonedOrErrorMessageData::TClonedMessageData
);
66 StructuredCloneData::CopyFromClonedMessageData(aClonedData
);
69 void ServiceWorkerCloneData::SetAsErrorMessageData() {
70 MOZ_ASSERT(CloneScope() ==
71 StructuredCloneHolder::StructuredCloneScope::SameProcess
);
73 mIsErrorMessageData
= true;
76 bool ServiceWorkerCloneData::IsErrorMessageData() const {
77 return mIsErrorMessageData
;
80 } // namespace mozilla::dom