Fix build break
[chromium-blink-merge.git] / content / renderer / media / rtc_data_channel_handler.cc
blob4b0006a2b3d9bad27acad0b1bac6edad63cbb0e6
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.
5 #include "content/renderer/media/rtc_data_channel_handler.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/utf_string_conversions.h"
12 namespace content {
14 RtcDataChannelHandler::RtcDataChannelHandler(
15 webrtc::DataChannelInterface* channel)
16 : channel_(channel),
17 webkit_client_(NULL) {
18 DVLOG(1) << "::ctor";
19 channel_->RegisterObserver(this);
22 RtcDataChannelHandler::~RtcDataChannelHandler() {
23 DVLOG(1) << "::dtor";
24 channel_->UnregisterObserver();
27 void RtcDataChannelHandler::setClient(
28 WebKit::WebRTCDataChannelHandlerClient* client) {
29 webkit_client_ = client;
32 WebKit::WebString RtcDataChannelHandler::label() {
33 return UTF8ToUTF16(channel_->label());
36 bool RtcDataChannelHandler::isReliable() {
37 return channel_->reliable();
40 unsigned long RtcDataChannelHandler::bufferedAmount() {
41 return channel_->buffered_amount();
44 bool RtcDataChannelHandler::sendStringData(const WebKit::WebString& data) {
45 std::string utf8_buffer = UTF16ToUTF8(data);
46 webrtc::DataBuffer buffer;
47 buffer.binary = false;
48 buffer.data.SetData(utf8_buffer.c_str(), utf8_buffer.length());
49 return channel_->Send(buffer);
52 bool RtcDataChannelHandler::sendRawData(const char* data, size_t length) {
53 webrtc::DataBuffer buffer;
54 buffer.data.SetData(data, length);
55 buffer.binary = true;
56 return channel_->Send(buffer);
59 void RtcDataChannelHandler::close() {
60 channel_->Close();
63 void RtcDataChannelHandler::OnStateChange() {
64 if (!webkit_client_) {
65 LOG(ERROR) << "WebRTCDataChannelHandlerClient not set.";
66 return;
68 DVLOG(1) << "OnStateChange " << channel_->state();
69 switch (channel_->state()) {
70 case webrtc::DataChannelInterface::kConnecting:
71 webkit_client_->didChangeReadyState(
72 WebKit::WebRTCDataChannelHandlerClient::ReadyStateConnecting);
73 break;
74 case webrtc::DataChannelInterface::kOpen:
75 webkit_client_->didChangeReadyState(
76 WebKit::WebRTCDataChannelHandlerClient::ReadyStateOpen);
77 break;
78 case webrtc::DataChannelInterface::kClosing:
79 webkit_client_->didChangeReadyState(
80 WebKit::WebRTCDataChannelHandlerClient::ReadyStateClosing);
81 break;
82 case webrtc::DataChannelInterface::kClosed:
83 webkit_client_->didChangeReadyState(
84 WebKit::WebRTCDataChannelHandlerClient::ReadyStateClosed);
85 break;
86 default:
87 NOTREACHED();
88 break;
92 void RtcDataChannelHandler::OnMessage(const webrtc::DataBuffer& buffer) {
93 if (!webkit_client_) {
94 LOG(ERROR) << "WebRTCDataChannelHandlerClient not set.";
95 return;
98 if (buffer.binary) {
99 webkit_client_->didReceiveRawData(buffer.data.data(), buffer.data.length());
100 } else {
101 string16 utf16;
102 if (!UTF8ToUTF16(buffer.data.data(), buffer.data.length(), &utf16)) {
103 LOG(ERROR) << "Failed convert received data to UTF16";
104 return;
106 webkit_client_->didReceiveStringData(utf16);
110 } // namespace content