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..7d08aa84da510dc736515e7b83d00c2dedb10743 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/browser_presentation_connection_proxy.cc |
| @@ -0,0 +1,107 @@ |
| +// 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> |
| +PresentationConnectionMessageFromMojo( |
| + blink::mojom::ConnectionMessagePtr input) { |
| + 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( |
| + MediaRouter* router, |
| + const MediaRoute* route) |
| + : router_(router), |
| + route_(route), |
| + binding_(this), |
| + target_connection_ptr_(nullptr) {} |
| + |
| +BrowserPresentationConnectionProxy::~BrowserPresentationConnectionProxy() {} |
| + |
| +void BrowserPresentationConnectionProxy::Bind( |
| + blink::mojom::PresentationConnectionRequest receiver_connection_request) { |
| + binding_.Bind(std::move(receiver_connection_request)); |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::BindControllerConnection( |
| + blink::mojom::PresentationConnectionPtr controller_connection_ptr) { |
| + DCHECK(!target_connection_ptr_); |
| + |
| + target_connection_ptr_ = std::move(controller_connection_ptr); |
| + target_connection_ptr_->DidChangeState( |
| + blink::mojom::PresentationConnectionState::CONNECTED); |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::OnMessage( |
| + blink::mojom::ConnectionMessagePtr connection_message, |
| + const OnMessageCallback& on_message_callback) { |
| + DVLOG(2) << "BrowserPresentationConnectionProxy::OnMessage"; |
| + |
| + DCHECK(!connection_message.is_null()); |
| + if (!router_) { |
|
imcheng
2017/01/31 01:53:24
Why would this be nullptr? Maybe there should be a
zhaobin
2017/01/31 18:44:15
Done.
|
| + on_message_callback.Run(false); |
| + return; |
| + } |
| + |
| + auto message = |
| + PresentationConnectionMessageFromMojo(std::move(connection_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 |