| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/navigatorconnect/AcceptConnectionObserver.h" | 6 #include "modules/navigatorconnect/AcceptConnectionObserver.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptFunction.h" | 9 #include "bindings/core/v8/ScriptFunction.h" |
| 10 #include "bindings/core/v8/ScriptPromise.h" | 10 #include "bindings/core/v8/ScriptPromise.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 else | 57 else |
| 58 m_observer->responseWasResolved(value); | 58 m_observer->responseWasResolved(value); |
| 59 m_observer = nullptr; | 59 m_observer = nullptr; |
| 60 return value; | 60 return value; |
| 61 } | 61 } |
| 62 | 62 |
| 63 Member<AcceptConnectionObserver> m_observer; | 63 Member<AcceptConnectionObserver> m_observer; |
| 64 ResolveType m_resolveType; | 64 ResolveType m_resolveType; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 AcceptConnectionObserver* AcceptConnectionObserver::create(ExecutionContext* con
text, int eventID) | |
| 68 { | |
| 69 return new AcceptConnectionObserver(context, eventID); | |
| 70 } | |
| 71 | |
| 72 AcceptConnectionObserver* AcceptConnectionObserver::create(ServicePortCollection
* collection, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServ
icePortID portID, const KURL& targetURL) | 67 AcceptConnectionObserver* AcceptConnectionObserver::create(ServicePortCollection
* collection, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServ
icePortID portID, const KURL& targetURL) |
| 73 { | 68 { |
| 74 return new AcceptConnectionObserver(collection, callbacks, portID, targetURL
); | 69 return new AcceptConnectionObserver(collection, callbacks, portID, targetURL
); |
| 75 } | 70 } |
| 76 | 71 |
| 77 void AcceptConnectionObserver::contextDestroyed() | 72 void AcceptConnectionObserver::contextDestroyed() |
| 78 { | 73 { |
| 79 ContextLifecycleObserver::contextDestroyed(); | 74 ContextLifecycleObserver::contextDestroyed(); |
| 80 m_state = Done; | 75 m_state = Done; |
| 81 } | 76 } |
| 82 | 77 |
| 83 void AcceptConnectionObserver::didDispatchEvent() | 78 void AcceptConnectionObserver::didDispatchEvent() |
| 84 { | 79 { |
| 85 ASSERT(executionContext()); | 80 ASSERT(executionContext()); |
| 86 if (m_state != Initial) | 81 if (m_state != Initial) |
| 87 return; | 82 return; |
| 88 responseWasRejected(); | 83 responseWasRejected(); |
| 89 } | 84 } |
| 90 | 85 |
| 91 void AcceptConnectionObserver::acceptConnection(ScriptState* scriptState, Script
Promise value, ExceptionState& exceptionState) | |
| 92 { | |
| 93 if (m_state != Initial) { | |
| 94 exceptionState.throwDOMException(InvalidStateError, "acceptConnection wa
s already called."); | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 m_state = Pending; | |
| 99 value.then( | |
| 100 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | |
| 101 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | |
| 102 } | |
| 103 | |
| 104 ScriptPromise AcceptConnectionObserver::respondWith(ScriptState* scriptState, Sc
riptPromise value, ExceptionState& exceptionState) | 86 ScriptPromise AcceptConnectionObserver::respondWith(ScriptState* scriptState, Sc
riptPromise value, ExceptionState& exceptionState) |
| 105 { | 87 { |
| 106 if (m_state != Initial) { | 88 if (m_state != Initial) { |
| 107 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "respondWith was already called.")); | 89 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "respondWith was already called.")); |
| 108 } | 90 } |
| 109 | 91 |
| 110 m_state = Pending; | 92 m_state = Pending; |
| 111 m_resolver = ScriptPromiseResolver::create(scriptState); | 93 m_resolver = ScriptPromiseResolver::create(scriptState); |
| 112 ScriptPromise promise = m_resolver->promise(); | 94 ScriptPromise promise = m_resolver->promise(); |
| 113 value.then( | 95 value.then( |
| 114 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 96 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 115 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 97 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 116 return promise; | 98 return promise; |
| 117 } | 99 } |
| 118 | 100 |
| 119 void AcceptConnectionObserver::responseWasRejected() | 101 void AcceptConnectionObserver::responseWasRejected() |
| 120 { | 102 { |
| 121 ASSERT(executionContext()); | 103 ASSERT(executionContext()); |
| 122 if (m_resolver) | 104 if (m_resolver) |
| 123 m_resolver->reject(DOMException::create(AbortError)); | 105 m_resolver->reject(DOMException::create(AbortError)); |
| 124 if (m_callbacks) { | 106 m_callbacks->onError(); |
| 125 m_callbacks->onError(); | |
| 126 } else { | |
| 127 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleCross
OriginConnectEvent(m_eventID, false); | |
| 128 } | |
| 129 m_state = Done; | 107 m_state = Done; |
| 130 } | 108 } |
| 131 | 109 |
| 132 void AcceptConnectionObserver::responseWasResolved(const ScriptValue& value) | 110 void AcceptConnectionObserver::responseWasResolved(const ScriptValue& value) |
| 133 { | 111 { |
| 134 ASSERT(executionContext()); | 112 ASSERT(executionContext()); |
| 135 if (!m_resolver) { | |
| 136 // TODO(mek): Get rid of this block when observer is only used for | |
| 137 // service port connect events. | |
| 138 if (!value.v8Value()->IsTrue()) { | |
| 139 responseWasRejected(); | |
| 140 return; | |
| 141 } | |
| 142 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleCross
OriginConnectEvent(m_eventID, true); | |
| 143 m_state = Done; | |
| 144 return; | |
| 145 } | |
| 146 | 113 |
| 147 ScriptState* scriptState = m_resolver->scriptState(); | 114 ScriptState* scriptState = m_resolver->scriptState(); |
| 148 ExceptionState exceptionState(ExceptionState::UnknownContext, nullptr, nullp
tr, scriptState->context()->Global(), scriptState->isolate()); | 115 ExceptionState exceptionState(ExceptionState::UnknownContext, nullptr, nullp
tr, scriptState->context()->Global(), scriptState->isolate()); |
| 149 ServicePortConnectResponse response = ScriptValue::to<ServicePortConnectResp
onse>(scriptState->isolate(), value, exceptionState); | 116 ServicePortConnectResponse response = ScriptValue::to<ServicePortConnectResp
onse>(scriptState->isolate(), value, exceptionState); |
| 150 if (exceptionState.hadException()) { | 117 if (exceptionState.hadException()) { |
| 151 exceptionState.reject(m_resolver.get()); | 118 exceptionState.reject(m_resolver.get()); |
| 152 m_resolver = nullptr; | 119 m_resolver = nullptr; |
| 153 responseWasRejected(); | 120 responseWasRejected(); |
| 154 return; | 121 return; |
| 155 } | 122 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 171 } | 138 } |
| 172 } | 139 } |
| 173 webPort.id = m_portID; | 140 webPort.id = m_portID; |
| 174 ServicePort* port = ServicePort::create(m_collection, webPort); | 141 ServicePort* port = ServicePort::create(m_collection, webPort); |
| 175 m_collection->addPort(port); | 142 m_collection->addPort(port); |
| 176 m_resolver->resolve(port); | 143 m_resolver->resolve(port); |
| 177 m_callbacks->onSuccess(&webPort); | 144 m_callbacks->onSuccess(&webPort); |
| 178 m_state = Done; | 145 m_state = Done; |
| 179 } | 146 } |
| 180 | 147 |
| 181 AcceptConnectionObserver::AcceptConnectionObserver(ExecutionContext* context, in
t eventID) | |
| 182 : ContextLifecycleObserver(context) | |
| 183 , m_eventID(eventID) | |
| 184 , m_portID(-1) | |
| 185 , m_state(Initial) | |
| 186 { | |
| 187 } | |
| 188 | |
| 189 AcceptConnectionObserver::AcceptConnectionObserver(ServicePortCollection* collec
tion, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServicePortI
D portID, const KURL& targetURL) | 148 AcceptConnectionObserver::AcceptConnectionObserver(ServicePortCollection* collec
tion, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServicePortI
D portID, const KURL& targetURL) |
| 190 : ContextLifecycleObserver(collection->executionContext()) | 149 : ContextLifecycleObserver(collection->executionContext()) |
| 191 , m_eventID(-1) | |
| 192 , m_callbacks(callbacks) | 150 , m_callbacks(callbacks) |
| 193 , m_collection(collection) | 151 , m_collection(collection) |
| 194 , m_portID(portID) | 152 , m_portID(portID) |
| 195 , m_targetURL(targetURL) | 153 , m_targetURL(targetURL) |
| 196 , m_state(Initial) | 154 , m_state(Initial) |
| 197 { | 155 { |
| 198 } | 156 } |
| 199 | 157 |
| 200 DEFINE_TRACE(AcceptConnectionObserver) | 158 DEFINE_TRACE(AcceptConnectionObserver) |
| 201 { | 159 { |
| 202 visitor->trace(m_collection); | 160 visitor->trace(m_collection); |
| 203 visitor->trace(m_resolver); | 161 visitor->trace(m_resolver); |
| 204 ContextLifecycleObserver::trace(visitor); | 162 ContextLifecycleObserver::trace(visitor); |
| 205 } | 163 } |
| 206 | 164 |
| 207 } // namespace blink | 165 } // namespace blink |
| OLD | NEW |