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/presentation/PresentationSession.h" | 6 #include "modules/presentation/PresentationSession.h" |
| 7 | 7 |
| 8 #include "core/dom/DOMArrayBuffer.h" | 8 #include "core/dom/DOMArrayBuffer.h" |
| 9 #include "core/dom/DOMArrayBufferView.h" | 9 #include "core/dom/DOMArrayBufferView.h" |
| 10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/events/Event.h" | 12 #include "core/events/Event.h" |
| 13 #include "core/events/MessageEvent.h" | 13 #include "core/events/MessageEvent.h" |
| 14 #include "core/fileapi/FileReaderLoader.h" | |
| 15 #include "core/fileapi/FileReaderLoaderClient.h" | |
| 14 #include "core/frame/LocalFrame.h" | 16 #include "core/frame/LocalFrame.h" |
| 15 #include "modules/EventTargetModules.h" | 17 #include "modules/EventTargetModules.h" |
| 16 #include "modules/presentation/Presentation.h" | 18 #include "modules/presentation/Presentation.h" |
| 17 #include "modules/presentation/PresentationController.h" | 19 #include "modules/presentation/PresentationController.h" |
| 18 #include "public/platform/modules/presentation/WebPresentationSessionClient.h" | 20 #include "public/platform/modules/presentation/WebPresentationSessionClient.h" |
| 19 #include "wtf/Assertions.h" | 21 #include "wtf/Assertions.h" |
| 20 #include "wtf/OwnPtr.h" | 22 #include "wtf/OwnPtr.h" |
| 21 #include "wtf/text/AtomicString.h" | 23 #include "wtf/text/AtomicString.h" |
| 22 | 24 |
| 23 namespace blink { | 25 namespace blink { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 40 return disconnectedValue; | 42 return disconnectedValue; |
| 41 } | 43 } |
| 42 | 44 |
| 43 void throwPresentationDisconnectedError(ExceptionState& exceptionState) | 45 void throwPresentationDisconnectedError(ExceptionState& exceptionState) |
| 44 { | 46 { |
| 45 exceptionState.throwDOMException(InvalidStateError, "Presentation session is disconnected."); | 47 exceptionState.throwDOMException(InvalidStateError, "Presentation session is disconnected."); |
| 46 } | 48 } |
| 47 | 49 |
| 48 } // namespace | 50 } // namespace |
| 49 | 51 |
| 52 class PresentationSession::BlobLoader final : public GarbageCollectedFinalized<P resentationSession::BlobLoader>, public FileReaderLoaderClient { | |
| 53 public: | |
| 54 BlobLoader(PassRefPtr<BlobDataHandle> blobDataHandle, PresentationSession* p resentationSession) | |
| 55 : m_presentationSession(presentationSession) | |
| 56 , m_loader(FileReaderLoader::ReadAsArrayBuffer, this) | |
| 57 { | |
| 58 m_loader.start(m_presentationSession->executionContext(), blobDataHandle ); | |
| 59 } | |
| 60 virtual ~BlobLoader() { } | |
|
mlamouri (slow - plz ping)
2015/05/19 13:08:55
override maybe?
USE s.singapati at gmail.com
2015/05/21 19:49:54
Done.
| |
| 61 | |
| 62 // FileReaderLoaderClient functions. | |
| 63 virtual void didStartLoading() override { } | |
| 64 virtual void didReceiveData() override { } | |
| 65 virtual void didFinishLoading() override | |
|
mlamouri (slow - plz ping)
2015/05/19 13:08:54
No need for virtual if you already have override.
USE s.singapati at gmail.com
2015/05/21 19:49:54
Done.
| |
| 66 { | |
| 67 m_presentationSession->didFinishLoadingBlob(m_loader.arrayBufferResult() ); | |
| 68 } | |
| 69 virtual void didFail(FileError::ErrorCode errorCode) override | |
| 70 { | |
| 71 m_presentationSession->didFailLoadingBlob(errorCode); | |
| 72 } | |
| 73 | |
| 74 void cancel() | |
| 75 { | |
| 76 m_loader.cancel(); | |
| 77 } | |
| 78 | |
| 79 DEFINE_INLINE_TRACE() | |
| 80 { | |
| 81 visitor->trace(m_presentationSession); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 Member<PresentationSession> m_presentationSession; | |
| 86 FileReaderLoader m_loader; | |
| 87 }; | |
| 88 | |
| 50 PresentationSession::PresentationSession(LocalFrame* frame, const String& id, co nst String& url) | 89 PresentationSession::PresentationSession(LocalFrame* frame, const String& id, co nst String& url) |
| 51 : DOMWindowProperty(frame) | 90 : DOMWindowProperty(frame) |
| 52 , m_id(id) | 91 , m_id(id) |
| 53 , m_url(url) | 92 , m_url(url) |
| 54 , m_state(WebPresentationSessionState::Disconnected) | 93 , m_state(WebPresentationSessionState::Disconnected) |
| 55 { | 94 { |
| 56 } | 95 } |
| 57 | 96 |
| 58 PresentationSession::~PresentationSession() | 97 PresentationSession::~PresentationSession() |
| 59 { | 98 { |
| 99 ASSERT(!m_blobLoader); | |
| 60 } | 100 } |
| 61 | 101 |
| 62 // static | 102 // static |
| 63 PresentationSession* PresentationSession::take(WebPresentationSessionClient* cli entRaw, Presentation* presentation) | 103 PresentationSession* PresentationSession::take(WebPresentationSessionClient* cli entRaw, Presentation* presentation) |
| 64 { | 104 { |
| 65 ASSERT(clientRaw); | 105 ASSERT(clientRaw); |
| 66 ASSERT(presentation); | 106 ASSERT(presentation); |
| 67 OwnPtr<WebPresentationSessionClient> client = adoptPtr(clientRaw); | 107 OwnPtr<WebPresentationSessionClient> client = adoptPtr(clientRaw); |
| 68 | 108 |
| 69 PresentationSession* session = new PresentationSession(presentation->frame() , client->getId(), client->getUrl()); | 109 PresentationSession* session = new PresentationSession(presentation->frame() , client->getId(), client->getUrl()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 83 } | 123 } |
| 84 | 124 |
| 85 ExecutionContext* PresentationSession::executionContext() const | 125 ExecutionContext* PresentationSession::executionContext() const |
| 86 { | 126 { |
| 87 if (!frame()) | 127 if (!frame()) |
| 88 return nullptr; | 128 return nullptr; |
| 89 return frame()->document();} | 129 return frame()->document();} |
| 90 | 130 |
| 91 DEFINE_TRACE(PresentationSession) | 131 DEFINE_TRACE(PresentationSession) |
| 92 { | 132 { |
| 133 visitor->trace(m_blobLoader); | |
| 93 RefCountedGarbageCollectedEventTargetWithInlineData<PresentationSession>::tr ace(visitor); | 134 RefCountedGarbageCollectedEventTargetWithInlineData<PresentationSession>::tr ace(visitor); |
| 94 DOMWindowProperty::trace(visitor); | 135 DOMWindowProperty::trace(visitor); |
| 95 } | 136 } |
| 96 | 137 |
| 97 const AtomicString& PresentationSession::state() const | 138 const AtomicString& PresentationSession::state() const |
| 98 { | 139 { |
| 99 return SessionStateToString(m_state); | 140 return SessionStateToString(m_state); |
| 100 } | 141 } |
| 101 | 142 |
| 102 void PresentationSession::send(const String& message, ExceptionState& exceptionS tate) | 143 void PresentationSession::send(const String& message, ExceptionState& exceptionS tate) |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 129 if (m_state == WebPresentationSessionState::Disconnected) { | 170 if (m_state == WebPresentationSessionState::Disconnected) { |
| 130 throwPresentationDisconnectedError(exceptionState); | 171 throwPresentationDisconnectedError(exceptionState); |
| 131 return; | 172 return; |
| 132 } | 173 } |
| 133 | 174 |
| 134 PresentationController* controller = presentationController(); | 175 PresentationController* controller = presentationController(); |
| 135 if (controller) | 176 if (controller) |
| 136 controller->send(m_url, m_id, data, size); | 177 controller->send(m_url, m_id, data, size); |
| 137 } | 178 } |
| 138 | 179 |
| 180 void PresentationSession::send(Blob* data, ExceptionState& exceptionState) | |
| 181 { | |
| 182 ASSERT(data); | |
| 183 if (m_state == WebPresentationSessionState::Disconnected) { | |
| 184 throwPresentationDisconnectedError(exceptionState); | |
| 185 return; | |
| 186 } | |
| 187 | |
| 188 m_messages.append(adoptPtr(new Message(data->blobDataHandle()))); | |
| 189 handleMessageQueue(); | |
| 190 } | |
| 191 | |
| 192 void PresentationSession::handleMessageQueue() | |
| 193 { | |
| 194 if (!m_messages.isEmpty() && !m_blobLoader) { | |
|
mlamouri (slow - plz ping)
2015/05/19 13:08:54
if (m_messages.isEmpty() || m_blobLoader)
return
USE s.singapati at gmail.com
2015/05/21 19:49:54
Done.
| |
| 195 Message* message = m_messages.first().get(); | |
| 196 ASSERT(!m_blobLoader); | |
| 197 m_blobLoader = new BlobLoader(message->blobDataHandle, this); | |
| 198 } | |
| 199 } | |
| 200 | |
| 139 void PresentationSession::didReceiveTextMessage(const String& message) | 201 void PresentationSession::didReceiveTextMessage(const String& message) |
| 140 { | 202 { |
| 141 dispatchEvent(MessageEvent::create(message)); | 203 dispatchEvent(MessageEvent::create(message)); |
| 142 } | 204 } |
| 143 | 205 |
| 144 void PresentationSession::close() | 206 void PresentationSession::close() |
| 145 { | 207 { |
| 146 if (m_state != WebPresentationSessionState::Connected) | 208 if (m_state != WebPresentationSessionState::Connected) |
| 147 return; | 209 return; |
| 148 PresentationController* controller = presentationController(); | 210 PresentationController* controller = presentationController(); |
| 149 if (controller) | 211 if (controller) |
| 150 controller->closeSession(m_url, m_id); | 212 controller->closeSession(m_url, m_id); |
| 213 | |
| 214 // Cancel current Blob loading if any. | |
| 215 if (m_blobLoader) { | |
| 216 m_blobLoader->cancel(); | |
| 217 m_blobLoader.clear(); | |
| 218 } | |
| 219 | |
| 220 // Clear Blob message queue. | |
| 221 Deque<OwnPtr<Message>> empty; | |
| 222 m_messages.swap(empty); | |
| 151 } | 223 } |
| 152 | 224 |
| 153 bool PresentationSession::matches(WebPresentationSessionClient* client) const | 225 bool PresentationSession::matches(WebPresentationSessionClient* client) const |
| 154 { | 226 { |
| 155 return client && m_url == static_cast<String>(client->getUrl()) && m_id == s tatic_cast<String>(client->getId()); | 227 return client && m_url == static_cast<String>(client->getUrl()) && m_id == s tatic_cast<String>(client->getId()); |
| 156 } | 228 } |
| 157 | 229 |
| 158 void PresentationSession::didChangeState(WebPresentationSessionState state) | 230 void PresentationSession::didChangeState(WebPresentationSessionState state) |
| 159 { | 231 { |
| 160 if (m_state == state) | 232 if (m_state == state) |
| 161 return; | 233 return; |
| 162 | 234 |
| 163 m_state = state; | 235 m_state = state; |
| 164 dispatchEvent(Event::create(EventTypeNames::statechange)); | 236 dispatchEvent(Event::create(EventTypeNames::statechange)); |
| 165 } | 237 } |
| 166 | 238 |
| 167 PresentationController* PresentationSession::presentationController() | 239 PresentationController* PresentationSession::presentationController() |
| 168 { | 240 { |
| 169 if (!frame()) | 241 if (!frame()) |
| 170 return nullptr; | 242 return nullptr; |
| 171 return PresentationController::from(*frame()); | 243 return PresentationController::from(*frame()); |
| 172 } | 244 } |
| 173 | 245 |
| 246 void PresentationSession::didFinishLoadingBlob(PassRefPtr<DOMArrayBuffer> buffer ) | |
| 247 { | |
| 248 m_blobLoader.clear(); | |
| 249 ASSERT(buffer && buffer->buffer()); | |
| 250 | |
| 251 PresentationController* controller = presentationController(); | |
| 252 if (controller) | |
| 253 controller->sendBlobData(m_url, m_id, static_cast<const uint8_t*>(buffer ->data()), buffer->byteLength()); | |
| 254 | |
| 255 ASSERT(m_messages.size() > 0); | |
|
mlamouri (slow - plz ping)
2015/05/19 13:08:55
You probably just want to check that it is not emp
USE s.singapati at gmail.com
2015/05/21 19:49:54
Done.
| |
| 256 m_messages.removeFirst(); | |
| 257 handleMessageQueue(); | |
| 258 } | |
| 259 | |
| 260 void PresentationSession::didFailLoadingBlob(FileError::ErrorCode errorCode) | |
| 261 { | |
| 262 m_blobLoader.clear(); | |
| 263 // FIXME: generate error message? | |
|
mlamouri (slow - plz ping)
2015/05/19 13:08:54
Yes, probably. Could .send() return a Promise that
USE s.singapati at gmail.com
2015/05/21 19:49:54
Seems to be API change.
Mark: Do you have any comm
| |
| 264 // Ignore the current failed blob item and continue with next items. | |
| 265 ASSERT(m_messages.size() > 0); | |
| 266 m_messages.removeFirst(); | |
| 267 handleMessageQueue(); | |
| 268 } | |
| 269 | |
| 174 } // namespace blink | 270 } // namespace blink |
| OLD | NEW |