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..9434d22db22b3fdee1ed7930059f078b3d57c532 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/browser_presentation_connection_proxy.cc |
| @@ -0,0 +1,151 @@ |
| +// Copyright 2016 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 { |
| + |
| +std::unique_ptr<content::PresentationSessionMessage> |
| +GetPresentationSessionMessage(blink::mojom::SessionMessagePtr input) { |
| + std::unique_ptr<content::PresentationSessionMessage> 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). |
|
mark a. foltz
2016/12/02 23:06:48
Rewrap comment.
zhaobin
2017/01/13 00:05:49
Done.
|
| + if (input->data || !input->message || |
| + input->message->size() > content::kMaxPresentationSessionMessageSize) |
| + return output; |
| + |
| + output.reset(new content::PresentationSessionMessage( |
| + content::PresentationMessageType::TEXT)); |
| + output->message = std::move(input->message.value()); |
| + return output; |
| + } |
| + case blink::mojom::PresentationMessageType::ARRAY_BUFFER: { |
| + // Return nullptr content::PresentationSessionMessage if invalid (unset |
| + // |data|, set |
| + // |message|, or size too large). |
| + if (!input->data || input->message || |
| + input->data->size() > content::kMaxPresentationSessionMessageSize) |
| + return output; |
| + |
| + output.reset(new content::PresentationSessionMessage( |
| + content::PresentationMessageType::ARRAY_BUFFER)); |
| + output->data.reset( |
| + new std::vector<uint8_t>(std::move(input->data.value()))); |
| + return output; |
| + } |
| + case blink::mojom::PresentationMessageType::BLOB: { |
| + // Return nullptr content::PresentationSessionMessage if invalid (unset |
| + // |data|, set |
| + // |message|, or size too large). |
| + if (!input->data || input->message || |
| + input->data->size() > content::kMaxPresentationSessionMessageSize) |
| + return output; |
| + |
| + output.reset(new content::PresentationSessionMessage( |
| + content::PresentationMessageType::BLOB)); |
| + 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) { |
| + DVLOG(2) << __FUNCTION__ << " [id]: " << session_info.presentation_id; |
| +} |
| + |
| +BrowserPresentationConnectionProxy::~BrowserPresentationConnectionProxy() { |
| + DVLOG(2) << __FUNCTION__; |
| + if (!on_message_callback_) |
| + return; |
| + // Run the callback with false, indicating the renderer to stop sending |
| + // the requests and invalidate all pending requests. |
| + on_message_callback_->Run(false); |
| + on_message_callback_.reset(); |
| +} |
| + |
| +blink::mojom::PresentationConnectionPtr |
| +BrowserPresentationConnectionProxy::Bind() { |
| + return binding_.CreateInterfacePtrAndBind(); |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::SetTargetConnection( |
| + blink::mojom::PresentationConnectionPtr connection) { |
| + DVLOG(2) << __FUNCTION__; |
| + target_connection_ = std::move(connection); |
| + target_connection_->DidChangeState( |
| + blink::mojom::PresentationConnectionState::CONNECTED); |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::OnMessage( |
| + blink::mojom::SessionMessagePtr session_message, |
| + const OnMessageCallback& on_message_callback) { |
| + DVLOG(2) << __FUNCTION__ << " [id]: " << session_info_.presentation_id; |
| + |
| + DCHECK(!session_message.is_null()); |
| + // on_message_callback_ should be null by now, otherwise resetting of |
| + // on_message_callback_ with new callback will drop the old callback. |
| + if (!router_ || on_message_callback_) { |
| + on_message_callback_->Run(false); |
| + return; |
| + } |
| + on_message_callback_.reset(new OnMessageCallback(on_message_callback)); |
| + |
| + std::unique_ptr<content::PresentationSessionMessage> message = |
| + GetPresentationSessionMessage(std::move(session_message)); |
| + |
| + if (message->is_binary()) { |
| + router_->SendRouteBinaryMessage( |
| + route_->media_route_id(), std::move(message->data), |
| + base::Bind( |
| + &BrowserPresentationConnectionProxy::OnConnectionMessageCallback, |
| + base::Unretained(this))); |
| + } else { |
| + router_->SendRouteMessage( |
| + route_->media_route_id(), message->message, |
| + base::Bind( |
| + &BrowserPresentationConnectionProxy::OnConnectionMessageCallback, |
| + base::Unretained(this))); |
| + } |
| +} |
| + |
| +void BrowserPresentationConnectionProxy::DidChangeState( |
| + blink::mojom::PresentationConnectionState state) {} |
| + |
| +void BrowserPresentationConnectionProxy::OnConnectionMessageCallback( |
| + bool sent) { |
| + DVLOG(2) << __FUNCTION__ << " [sent]: " << sent; |
| + // It is possible that Reset() is invoked before receiving this callback. |
| + // So, always check on_message_callback_ for non-null. |
| + if (on_message_callback_) { |
| + on_message_callback_->Run(sent); |
| + on_message_callback_.reset(); |
| + } |
| +} |
| + |
| +} // namespace media_router |