OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/renderer/presentation/presentation_dispatcher.h" | 5 #include "content/renderer/presentation/presentation_dispatcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/common/presentation/presentation_service.mojom.h" | 8 #include "content/common/presentation/presentation_service.mojom.h" |
9 #include "content/public/common/service_registry.h" | 9 #include "content/public/common/service_registry.h" |
10 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
11 #include "content/renderer/presentation/presentation_session_client.h" | 11 #include "content/renderer/presentation/presentation_session_client.h" |
12 #include "third_party/WebKit/public/platform/WebString.h" | 12 #include "third_party/WebKit/public/platform/WebString.h" |
13 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h" | 13 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h" |
14 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h" | 14 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h" |
15 #include "third_party/WebKit/public/web/WebDocument.h" | 15 #include "third_party/WebKit/public/web/WebDocument.h" |
16 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 16 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Maximum number of bytes allowed in a presentation session message. | |
22 static const size_t kMaxPresentationSessionMessageSize = 64 * 1024; // 64 KB. | |
mark a. foltz
2015/05/07 01:34:30
This should be declared in one place!
Create prese
USE s.singapati at gmail.com
2015/05/07 14:08:52
Done.
| |
23 | |
21 blink::WebPresentationError::ErrorType GetWebPresentationErrorTypeFromMojo( | 24 blink::WebPresentationError::ErrorType GetWebPresentationErrorTypeFromMojo( |
22 presentation::PresentationErrorType mojoErrorType) { | 25 presentation::PresentationErrorType mojoErrorType) { |
23 switch (mojoErrorType) { | 26 switch (mojoErrorType) { |
24 case presentation::PRESENTATION_ERROR_TYPE_NO_AVAILABLE_SCREENS: | 27 case presentation::PRESENTATION_ERROR_TYPE_NO_AVAILABLE_SCREENS: |
25 return blink::WebPresentationError::ErrorTypeNoAvailableScreens; | 28 return blink::WebPresentationError::ErrorTypeNoAvailableScreens; |
26 case presentation::PRESENTATION_ERROR_TYPE_SESSION_REQUEST_CANCELLED: | 29 case presentation::PRESENTATION_ERROR_TYPE_SESSION_REQUEST_CANCELLED: |
27 return blink::WebPresentationError::ErrorTypeSessionRequestCancelled; | 30 return blink::WebPresentationError::ErrorTypeSessionRequestCancelled; |
28 case presentation::PRESENTATION_ERROR_TYPE_NO_PRESENTATION_FOUND: | 31 case presentation::PRESENTATION_ERROR_TYPE_NO_PRESENTATION_FOUND: |
29 return blink::WebPresentationError::ErrorTypeNoPresentationFound; | 32 return blink::WebPresentationError::ErrorTypeNoPresentationFound; |
30 case presentation::PRESENTATION_ERROR_TYPE_UNKNOWN: | 33 case presentation::PRESENTATION_ERROR_TYPE_UNKNOWN: |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 // OnSessionCreated() is called. |callback| needs to be alive and also needs | 128 // OnSessionCreated() is called. |callback| needs to be alive and also needs |
126 // to be destroyed so we transfer its ownership to the mojo callback. | 129 // to be destroyed so we transfer its ownership to the mojo callback. |
127 presentation_service_->JoinSession( | 130 presentation_service_->JoinSession( |
128 presentationUrl.utf8(), | 131 presentationUrl.utf8(), |
129 presentationId.utf8(), | 132 presentationId.utf8(), |
130 base::Bind(&PresentationDispatcher::OnSessionCreated, | 133 base::Bind(&PresentationDispatcher::OnSessionCreated, |
131 base::Unretained(this), | 134 base::Unretained(this), |
132 base::Owned(callback))); | 135 base::Owned(callback))); |
133 } | 136 } |
134 | 137 |
138 void PresentationDispatcher::sendString( | |
139 const blink::WebString& presentationUrl, | |
140 const blink::WebString& presentationId, | |
141 const blink::WebString& message) { | |
142 if (message.utf8().size() > kMaxPresentationSessionMessageSize) { | |
143 // TODO(crbug.com/459008): Limit the size of individual messages to 64k | |
144 // for now. Consider throwing DOMException or splitting bigger messages | |
145 // into smaller chunks later. | |
146 LOG(WARNING) << "message size exceeded limit!"; | |
147 return; | |
148 } | |
149 | |
150 presentation::SessionMessage* session_message = | |
151 new presentation::SessionMessage(); | |
152 session_message->presentation_url = presentationUrl.utf8(); | |
153 session_message->presentation_id = presentationId.utf8(); | |
154 session_message->type = presentation::PresentationMessageType:: | |
155 PRESENTATION_MESSAGE_TYPE_TEXT; | |
156 session_message->message = message.utf8(); | |
157 | |
158 message_request_queue_.push(make_linked_ptr(session_message)); | |
159 // Start processing request if only one in the queue. | |
160 if (message_request_queue_.size() == 1) { | |
161 const linked_ptr<presentation::SessionMessage>& request = | |
162 message_request_queue_.front(); | |
163 DoSendMessage(*request); | |
164 } | |
165 } | |
166 | |
167 void PresentationDispatcher::sendArrayBuffer( | |
168 const blink::WebString& presentationUrl, | |
169 const blink::WebString& presentationId, | |
170 const uint8* data, | |
171 size_t length) { | |
172 DCHECK(data); | |
173 if (length > kMaxPresentationSessionMessageSize) { | |
174 // TODO(crbug.com/459008): Same as in sendString(). | |
175 LOG(WARNING) << "data size exceeded limit!"; | |
176 return; | |
177 } | |
178 | |
179 const std::vector<uint8> vector(data, data + length); | |
180 presentation::SessionMessage* session_message = | |
181 new presentation::SessionMessage(); | |
182 session_message->presentation_url = presentationUrl.utf8(); | |
183 session_message->presentation_id = presentationId.utf8(); | |
184 session_message->type = presentation::PresentationMessageType:: | |
185 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; | |
186 session_message->data = mojo::Array<uint8>::From(vector); | |
187 | |
188 message_request_queue_.push(make_linked_ptr(session_message)); | |
189 // Start processing request if only one in the queue. | |
190 if (message_request_queue_.size() == 1) { | |
191 const linked_ptr<presentation::SessionMessage>& request = | |
192 message_request_queue_.front(); | |
193 DoSendMessage(*request); | |
194 } | |
195 } | |
196 | |
197 void PresentationDispatcher::DoSendMessage( | |
198 const presentation::SessionMessage& session_message) { | |
199 ConnectToPresentationServiceIfNeeded(); | |
200 | |
201 presentation::SessionMessagePtr message_request( | |
202 presentation::SessionMessage::New()); | |
203 message_request->presentation_url = session_message.presentation_url; | |
204 message_request->presentation_id = session_message.presentation_id; | |
205 message_request->type = session_message.type; | |
206 if (session_message.type == presentation::PresentationMessageType:: | |
207 PRESENTATION_MESSAGE_TYPE_TEXT) { | |
208 message_request->message = session_message.message; | |
209 } else if (session_message.type == presentation::PresentationMessageType:: | |
210 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER) { | |
211 message_request->data = mojo::Array<uint8>::From( | |
212 session_message.data.storage()); | |
213 } | |
214 | |
215 presentation_service_->SendMessage( | |
216 message_request.Pass(), | |
217 base::Bind(&PresentationDispatcher::HandleSendMessageRequests, | |
218 base::Unretained(this))); | |
219 } | |
220 | |
221 void PresentationDispatcher::HandleSendMessageRequests(bool success) { | |
222 // In normal cases, message_request_queue_ should not be empty at this point | |
223 // of time, but when DidCommitProvisionalLoad() is invoked before receiving | |
224 // the callback for previous send mojo call, queue would have been emptied. | |
225 if (message_request_queue_.empty()) | |
226 return; | |
227 | |
228 if (!success) { | |
229 // PresentationServiceImpl is informing that Frame has been detached or | |
230 // navigated away. Invalidate all pending requests. | |
231 MessageRequestQueue empty; | |
232 std::swap(message_request_queue_, empty); | |
233 return; | |
234 } | |
235 | |
236 message_request_queue_.pop(); | |
237 if (!message_request_queue_.empty()) { | |
238 const linked_ptr<presentation::SessionMessage>& request = | |
239 message_request_queue_.front(); | |
240 DoSendMessage(*request); | |
241 } | |
242 } | |
243 | |
135 void PresentationDispatcher::closeSession( | 244 void PresentationDispatcher::closeSession( |
136 const blink::WebString& presentationUrl, | 245 const blink::WebString& presentationUrl, |
137 const blink::WebString& presentationId) { | 246 const blink::WebString& presentationId) { |
138 ConnectToPresentationServiceIfNeeded(); | 247 ConnectToPresentationServiceIfNeeded(); |
139 | 248 |
140 presentation_service_->CloseSession( | 249 presentation_service_->CloseSession( |
141 presentationUrl.utf8(), | 250 presentationUrl.utf8(), |
142 presentationId.utf8()); | 251 presentationId.utf8()); |
143 } | 252 } |
144 | 253 |
145 void PresentationDispatcher::DidChangeDefaultPresentation() { | 254 void PresentationDispatcher::DidChangeDefaultPresentation() { |
146 GURL presentation_url(GetPresentationURLFromFrame(render_frame())); | 255 GURL presentation_url(GetPresentationURLFromFrame(render_frame())); |
147 | 256 |
148 ConnectToPresentationServiceIfNeeded(); | 257 ConnectToPresentationServiceIfNeeded(); |
149 presentation_service_->SetDefaultPresentationURL( | 258 presentation_service_->SetDefaultPresentationURL( |
150 presentation_url.spec(), mojo::String()); | 259 presentation_url.spec(), mojo::String()); |
151 } | 260 } |
152 | 261 |
262 void PresentationDispatcher::DidCommitProvisionalLoad( | |
263 bool is_new_navigation, | |
264 bool is_same_page_navigation) { | |
265 blink::WebFrame* frame = render_frame()->GetWebFrame(); | |
266 // If not top-level navigation. | |
267 if (frame->parent() || is_same_page_navigation) | |
268 return; | |
269 | |
270 // Remove all pending send message requests. | |
271 MessageRequestQueue empty; | |
272 std::swap(message_request_queue_, empty); | |
273 } | |
274 | |
153 void PresentationDispatcher::OnScreenAvailabilityChanged( | 275 void PresentationDispatcher::OnScreenAvailabilityChanged( |
154 const std::string& presentation_url, bool available) { | 276 const std::string& presentation_url, bool available) { |
155 if (!controller_) | 277 if (!controller_) |
156 return; | 278 return; |
157 | 279 |
158 // Reset the callback to get the next event. | 280 // Reset the callback to get the next event. |
159 DoUpdateAvailableChangeWatched(presentation_url, | 281 DoUpdateAvailableChangeWatched(presentation_url, |
160 controller_->isAvailableChangeWatched()); | 282 controller_->isAvailableChangeWatched()); |
161 | 283 |
162 controller_->didChangeAvailability(available); | 284 controller_->didChangeAvailability(available); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 presentation_service_->ListenForSessionStateChange(base::Bind( | 371 presentation_service_->ListenForSessionStateChange(base::Bind( |
250 &PresentationDispatcher::OnSessionStateChange, | 372 &PresentationDispatcher::OnSessionStateChange, |
251 base::Unretained(this))); | 373 base::Unretained(this))); |
252 presentation_service_->ListenForSessionMessages( | 374 presentation_service_->ListenForSessionMessages( |
253 base::Bind(&PresentationDispatcher::OnSessionMessagesReceived, | 375 base::Bind(&PresentationDispatcher::OnSessionMessagesReceived, |
254 base::Unretained(this))); | 376 base::Unretained(this))); |
255 */ | 377 */ |
256 } | 378 } |
257 | 379 |
258 } // namespace content | 380 } // namespace content |
OLD | NEW |