| 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 }; |
| 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::contextDestroyed() |
| 67 { |
| 68 ContextLifecycleObserver::contextDestroyed(); |
| 69 m_state = Done; |
| 70 } |
| 71 |
| 72 void RespondWithObserver::didDispatchEvent() |
| 73 { |
| 74 if (m_state == Initial) |
| 75 sendResponse(nullptr); |
| 76 } |
| 77 |
| 78 void RespondWithObserver::respondWith(const ScriptValue& value) |
| 79 { |
| 80 if (m_state != Initial) |
| 81 return; |
| 82 |
| 83 m_state = Pending; |
| 84 ScriptPromise(value).then( |
| 85 ThenFunction::create(this, ThenFunction::Fulfilled), |
| 86 ThenFunction::create(this, ThenFunction::Rejected)); |
| 87 } |
| 88 |
| 89 void RespondWithObserver::sendResponse(PassRefPtr<Response> response) |
| 90 { |
| 91 if (!executionContext()) |
| 92 return; |
| 93 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven
t(m_eventID, response); |
| 94 m_state = Done; |
| 95 } |
| 96 |
| 97 void RespondWithObserver::responseWasRejected() |
| 98 { |
| 99 // FIXME: Throw a NetworkError to service worker's execution context. |
| 100 sendResponse(nullptr); |
| 101 } |
| 102 |
| 103 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) |
| 104 { |
| 105 if (!executionContext()) |
| 106 return; |
| 107 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext()))
) { |
| 108 responseWasRejected(); |
| 109 return; |
| 110 } |
| 111 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value.v8Value()
); |
| 112 sendResponse(V8Response::toNative(object)); |
| 113 } |
| 114 |
| 115 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) |
| 116 : ContextLifecycleObserver(context) |
| 117 , m_eventID(eventID) |
| 118 , m_state(Initial) |
| 119 { |
| 120 } |
| 121 |
| 122 } // namespace WebCore |
| OLD | NEW |