Source code

Revision control

Other Tools

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/. */
6
7
#include "FetchEventOpProxyParent.h"
8
9
#include <utility>
10
11
#include "nsCOMPtr.h"
12
#include "nsIInputStream.h"
13
14
#include "mozilla/Assertions.h"
15
#include "mozilla/DebugOnly.h"
16
#include "mozilla/UniquePtr.h"
17
#include "mozilla/Unused.h"
18
#include "mozilla/dom/FetchEventOpParent.h"
19
#include "mozilla/ipc/BackgroundParent.h"
20
#include "mozilla/ipc/IPCStreamUtils.h"
21
22
namespace mozilla {
23
24
using namespace ipc;
25
26
namespace dom {
27
28
namespace {
29
30
void MaybeDeserializeAndReserialize(const Maybe<IPCStream>& aDeserialize,
31
Maybe<IPCStream>& aReserialize,
32
UniquePtr<AutoIPCStream>& aAutoStream,
33
PBackgroundParent* aManager) {
34
nsCOMPtr<nsIInputStream> maybeDeserialized =
35
DeserializeIPCStream(aDeserialize);
36
37
if (!maybeDeserialized) {
38
return;
39
}
40
41
aAutoStream.reset(new AutoIPCStream(aReserialize));
42
DebugOnly<bool> ok = aAutoStream->Serialize(maybeDeserialized, aManager);
43
MOZ_ASSERT(ok);
44
}
45
46
} // anonymous namespace
47
48
/* static */ void FetchEventOpProxyParent::Create(
49
PRemoteWorkerParent* aManager, const ServiceWorkerFetchEventOpArgs& aArgs,
50
RefPtr<FetchEventOpParent> aReal) {
51
AssertIsInMainProcess();
52
AssertIsOnBackgroundThread();
53
MOZ_ASSERT(aManager);
54
MOZ_ASSERT(aReal);
55
56
FetchEventOpProxyParent* actor =
57
new FetchEventOpProxyParent(std::move(aReal));
58
59
if (aArgs.internalRequest().body().isNothing()) {
60
Unused << aManager->SendPFetchEventOpProxyConstructor(actor, aArgs);
61
return;
62
}
63
64
ServiceWorkerFetchEventOpArgs copyArgs = aArgs;
65
IPCInternalRequest& copyRequest = copyArgs.internalRequest();
66
67
PBackgroundParent* bgParent = aManager->Manager();
68
MOZ_ASSERT(bgParent);
69
70
UniquePtr<AutoIPCStream> autoBodyStream = MakeUnique<AutoIPCStream>();
71
MaybeDeserializeAndReserialize(aArgs.internalRequest().body(),
72
copyRequest.body(), autoBodyStream, bgParent);
73
74
Unused << aManager->SendPFetchEventOpProxyConstructor(actor, copyArgs);
75
autoBodyStream->TakeOptionalValue();
76
}
77
78
FetchEventOpProxyParent::~FetchEventOpProxyParent() {
79
AssertIsOnBackgroundThread();
80
}
81
82
FetchEventOpProxyParent::FetchEventOpProxyParent(
83
RefPtr<FetchEventOpParent>&& aReal)
84
: mReal(std::move(aReal)) {}
85
86
mozilla::ipc::IPCResult FetchEventOpProxyParent::RecvAsyncLog(
87
const nsCString& aScriptSpec, const uint32_t& aLineNumber,
88
const uint32_t& aColumnNumber, const nsCString& aMessageName,
89
nsTArray<nsString>&& aParams) {
90
AssertIsOnBackgroundThread();
91
MOZ_ASSERT(mReal);
92
93
Unused << mReal->SendAsyncLog(aScriptSpec, aLineNumber, aColumnNumber,
94
aMessageName, aParams);
95
96
return IPC_OK();
97
}
98
99
mozilla::ipc::IPCResult FetchEventOpProxyParent::RecvRespondWith(
100
const IPCFetchEventRespondWithResult& aResult) {
101
AssertIsOnBackgroundThread();
102
MOZ_ASSERT(mReal);
103
104
// IPCSynthesizeResponseArgs possibly contains an IPCStream. If so,
105
// deserialize it and reserialize it before forwarding it to the main thread.
106
if (aResult.type() ==
107
IPCFetchEventRespondWithResult::TIPCSynthesizeResponseArgs) {
108
const IPCSynthesizeResponseArgs& originalArgs =
109
aResult.get_IPCSynthesizeResponseArgs();
110
const IPCInternalResponse& originalResponse =
111
originalArgs.internalResponse();
112
113
// Do nothing if neither the body nor the alt. body can be deserialized.
114
if (!originalResponse.body() && !originalResponse.alternativeBody()) {
115
Unused << mReal->SendRespondWith(aResult);
116
return IPC_OK();
117
}
118
119
IPCSynthesizeResponseArgs copyArgs = originalArgs;
120
IPCInternalResponse& copyResponse = copyArgs.internalResponse();
121
122
PRemoteWorkerControllerParent* manager = mReal->Manager();
123
MOZ_ASSERT(manager);
124
125
PBackgroundParent* bgParent = manager->Manager();
126
MOZ_ASSERT(bgParent);
127
128
UniquePtr<AutoIPCStream> autoBodyStream = MakeUnique<AutoIPCStream>();
129
UniquePtr<AutoIPCStream> autoAlternativeBodyStream =
130
MakeUnique<AutoIPCStream>();
131
132
MaybeDeserializeAndReserialize(originalResponse.body(), copyResponse.body(),
133
autoBodyStream, bgParent);
134
MaybeDeserializeAndReserialize(originalResponse.alternativeBody(),
135
copyResponse.alternativeBody(),
136
autoAlternativeBodyStream, bgParent);
137
138
Unused << mReal->SendRespondWith(copyArgs);
139
140
autoBodyStream->TakeOptionalValue();
141
autoAlternativeBodyStream->TakeOptionalValue();
142
} else {
143
Unused << mReal->SendRespondWith(aResult);
144
}
145
146
return IPC_OK();
147
}
148
149
mozilla::ipc::IPCResult FetchEventOpProxyParent::Recv__delete__(
150
const ServiceWorkerFetchEventOpResult& aResult) {
151
AssertIsOnBackgroundThread();
152
MOZ_ASSERT(mReal);
153
154
Unused << mReal->Send__delete__(mReal, aResult);
155
mReal = nullptr;
156
157
return IPC_OK();
158
}
159
160
void FetchEventOpProxyParent::ActorDestroy(ActorDestroyReason) {
161
AssertIsOnBackgroundThread();
162
}
163
164
} // namespace dom
165
} // namespace mozilla