Chromium Code Reviews| Index: chrome/browser/media/router/browser_presentation_connection_proxy.cc |
| diff --git a/chrome/browser/media/router/browser_presentation_connection_proxy.cc b/chrome/browser/media/router/browser_presentation_connection_proxy.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e6e12536e55ce836243080d4c87ceb222efcac9e |
| --- /dev/null |
| +++ b/chrome/browser/media/router/browser_presentation_connection_proxy.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/router/browser_presentation_connection_proxy.h" |
| + |
| +#include "chrome/browser/media/router/media_route.h" |
| +#include "chrome/browser/media/router/media_router.h" |
| +#include "content/public/common/presentation_constants.h" |
| + |
| +namespace { |
| + |
| +// TODO(crbug.com/632623): remove this function when we finish typemaps for |
| +// presentation.mojom. |
| +std::unique_ptr<content::PresentationConnectionMessage> |
| +GetPresentationConnectionMessage(blink::mojom::ConnectionMessagePtr input) { |
|
mark a. foltz
2017/01/27 00:44:21
PresentationConnectionMessageFromMojo
zhaobin
2017/01/27 05:05:46
Done.
|
| + std::unique_ptr<content::PresentationConnectionMessage> output; |
| + if (input.is_null()) |
| + return output; |
| + |
| + switch (input->type) { |
| + case blink::mojom::PresentationMessageType::TEXT: { |
| + // Return nullptr content::PresentationSessionMessage if invalid (unset |
| + // |message|, set |data|, or size too large). |
| + if (input->data || !input->message || |
| + input->message->size() > |
| + content::kMaxPresentationConnectionMessageSize) |
| + return output; |
| + |
| + output.reset(new content::PresentationConnectionMessage( |
| + content::PresentationMessageType::TEXT)); |
| + output->message = std::move(input->message.value()); |
| + return output; |
| + } |
| + case blink::mojom::PresentationMessageType::BINARY: { |
| + // Return nullptr content::PresentationSessionMessage if invalid (unset |
| + // |data|, set |message|, or size too large). |
| + if (!input->data || input->message || |
| + input->data->size() > content::kMaxPresentationConnectionMessageSize) |
| + return output; |
| + |
| + output.reset(new content::PresentationConnectionMessage( |
| + content::PresentationMessageType::BINARY)); |
| + output->data.reset( |
| + new std::vector<uint8_t>(std::move(input->data.value()))); |
| + return output; |
| + } |
| + } |
| + |
| + NOTREACHED() << "Invalid presentation message type " << input->type; |
| + return output; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace media_router { |
| + |
| +BrowserPresentationConnectionProxy::BrowserPresentationConnectionProxy( |
| + const content::PresentationSessionInfo& session_info, |
| + MediaRouter* router, |
| + MediaRoute* route) |
| + : session_info_(session_info), |
| + router_(router), |
| + route_(route), |
| + binding_(this) {} |
| + |
| +BrowserPresentationConnectionProxy::~BrowserPresentationConnectionProxy() {} |
| + |
| +void BrowserPresentationConnectionProxy::Bind( |
| + blink::mojom::PresentationConnectionRequest target_conn_request) { |
| + binding_.Bind(std::move(target_conn_request)); |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::SetTargetConnection( |
| + blink::mojom::PresentationConnectionPtr connection) { |
| + DCHECK(!target_connection_); |
| + |
| + target_connection_ = std::move(connection); |
| + target_connection_->DidChangeState( |
| + blink::mojom::PresentationConnectionState::CONNECTED); |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::OnMessage( |
| + blink::mojom::ConnectionMessagePtr session_message, |
| + const OnMessageCallback& on_message_callback) { |
| + DVLOG(2) << "BrowserPresentationConnectionProxy::OnMessage " |
| + << "[presentation_id]: " << session_info_.presentation_id; |
| + DCHECK(!session_message.is_null()); |
| + |
| + if (!router_) { |
| + on_message_callback.Run(false); |
| + return; |
| + } |
| + |
| + std::unique_ptr<content::PresentationConnectionMessage> message = |
| + GetPresentationConnectionMessage(std::move(session_message)); |
| + |
| + if (message->is_binary()) { |
| + router_->SendRouteBinaryMessage(route_->media_route_id(), |
| + std::move(message->data), |
| + on_message_callback); |
| + } else { |
| + router_->SendRouteMessage(route_->media_route_id(), message->message, |
| + on_message_callback); |
| + } |
| +} |
| + |
| +} // namespace media_router |