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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "content/browser/presentation/presentation_type_converters.h" | 14 #include "content/browser/presentation/presentation_type_converters.h" |
15 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
16 #include "content/public/browser/navigation_details.h" | 16 #include "content/public/browser/navigation_details.h" |
17 #include "content/public/browser/presentation_session_message.h" | 17 #include "content/public/browser/presentation_connection_message.h" |
18 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
19 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "content/public/common/content_client.h" | 21 #include "content/public/common/content_client.h" |
22 #include "content/public/common/frame_navigate_params.h" | 22 #include "content/public/common/frame_navigate_params.h" |
23 #include "content/public/common/presentation_constants.h" | 23 #include "content/public/common/presentation_constants.h" |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 const int kInvalidRequestSessionId = -1; | 29 const int kInvalidRequestSessionId = -1; |
30 | 30 |
31 int GetNextRequestSessionId() { | 31 int GetNextRequestSessionId() { |
32 static int next_request_session_id = 0; | 32 static int next_request_session_id = 0; |
33 return ++next_request_session_id; | 33 return ++next_request_session_id; |
34 } | 34 } |
35 | 35 |
36 // Converts a PresentationSessionMessage |input| to a SessionMessage. | 36 // Converts a PresentationConnectionMessage |input| to a ConnectionMessage. |
37 // |input|: The message to convert. | 37 // |input|: The message to convert. |
38 // |pass_ownership|: If true, function may reuse strings or buffers from | 38 // |pass_ownership|: If true, function may reuse strings or buffers from |
39 // |input| without copying. |input| can be freely modified. | 39 // |input| without copying. |input| can be freely modified. |
40 blink::mojom::SessionMessagePtr ToMojoSessionMessage( | 40 blink::mojom::ConnectionMessagePtr ToMojoConnectionMessage( |
41 content::PresentationSessionMessage* input, | 41 content::PresentationConnectionMessage* input, |
42 bool pass_ownership) { | 42 bool pass_ownership) { |
43 DCHECK(input); | 43 DCHECK(input); |
44 blink::mojom::SessionMessagePtr output(blink::mojom::SessionMessage::New()); | 44 blink::mojom::ConnectionMessagePtr output( |
| 45 blink::mojom::ConnectionMessage::New()); |
45 if (input->is_binary()) { | 46 if (input->is_binary()) { |
46 // binary data | 47 // binary data |
47 DCHECK(input->data); | 48 DCHECK(input->data); |
48 output->type = blink::mojom::PresentationMessageType::ARRAY_BUFFER; | 49 output->type = blink::mojom::PresentationMessageType::BINARY; |
49 if (pass_ownership) { | 50 if (pass_ownership) { |
50 output->data = std::move(*(input->data)); | 51 output->data = std::move(*(input->data)); |
51 } else { | 52 } else { |
52 output->data = *(input->data); | 53 output->data = *(input->data); |
53 } | 54 } |
54 } else { | 55 } else { |
55 // string message | 56 // string message |
56 output->type = blink::mojom::PresentationMessageType::TEXT; | 57 output->type = blink::mojom::PresentationMessageType::TEXT; |
57 if (pass_ownership) { | 58 if (pass_ownership) { |
58 output->message = std::move(input->message); | 59 output->message = std::move(input->message); |
59 } else { | 60 } else { |
60 output->message = input->message; | 61 output->message = input->message; |
61 } | 62 } |
62 } | 63 } |
63 return output; | 64 return output; |
64 } | 65 } |
65 | 66 |
66 std::unique_ptr<PresentationSessionMessage> GetPresentationSessionMessage( | 67 std::unique_ptr<PresentationConnectionMessage> GetPresentationConnectionMessage( |
67 blink::mojom::SessionMessagePtr input) { | 68 blink::mojom::ConnectionMessagePtr input) { |
68 std::unique_ptr<content::PresentationSessionMessage> output; | 69 std::unique_ptr<content::PresentationConnectionMessage> output; |
69 if (input.is_null()) | 70 if (input.is_null()) |
70 return output; | 71 return output; |
71 | 72 |
72 switch (input->type) { | 73 switch (input->type) { |
73 case blink::mojom::PresentationMessageType::TEXT: { | 74 case blink::mojom::PresentationMessageType::TEXT: { |
74 // Return nullptr PresentationSessionMessage if invalid (unset |message|, | 75 // Return nullptr PresentationConnectionMessage if invalid (unset |
| 76 // |message|, |
75 // set |data|, or size too large). | 77 // set |data|, or size too large). |
76 if (input->data || !input->message || | 78 if (input->data || !input->message || |
77 input->message->size() > content::kMaxPresentationSessionMessageSize) | 79 input->message->size() > |
| 80 content::kMaxPresentationConnectionMessageSize) |
78 return output; | 81 return output; |
79 | 82 |
80 output.reset( | 83 output.reset( |
81 new PresentationSessionMessage(PresentationMessageType::TEXT)); | 84 new PresentationConnectionMessage(PresentationMessageType::TEXT)); |
82 output->message = std::move(input->message.value()); | 85 output->message = std::move(input->message.value()); |
83 return output; | 86 return output; |
84 } | 87 } |
85 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: { | 88 case blink::mojom::PresentationMessageType::BINARY: { |
86 // Return nullptr PresentationSessionMessage if invalid (unset |data|, set | 89 // Return nullptr PresentationConnectionMessage if invalid (unset |data|, |
| 90 // set |
87 // |message|, or size too large). | 91 // |message|, or size too large). |
88 if (!input->data || input->message || | 92 if (!input->data || input->message || |
89 input->data->size() > content::kMaxPresentationSessionMessageSize) | 93 input->data->size() > content::kMaxPresentationConnectionMessageSize) |
90 return output; | 94 return output; |
91 | 95 |
92 output.reset(new PresentationSessionMessage( | 96 output.reset( |
93 PresentationMessageType::ARRAY_BUFFER)); | 97 new PresentationConnectionMessage(PresentationMessageType::BINARY)); |
94 output->data.reset( | 98 output->data.reset( |
95 new std::vector<uint8_t>(std::move(input->data.value()))); | 99 new std::vector<uint8_t>(std::move(input->data.value()))); |
96 return output; | 100 return output; |
97 } | |
98 case blink::mojom::PresentationMessageType::BLOB: { | |
99 // Return nullptr PresentationSessionMessage if invalid (unset |data|, set | |
100 // |message|, or size too large). | |
101 if (!input->data || input->message || | |
102 input->data->size() > content::kMaxPresentationSessionMessageSize) | |
103 return output; | |
104 | |
105 output.reset( | |
106 new PresentationSessionMessage(PresentationMessageType::BLOB)); | |
107 output->data.reset( | |
108 new std::vector<uint8_t>(std::move(input->data.value()))); | |
109 return output; | |
110 } | 101 } |
111 } | 102 } |
112 | 103 |
113 NOTREACHED() << "Invalid presentation message type " << input->type; | 104 NOTREACHED() << "Invalid presentation message type " << input->type; |
114 return output; | 105 return output; |
115 } | 106 } |
116 | 107 |
117 void InvokeNewSessionCallbackWithError( | 108 void InvokeNewSessionCallbackWithError( |
118 const PresentationServiceImpl::NewSessionCallback& callback) { | 109 const PresentationServiceImpl::NewSessionCallback& callback) { |
119 callback.Run(blink::mojom::PresentationSessionInfoPtr(), | 110 callback.Run(blink::mojom::PresentationSessionInfoPtr(), |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 if (default_presentation_urls_ == presentation_urls) | 358 if (default_presentation_urls_ == presentation_urls) |
368 return; | 359 return; |
369 | 360 |
370 default_presentation_urls_ = presentation_urls; | 361 default_presentation_urls_ = presentation_urls; |
371 delegate_->SetDefaultPresentationUrls( | 362 delegate_->SetDefaultPresentationUrls( |
372 render_process_id_, render_frame_id_, presentation_urls, | 363 render_process_id_, render_frame_id_, presentation_urls, |
373 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, | 364 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, |
374 weak_factory_.GetWeakPtr())); | 365 weak_factory_.GetWeakPtr())); |
375 } | 366 } |
376 | 367 |
377 void PresentationServiceImpl::SendSessionMessage( | 368 void PresentationServiceImpl::SendConnectionMessage( |
378 blink::mojom::PresentationSessionInfoPtr session, | 369 blink::mojom::PresentationSessionInfoPtr session, |
379 blink::mojom::SessionMessagePtr session_message, | 370 blink::mojom::ConnectionMessagePtr connection_message, |
380 const SendSessionMessageCallback& callback) { | 371 const SendConnectionMessageCallback& callback) { |
381 DVLOG(2) << "SendSessionMessage"; | 372 DVLOG(2) << "SendConnectionMessage"; |
382 DCHECK(!session_message.is_null()); | 373 DCHECK(!connection_message.is_null()); |
383 // send_message_callback_ should be null by now, otherwise resetting of | 374 // send_message_callback_ should be null by now, otherwise resetting of |
384 // send_message_callback_ with new callback will drop the old callback. | 375 // send_message_callback_ with new callback will drop the old callback. |
385 if (!delegate_ || send_message_callback_) { | 376 if (!delegate_ || send_message_callback_) { |
386 callback.Run(false); | 377 callback.Run(false); |
387 return; | 378 return; |
388 } | 379 } |
389 | 380 |
390 send_message_callback_.reset(new SendSessionMessageCallback(callback)); | 381 send_message_callback_.reset(new SendConnectionMessageCallback(callback)); |
391 delegate_->SendMessage( | 382 delegate_->SendMessage( |
392 render_process_id_, render_frame_id_, | 383 render_process_id_, render_frame_id_, |
393 session.To<PresentationSessionInfo>(), | 384 session.To<PresentationSessionInfo>(), |
394 GetPresentationSessionMessage(std::move(session_message)), | 385 GetPresentationConnectionMessage(std::move(connection_message)), |
395 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, | 386 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, |
396 weak_factory_.GetWeakPtr())); | 387 weak_factory_.GetWeakPtr())); |
397 } | 388 } |
398 | 389 |
399 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { | 390 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { |
400 // It is possible that Reset() is invoked before receiving this callback. | 391 // It is possible that Reset() is invoked before receiving this callback. |
401 // So, always check send_message_callback_ for non-null. | 392 // So, always check send_message_callback_ for non-null. |
402 if (send_message_callback_) { | 393 if (send_message_callback_) { |
403 send_message_callback_->Run(sent); | 394 send_message_callback_->Run(sent); |
404 send_message_callback_.reset(); | 395 send_message_callback_.reset(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 | 433 |
443 bool PresentationServiceImpl::FrameMatches( | 434 bool PresentationServiceImpl::FrameMatches( |
444 content::RenderFrameHost* render_frame_host) const { | 435 content::RenderFrameHost* render_frame_host) const { |
445 if (!render_frame_host) | 436 if (!render_frame_host) |
446 return false; | 437 return false; |
447 | 438 |
448 return render_frame_host->GetProcess()->GetID() == render_process_id_ && | 439 return render_frame_host->GetProcess()->GetID() == render_process_id_ && |
449 render_frame_host->GetRoutingID() == render_frame_id_; | 440 render_frame_host->GetRoutingID() == render_frame_id_; |
450 } | 441 } |
451 | 442 |
452 void PresentationServiceImpl::ListenForSessionMessages( | 443 void PresentationServiceImpl::ListenForConnectionMessages( |
453 blink::mojom::PresentationSessionInfoPtr session) { | 444 blink::mojom::PresentationSessionInfoPtr session) { |
454 DVLOG(2) << "ListenForSessionMessages"; | 445 DVLOG(2) << "ListenForConnectionMessages"; |
455 if (!delegate_) | 446 if (!delegate_) |
456 return; | 447 return; |
457 | 448 |
458 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>()); | 449 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>()); |
459 delegate_->ListenForSessionMessages( | 450 delegate_->ListenForConnectionMessages( |
460 render_process_id_, render_frame_id_, session_info, | 451 render_process_id_, render_frame_id_, session_info, |
461 base::Bind(&PresentationServiceImpl::OnSessionMessages, | 452 base::Bind(&PresentationServiceImpl::OnConnectionMessages, |
462 weak_factory_.GetWeakPtr(), session_info)); | 453 weak_factory_.GetWeakPtr(), session_info)); |
463 } | 454 } |
464 | 455 |
465 void PresentationServiceImpl::OnSessionMessages( | 456 void PresentationServiceImpl::OnConnectionMessages( |
466 const PresentationSessionInfo& session, | 457 const PresentationSessionInfo& session, |
467 const ScopedVector<PresentationSessionMessage>& messages, | 458 const ScopedVector<PresentationConnectionMessage>& messages, |
468 bool pass_ownership) { | 459 bool pass_ownership) { |
469 DCHECK(client_); | 460 DCHECK(client_); |
470 | 461 |
471 DVLOG(2) << "OnSessionMessages"; | 462 DVLOG(2) << "OnConnectionMessages"; |
472 std::vector<blink::mojom::SessionMessagePtr> mojo_messages(messages.size()); | 463 std::vector<blink::mojom::ConnectionMessagePtr> mojo_messages( |
| 464 messages.size()); |
473 std::transform(messages.begin(), messages.end(), mojo_messages.begin(), | 465 std::transform(messages.begin(), messages.end(), mojo_messages.begin(), |
474 [pass_ownership](PresentationSessionMessage* message) { | 466 [pass_ownership](PresentationConnectionMessage* message) { |
475 return ToMojoSessionMessage(message, pass_ownership); | 467 return ToMojoConnectionMessage(message, pass_ownership); |
476 }); | 468 }); |
477 | 469 |
478 client_->OnSessionMessagesReceived( | 470 client_->OnConnectionMessagesReceived( |
479 blink::mojom::PresentationSessionInfo::From(session), | 471 blink::mojom::PresentationSessionInfo::From(session), |
480 std::move(mojo_messages)); | 472 std::move(mojo_messages)); |
481 } | 473 } |
482 | 474 |
483 void PresentationServiceImpl::DidNavigateAnyFrame( | 475 void PresentationServiceImpl::DidNavigateAnyFrame( |
484 content::RenderFrameHost* render_frame_host, | 476 content::RenderFrameHost* render_frame_host, |
485 const content::LoadCommittedDetails& details, | 477 const content::LoadCommittedDetails& details, |
486 const content::FrameNavigateParams& params) { | 478 const content::FrameNavigateParams& params) { |
487 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; | 479 DVLOG(2) << "PresentationServiceImpl::DidNavigateAnyFrame"; |
488 if (!FrameMatches(render_frame_host)) | 480 if (!FrameMatches(render_frame_host)) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 | 522 |
531 default_presentation_urls_.clear(); | 523 default_presentation_urls_.clear(); |
532 | 524 |
533 screen_availability_listeners_.clear(); | 525 screen_availability_listeners_.clear(); |
534 | 526 |
535 start_session_request_id_ = kInvalidRequestSessionId; | 527 start_session_request_id_ = kInvalidRequestSessionId; |
536 pending_start_session_cb_.reset(); | 528 pending_start_session_cb_.reset(); |
537 | 529 |
538 pending_join_session_cbs_.clear(); | 530 pending_join_session_cbs_.clear(); |
539 | 531 |
540 if (on_session_messages_callback_.get()) { | 532 if (on_connection_messages_callback_.get()) { |
541 on_session_messages_callback_->Run( | 533 on_connection_messages_callback_->Run( |
542 mojo::Array<blink::mojom::SessionMessagePtr>()); | 534 mojo::Array<blink::mojom::ConnectionMessagePtr>()); |
543 on_session_messages_callback_.reset(); | 535 on_connection_messages_callback_.reset(); |
544 } | 536 } |
545 | 537 |
546 if (send_message_callback_) { | 538 if (send_message_callback_) { |
547 // Run the callback with false, indicating the renderer to stop sending | 539 // Run the callback with false, indicating the renderer to stop sending |
548 // the requests and invalidate all pending requests. | 540 // the requests and invalidate all pending requests. |
549 send_message_callback_->Run(false); | 541 send_message_callback_->Run(false); |
550 send_message_callback_.reset(); | 542 send_message_callback_.reset(); |
551 } | 543 } |
552 } | 544 } |
553 | 545 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 | 597 |
606 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( | 598 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( |
607 blink::mojom::PresentationSessionInfoPtr session, | 599 blink::mojom::PresentationSessionInfoPtr session, |
608 blink::mojom::PresentationErrorPtr error) { | 600 blink::mojom::PresentationErrorPtr error) { |
609 DCHECK(!callback_.is_null()); | 601 DCHECK(!callback_.is_null()); |
610 callback_.Run(std::move(session), std::move(error)); | 602 callback_.Run(std::move(session), std::move(error)); |
611 callback_.Reset(); | 603 callback_.Reset(); |
612 } | 604 } |
613 | 605 |
614 } // namespace content | 606 } // namespace content |
OLD | NEW |