Chromium Code Reviews| 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..f0858ffdcdf6dd6ae46dd2fffe3edfa6ccb8f1ae |
| --- /dev/null |
| +++ b/Source/modules/serviceworkers/RespondWithObserver.cpp |
| @@ -0,0 +1,113 @@ |
| +// 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, |
| + }; |
|
kinuko
2014/02/26 05:42:49
(these promise-waiter functions could be probably
falken
2014/02/27 06:45:09
Yes, this copied WaitUntilObserver. Let me do a re
|
| + |
| + 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::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) |
| +{ |
|
kinuko
2014/02/26 05:42:49
Shouldn't we check executionContext() is non-null
falken
2014/02/27 06:45:09
Oops, right. I guess this happens if the SW contex
|
| + ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEvent(m_eventID, response); |
| + m_state = Done; |
| + observeContext(0); |
|
kinuko
2014/02/26 05:42:49
I don't think this is necessary
falken
2014/02/27 06:45:09
Done.
|
| +} |
| + |
| +void RespondWithObserver::responseWasRejected() |
| +{ |
| + // FIXME: Throw a NetworkError to service worker's execution context. |
| + sendResponse(nullptr); |
| +} |
| + |
| +void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) |
| +{ |
| + 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 |