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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "mozilla/dom/EventBinding.h"
#include "mozilla/dom/FlyWebFetchEventBinding.h"
#include "mozilla/dom/FlyWebPublishedServer.h"
#include "mozilla/dom/FlyWebServerEvents.h"
#include "mozilla/dom/FlyWebWebSocketEventBinding.h"
#include "mozilla/dom/Nullable.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/Response.h"
#include "js/GCAPI.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_CLASS(FlyWebFetchEvent)
NS_IMPL_ADDREF_INHERITED(FlyWebFetchEvent, Event)
NS_IMPL_RELEASE_INHERITED(FlyWebFetchEvent, Event)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FlyWebFetchEvent, Event)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRequest)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(FlyWebFetchEvent, Event)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FlyWebFetchEvent, Event)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRequest)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FlyWebFetchEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)
FlyWebFetchEvent::FlyWebFetchEvent(FlyWebPublishedServer* aServer,
class Request* aRequest,
InternalRequest* aInternalRequest)
: Event(aServer, nullptr, nullptr)
, mRequest(aRequest)
, mInternalRequest(aInternalRequest)
, mServer(aServer)
, mResponded(false)
{
MOZ_ASSERT(aServer);
MOZ_ASSERT(aRequest);
MOZ_ASSERT(aInternalRequest);
}
FlyWebFetchEvent::~FlyWebFetchEvent()
{
}
JSObject*
FlyWebFetchEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return FlyWebFetchEventBinding::Wrap(aCx, this, aGivenProto);
}
void
FlyWebFetchEvent::RespondWith(Promise& aArg, ErrorResult& aRv)
{
if (mResponded) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
mResponded = true;
aArg.AppendNativeHandler(this);
}
void
FlyWebFetchEvent::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
{
RefPtr<Response> response;
if (aValue.isObject()) {
UNWRAP_OBJECT(Response, &aValue.toObject(), response);
}
RefPtr<InternalResponse> intResponse;
if (response && response->Type() != ResponseType::Opaque) {
intResponse = response->GetInternalResponse();
}
if (!intResponse) {
intResponse = InternalResponse::NetworkError();
}
NotifyServer(intResponse);
}
void
FlyWebFetchEvent::NotifyServer(InternalResponse* aResponse)
{
mServer->OnFetchResponse(mInternalRequest, aResponse);
}
void
FlyWebFetchEvent::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
{
RefPtr<InternalResponse> err = InternalResponse::NetworkError();
NotifyServer(err);
}
JSObject*
FlyWebWebSocketEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return FlyWebWebSocketEventBinding::Wrap(aCx, this, aGivenProto);
}
already_AddRefed<WebSocket>
FlyWebWebSocketEvent::Accept(const Optional<nsAString>& aProtocol,
ErrorResult& aRv)
{
if (mResponded) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return nullptr;
}
mResponded = true;
return mServer->OnWebSocketAccept(mInternalRequest, aProtocol, aRv);
}
void
FlyWebWebSocketEvent::NotifyServer(InternalResponse* aResponse)
{
mServer->OnWebSocketResponse(mInternalRequest, aResponse);
}
} // namespace dom
} // namespace mozilla
|