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
|
/* -*- 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/. */
#include "StreamFilter.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "xpcpublic.h"
#include "mozilla/AbstractThread.h"
#include "mozilla/extensions/StreamFilterChild.h"
#include "mozilla/extensions/StreamFilterEvents.h"
#include "mozilla/extensions/StreamFilterParent.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/ipc/Endpoint.h"
#include "nsContentUtils.h"
#include "nsCycleCollectionParticipant.h"
#include "nsLiteralString.h"
#include "nsThreadUtils.h"
#include "nsTArray.h"
using namespace JS;
using namespace mozilla::dom;
namespace mozilla {
namespace extensions {
/*****************************************************************************
* Initialization
*****************************************************************************/
StreamFilter::StreamFilter(nsIGlobalObject* aParent, uint64_t aRequestId,
const nsAString& aAddonId)
: mParent(aParent), mChannelId(aRequestId), mAddonId(NS_Atomize(aAddonId)) {
MOZ_ASSERT(aParent);
Connect();
};
StreamFilter::~StreamFilter() { ForgetActor(); }
void StreamFilter::ForgetActor() {
if (mActor) {
mActor->Cleanup();
mActor->SetStreamFilter(nullptr);
}
}
/* static */
already_AddRefed<StreamFilter> StreamFilter::Create(GlobalObject& aGlobal,
uint64_t aRequestId,
const nsAString& aAddonId) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
MOZ_ASSERT(global);
RefPtr<StreamFilter> filter = new StreamFilter(global, aRequestId, aAddonId);
return filter.forget();
}
/*****************************************************************************
* Actor allocation
*****************************************************************************/
void StreamFilter::Connect() {
MOZ_ASSERT(!mActor);
mActor = new StreamFilterChild();
mActor->SetStreamFilter(this);
nsAutoString addonId;
mAddonId->ToString(addonId);
ContentChild* cc = ContentChild::GetSingleton();
RefPtr<StreamFilter> self(this);
if (cc) {
cc->SendInitStreamFilter(mChannelId, addonId)
->Then(
GetCurrentSerialEventTarget(), __func__,
[self](mozilla::ipc::Endpoint<PStreamFilterChild>&& aEndpoint) {
self->FinishConnect(std::move(aEndpoint));
},
[self](mozilla::ipc::ResponseRejectReason&& aReason) {
self->mActor->RecvInitialized(false);
});
} else {
StreamFilterParent::Create(nullptr, mChannelId, addonId)
->Then(
GetCurrentSerialEventTarget(), __func__,
[self](mozilla::ipc::Endpoint<PStreamFilterChild>&& aEndpoint) {
self->FinishConnect(std::move(aEndpoint));
},
[self](bool aDummy) { self->mActor->RecvInitialized(false); });
}
}
void StreamFilter::FinishConnect(
mozilla::ipc::Endpoint<PStreamFilterChild>&& aEndpoint) {
if (aEndpoint.IsValid()) {
MOZ_RELEASE_ASSERT(aEndpoint.Bind(mActor));
mActor->RecvInitialized(true);
} else {
mActor->RecvInitialized(false);
}
}
bool StreamFilter::CheckAlive() {
// Check whether the global that owns this StreamFitler is still scriptable
// and, if not, disconnect the actor so that it can be cleaned up.
JSObject* wrapper = GetWrapperPreserveColor();
if (!wrapper || !xpc::Scriptability::Get(wrapper).Allowed()) {
ForgetActor();
return false;
}
return true;
}
/*****************************************************************************
* Binding methods
*****************************************************************************/
template <typename T>
static inline bool ReadTypedArrayData(nsTArray<uint8_t>& aData, const T& aArray,
ErrorResult& aRv) {
aArray.ComputeState();
if (!aData.SetLength(aArray.Length(), fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return false;
}
memcpy(aData.Elements(), aArray.Data(), aArray.Length());
return true;
}
void StreamFilter::Write(const ArrayBufferOrUint8Array& aData,
ErrorResult& aRv) {
if (!mActor) {
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
return;
}
nsTArray<uint8_t> data;
bool ok;
if (aData.IsArrayBuffer()) {
ok = ReadTypedArrayData(data, aData.GetAsArrayBuffer(), aRv);
} else if (aData.IsUint8Array()) {
ok = ReadTypedArrayData(data, aData.GetAsUint8Array(), aRv);
} else {
MOZ_ASSERT_UNREACHABLE("Argument should be ArrayBuffer or Uint8Array");
return;
}
if (ok) {
mActor->Write(std::move(data), aRv);
}
}
StreamFilterStatus StreamFilter::Status() const {
if (!mActor) {
return StreamFilterStatus::Uninitialized;
}
return mActor->Status();
}
void StreamFilter::Suspend(ErrorResult& aRv) {
if (mActor) {
mActor->Suspend(aRv);
} else {
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
}
}
void StreamFilter::Resume(ErrorResult& aRv) {
if (mActor) {
mActor->Resume(aRv);
} else {
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
}
}
void StreamFilter::Disconnect(ErrorResult& aRv) {
if (mActor) {
mActor->Disconnect(aRv);
} else {
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
}
}
void StreamFilter::Close(ErrorResult& aRv) {
if (mActor) {
mActor->Close(aRv);
} else {
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
}
}
/*****************************************************************************
* Event emitters
*****************************************************************************/
void StreamFilter::FireEvent(const nsAString& aType) {
EventInit init;
init.mBubbles = false;
init.mCancelable = false;
RefPtr<Event> event = Event::Constructor(this, aType, init);
event->SetTrusted(true);
DispatchEvent(*event);
}
void StreamFilter::FireDataEvent(const nsTArray<uint8_t>& aData) {
AutoEntryScript aes(mParent, "StreamFilter data event");
JSContext* cx = aes.cx();
RootedDictionary<StreamFilterDataEventInit> init(cx);
init.mBubbles = false;
init.mCancelable = false;
auto buffer = ArrayBuffer::Create(cx, aData.Length(), aData.Elements());
if (!buffer) {
// TODO: There is no way to recover from this. This chunk of data is lost.
FireErrorEvent(u"Out of memory"_ns);
return;
}
init.mData.Init(buffer);
RefPtr<StreamFilterDataEvent> event =
StreamFilterDataEvent::Constructor(this, u"data"_ns, init);
event->SetTrusted(true);
DispatchEvent(*event);
}
void StreamFilter::FireErrorEvent(const nsAString& aError) {
MOZ_ASSERT(mError.IsEmpty());
mError = aError;
FireEvent(u"error"_ns);
}
/*****************************************************************************
* Glue
*****************************************************************************/
/* static */
bool StreamFilter::IsAllowedInContext(JSContext* aCx, JSObject* /* unused */) {
return nsContentUtils::CallerHasPermission(aCx,
nsGkAtoms::webRequestBlocking);
}
JSObject* StreamFilter::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return StreamFilter_Binding::Wrap(aCx, this, aGivenProto);
}
NS_IMPL_CYCLE_COLLECTION_CLASS(StreamFilter)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StreamFilter)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(StreamFilter,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(StreamFilter,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(StreamFilter,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_ADDREF_INHERITED(StreamFilter, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(StreamFilter, DOMEventTargetHelper)
} // namespace extensions
} // namespace mozilla
|