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

Unified Diff: Source/modules/serviceworkers/RespondWithObserver.cpp

Issue 180843003: Start to implement FetchEvent for ServiceWorker (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: patch for landing Created 6 years, 10 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
« no previous file with comments | « Source/modules/serviceworkers/RespondWithObserver.h ('k') | Source/modules/serviceworkers/Response.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/serviceworkers/RespondWithObserver.cpp
diff --git a/Source/modules/serviceworkers/RespondWithObserver.cpp b/Source/modules/serviceworkers/RespondWithObserver.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..7528aa0cb73c2f7a5b32896d31ec8045b6a4bd19
--- /dev/null
+++ b/Source/modules/serviceworkers/RespondWithObserver.cpp
@@ -0,0 +1,122 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "modules/serviceworkers/RespondWithObserver.h"
+
+#include "V8Response.h"
+#include "bindings/v8/ScriptFunction.h"
+#include "bindings/v8/ScriptPromise.h"
+#include "bindings/v8/ScriptValue.h"
+#include "bindings/v8/V8Binding.h"
+#include "core/dom/ExecutionContext.h"
+#include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
+#include "wtf/Assertions.h"
+#include "wtf/RefPtr.h"
+
+namespace WebCore {
+
+class RespondWithObserver::ThenFunction FINAL : public ScriptFunction {
+public:
+ enum ResolveType {
+ Fulfilled,
+ Rejected,
+ };
+
+ static PassOwnPtr<ScriptFunction> create(PassRefPtr<RespondWithObserver> observer, ResolveType type)
+ {
+ return adoptPtr(new ThenFunction(toIsolate(observer->executionContext()), observer, type));
+ }
+
+private:
+ ThenFunction(v8::Isolate* isolate, PassRefPtr<RespondWithObserver> observer, ResolveType type)
+ : ScriptFunction(isolate)
+ , m_observer(observer)
+ , m_resolveType(type)
+ {
+ }
+
+ virtual ScriptValue call(ScriptValue value) OVERRIDE
+ {
+ ASSERT(m_observer);
+ ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
+ if (m_resolveType == Rejected)
+ m_observer->responseWasRejected();
+ else
+ m_observer->responseWasFulfilled(value);
+ m_observer = nullptr;
+ return value;
+ }
+
+ RefPtr<RespondWithObserver> m_observer;
+ ResolveType m_resolveType;
+};
+
+PassRefPtr<RespondWithObserver> RespondWithObserver::create(ExecutionContext* context, int eventID)
+{
+ return adoptRef(new RespondWithObserver(context, eventID));
+}
+
+RespondWithObserver::~RespondWithObserver()
+{
+ ASSERT(m_state == Done);
+}
+
+void RespondWithObserver::contextDestroyed()
+{
+ ContextLifecycleObserver::contextDestroyed();
+ m_state = Done;
+}
+
+void RespondWithObserver::didDispatchEvent()
+{
+ if (m_state == Initial)
+ sendResponse(nullptr);
+}
+
+void RespondWithObserver::respondWith(const ScriptValue& value)
+{
+ if (m_state != Initial)
+ return;
+
+ m_state = Pending;
+ ScriptPromise(value).then(
+ ThenFunction::create(this, ThenFunction::Fulfilled),
+ ThenFunction::create(this, ThenFunction::Rejected));
+}
+
+void RespondWithObserver::sendResponse(PassRefPtr<Response> response)
+{
+ if (!executionContext())
+ return;
+ ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEvent(m_eventID, response);
+ m_state = Done;
+}
+
+void RespondWithObserver::responseWasRejected()
+{
+ // FIXME: Throw a NetworkError to service worker's execution context.
+ sendResponse(nullptr);
+}
+
+void RespondWithObserver::responseWasFulfilled(const ScriptValue& value)
+{
+ if (!executionContext())
+ return;
+ if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext()))) {
+ responseWasRejected();
+ return;
+ }
+ v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value.v8Value());
+ sendResponse(V8Response::toNative(object));
+}
+
+RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID)
+ : ContextLifecycleObserver(context)
+ , m_eventID(eventID)
+ , m_state(Initial)
+{
+}
+
+} // namespace WebCore
« no previous file with comments | « Source/modules/serviceworkers/RespondWithObserver.h ('k') | Source/modules/serviceworkers/Response.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698