Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "modules/serviceworkers/RespondWithObserver.h" | |
| 7 | |
| 8 #include "V8Response.h" | |
| 9 #include "bindings/v8/ScriptFunction.h" | |
| 10 #include "bindings/v8/ScriptPromise.h" | |
| 11 #include "bindings/v8/ScriptValue.h" | |
| 12 #include "bindings/v8/V8Binding.h" | |
| 13 #include "core/dom/ExecutionContext.h" | |
| 14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | |
| 15 #include "wtf/Assertions.h" | |
| 16 #include "wtf/RefPtr.h" | |
| 17 | |
| 18 namespace WebCore { | |
| 19 | |
| 20 class RespondWithObserver::ThenFunction FINAL : public ScriptFunction { | |
| 21 public: | |
| 22 enum ResolveType { | |
| 23 Fulfilled, | |
| 24 Rejected, | |
| 25 }; | |
|
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
| |
| 26 | |
| 27 static PassOwnPtr<ScriptFunction> create(PassRefPtr<RespondWithObserver> obs erver, ResolveType type) | |
| 28 { | |
| 29 return adoptPtr(new ThenFunction(toIsolate(observer->executionContext()) , observer, type)); | |
| 30 } | |
| 31 | |
| 32 private: | |
| 33 ThenFunction(v8::Isolate* isolate, PassRefPtr<RespondWithObserver> observer, ResolveType type) | |
| 34 : ScriptFunction(isolate) | |
| 35 , m_observer(observer) | |
| 36 , m_resolveType(type) | |
| 37 { | |
| 38 } | |
| 39 | |
| 40 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
| 41 { | |
| 42 ASSERT(m_observer); | |
| 43 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected); | |
| 44 if (m_resolveType == Rejected) | |
| 45 m_observer->responseWasRejected(); | |
| 46 else | |
| 47 m_observer->responseWasFulfilled(value); | |
| 48 m_observer = nullptr; | |
| 49 return value; | |
| 50 } | |
| 51 | |
| 52 RefPtr<RespondWithObserver> m_observer; | |
| 53 ResolveType m_resolveType; | |
| 54 }; | |
| 55 | |
| 56 PassRefPtr<RespondWithObserver> RespondWithObserver::create(ExecutionContext* co ntext, int eventID) | |
| 57 { | |
| 58 return adoptRef(new RespondWithObserver(context, eventID)); | |
| 59 } | |
| 60 | |
| 61 RespondWithObserver::~RespondWithObserver() | |
| 62 { | |
| 63 ASSERT(m_state == Done); | |
| 64 } | |
| 65 | |
| 66 void RespondWithObserver::didDispatchEvent() | |
| 67 { | |
| 68 if (m_state == Initial) | |
| 69 sendResponse(nullptr); | |
| 70 } | |
| 71 | |
| 72 void RespondWithObserver::respondWith(const ScriptValue& value) | |
| 73 { | |
| 74 if (m_state != Initial) | |
| 75 return; | |
| 76 | |
| 77 m_state = Pending; | |
| 78 ScriptPromise(value).then( | |
| 79 ThenFunction::create(this, ThenFunction::Fulfilled), | |
| 80 ThenFunction::create(this, ThenFunction::Rejected)); | |
| 81 } | |
| 82 | |
| 83 void RespondWithObserver::sendResponse(PassRefPtr<Response> response) | |
| 84 { | |
|
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
| |
| 85 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, response); | |
| 86 m_state = Done; | |
| 87 observeContext(0); | |
|
kinuko
2014/02/26 05:42:49
I don't think this is necessary
falken
2014/02/27 06:45:09
Done.
| |
| 88 } | |
| 89 | |
| 90 void RespondWithObserver::responseWasRejected() | |
| 91 { | |
| 92 // FIXME: Throw a NetworkError to service worker's execution context. | |
| 93 sendResponse(nullptr); | |
| 94 } | |
| 95 | |
| 96 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) | |
| 97 { | |
| 98 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext())) ) { | |
| 99 responseWasRejected(); | |
| 100 return; | |
| 101 } | |
| 102 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value.v8Value() ); | |
| 103 sendResponse(V8Response::toNative(object)); | |
| 104 } | |
| 105 | |
| 106 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) | |
| 107 : ContextLifecycleObserver(context) | |
| 108 , m_eventID(eventID) | |
| 109 , m_state(Initial) | |
| 110 { | |
| 111 } | |
| 112 | |
| 113 } // namespace WebCore | |
| OLD | NEW |