Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Unified Diff: third_party/WebKit/Source/modules/serviceworkers/FetchRespondWithObserver.cpp

Issue 2715663002: ServiceWorker: Factor out FetchEvent related logics from RespondWithObserver. (Closed)
Patch Set: ServiceWorker: Factor out FetchEvent related logics from RespondWithObserver. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/serviceworkers/FetchRespondWithObserver.cpp
diff --git a/third_party/WebKit/Source/modules/serviceworkers/RespondWithObserver.cpp b/third_party/WebKit/Source/modules/serviceworkers/FetchRespondWithObserver.cpp
similarity index 67%
copy from third_party/WebKit/Source/modules/serviceworkers/RespondWithObserver.cpp
copy to third_party/WebKit/Source/modules/serviceworkers/FetchRespondWithObserver.cpp
index abde40fe3f13b834c847d000bce79648f3322295..39cd2ddd8f53c45c38f02064ef3f5b8ccbd0ac3c 100644
--- a/third_party/WebKit/Source/modules/serviceworkers/RespondWithObserver.cpp
+++ b/third_party/WebKit/Source/modules/serviceworkers/FetchRespondWithObserver.cpp
@@ -2,25 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "modules/serviceworkers/RespondWithObserver.h"
+#include "modules/serviceworkers/FetchRespondWithObserver.h"
-#include "bindings/core/v8/ScriptFunction.h"
-#include "bindings/core/v8/ScriptPromise.h"
+#include <v8.h>
#include "bindings/core/v8/ScriptValue.h"
#include "bindings/core/v8/V8Binding.h"
#include "bindings/modules/v8/V8Response.h"
-#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "core/inspector/ConsoleMessage.h"
+#include "core/inspector/ConsoleTypes.h"
#include "core/streams/Stream.h"
#include "modules/fetch/BodyStreamBuffer.h"
#include "modules/fetch/BytesConsumer.h"
#include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
-#include "platform/RuntimeEnabledFeatures.h"
+#include "modules/serviceworkers/WaitUntilObserver.h"
#include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h"
-#include "v8/include/v8.h"
-#include "wtf/Assertions.h"
-#include "wtf/RefPtr.h"
namespace blink {
namespace {
@@ -55,7 +51,7 @@ const String getMessageForResponseError(WebServiceWorkerResponseError error,
"is not no-cors";
break;
case WebServiceWorkerResponseErrorResponseTypeNotBasicOrDefault:
- ASSERT_NOT_REACHED();
+ NOTREACHED();
break;
case WebServiceWorkerResponseErrorBodyUsed:
errorMessage = errorMessage +
@@ -145,55 +141,7 @@ class NoopLoaderClient final
} // namespace
-class RespondWithObserver::ThenFunction final : public ScriptFunction {
- public:
- enum ResolveType {
- Fulfilled,
- Rejected,
- };
-
- static v8::Local<v8::Function> createFunction(ScriptState* scriptState,
- RespondWithObserver* observer,
- ResolveType type) {
- ThenFunction* self = new ThenFunction(scriptState, observer, type);
- return self->bindToV8Function();
- }
-
- DEFINE_INLINE_VIRTUAL_TRACE() {
- visitor->trace(m_observer);
- ScriptFunction::trace(visitor);
- }
-
- private:
- ThenFunction(ScriptState* scriptState,
- RespondWithObserver* observer,
- ResolveType type)
- : ScriptFunction(scriptState),
- m_observer(observer),
- m_resolveType(type) {}
-
- ScriptValue call(ScriptValue value) override {
- ASSERT(m_observer);
- ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
- if (m_resolveType == Rejected) {
- m_observer->responseWasRejected(
- WebServiceWorkerResponseErrorPromiseRejected);
- value =
- ScriptPromise::reject(value.getScriptState(), value).getScriptValue();
- } else {
- m_observer->responseWasFulfilled(value);
- }
- m_observer = nullptr;
- return value;
- }
-
- Member<RespondWithObserver> m_observer;
- ResolveType m_resolveType;
-};
-
-RespondWithObserver::~RespondWithObserver() {}
-
-RespondWithObserver* RespondWithObserver::create(
+FetchRespondWithObserver* FetchRespondWithObserver::create(
ExecutionContext* context,
int fetchEventID,
const KURL& requestURL,
@@ -202,59 +150,14 @@ RespondWithObserver* RespondWithObserver::create(
WebURLRequest::FrameType frameType,
WebURLRequest::RequestContext requestContext,
WaitUntilObserver* observer) {
- return new RespondWithObserver(context, fetchEventID, requestURL, requestMode,
- redirectMode, frameType, requestContext,
- observer);
+ return new FetchRespondWithObserver(context, fetchEventID, requestURL,
+ requestMode, redirectMode, frameType,
+ requestContext, observer);
}
-void RespondWithObserver::contextDestroyed(ExecutionContext*) {
- if (m_observer) {
- DCHECK_EQ(Pending, m_state);
- m_observer.clear();
- }
- m_state = Done;
-}
-
-void RespondWithObserver::willDispatchEvent() {
- m_eventDispatchTime = WTF::currentTime();
-}
-
-void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult) {
- ASSERT(getExecutionContext());
- if (m_state != Initial)
- return;
-
- if (dispatchResult != DispatchEventResult::NotCanceled) {
- m_observer->incrementPendingActivity();
- responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented);
- return;
- }
-
- ServiceWorkerGlobalScopeClient::from(getExecutionContext())
- ->respondToFetchEvent(m_fetchEventID, m_eventDispatchTime);
- m_state = Done;
- m_observer.clear();
-}
-
-void RespondWithObserver::respondWith(ScriptState* scriptState,
- ScriptPromise scriptPromise,
- ExceptionState& exceptionState) {
- if (m_state != Initial) {
- exceptionState.throwDOMException(
- InvalidStateError, "The fetch event has already been responded to.");
- return;
- }
-
- m_state = Pending;
- m_observer->incrementPendingActivity();
- scriptPromise.then(
- ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled),
- ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected));
-}
-
-void RespondWithObserver::responseWasRejected(
+void FetchRespondWithObserver::onResponseRejected(
WebServiceWorkerResponseError error) {
- ASSERT(getExecutionContext());
+ DCHECK(getExecutionContext());
getExecutionContext()->addConsoleMessage(
ConsoleMessage::create(JSMessageSource, WarningMessageLevel,
getMessageForResponseError(error, m_requestURL)));
@@ -264,17 +167,14 @@ void RespondWithObserver::responseWasRejected(
WebServiceWorkerResponse webResponse;
webResponse.setError(error);
ServiceWorkerGlobalScopeClient::from(getExecutionContext())
- ->respondToFetchEvent(m_fetchEventID, webResponse, m_eventDispatchTime);
- m_state = Done;
- m_observer->decrementPendingActivity();
- m_observer.clear();
+ ->respondToFetchEvent(m_eventID, webResponse, m_eventDispatchTime);
}
-void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) {
- ASSERT(getExecutionContext());
+void FetchRespondWithObserver::onResponseFulfilled(const ScriptValue& value) {
+ DCHECK(getExecutionContext());
if (!V8Response::hasInstance(value.v8Value(),
toIsolate(getExecutionContext()))) {
- responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance);
+ onResponseRejected(WebServiceWorkerResponseErrorNoV8Instance);
return;
}
Response* response = V8Response::toImplWithTypeCheck(
@@ -286,12 +186,12 @@ void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) {
// |basic| nor |default|."
const FetchResponseData::Type responseType = response->response()->getType();
if (responseType == FetchResponseData::ErrorType) {
- responseWasRejected(WebServiceWorkerResponseErrorResponseTypeError);
+ onResponseRejected(WebServiceWorkerResponseErrorResponseTypeError);
return;
}
if (responseType == FetchResponseData::OpaqueType) {
if (m_requestMode != WebURLRequest::FetchRequestModeNoCORS) {
- responseWasRejected(WebServiceWorkerResponseErrorResponseTypeOpaque);
+ onResponseRejected(WebServiceWorkerResponseErrorResponseTypeOpaque);
return;
}
@@ -301,21 +201,20 @@ void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) {
// remove this check when the spec will be updated.
// Spec issue: https://github.com/whatwg/fetch/issues/101
if (isClientRequest(m_frameType, m_requestContext)) {
- responseWasRejected(
+ onResponseRejected(
WebServiceWorkerResponseErrorResponseTypeOpaqueForClientRequest);
return;
}
}
if (m_redirectMode != WebURLRequest::FetchRedirectModeManual &&
responseType == FetchResponseData::OpaqueRedirectType) {
- responseWasRejected(
- WebServiceWorkerResponseErrorResponseTypeOpaqueRedirect);
+ onResponseRejected(WebServiceWorkerResponseErrorResponseTypeOpaqueRedirect);
return;
}
if (m_redirectMode != WebURLRequest::FetchRedirectModeFollow &&
response->redirected()) {
if (!isNavigationRequest(m_frameType)) {
- responseWasRejected(
+ onResponseRejected(
WebServiceWorkerResponseErrorRedirectedResponseForNotFollowRequest);
return;
}
@@ -328,11 +227,11 @@ void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) {
m_requestURL, response->internalURLList())));
}
if (response->isBodyLocked()) {
- responseWasRejected(WebServiceWorkerResponseErrorBodyLocked);
+ onResponseRejected(WebServiceWorkerResponseErrorBodyLocked);
return;
}
if (response->bodyUsed()) {
- responseWasRejected(WebServiceWorkerResponseErrorBodyUsed);
+ onResponseRejected(WebServiceWorkerResponseErrorBodyUsed);
return;
}
@@ -352,13 +251,15 @@ void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) {
}
}
ServiceWorkerGlobalScopeClient::from(getExecutionContext())
- ->respondToFetchEvent(m_fetchEventID, webResponse, m_eventDispatchTime);
- m_state = Done;
- m_observer->decrementPendingActivity();
- m_observer.clear();
+ ->respondToFetchEvent(m_eventID, webResponse, m_eventDispatchTime);
+}
+
+void FetchRespondWithObserver::onNoResponse() {
+ ServiceWorkerGlobalScopeClient::from(getExecutionContext())
+ ->respondToFetchEvent(m_eventID, m_eventDispatchTime);
}
-RespondWithObserver::RespondWithObserver(
+FetchRespondWithObserver::FetchRespondWithObserver(
ExecutionContext* context,
int fetchEventID,
const KURL& requestURL,
@@ -367,19 +268,15 @@ RespondWithObserver::RespondWithObserver(
WebURLRequest::FrameType frameType,
WebURLRequest::RequestContext requestContext,
WaitUntilObserver* observer)
- : ContextLifecycleObserver(context),
- m_fetchEventID(fetchEventID),
+ : RespondWithObserver(context, fetchEventID, observer),
m_requestURL(requestURL),
m_requestMode(requestMode),
m_redirectMode(redirectMode),
m_frameType(frameType),
- m_requestContext(requestContext),
- m_state(Initial),
- m_observer(observer) {}
+ m_requestContext(requestContext) {}
-DEFINE_TRACE(RespondWithObserver) {
- visitor->trace(m_observer);
- ContextLifecycleObserver::trace(visitor);
+DEFINE_TRACE(FetchRespondWithObserver) {
+ RespondWithObserver::trace(visitor);
}
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698