Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 "mozilla/dom/EventBinding.h"
8 : #include "mozilla/dom/FlyWebFetchEventBinding.h"
9 : #include "mozilla/dom/FlyWebPublishedServer.h"
10 : #include "mozilla/dom/FlyWebServerEvents.h"
11 : #include "mozilla/dom/FlyWebWebSocketEventBinding.h"
12 : #include "mozilla/dom/Nullable.h"
13 : #include "mozilla/dom/Promise.h"
14 : #include "mozilla/dom/Response.h"
15 :
16 : #include "js/GCAPI.h"
17 :
18 : namespace mozilla {
19 : namespace dom {
20 :
21 :
22 : NS_IMPL_CYCLE_COLLECTION_CLASS(FlyWebFetchEvent)
23 :
24 0 : NS_IMPL_ADDREF_INHERITED(FlyWebFetchEvent, Event)
25 0 : NS_IMPL_RELEASE_INHERITED(FlyWebFetchEvent, Event)
26 :
27 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FlyWebFetchEvent, Event)
28 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRequest)
29 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
30 :
31 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(FlyWebFetchEvent, Event)
32 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_END
33 :
34 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FlyWebFetchEvent, Event)
35 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK(mRequest)
36 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
37 :
38 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FlyWebFetchEvent)
39 0 : NS_INTERFACE_MAP_END_INHERITING(Event)
40 :
41 0 : FlyWebFetchEvent::FlyWebFetchEvent(FlyWebPublishedServer* aServer,
42 : class Request* aRequest,
43 0 : InternalRequest* aInternalRequest)
44 : : Event(aServer, nullptr, nullptr)
45 : , mRequest(aRequest)
46 : , mInternalRequest(aInternalRequest)
47 : , mServer(aServer)
48 0 : , mResponded(false)
49 : {
50 0 : MOZ_ASSERT(aServer);
51 0 : MOZ_ASSERT(aRequest);
52 0 : MOZ_ASSERT(aInternalRequest);
53 0 : }
54 :
55 0 : FlyWebFetchEvent::~FlyWebFetchEvent()
56 : {
57 0 : }
58 :
59 : JSObject*
60 0 : FlyWebFetchEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
61 : {
62 0 : return FlyWebFetchEventBinding::Wrap(aCx, this, aGivenProto);
63 : }
64 :
65 : void
66 0 : FlyWebFetchEvent::RespondWith(Promise& aArg, ErrorResult& aRv)
67 : {
68 0 : if (mResponded) {
69 0 : aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
70 0 : return;
71 : }
72 :
73 0 : mResponded = true;
74 :
75 0 : aArg.AppendNativeHandler(this);
76 : }
77 :
78 : void
79 0 : FlyWebFetchEvent::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
80 : {
81 0 : RefPtr<Response> response;
82 0 : if (aValue.isObject()) {
83 0 : UNWRAP_OBJECT(Response, &aValue.toObject(), response);
84 : }
85 :
86 0 : RefPtr<InternalResponse> intResponse;
87 0 : if (response && response->Type() != ResponseType::Opaque) {
88 0 : intResponse = response->GetInternalResponse();
89 : }
90 :
91 0 : if (!intResponse) {
92 0 : intResponse = InternalResponse::NetworkError();
93 : }
94 :
95 0 : NotifyServer(intResponse);
96 0 : }
97 :
98 : void
99 0 : FlyWebFetchEvent::NotifyServer(InternalResponse* aResponse)
100 : {
101 0 : mServer->OnFetchResponse(mInternalRequest, aResponse);
102 0 : }
103 :
104 : void
105 0 : FlyWebFetchEvent::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
106 : {
107 0 : RefPtr<InternalResponse> err = InternalResponse::NetworkError();
108 :
109 0 : NotifyServer(err);
110 0 : }
111 :
112 : JSObject*
113 0 : FlyWebWebSocketEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
114 : {
115 0 : return FlyWebWebSocketEventBinding::Wrap(aCx, this, aGivenProto);
116 : }
117 :
118 : already_AddRefed<WebSocket>
119 0 : FlyWebWebSocketEvent::Accept(const Optional<nsAString>& aProtocol,
120 : ErrorResult& aRv)
121 : {
122 0 : if (mResponded) {
123 0 : aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
124 0 : return nullptr;
125 : }
126 :
127 0 : mResponded = true;
128 :
129 0 : return mServer->OnWebSocketAccept(mInternalRequest, aProtocol, aRv);
130 : }
131 :
132 :
133 : void
134 0 : FlyWebWebSocketEvent::NotifyServer(InternalResponse* aResponse)
135 : {
136 0 : mServer->OnWebSocketResponse(mInternalRequest, aResponse);
137 0 : }
138 :
139 :
140 : } // namespace dom
141 : } // namespace mozilla
|