Chromium Code Reviews| 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 "chrome/browser/media/router/browser_presentation_connection_proxy.h" | |
| 6 | |
| 7 #include "chrome/browser/media/router/media_route.h" | |
| 8 #include "chrome/browser/media/router/media_router.h" | |
| 9 #include "content/public/common/presentation_constants.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 std::unique_ptr<content::PresentationSessionMessage> | |
| 14 GetPresentationSessionMessage(blink::mojom::SessionMessagePtr input) { | |
| 15 std::unique_ptr<content::PresentationSessionMessage> output; | |
| 16 if (input.is_null()) | |
| 17 return output; | |
| 18 | |
| 19 switch (input->type) { | |
| 20 case blink::mojom::PresentationMessageType::TEXT: { | |
| 21 // Return nullptr content::PresentationSessionMessage if invalid (unset | |
| 22 // |message|, | |
| 23 // 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.
| |
| 24 if (input->data || !input->message || | |
| 25 input->message->size() > content::kMaxPresentationSessionMessageSize) | |
| 26 return output; | |
| 27 | |
| 28 output.reset(new content::PresentationSessionMessage( | |
| 29 content::PresentationMessageType::TEXT)); | |
| 30 output->message = std::move(input->message.value()); | |
| 31 return output; | |
| 32 } | |
| 33 case blink::mojom::PresentationMessageType::ARRAY_BUFFER: { | |
| 34 // Return nullptr content::PresentationSessionMessage if invalid (unset | |
| 35 // |data|, set | |
| 36 // |message|, or size too large). | |
| 37 if (!input->data || input->message || | |
| 38 input->data->size() > content::kMaxPresentationSessionMessageSize) | |
| 39 return output; | |
| 40 | |
| 41 output.reset(new content::PresentationSessionMessage( | |
| 42 content::PresentationMessageType::ARRAY_BUFFER)); | |
| 43 output->data.reset( | |
| 44 new std::vector<uint8_t>(std::move(input->data.value()))); | |
| 45 return output; | |
| 46 } | |
| 47 case blink::mojom::PresentationMessageType::BLOB: { | |
| 48 // Return nullptr content::PresentationSessionMessage if invalid (unset | |
| 49 // |data|, set | |
| 50 // |message|, or size too large). | |
| 51 if (!input->data || input->message || | |
| 52 input->data->size() > content::kMaxPresentationSessionMessageSize) | |
| 53 return output; | |
| 54 | |
| 55 output.reset(new content::PresentationSessionMessage( | |
| 56 content::PresentationMessageType::BLOB)); | |
| 57 output->data.reset( | |
| 58 new std::vector<uint8_t>(std::move(input->data.value()))); | |
| 59 return output; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 NOTREACHED() << "Invalid presentation message type " << input->type; | |
| 64 return output; | |
| 65 } | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 namespace media_router { | |
| 70 | |
| 71 BrowserPresentationConnectionProxy::BrowserPresentationConnectionProxy( | |
| 72 const content::PresentationSessionInfo& session_info, | |
| 73 MediaRouter* router, | |
| 74 MediaRoute* route) | |
| 75 : session_info_(session_info), | |
| 76 router_(router), | |
| 77 route_(route), | |
| 78 binding_(this) { | |
| 79 DVLOG(2) << __FUNCTION__ << " [id]: " << session_info.presentation_id; | |
| 80 } | |
| 81 | |
| 82 BrowserPresentationConnectionProxy::~BrowserPresentationConnectionProxy() { | |
| 83 DVLOG(2) << __FUNCTION__; | |
| 84 if (!on_message_callback_) | |
| 85 return; | |
| 86 // Run the callback with false, indicating the renderer to stop sending | |
| 87 // the requests and invalidate all pending requests. | |
| 88 on_message_callback_->Run(false); | |
| 89 on_message_callback_.reset(); | |
| 90 } | |
| 91 | |
| 92 blink::mojom::PresentationConnectionPtr | |
| 93 BrowserPresentationConnectionProxy::Bind() { | |
| 94 return binding_.CreateInterfacePtrAndBind(); | |
| 95 } | |
| 96 | |
| 97 void BrowserPresentationConnectionProxy::SetTargetConnection( | |
| 98 blink::mojom::PresentationConnectionPtr connection) { | |
| 99 DVLOG(2) << __FUNCTION__; | |
| 100 target_connection_ = std::move(connection); | |
| 101 target_connection_->DidChangeState( | |
| 102 blink::mojom::PresentationConnectionState::CONNECTED); | |
| 103 } | |
| 104 | |
| 105 void BrowserPresentationConnectionProxy::OnMessage( | |
| 106 blink::mojom::SessionMessagePtr session_message, | |
| 107 const OnMessageCallback& on_message_callback) { | |
| 108 DVLOG(2) << __FUNCTION__ << " [id]: " << session_info_.presentation_id; | |
| 109 | |
| 110 DCHECK(!session_message.is_null()); | |
| 111 // on_message_callback_ should be null by now, otherwise resetting of | |
| 112 // on_message_callback_ with new callback will drop the old callback. | |
| 113 if (!router_ || on_message_callback_) { | |
| 114 on_message_callback_->Run(false); | |
| 115 return; | |
| 116 } | |
| 117 on_message_callback_.reset(new OnMessageCallback(on_message_callback)); | |
| 118 | |
| 119 std::unique_ptr<content::PresentationSessionMessage> message = | |
| 120 GetPresentationSessionMessage(std::move(session_message)); | |
| 121 | |
| 122 if (message->is_binary()) { | |
| 123 router_->SendRouteBinaryMessage( | |
| 124 route_->media_route_id(), std::move(message->data), | |
| 125 base::Bind( | |
| 126 &BrowserPresentationConnectionProxy::OnConnectionMessageCallback, | |
| 127 base::Unretained(this))); | |
| 128 } else { | |
| 129 router_->SendRouteMessage( | |
| 130 route_->media_route_id(), message->message, | |
| 131 base::Bind( | |
| 132 &BrowserPresentationConnectionProxy::OnConnectionMessageCallback, | |
| 133 base::Unretained(this))); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void BrowserPresentationConnectionProxy::DidChangeState( | |
| 138 blink::mojom::PresentationConnectionState state) {} | |
| 139 | |
| 140 void BrowserPresentationConnectionProxy::OnConnectionMessageCallback( | |
| 141 bool sent) { | |
| 142 DVLOG(2) << __FUNCTION__ << " [sent]: " << sent; | |
| 143 // It is possible that Reset() is invoked before receiving this callback. | |
| 144 // So, always check on_message_callback_ for non-null. | |
| 145 if (on_message_callback_) { | |
| 146 on_message_callback_->Run(sent); | |
| 147 on_message_callback_.reset(); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 } // namespace media_router | |
| OLD | NEW |