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/browser/presentation/presentation_service_impl.h" | 5 #include "content/browser/presentation/presentation_service_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "content/browser/presentation/presentation_type_converters.h" | 10 #include "content/browser/presentation/presentation_type_converters.h" |
11 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
12 #include "content/public/browser/navigation_details.h" | 12 #include "content/public/browser/navigation_details.h" |
13 #include "content/public/browser/presentation_session_message.h" | 13 #include "content/public/browser/presentation_session_message.h" |
14 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
15 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/content_client.h" | 17 #include "content/public/common/content_client.h" |
18 #include "content/public/common/frame_navigate_params.h" | 18 #include "content/public/common/frame_navigate_params.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 static const size_t kMaxPresentationSessionMessageSize = 64 * 1024; // 64 KB. | |
23 | |
22 // The return value takes ownership of the contents of |input|. | 24 // The return value takes ownership of the contents of |input|. |
23 presentation::SessionMessagePtr ToMojoSessionMessage( | 25 presentation::SessionMessagePtr ToMojoSessionMessage( |
24 content::PresentationSessionMessage* input) { | 26 content::PresentationSessionMessage* input) { |
25 presentation::SessionMessagePtr output(presentation::SessionMessage::New()); | 27 presentation::SessionMessagePtr output(presentation::SessionMessage::New()); |
26 output->presentation_url.Swap(&input->presentation_url); | 28 output->presentation_url.Swap(&input->presentation_url); |
27 output->presentation_id.Swap(&input->presentation_id); | 29 output->presentation_id.Swap(&input->presentation_id); |
28 if (input->is_binary()) { | 30 if (input->is_binary()) { |
29 // binary data | 31 // binary data |
30 output->type = presentation::PresentationMessageType:: | 32 output->type = presentation::PresentationMessageType:: |
31 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; | 33 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER; |
32 output->data.Swap(input->data.get()); | 34 output->data.Swap(input->data.get()); |
33 } else { | 35 } else { |
34 // string message | 36 // string message |
35 output->type = | 37 output->type = |
36 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; | 38 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT; |
37 output->message.Swap(input->message.get()); | 39 output->message.Swap(input->message.get()); |
38 } | 40 } |
39 return output.Pass(); | 41 return output.Pass(); |
40 } | 42 } |
41 | 43 |
42 } // namespace | 44 scoped_ptr<content::PresentationSessionMessage> GetPresentationSessionMessage( |
45 presentation::SessionMessagePtr input) { | |
46 DCHECK(!input.is_null()); | |
47 scoped_ptr<content::PresentationSessionMessage> output; | |
48 if (input->type == presentation::PresentationMessageType:: | |
49 PRESENTATION_MESSAGE_TYPE_TEXT) { | |
50 DCHECK(!input->message.is_null()); | |
51 DCHECK_LE(input->message.size(), | |
mark a. foltz
2015/05/07 01:34:30
Don't we want to validate this in all builds and r
USE s.singapati at gmail.com
2015/05/07 14:08:51
Done. Now it returns null PresentationSessionMessa
| |
52 kMaxPresentationSessionMessageSize); | |
53 DCHECK(input->data.is_null()); // And size is 0. | |
54 output = content::PresentationSessionMessage::CreateStringMessage( | |
55 input->presentation_url, | |
56 input->presentation_id, | |
57 scoped_ptr<std::string>(new std::string(input->message))); | |
mark a. foltz
2015/05/07 01:34:30
Instead of allocating a new string, pass an empty
USE s.singapati at gmail.com
2015/05/07 14:08:51
swap() can not take input->message.get() which is
whywhat
2015/05/07 14:22:01
Swap is reflective so you can use input->message.S
| |
58 | |
59 } else if (input->type == presentation::PresentationMessageType:: | |
60 PRESENTATION_MESSAGE_TYPE_ARRAY_BUFFER) { | |
61 DCHECK(!input->data.is_null()); | |
62 DCHECK_LE(input->data.size(), | |
63 kMaxPresentationSessionMessageSize); | |
64 DCHECK(input->message.is_null()); // And size is 0. | |
65 output = content::PresentationSessionMessage::CreateBinaryMessage( | |
66 input->presentation_url, | |
67 input->presentation_id, | |
68 scoped_ptr<std::vector<uint8_t>>( | |
69 new std::vector<uint8_t>(input->data))); | |
mark a. foltz
2015/05/07 01:34:30
Same comment as above.
USE s.singapati at gmail.com
2015/05/07 14:08:51
swap() can not take input->data.storage() which is
whywhat
2015/05/07 14:22:01
Ditto.
| |
70 } | |
71 | |
72 return output.Pass(); | |
73 } | |
74 | |
75 } // namespace | |
43 | 76 |
44 namespace content { | 77 namespace content { |
45 | 78 |
46 PresentationServiceImpl::PresentationServiceImpl( | 79 PresentationServiceImpl::PresentationServiceImpl( |
47 RenderFrameHost* render_frame_host, | 80 RenderFrameHost* render_frame_host, |
48 WebContents* web_contents, | 81 WebContents* web_contents, |
49 PresentationServiceDelegate* delegate) | 82 PresentationServiceDelegate* delegate) |
50 : WebContentsObserver(web_contents), | 83 : WebContentsObserver(web_contents), |
51 delegate_(delegate), | 84 delegate_(delegate), |
52 is_start_session_pending_(false), | 85 is_start_session_pending_(false), |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 | 364 |
332 // Remove listener for old default presentation URL. | 365 // Remove listener for old default presentation URL. |
333 delegate_->RemoveScreenAvailabilityListener( | 366 delegate_->RemoveScreenAvailabilityListener( |
334 render_process_id_, | 367 render_process_id_, |
335 render_frame_id_, | 368 render_frame_id_, |
336 old_it->second.get()); | 369 old_it->second.get()); |
337 availability_contexts_.erase(old_it); | 370 availability_contexts_.erase(old_it); |
338 DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); | 371 DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); |
339 } | 372 } |
340 | 373 |
374 | |
375 void PresentationServiceImpl::SendMessage( | |
376 presentation::SessionMessagePtr session_message, | |
377 const SendMessageMojoCallback& callback) { | |
378 DVLOG(2) << "SendMessage"; | |
379 DCHECK(!session_message.is_null()); | |
380 // send_message_cb_ptr_ should be null by now, otherwise resetting of | |
381 // send_message_cb_ptr_ with new callback will drop the old callback. | |
382 if (!delegate_ || send_message_cb_ptr_) { | |
383 callback.Run(false); | |
384 return; | |
385 } | |
386 | |
387 send_message_cb_ptr_.reset(new SendMessageMojoCallback(callback)); | |
388 delegate_->SendMessage( | |
389 render_process_id_, | |
390 render_frame_id_, | |
391 GetPresentationSessionMessage(session_message.Pass()), | |
392 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, | |
393 weak_factory_.GetWeakPtr())); | |
394 } | |
395 | |
396 void PresentationServiceImpl::OnSendMessageCallback() { | |
397 // It is possible that Reset() is invoked before receiving this callback. | |
398 // So, always check send_message_cb_ptr_ for non-null. | |
399 if (send_message_cb_ptr_) { | |
400 send_message_cb_ptr_->Run(true); | |
401 send_message_cb_ptr_.reset(); | |
402 } | |
403 } | |
404 | |
341 void PresentationServiceImpl::CloseSession( | 405 void PresentationServiceImpl::CloseSession( |
342 const mojo::String& presentation_url, | 406 const mojo::String& presentation_url, |
343 const mojo::String& presentation_id) { | 407 const mojo::String& presentation_id) { |
344 NOTIMPLEMENTED(); | 408 NOTIMPLEMENTED(); |
345 } | 409 } |
346 | 410 |
347 void PresentationServiceImpl::ListenForSessionStateChange( | 411 void PresentationServiceImpl::ListenForSessionStateChange( |
348 const SessionStateCallback& callback) { | 412 const SessionStateCallback& callback) { |
349 NOTIMPLEMENTED(); | 413 NOTIMPLEMENTED(); |
350 } | 414 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 default_presentation_id_.clear(); | 505 default_presentation_id_.clear(); |
442 availability_contexts_.clear(); | 506 availability_contexts_.clear(); |
443 queued_start_session_requests_.clear(); | 507 queued_start_session_requests_.clear(); |
444 FlushNewSessionCallbacks(); | 508 FlushNewSessionCallbacks(); |
445 default_session_start_context_.reset(); | 509 default_session_start_context_.reset(); |
446 if (on_session_messages_callback_.get()) { | 510 if (on_session_messages_callback_.get()) { |
447 on_session_messages_callback_->Run( | 511 on_session_messages_callback_->Run( |
448 mojo::Array<presentation::SessionMessagePtr>()); | 512 mojo::Array<presentation::SessionMessagePtr>()); |
449 on_session_messages_callback_.reset(); | 513 on_session_messages_callback_.reset(); |
450 } | 514 } |
515 if (send_message_cb_ptr_) { | |
516 // Run the callback with false, indicating the renderer to stop sending | |
517 // the requests and invalidate all pending requests. | |
518 send_message_cb_ptr_->Run(false); | |
519 send_message_cb_ptr_.reset(); | |
520 } | |
451 } | 521 } |
452 | 522 |
453 // static | 523 // static |
454 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError( | 524 void PresentationServiceImpl::InvokeNewSessionMojoCallbackWithError( |
455 const NewSessionMojoCallback& callback) { | 525 const NewSessionMojoCallback& callback) { |
456 callback.Run( | 526 callback.Run( |
457 presentation::PresentationSessionInfoPtr(), | 527 presentation::PresentationSessionInfoPtr(), |
458 presentation::PresentationError::From( | 528 presentation::PresentationError::From( |
459 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); | 529 PresentationError(PRESENTATION_ERROR_UNKNOWN, "Internal error"))); |
460 } | 530 } |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
590 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { | 660 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { |
591 ScopedVector<DefaultSessionMojoCallback> callbacks; | 661 ScopedVector<DefaultSessionMojoCallback> callbacks; |
592 callbacks.swap(callbacks_); | 662 callbacks.swap(callbacks_); |
593 for (const auto& callback : callbacks) | 663 for (const auto& callback : callbacks) |
594 callback->Run(presentation::PresentationSessionInfoPtr()); | 664 callback->Run(presentation::PresentationSessionInfoPtr()); |
595 session_.reset(); | 665 session_.reset(); |
596 } | 666 } |
597 | 667 |
598 } // namespace content | 668 } // namespace content |
599 | 669 |
OLD | NEW |