OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/presentation/presentation_connection_proxy.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "third_party/WebKit/public/platform/WebString.h" |
| 9 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnection.h" |
| 10 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nConnectionClient.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 PresentationConnectionProxy::PresentationConnectionProxy( |
| 15 const OnMessageCallback& on_message_callback) |
| 16 : binding_(this), |
| 17 target_connection_(nullptr), |
| 18 source_connection_(nullptr), |
| 19 on_message_callback_(on_message_callback) {} |
| 20 |
| 21 PresentationConnectionProxy::~PresentationConnectionProxy() = default; |
| 22 |
| 23 blink::mojom::PresentationConnectionPtr PresentationConnectionProxy::Bind() { |
| 24 return binding_.CreateInterfacePtrAndBind(); |
| 25 } |
| 26 |
| 27 void PresentationConnectionProxy::SendSessionMessage( |
| 28 blink::mojom::PresentationSessionInfoPtr session, |
| 29 blink::mojom::SessionMessagePtr session_message) const { |
| 30 DCHECK(target_connection_); |
| 31 target_connection_->OnMessage(std::move(session_message), |
| 32 on_message_callback_); |
| 33 } |
| 34 |
| 35 void PresentationConnectionProxy::SetSourceConnection( |
| 36 blink::WebPresentationConnection* connection) { |
| 37 DCHECK(!source_connection_); |
| 38 source_connection_ = connection; |
| 39 } |
| 40 |
| 41 void PresentationConnectionProxy::SetTargetConnection( |
| 42 blink::mojom::PresentationConnectionPtr connection) { |
| 43 DCHECK(!target_connection_); |
| 44 target_connection_ = std::move(connection); |
| 45 target_connection_->DidChangeState( |
| 46 blink::mojom::PresentationConnectionState::CONNECTED); |
| 47 } |
| 48 |
| 49 void PresentationConnectionProxy::OnMessage( |
| 50 blink::mojom::SessionMessagePtr session_message, |
| 51 const OnMessageCallback& callback) { |
| 52 DCHECK(source_connection_); |
| 53 DCHECK(!callback.is_null()); |
| 54 |
| 55 switch (session_message->type) { |
| 56 case blink::mojom::PresentationMessageType::TEXT: { |
| 57 DCHECK(session_message->message); |
| 58 source_connection_->didReceiveTextMessage( |
| 59 blink::WebString::fromUTF8(session_message->message.value())); |
| 60 break; |
| 61 } |
| 62 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: |
| 63 case blink::mojom::PresentationMessageType::BLOB: { |
| 64 DCHECK(session_message->data); |
| 65 source_connection_->didReceiveBinaryMessage( |
| 66 &(session_message->data->front()), session_message->data->size()); |
| 67 break; |
| 68 } |
| 69 default: { |
| 70 callback.Run(false); |
| 71 NOTREACHED(); |
| 72 break; |
| 73 } |
| 74 } |
| 75 |
| 76 callback.Run(true); |
| 77 } |
| 78 |
| 79 void PresentationConnectionProxy::DidChangeState( |
| 80 blink::mojom::PresentationConnectionState state) { |
| 81 DCHECK(source_connection_); |
| 82 if (state == blink::mojom::PresentationConnectionState::CONNECTED) { |
| 83 source_connection_->didChangeState( |
| 84 blink::WebPresentationConnectionState::Connected); |
| 85 } else { |
| 86 // |DidChangeState| should only handles state transition from connecting -> |
| 87 // connected. PresentationService and MRP handles other state transitions. |
| 88 NOTREACHED(); |
| 89 } |
| 90 } |
| 91 |
| 92 } // namespace content |
OLD | NEW |