1 | /* |
2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "FetchEvent.h" |
28 | |
29 | #include "JSDOMPromise.h" |
30 | #include "JSFetchResponse.h" |
31 | |
32 | #if ENABLE(SERVICE_WORKER) |
33 | |
34 | namespace WebCore { |
35 | |
36 | Ref<FetchEvent> FetchEvent::createForTesting(ScriptExecutionContext& context) |
37 | { |
38 | FetchEvent::Init init; |
39 | init.request = FetchRequest::create(context, { }, FetchHeaders::create(FetchHeaders::Guard::Immutable, { }), { }, { }, { }); |
40 | return FetchEvent::create("fetch" , WTFMove(init), Event::IsTrusted::Yes); |
41 | } |
42 | |
43 | FetchEvent::FetchEvent(const AtomicString& type, Init&& initializer, IsTrusted isTrusted) |
44 | : ExtendableEvent(type, initializer, isTrusted) |
45 | , m_request(initializer.request.releaseNonNull()) |
46 | , m_clientId(WTFMove(initializer.clientId)) |
47 | , m_reservedClientId(WTFMove(initializer.reservedClientId)) |
48 | , m_targetClientId(WTFMove(initializer.targetClientId)) |
49 | { |
50 | } |
51 | |
52 | FetchEvent::~FetchEvent() |
53 | { |
54 | if (auto callback = WTFMove(m_onResponse)) |
55 | callback(makeUnexpected(ResourceError { errorDomainWebKitServiceWorker, 0, m_request->url(), "Fetch event is destroyed."_s , ResourceError::Type::Cancellation })); |
56 | } |
57 | |
58 | ResourceError FetchEvent::createResponseError(const URL& url, const String& errorMessage) |
59 | { |
60 | return ResourceError { errorDomainWebKitServiceWorker, 0, url, makeString("FetchEvent.respondWith received an error: " , errorMessage), ResourceError::Type::General }; |
61 | |
62 | } |
63 | |
64 | ExceptionOr<void> FetchEvent::respondWith(Ref<DOMPromise>&& promise) |
65 | { |
66 | if (!isBeingDispatched()) |
67 | return Exception { InvalidStateError, "Event is not being dispatched"_s }; |
68 | |
69 | if (m_respondWithEntered) |
70 | return Exception { InvalidStateError, "Event respondWith flag is set"_s }; |
71 | |
72 | m_respondPromise = WTFMove(promise); |
73 | addExtendLifetimePromise(*m_respondPromise); |
74 | |
75 | m_respondPromise->whenSettled([this, weakThis = makeWeakPtr(*this)] () { |
76 | if (!weakThis) |
77 | return; |
78 | promiseIsSettled(); |
79 | }); |
80 | |
81 | stopPropagation(); |
82 | stopImmediatePropagation(); |
83 | |
84 | m_respondWithEntered = true; |
85 | m_waitToRespond = true; |
86 | |
87 | return { }; |
88 | } |
89 | |
90 | void FetchEvent::onResponse(ResponseCallback&& callback) |
91 | { |
92 | ASSERT(!m_onResponse); |
93 | m_onResponse = WTFMove(callback); |
94 | } |
95 | |
96 | void FetchEvent::respondWithError(ResourceError&& error) |
97 | { |
98 | m_respondWithError = true; |
99 | processResponse(makeUnexpected(WTFMove(error))); |
100 | } |
101 | |
102 | void FetchEvent::processResponse(Expected<Ref<FetchResponse>, ResourceError>&& result) |
103 | { |
104 | m_respondPromise = nullptr; |
105 | m_waitToRespond = false; |
106 | if (auto callback = WTFMove(m_onResponse)) |
107 | callback(WTFMove(result)); |
108 | } |
109 | |
110 | void FetchEvent::promiseIsSettled() |
111 | { |
112 | if (m_respondPromise->status() == DOMPromise::Status::Rejected) { |
113 | auto reason = m_respondPromise->result().toWTFString(m_respondPromise->globalObject()->globalExec()); |
114 | respondWithError(createResponseError(m_request->url(), reason)); |
115 | return; |
116 | } |
117 | |
118 | ASSERT(m_respondPromise->status() == DOMPromise::Status::Fulfilled); |
119 | auto response = JSFetchResponse::toWrapped(m_respondPromise->globalObject()->globalExec()->vm(), m_respondPromise->result()); |
120 | if (!response) { |
121 | respondWithError(createResponseError(m_request->url(), "Returned response is null."_s )); |
122 | return; |
123 | } |
124 | |
125 | if (response->isDisturbedOrLocked()) { |
126 | respondWithError(createResponseError(m_request->url(), "Response is disturbed or locked."_s )); |
127 | return; |
128 | } |
129 | |
130 | processResponse(makeRef(*response)); |
131 | } |
132 | |
133 | } // namespace WebCore |
134 | |
135 | #endif // ENABLE(SERVICE_WORKER) |
136 | |