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