1 // Copyright (c) 2012 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/media/rtc_data_channel_handler.h" 6 7 #include <string> 8 9 #include "base/logging.h" 10 #include "base/strings/utf_string_conversions.h" 11 12 namespace content { 13 14 RtcDataChannelHandler::RtcDataChannelHandler( 15 webrtc::DataChannelInterface* channel) 16 : channel_(channel), 17 webkit_client_(NULL) { 18 DVLOG(1) << "::ctor"; 19 channel_->RegisterObserver(this); 20 } 21 22 RtcDataChannelHandler::~RtcDataChannelHandler() { 23 DVLOG(1) << "::dtor"; 24 channel_->UnregisterObserver(); 25 } 26 27 void RtcDataChannelHandler::setClient( 28 blink::WebRTCDataChannelHandlerClient* client) { 29 webkit_client_ = client; 30 } 31 32 blink::WebString RtcDataChannelHandler::label() { 33 return UTF8ToUTF16(channel_->label()); 34 } 35 36 bool RtcDataChannelHandler::isReliable() { 37 return channel_->reliable(); 38 } 39 40 bool RtcDataChannelHandler::ordered() const { 41 return channel_->ordered(); 42 } 43 44 unsigned short RtcDataChannelHandler::maxRetransmitTime() const { 45 return channel_->maxRetransmitTime(); 46 } 47 48 unsigned short RtcDataChannelHandler::maxRetransmits() const { 49 return channel_->maxRetransmits(); 50 } 51 52 blink::WebString RtcDataChannelHandler::protocol() const { 53 return UTF8ToUTF16(channel_->protocol()); 54 } 55 56 bool RtcDataChannelHandler::negotiated() const { 57 return channel_->negotiated(); 58 } 59 60 unsigned short RtcDataChannelHandler::id() const { 61 return channel_->id(); 62 } 63 64 unsigned long RtcDataChannelHandler::bufferedAmount() { 65 return channel_->buffered_amount(); 66 } 67 68 bool RtcDataChannelHandler::sendStringData(const blink::WebString& data) { 69 std::string utf8_buffer = UTF16ToUTF8(data); 70 talk_base::Buffer buffer(utf8_buffer.c_str(), utf8_buffer.length()); 71 webrtc::DataBuffer data_buffer(buffer, false); 72 return channel_->Send(data_buffer); 73 } 74 75 bool RtcDataChannelHandler::sendRawData(const char* data, size_t length) { 76 talk_base::Buffer buffer(data, length); 77 webrtc::DataBuffer data_buffer(buffer, true); 78 return channel_->Send(data_buffer); 79 } 80 81 void RtcDataChannelHandler::close() { 82 channel_->Close(); 83 } 84 85 void RtcDataChannelHandler::OnStateChange() { 86 if (!webkit_client_) { 87 LOG(ERROR) << "WebRTCDataChannelHandlerClient not set."; 88 return; 89 } 90 DVLOG(1) << "OnStateChange " << channel_->state(); 91 switch (channel_->state()) { 92 case webrtc::DataChannelInterface::kConnecting: 93 webkit_client_->didChangeReadyState( 94 blink::WebRTCDataChannelHandlerClient::ReadyStateConnecting); 95 break; 96 case webrtc::DataChannelInterface::kOpen: 97 webkit_client_->didChangeReadyState( 98 blink::WebRTCDataChannelHandlerClient::ReadyStateOpen); 99 break; 100 case webrtc::DataChannelInterface::kClosing: 101 webkit_client_->didChangeReadyState( 102 blink::WebRTCDataChannelHandlerClient::ReadyStateClosing); 103 break; 104 case webrtc::DataChannelInterface::kClosed: 105 webkit_client_->didChangeReadyState( 106 blink::WebRTCDataChannelHandlerClient::ReadyStateClosed); 107 break; 108 default: 109 NOTREACHED(); 110 break; 111 } 112 } 113 114 void RtcDataChannelHandler::OnMessage(const webrtc::DataBuffer& buffer) { 115 if (!webkit_client_) { 116 LOG(ERROR) << "WebRTCDataChannelHandlerClient not set."; 117 return; 118 } 119 120 if (buffer.binary) { 121 webkit_client_->didReceiveRawData(buffer.data.data(), buffer.data.length()); 122 } else { 123 base::string16 utf16; 124 if (!UTF8ToUTF16(buffer.data.data(), buffer.data.length(), &utf16)) { 125 LOG(ERROR) << "Failed convert received data to UTF16"; 126 return; 127 } 128 webkit_client_->didReceiveStringData(utf16); 129 } 130 } 131 132 } // namespace content 133