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 ~BlobLoader() override { } | |
61 | |
62 // FileReaderLoaderClient functions. | |
63 void didStartLoading() override { } | |
64 void didReceiveData() override { } | |
65 void didFinishLoading() override | |
66 { | |
67 m_presentationSession->didFinishLoadingBlob(m_loader.arrayBufferResult() ); | |
68 } | |
69 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) | |
195 return; | |
196 | |
197 Message* message = m_messages.first().get(); | |
198 ASSERT(!m_blobLoader); | |
mark a. foltz
2015/05/26 20:41:48
I don't think this is necessary because of the ret
USE s.singapati at gmail.com
2015/05/27 19:37:51
Done.
| |
199 m_blobLoader = new BlobLoader(message->blobDataHandle, this); | |
200 } | |
201 | |
139 void PresentationSession::didReceiveTextMessage(const String& message) | 202 void PresentationSession::didReceiveTextMessage(const String& message) |
140 { | 203 { |
141 dispatchEvent(MessageEvent::create(message)); | 204 dispatchEvent(MessageEvent::create(message)); |
142 } | 205 } |
143 | 206 |
144 void PresentationSession::close() | 207 void PresentationSession::close() |
145 { | 208 { |
146 if (m_state != WebPresentationSessionState::Connected) | 209 if (m_state != WebPresentationSessionState::Connected) |
147 return; | 210 return; |
148 PresentationController* controller = presentationController(); | 211 PresentationController* controller = presentationController(); |
149 if (controller) | 212 if (controller) |
150 controller->closeSession(m_url, m_id); | 213 controller->closeSession(m_url, m_id); |
214 | |
215 // Cancel current Blob loading if any. | |
216 if (m_blobLoader) { | |
217 m_blobLoader->cancel(); | |
218 m_blobLoader.clear(); | |
219 } | |
220 | |
221 // Clear Blob message queue. | |
222 Deque<OwnPtr<Message>> empty; | |
223 m_messages.swap(empty); | |
151 } | 224 } |
152 | 225 |
153 bool PresentationSession::matches(WebPresentationSessionClient* client) const | 226 bool PresentationSession::matches(WebPresentationSessionClient* client) const |
154 { | 227 { |
155 return client && m_url == static_cast<String>(client->getUrl()) && m_id == s tatic_cast<String>(client->getId()); | 228 return client && m_url == static_cast<String>(client->getUrl()) && m_id == s tatic_cast<String>(client->getId()); |
156 } | 229 } |
157 | 230 |
158 void PresentationSession::didChangeState(WebPresentationSessionState state) | 231 void PresentationSession::didChangeState(WebPresentationSessionState state) |
159 { | 232 { |
160 if (m_state == state) | 233 if (m_state == state) |
161 return; | 234 return; |
162 | 235 |
163 m_state = state; | 236 m_state = state; |
164 dispatchEvent(Event::create(EventTypeNames::statechange)); | 237 dispatchEvent(Event::create(EventTypeNames::statechange)); |
165 } | 238 } |
166 | 239 |
167 PresentationController* PresentationSession::presentationController() | 240 PresentationController* PresentationSession::presentationController() |
168 { | 241 { |
169 if (!frame()) | 242 if (!frame()) |
170 return nullptr; | 243 return nullptr; |
171 return PresentationController::from(*frame()); | 244 return PresentationController::from(*frame()); |
172 } | 245 } |
173 | 246 |
247 void PresentationSession::didFinishLoadingBlob(PassRefPtr<DOMArrayBuffer> buffer ) | |
248 { | |
249 ASSERT(!m_messages.isEmpty()); | |
250 m_blobLoader.clear(); | |
251 ASSERT(buffer && buffer->buffer()); | |
252 | |
253 PresentationController* controller = presentationController(); | |
254 if (controller) | |
255 controller->sendBlobData(m_url, m_id, static_cast<const uint8_t*>(buffer ->data()), buffer->byteLength()); | |
256 | |
257 m_messages.removeFirst(); | |
258 handleMessageQueue(); | |
259 } | |
260 | |
261 void PresentationSession::didFailLoadingBlob(FileError::ErrorCode errorCode) | |
262 { | |
263 ASSERT(!m_messages.isEmpty()); | |
264 m_blobLoader.clear(); | |
265 // FIXME: generate error message? | |
mark a. foltz
2015/05/26 20:41:48
There's currently no clean way to signal to the pa
| |
266 // Ignore the current failed blob item and continue with next items. | |
267 m_messages.removeFirst(); | |
268 handleMessageQueue(); | |
269 } | |
270 | |
174 } // namespace blink | 271 } // namespace blink |
OLD | NEW |