Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: content/renderer/presentation/presentation_dispatcher.cc

Issue 2613153003: [Presentation API] Replaces type converters with typemaps (Closed)
Patch Set: Add more OWNERS foo Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
15 #include "content/public/common/presentation_constants.h" 15 #include "content/public/common/presentation_constants.h"
16 #include "content/public/renderer/render_frame.h" 16 #include "content/public/renderer/render_frame.h"
17 #include "mojo/public/cpp/bindings/type_converter.h"
18 #include "services/service_manager/public/cpp/interface_provider.h" 17 #include "services/service_manager/public/cpp/interface_provider.h"
19 #include "third_party/WebKit/public/platform/WebString.h" 18 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebURL.h" 19 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/platform/WebVector.h" 20 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nAvailabilityObserver.h" 21 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nAvailabilityObserver.h"
23 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h" 22 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nController.h"
24 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h" 23 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nError.h"
25 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nReceiver.h" 24 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nReceiver.h"
26 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nSessionInfo.h" 25 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio nSessionInfo.h"
27 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m ojom.h" 26 #include "third_party/WebKit/public/platform/modules/presentation/presentation.m ojom.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h" 27 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "url/gurl.h" 28 #include "url/gurl.h"
30 29
31 namespace mojo { 30 namespace content {
32
33 // Temporary type converter since Presentation API has not been Onion Soup-ed.
34 template <>
35 struct TypeConverter<blink::WebPresentationSessionInfo,
36 blink::mojom::PresentationSessionInfoPtr> {
37 static blink::WebPresentationSessionInfo Convert(
38 const blink::mojom::PresentationSessionInfoPtr& input) {
39 return blink::WebPresentationSessionInfo(
40 blink::WebURL(input->url), blink::WebString::fromUTF8(input->id));
41 }
42 };
43
44 } // namespace mojo
45 31
46 namespace { 32 namespace {
47 33
48 blink::WebPresentationError::ErrorType GetWebPresentationErrorTypeFromMojo( 34 blink::WebPresentationError::ErrorType GetWebPresentationErrorType(
49 blink::mojom::PresentationErrorType mojoErrorType) { 35 PresentationErrorType errorType) {
50 switch (mojoErrorType) { 36 switch (errorType) {
51 case blink::mojom::PresentationErrorType::NO_AVAILABLE_SCREENS: 37 case PresentationErrorType::PRESENTATION_ERROR_NO_AVAILABLE_SCREENS:
52 return blink::WebPresentationError::ErrorTypeNoAvailableScreens; 38 return blink::WebPresentationError::ErrorTypeNoAvailableScreens;
53 case blink::mojom::PresentationErrorType::SESSION_REQUEST_CANCELLED: 39 case PresentationErrorType::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED:
54 return blink::WebPresentationError::ErrorTypeSessionRequestCancelled; 40 return blink::WebPresentationError::ErrorTypeSessionRequestCancelled;
55 case blink::mojom::PresentationErrorType::NO_PRESENTATION_FOUND: 41 case PresentationErrorType::PRESENTATION_ERROR_NO_PRESENTATION_FOUND:
56 return blink::WebPresentationError::ErrorTypeNoPresentationFound; 42 return blink::WebPresentationError::ErrorTypeNoPresentationFound;
57 case blink::mojom::PresentationErrorType::UNKNOWN: 43 case PresentationErrorType::PRESENTATION_ERROR_UNKNOWN:
58 default: 44 default:
59 return blink::WebPresentationError::ErrorTypeUnknown; 45 return blink::WebPresentationError::ErrorTypeUnknown;
60 } 46 }
61 } 47 }
62 48
63 blink::WebPresentationConnectionState GetWebPresentationConnectionStateFromMojo( 49 blink::WebPresentationConnectionState GetWebPresentationConnectionState(
64 blink::mojom::PresentationConnectionState mojoSessionState) { 50 PresentationConnectionState sessionState) {
65 switch (mojoSessionState) { 51 switch (sessionState) {
66 case blink::mojom::PresentationConnectionState::CONNECTING: 52 case PresentationConnectionState::PRESENTATION_CONNECTION_STATE_CONNECTING:
67 return blink::WebPresentationConnectionState::Connecting; 53 return blink::WebPresentationConnectionState::Connecting;
68 case blink::mojom::PresentationConnectionState::CONNECTED: 54 case PresentationConnectionState::PRESENTATION_CONNECTION_STATE_CONNECTED:
69 return blink::WebPresentationConnectionState::Connected; 55 return blink::WebPresentationConnectionState::Connected;
70 case blink::mojom::PresentationConnectionState::CLOSED: 56 case PresentationConnectionState::PRESENTATION_CONNECTION_STATE_CLOSED:
71 return blink::WebPresentationConnectionState::Closed; 57 return blink::WebPresentationConnectionState::Closed;
72 case blink::mojom::PresentationConnectionState::TERMINATED: 58 case PresentationConnectionState::PRESENTATION_CONNECTION_STATE_TERMINATED:
73 return blink::WebPresentationConnectionState::Terminated; 59 return blink::WebPresentationConnectionState::Terminated;
74 default: 60 default:
75 NOTREACHED(); 61 NOTREACHED();
76 return blink::WebPresentationConnectionState::Terminated; 62 return blink::WebPresentationConnectionState::Terminated;
77 } 63 }
78 } 64 }
79 65
80 blink::WebPresentationConnectionCloseReason 66 blink::WebPresentationConnectionCloseReason
81 GetWebPresentationConnectionCloseReasonFromMojo( 67 GetWebPresentationConnectionCloseReason(
82 blink::mojom::PresentationConnectionCloseReason mojoConnectionCloseReason) { 68 PresentationConnectionCloseReason connectionCloseReason) {
83 switch (mojoConnectionCloseReason) { 69 switch (connectionCloseReason) {
84 case blink::mojom::PresentationConnectionCloseReason::CONNECTION_ERROR: 70 case PresentationConnectionCloseReason::
71 PRESENTATION_CONNECTION_CLOSE_REASON_CONNECTION_ERROR:
85 return blink::WebPresentationConnectionCloseReason::Error; 72 return blink::WebPresentationConnectionCloseReason::Error;
86 case blink::mojom::PresentationConnectionCloseReason::CLOSED: 73 case PresentationConnectionCloseReason::
74 PRESENTATION_CONNECTION_CLOSE_REASON_CLOSED:
87 return blink::WebPresentationConnectionCloseReason::Closed; 75 return blink::WebPresentationConnectionCloseReason::Closed;
88 case blink::mojom::PresentationConnectionCloseReason::WENT_AWAY: 76 case PresentationConnectionCloseReason::
77 PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY:
89 return blink::WebPresentationConnectionCloseReason::WentAway; 78 return blink::WebPresentationConnectionCloseReason::WentAway;
90 default: 79 default:
91 NOTREACHED(); 80 NOTREACHED();
92 return blink::WebPresentationConnectionCloseReason::Error; 81 return blink::WebPresentationConnectionCloseReason::Error;
93 } 82 }
94 } 83 }
95 84
96 } // namespace 85 } // namespace
97 86
98 namespace content {
99
100 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) 87 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame)
101 : RenderFrameObserver(render_frame), 88 : RenderFrameObserver(render_frame),
102 controller_(nullptr), 89 controller_(nullptr),
103 binding_(this) { 90 binding_(this) {
104 } 91 }
105 92
106 PresentationDispatcher::~PresentationDispatcher() { 93 PresentationDispatcher::~PresentationDispatcher() {
107 // Controller should be destroyed before the dispatcher when frame is 94 // Controller should be destroyed before the dispatcher when frame is
108 // destroyed. 95 // destroyed.
109 DCHECK(!controller_); 96 DCHECK(!controller_);
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 !iter.IsAtEnd(); iter.Advance()) { 369 !iter.IsAtEnd(); iter.Advance()) {
383 iter.GetCurrentValue()->onError(blink::WebPresentationError( 370 iter.GetCurrentValue()->onError(blink::WebPresentationError(
384 blink::WebPresentationError::ErrorTypeAvailabilityNotSupported, 371 blink::WebPresentationError::ErrorTypeAvailabilityNotSupported,
385 not_supported_error)); 372 not_supported_error));
386 } 373 }
387 status->availability_callbacks.Clear(); 374 status->availability_callbacks.Clear();
388 UpdateListeningState(status); 375 UpdateListeningState(status);
389 } 376 }
390 377
391 void PresentationDispatcher::OnDefaultSessionStarted( 378 void PresentationDispatcher::OnDefaultSessionStarted(
392 blink::mojom::PresentationSessionInfoPtr session_info) { 379 const PresentationSessionInfo& session_info) {
393 if (!controller_) 380 if (!controller_)
394 return; 381 return;
395 382
396 if (!session_info.is_null()) { 383 presentation_service_->ListenForConnectionMessages(session_info);
397 presentation_service_->ListenForConnectionMessages(session_info.Clone()); 384 controller_->didStartDefaultSession(blink::WebPresentationSessionInfo(
398 controller_->didStartDefaultSession( 385 blink::WebURL(session_info.presentation_url),
imcheng 2017/01/24 23:31:40 This one invokes the blink::WebURL constructor exp
mark a. foltz 2017/01/27 22:41:08 WebURL has a GURL constructor, so this was not nec
399 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); 386 blink::WebString::fromUTF8(session_info.presentation_id)));
400 }
401 } 387 }
402 388
403 void PresentationDispatcher::OnSessionCreated( 389 void PresentationDispatcher::OnSessionCreated(
404 std::unique_ptr<blink::WebPresentationConnectionCallback> callback, 390 std::unique_ptr<blink::WebPresentationConnectionCallback> callback,
405 blink::mojom::PresentationSessionInfoPtr session_info, 391 const base::Optional<PresentationSessionInfo>& session_info,
406 blink::mojom::PresentationErrorPtr error) { 392 const base::Optional<PresentationError>& error) {
407 DCHECK(callback); 393 DCHECK(callback);
408 if (!error.is_null()) { 394 if (error) {
409 DCHECK(session_info.is_null()); 395 DCHECK(!session_info);
410 callback->onError(blink::WebPresentationError( 396 callback->onError(blink::WebPresentationError(
411 GetWebPresentationErrorTypeFromMojo(error->error_type), 397 GetWebPresentationErrorType(error->error_type),
412 blink::WebString::fromUTF8(error->message))); 398 blink::WebString::fromUTF8(error->message)));
413 return; 399 return;
414 } 400 }
415 401
416 DCHECK(!session_info.is_null()); 402 DCHECK(session_info);
417 presentation_service_->ListenForConnectionMessages(session_info.Clone()); 403 presentation_service_->ListenForConnectionMessages(session_info.value());
418 callback->onSuccess( 404 callback->onSuccess(blink::WebPresentationSessionInfo(
419 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); 405 session_info->presentation_url,
406 blink::WebString::fromUTF8(session_info->presentation_id)));
420 } 407 }
421 408
422 void PresentationDispatcher::OnReceiverConnectionAvailable( 409 void PresentationDispatcher::OnReceiverConnectionAvailable(
423 blink::mojom::PresentationSessionInfoPtr session_info, 410 const PresentationSessionInfo& session_info,
424 blink::mojom::PresentationConnectionPtr, 411 blink::mojom::PresentationConnectionPtr,
425 blink::mojom::PresentationConnectionRequest) { 412 blink::mojom::PresentationConnectionRequest) {
426 if (receiver_) { 413 if (receiver_) {
427 receiver_->onReceiverConnectionAvailable( 414 receiver_->onReceiverConnectionAvailable(blink::WebPresentationSessionInfo(
428 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); 415 session_info.presentation_url,
416 blink::WebString::fromUTF8(session_info.presentation_id)));
429 } 417 }
430 } 418 }
431 419
432 void PresentationDispatcher::OnConnectionStateChanged( 420 void PresentationDispatcher::OnConnectionStateChanged(
433 blink::mojom::PresentationSessionInfoPtr session_info, 421 const PresentationSessionInfo& session_info,
434 blink::mojom::PresentationConnectionState state) { 422 PresentationConnectionState state) {
435 if (!controller_) 423 if (!controller_)
436 return; 424 return;
437 425
438 controller_->didChangeSessionState( 426 controller_->didChangeSessionState(
439 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info), 427 blink::WebPresentationSessionInfo(
440 GetWebPresentationConnectionStateFromMojo(state)); 428 session_info.presentation_url,
429 blink::WebString::fromUTF8(session_info.presentation_id)),
430 GetWebPresentationConnectionState(state));
441 } 431 }
442 432
443 void PresentationDispatcher::OnConnectionClosed( 433 void PresentationDispatcher::OnConnectionClosed(
444 blink::mojom::PresentationSessionInfoPtr session_info, 434 const PresentationSessionInfo& session_info,
445 blink::mojom::PresentationConnectionCloseReason reason, 435 PresentationConnectionCloseReason reason,
446 const std::string& message) { 436 const std::string& message) {
447 if (!controller_) 437 if (!controller_)
448 return; 438 return;
449 439
450 controller_->didCloseConnection( 440 controller_->didCloseConnection(
451 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info), 441 blink::WebPresentationSessionInfo(
452 GetWebPresentationConnectionCloseReasonFromMojo(reason), 442 session_info.presentation_url,
443 blink::WebString::fromUTF8(session_info.presentation_id)),
444 GetWebPresentationConnectionCloseReason(reason),
453 blink::WebString::fromUTF8(message)); 445 blink::WebString::fromUTF8(message));
454 } 446 }
455 447
456 void PresentationDispatcher::OnConnectionMessagesReceived( 448 void PresentationDispatcher::OnConnectionMessagesReceived(
457 blink::mojom::PresentationSessionInfoPtr session_info, 449 const PresentationSessionInfo& session_info,
458 std::vector<blink::mojom::ConnectionMessagePtr> messages) { 450 std::vector<blink::mojom::ConnectionMessagePtr> messages) {
459 if (!controller_) 451 if (!controller_)
460 return; 452 return;
461 453
462 for (size_t i = 0; i < messages.size(); ++i) { 454 for (size_t i = 0; i < messages.size(); ++i) {
463 // Note: Passing batches of messages to the Blink layer would be more 455 // Note: Passing batches of messages to the Blink layer would be more
464 // efficient. 456 // efficient.
465 auto web_session_info = 457 auto web_session_info = blink::WebPresentationSessionInfo(
466 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info); 458 session_info.presentation_url,
459 blink::WebString::fromUTF8(session_info.presentation_id));
460
467 switch (messages[i]->type) { 461 switch (messages[i]->type) {
468 case blink::mojom::PresentationMessageType::TEXT: { 462 case blink::mojom::PresentationMessageType::TEXT: {
469 // TODO(mfoltz): Do we need to DCHECK(messages[i]->message)? 463 // TODO(mfoltz): Do we need to DCHECK(messages[i]->message)?
470 controller_->didReceiveSessionTextMessage( 464 controller_->didReceiveSessionTextMessage(
471 web_session_info, 465 web_session_info,
472 blink::WebString::fromUTF8(messages[i]->message.value())); 466 blink::WebString::fromUTF8(messages[i]->message.value()));
473 break; 467 break;
474 } 468 }
475 case blink::mojom::PresentationMessageType::BINARY: { 469 case blink::mojom::PresentationMessageType::BINARY: {
476 // TODO(mfoltz): Do we need to DCHECK(messages[i]->data)? 470 // TODO(mfoltz): Do we need to DCHECK(messages[i]->data)?
(...skipping 30 matching lines...) Expand all
507 if (should_listen) { 501 if (should_listen) {
508 status->listening_state = ListeningState::WAITING; 502 status->listening_state = ListeningState::WAITING;
509 presentation_service_->ListenForScreenAvailability(status->url); 503 presentation_service_->ListenForScreenAvailability(status->url);
510 } else { 504 } else {
511 status->listening_state = ListeningState::INACTIVE; 505 status->listening_state = ListeningState::INACTIVE;
512 presentation_service_->StopListeningForScreenAvailability(status->url); 506 presentation_service_->StopListeningForScreenAvailability(status->url);
513 } 507 }
514 } 508 }
515 509
516 PresentationDispatcher::SendMessageRequest::SendMessageRequest( 510 PresentationDispatcher::SendMessageRequest::SendMessageRequest(
517 blink::mojom::PresentationSessionInfoPtr session_info, 511 const PresentationSessionInfo& session_info,
518 blink::mojom::ConnectionMessagePtr message) 512 blink::mojom::ConnectionMessagePtr message)
519 : session_info(std::move(session_info)), message(std::move(message)) {} 513 : session_info(session_info), message(std::move(message)) {}
520 514
521 PresentationDispatcher::SendMessageRequest::~SendMessageRequest() {} 515 PresentationDispatcher::SendMessageRequest::~SendMessageRequest() {}
522 516
523 // static 517 // static
524 PresentationDispatcher::SendMessageRequest* 518 PresentationDispatcher::SendMessageRequest*
525 PresentationDispatcher::CreateSendTextMessageRequest( 519 PresentationDispatcher::CreateSendTextMessageRequest(
526 const blink::WebURL& presentationUrl, 520 const blink::WebURL& presentationUrl,
527 const blink::WebString& presentationId, 521 const blink::WebString& presentationId,
528 const blink::WebString& message) { 522 const blink::WebString& message) {
529 blink::mojom::PresentationSessionInfoPtr session_info = 523 PresentationSessionInfo session_info(GURL(presentationUrl),
530 blink::mojom::PresentationSessionInfo::New(); 524 presentationId.utf8());
531 session_info->url = presentationUrl;
532 session_info->id = presentationId.utf8();
533 525
534 blink::mojom::ConnectionMessagePtr session_message = 526 blink::mojom::ConnectionMessagePtr session_message =
535 blink::mojom::ConnectionMessage::New(); 527 blink::mojom::ConnectionMessage::New();
536 session_message->type = blink::mojom::PresentationMessageType::TEXT; 528 session_message->type = blink::mojom::PresentationMessageType::TEXT;
537 session_message->message = message.utf8(); 529 session_message->message = message.utf8();
538 return new SendMessageRequest(std::move(session_info), 530 return new SendMessageRequest(session_info, std::move(session_message));
539 std::move(session_message));
540 } 531 }
541 532
542 // static 533 // static
543 PresentationDispatcher::SendMessageRequest* 534 PresentationDispatcher::SendMessageRequest*
544 PresentationDispatcher::CreateSendBinaryMessageRequest( 535 PresentationDispatcher::CreateSendBinaryMessageRequest(
545 const blink::WebURL& presentationUrl, 536 const blink::WebURL& presentationUrl,
546 const blink::WebString& presentationId, 537 const blink::WebString& presentationId,
547 blink::mojom::PresentationMessageType type, 538 blink::mojom::PresentationMessageType type,
548 const uint8_t* data, 539 const uint8_t* data,
549 size_t length) { 540 size_t length) {
550 blink::mojom::PresentationSessionInfoPtr session_info = 541 PresentationSessionInfo session_info(GURL(presentationUrl),
551 blink::mojom::PresentationSessionInfo::New(); 542 presentationId.utf8());
552 session_info->url = presentationUrl;
553 session_info->id = presentationId.utf8();
554 543
555 blink::mojom::ConnectionMessagePtr session_message = 544 blink::mojom::ConnectionMessagePtr session_message =
556 blink::mojom::ConnectionMessage::New(); 545 blink::mojom::ConnectionMessage::New();
557 session_message->type = type; 546 session_message->type = type;
558 session_message->data = std::vector<uint8_t>(data, data + length); 547 session_message->data = std::vector<uint8_t>(data, data + length);
559 return new SendMessageRequest(std::move(session_info), 548 return new SendMessageRequest(session_info, std::move(session_message));
560 std::move(session_message));
561 } 549 }
562 550
563 PresentationDispatcher::AvailabilityStatus::AvailabilityStatus( 551 PresentationDispatcher::AvailabilityStatus::AvailabilityStatus(
564 const GURL& availability_url) 552 const GURL& availability_url)
565 : url(availability_url), 553 : url(availability_url),
566 last_known_availability(false), 554 last_known_availability(false),
567 listening_state(ListeningState::INACTIVE) {} 555 listening_state(ListeningState::INACTIVE) {}
568 556
569 PresentationDispatcher::AvailabilityStatus::~AvailabilityStatus() { 557 PresentationDispatcher::AvailabilityStatus::~AvailabilityStatus() {
570 } 558 }
571 559
572 } // namespace content 560 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698