| 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" |
| 11 #include "bindings/core/v8/ScriptValue.h" | 11 #include "bindings/core/v8/ScriptValue.h" |
| 12 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
| 13 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
| 15 #include "modules/navigatorconnect/ServicePort.h" |
| 16 #include "modules/navigatorconnect/ServicePortConnectResponse.h" |
| 13 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 17 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 14 | 18 |
| 15 namespace blink { | 19 namespace blink { |
| 16 | 20 |
| 17 // Function that is called when the promise passed to acceptConnection is either | 21 // Function that is called when the promise passed to acceptConnection is either |
| 18 // rejected or fulfilled. Calls the corresponding method on | 22 // rejected or fulfilled. Calls the corresponding method on |
| 19 // AcceptConnectionObserver. | 23 // AcceptConnectionObserver. |
| 20 class AcceptConnectionObserver::ThenFunction final : public ScriptFunction { | 24 class AcceptConnectionObserver::ThenFunction final : public ScriptFunction { |
| 21 public: | 25 public: |
| 22 enum ResolveType { | 26 enum ResolveType { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 42 , m_observer(observer) | 46 , m_observer(observer) |
| 43 , m_resolveType(type) | 47 , m_resolveType(type) |
| 44 { | 48 { |
| 45 } | 49 } |
| 46 | 50 |
| 47 ScriptValue call(ScriptValue value) override | 51 ScriptValue call(ScriptValue value) override |
| 48 { | 52 { |
| 49 ASSERT(m_observer); | 53 ASSERT(m_observer); |
| 50 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected); | 54 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected); |
| 51 if (m_resolveType == Rejected) | 55 if (m_resolveType == Rejected) |
| 52 m_observer->connectionWasRejected(); | 56 m_observer->responseWasRejected(); |
| 53 else | 57 else |
| 54 m_observer->connectionWasAccepted(value); | 58 m_observer->responseWasResolved(value); |
| 55 m_observer = nullptr; | 59 m_observer = nullptr; |
| 56 return value; | 60 return value; |
| 57 } | 61 } |
| 58 | 62 |
| 59 Member<AcceptConnectionObserver> m_observer; | 63 Member<AcceptConnectionObserver> m_observer; |
| 60 ResolveType m_resolveType; | 64 ResolveType m_resolveType; |
| 61 }; | 65 }; |
| 62 | 66 |
| 63 AcceptConnectionObserver* AcceptConnectionObserver::create(ExecutionContext* con
text, int eventID) | 67 AcceptConnectionObserver* AcceptConnectionObserver::create(ExecutionContext* con
text, int eventID) |
| 64 { | 68 { |
| 65 return new AcceptConnectionObserver(context, eventID); | 69 return new AcceptConnectionObserver(context, eventID); |
| 66 } | 70 } |
| 67 | 71 |
| 72 AcceptConnectionObserver* AcceptConnectionObserver::create(ServicePortCollection
* collection, PassOwnPtr<WebServicePortConnectEventCallbacks> callbacks, WebServ
icePortID portID, const KURL& targetURL) |
| 73 { |
| 74 return new AcceptConnectionObserver(collection, callbacks, portID, targetURL
); |
| 75 } |
| 76 |
| 68 void AcceptConnectionObserver::contextDestroyed() | 77 void AcceptConnectionObserver::contextDestroyed() |
| 69 { | 78 { |
| 70 ContextLifecycleObserver::contextDestroyed(); | 79 ContextLifecycleObserver::contextDestroyed(); |
| 71 m_state = Done; | 80 m_state = Done; |
| 72 } | 81 } |
| 73 | 82 |
| 74 void AcceptConnectionObserver::didDispatchEvent() | 83 void AcceptConnectionObserver::didDispatchEvent() |
| 75 { | 84 { |
| 76 ASSERT(executionContext()); | 85 ASSERT(executionContext()); |
| 77 if (m_state != Initial) | 86 if (m_state != Initial) |
| 78 return; | 87 return; |
| 79 connectionWasRejected(); | 88 responseWasRejected(); |
| 80 } | 89 } |
| 81 | 90 |
| 82 void AcceptConnectionObserver::acceptConnection(ScriptState* scriptState, Script
Promise value, ExceptionState& exceptionState) | 91 void AcceptConnectionObserver::acceptConnection(ScriptState* scriptState, Script
Promise value, ExceptionState& exceptionState) |
| 83 { | 92 { |
| 84 if (m_state != Initial) { | 93 if (m_state != Initial) { |
| 85 exceptionState.throwDOMException(InvalidStateError, "acceptConnection wa
s already called."); | 94 exceptionState.throwDOMException(InvalidStateError, "acceptConnection wa
s already called."); |
| 86 return; | 95 return; |
| 87 } | 96 } |
| 88 | 97 |
| 89 m_state = Pending; | 98 m_state = Pending; |
| 90 value.then( | 99 value.then( |
| 91 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 100 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 92 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 101 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 93 } | 102 } |
| 94 | 103 |
| 95 void AcceptConnectionObserver::connectionWasRejected() | 104 ScriptPromise AcceptConnectionObserver::respondWith(ScriptState* scriptState, Sc
riptPromise value, ExceptionState& exceptionState) |
| 105 { |
| 106 if (m_state != Initial) { |
| 107 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "respondWith was already called.")); |
| 108 } |
| 109 |
| 110 m_state = Pending; |
| 111 m_resolver = ScriptPromiseResolver::create(scriptState); |
| 112 ScriptPromise promise = m_resolver->promise(); |
| 113 value.then( |
| 114 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 115 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 116 return promise; |
| 117 } |
| 118 |
| 119 void AcceptConnectionObserver::responseWasRejected() |
| 96 { | 120 { |
| 97 ASSERT(executionContext()); | 121 ASSERT(executionContext()); |
| 98 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleCrossOrig
inConnectEvent(m_eventID, false); | 122 if (m_resolver) |
| 123 m_resolver->reject(DOMException::create(AbortError)); |
| 124 if (m_callbacks) { |
| 125 m_callbacks->onError(); |
| 126 } else { |
| 127 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleCross
OriginConnectEvent(m_eventID, false); |
| 128 } |
| 99 m_state = Done; | 129 m_state = Done; |
| 100 } | 130 } |
| 101 | 131 |
| 102 void AcceptConnectionObserver::connectionWasAccepted(const ScriptValue& value) | 132 void AcceptConnectionObserver::responseWasResolved(const ScriptValue& value) |
| 103 { | 133 { |
| 104 ASSERT(executionContext()); | 134 ASSERT(executionContext()); |
| 105 if (!value.v8Value()->IsTrue()) { | 135 if (!m_resolver) { |
| 106 connectionWasRejected(); | 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; |
| 107 return; | 144 return; |
| 108 } | 145 } |
| 109 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleCrossOrig
inConnectEvent(m_eventID, true); | 146 |
| 147 ScriptState* scriptState = m_resolver->scriptState(); |
| 148 ExceptionState exceptionState(ExceptionState::UnknownContext, nullptr, nullp
tr, scriptState->context()->Global(), scriptState->isolate()); |
| 149 ServicePortConnectResponse response = ScriptValue::to<ServicePortConnectResp
onse>(scriptState->isolate(), value, exceptionState); |
| 150 if (exceptionState.hadException()) { |
| 151 exceptionState.reject(m_resolver.get()); |
| 152 m_resolver = nullptr; |
| 153 responseWasRejected(); |
| 154 return; |
| 155 } |
| 156 if (!response.hasAccept() || !response.accept()) { |
| 157 responseWasRejected(); |
| 158 return; |
| 159 } |
| 160 WebServicePort webPort; |
| 161 webPort.targetUrl = m_targetURL; |
| 162 if (response.hasName()) |
| 163 webPort.name = response.name(); |
| 164 if (response.hasData()) { |
| 165 webPort.data = SerializedScriptValueFactory::instance().create(scriptSta
te->isolate(), response.data(), nullptr, exceptionState)->toWireString(); |
| 166 if (exceptionState.hadException()) { |
| 167 exceptionState.reject(m_resolver.get()); |
| 168 m_resolver = nullptr; |
| 169 responseWasRejected(); |
| 170 return; |
| 171 } |
| 172 } |
| 173 webPort.id = m_portID; |
| 174 ServicePort* port = ServicePort::create(m_collection, webPort); |
| 175 m_collection->addPort(port); |
| 176 m_resolver->resolve(port); |
| 177 m_callbacks->onSuccess(&webPort); |
| 110 m_state = Done; | 178 m_state = Done; |
| 111 } | 179 } |
| 112 | 180 |
| 113 AcceptConnectionObserver::AcceptConnectionObserver(ExecutionContext* context, in
t eventID) | 181 AcceptConnectionObserver::AcceptConnectionObserver(ExecutionContext* context, in
t eventID) |
| 114 : ContextLifecycleObserver(context) | 182 : ContextLifecycleObserver(context) |
| 115 , m_eventID(eventID) | 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) |
| 190 : ContextLifecycleObserver(collection->executionContext()) |
| 191 , m_eventID(-1) |
| 192 , m_callbacks(callbacks) |
| 193 , m_collection(collection) |
| 194 , m_portID(portID) |
| 195 , m_targetURL(targetURL) |
| 116 , m_state(Initial) | 196 , m_state(Initial) |
| 117 { | 197 { |
| 118 } | 198 } |
| 119 | 199 |
| 120 DEFINE_TRACE(AcceptConnectionObserver) | 200 DEFINE_TRACE(AcceptConnectionObserver) |
| 121 { | 201 { |
| 202 visitor->trace(m_collection); |
| 122 ContextLifecycleObserver::trace(visitor); | 203 ContextLifecycleObserver::trace(visitor); |
| 123 } | 204 } |
| 124 | 205 |
| 125 } // namespace blink | 206 } // namespace blink |
| OLD | NEW |