| 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 "modules/navigatorconnect/AcceptConnectionObserver.h" | 5 #include "modules/navigatorconnect/AcceptConnectionObserver.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptValue.h" | 10 #include "bindings/core/v8/ScriptValue.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } | 69 } |
| 70 | 70 |
| 71 void AcceptConnectionObserver::contextDestroyed() | 71 void AcceptConnectionObserver::contextDestroyed() |
| 72 { | 72 { |
| 73 ContextLifecycleObserver::contextDestroyed(); | 73 ContextLifecycleObserver::contextDestroyed(); |
| 74 m_state = Done; | 74 m_state = Done; |
| 75 } | 75 } |
| 76 | 76 |
| 77 void AcceptConnectionObserver::didDispatchEvent() | 77 void AcceptConnectionObserver::didDispatchEvent() |
| 78 { | 78 { |
| 79 ASSERT(executionContext()); | 79 ASSERT(getExecutionContext()); |
| 80 if (m_state != Initial) | 80 if (m_state != Initial) |
| 81 return; | 81 return; |
| 82 responseWasRejected(); | 82 responseWasRejected(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 ScriptPromise AcceptConnectionObserver::respondWith(ScriptState* scriptState, Sc
riptPromise value, ExceptionState& exceptionState) | 85 ScriptPromise AcceptConnectionObserver::respondWith(ScriptState* scriptState, Sc
riptPromise value, ExceptionState& exceptionState) |
| 86 { | 86 { |
| 87 if (m_state != Initial) { | 87 if (m_state != Initial) { |
| 88 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "respondWith was already called.")); | 88 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "respondWith was already called.")); |
| 89 } | 89 } |
| 90 | 90 |
| 91 m_state = Pending; | 91 m_state = Pending; |
| 92 m_resolver = ScriptPromiseResolver::create(scriptState); | 92 m_resolver = ScriptPromiseResolver::create(scriptState); |
| 93 ScriptPromise promise = m_resolver->promise(); | 93 ScriptPromise promise = m_resolver->promise(); |
| 94 value.then( | 94 value.then( |
| 95 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 95 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 96 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 96 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 97 return promise; | 97 return promise; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void AcceptConnectionObserver::responseWasRejected() | 100 void AcceptConnectionObserver::responseWasRejected() |
| 101 { | 101 { |
| 102 ASSERT(executionContext()); | 102 ASSERT(getExecutionContext()); |
| 103 if (m_resolver) | 103 if (m_resolver) |
| 104 m_resolver->reject(DOMException::create(AbortError)); | 104 m_resolver->reject(DOMException::create(AbortError)); |
| 105 m_callbacks->onError(); | 105 m_callbacks->onError(); |
| 106 m_state = Done; | 106 m_state = Done; |
| 107 } | 107 } |
| 108 | 108 |
| 109 void AcceptConnectionObserver::responseWasResolved(const ScriptValue& value) | 109 void AcceptConnectionObserver::responseWasResolved(const ScriptValue& value) |
| 110 { | 110 { |
| 111 ASSERT(executionContext()); | 111 ASSERT(getExecutionContext()); |
| 112 | 112 |
| 113 ScriptState* scriptState = m_resolver->scriptState(); | 113 ScriptState* scriptState = m_resolver->getScriptState(); |
| 114 ExceptionState exceptionState(ExceptionState::UnknownContext, nullptr, nullp
tr, scriptState->context()->Global(), scriptState->isolate()); | 114 ExceptionState exceptionState(ExceptionState::UnknownContext, nullptr, nullp
tr, scriptState->context()->Global(), scriptState->isolate()); |
| 115 ServicePortConnectResponse response = ScriptValue::to<ServicePortConnectResp
onse>(scriptState->isolate(), value, exceptionState); | 115 ServicePortConnectResponse response = ScriptValue::to<ServicePortConnectResp
onse>(scriptState->isolate(), value, exceptionState); |
| 116 if (exceptionState.hadException()) { | 116 if (exceptionState.hadException()) { |
| 117 exceptionState.reject(m_resolver.get()); | 117 exceptionState.reject(m_resolver.get()); |
| 118 m_resolver = nullptr; | 118 m_resolver = nullptr; |
| 119 responseWasRejected(); | 119 responseWasRejected(); |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 if (!response.hasAccept() || !response.accept()) { | 122 if (!response.hasAccept() || !response.accept()) { |
| 123 responseWasRejected(); | 123 responseWasRejected(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 138 } | 138 } |
| 139 webPort.id = m_portID; | 139 webPort.id = m_portID; |
| 140 ServicePort* port = ServicePort::create(m_collection, webPort); | 140 ServicePort* port = ServicePort::create(m_collection, webPort); |
| 141 m_collection->addPort(port); | 141 m_collection->addPort(port); |
| 142 m_resolver->resolve(port); | 142 m_resolver->resolve(port); |
| 143 m_callbacks->onSuccess(webPort); | 143 m_callbacks->onSuccess(webPort); |
| 144 m_state = Done; | 144 m_state = Done; |
| 145 } | 145 } |
| 146 | 146 |
| 147 AcceptConnectionObserver::AcceptConnectionObserver(ServicePortCollection* collec
tion, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServicePortI
D portID, const KURL& targetURL) | 147 AcceptConnectionObserver::AcceptConnectionObserver(ServicePortCollection* collec
tion, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServicePortI
D portID, const KURL& targetURL) |
| 148 : ContextLifecycleObserver(collection->executionContext()) | 148 : ContextLifecycleObserver(collection->getExecutionContext()) |
| 149 , m_callbacks(callbacks) | 149 , m_callbacks(callbacks) |
| 150 , m_collection(collection) | 150 , m_collection(collection) |
| 151 , m_portID(portID) | 151 , m_portID(portID) |
| 152 , m_targetURL(targetURL) | 152 , m_targetURL(targetURL) |
| 153 , m_state(Initial) | 153 , m_state(Initial) |
| 154 { | 154 { |
| 155 } | 155 } |
| 156 | 156 |
| 157 DEFINE_TRACE(AcceptConnectionObserver) | 157 DEFINE_TRACE(AcceptConnectionObserver) |
| 158 { | 158 { |
| 159 visitor->trace(m_collection); | 159 visitor->trace(m_collection); |
| 160 visitor->trace(m_resolver); | 160 visitor->trace(m_resolver); |
| 161 ContextLifecycleObserver::trace(visitor); | 161 ContextLifecycleObserver::trace(visitor); |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace blink | 164 } // namespace blink |
| OLD | NEW |