Update API for Objective-C RTCDataChannel.

BUG=

Review URL: https://codereview.webrtc.org/1545393003

Cr-Commit-Position: refs/heads/master@{#11362}
diff --git a/webrtc/api/BUILD.gn b/webrtc/api/BUILD.gn
index 1c333e4..928b615 100644
--- a/webrtc/api/BUILD.gn
+++ b/webrtc/api/BUILD.gn
@@ -35,6 +35,9 @@
       #"objc/RTCAudioTrack+Private.h",
       #"objc/RTCAudioTrack.h",
       #"objc/RTCAudioTrack.mm",
+      #"objc/RTCDataChannel+Private.h",
+      #"objc/RTCDataChannel.h",
+      #"objc/RTCDataChannel.mm",
       #"objc/RTCIceCandidate+Private.h",
       #"objc/RTCIceCandidate.h",
       #"objc/RTCIceCandidate.mm",
diff --git a/webrtc/api/api.gyp b/webrtc/api/api.gyp
index 5f848d7..ea31c17 100644
--- a/webrtc/api/api.gyp
+++ b/webrtc/api/api.gyp
@@ -25,6 +25,9 @@
             'objc/RTCAudioTrack+Private.h',
             'objc/RTCAudioTrack.h',
             'objc/RTCAudioTrack.mm',
+            'objc/RTCDataChannel+Private.h',
+            'objc/RTCDataChannel.h',
+            'objc/RTCDataChannel.mm',
             'objc/RTCIceCandidate+Private.h',
             'objc/RTCIceCandidate.h',
             'objc/RTCIceCandidate.mm',
diff --git a/webrtc/api/objc/RTCDataChannel+Private.h b/webrtc/api/objc/RTCDataChannel+Private.h
new file mode 100644
index 0000000..cc44923
--- /dev/null
+++ b/webrtc/api/objc/RTCDataChannel+Private.h
@@ -0,0 +1,49 @@
+/*
+ *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "RTCDataChannel.h"
+
+#include "talk/app/webrtc/datachannelinterface.h"
+#include "webrtc/base/scoped_ref_ptr.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCDataBuffer ()
+
+/**
+ * The native DataBuffer representation of this RTCDatabuffer object. This is
+ * needed to pass to the underlying C++ APIs.
+ */
+@property(nonatomic, readonly) const webrtc::DataBuffer *nativeDataBuffer;
+
+/** Initialize an RTCDataBuffer from a native DataBuffer. */
+- (instancetype)initWithNativeBuffer:(const webrtc::DataBuffer&)nativeBuffer;
+
+@end
+
+
+@interface RTCDataChannel ()
+
+/** Initialize an RTCDataChannel from a native DataChannelInterface. */
+- (instancetype)initWithNativeDataChannel:
+    (rtc::scoped_refptr<webrtc::DataChannelInterface>)nativeDataChannel
+    NS_DESIGNATED_INITIALIZER;
+
++ (webrtc::DataChannelInterface::DataState)
+    nativeDataChannelStateForState:(RTCDataChannelState)state;
+
++ (RTCDataChannelState)dataChannelStateForNativeState:
+    (webrtc::DataChannelInterface::DataState)nativeState;
+
++ (NSString *)stringForState:(RTCDataChannelState)state;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCDataChannel.h b/webrtc/api/objc/RTCDataChannel.h
new file mode 100644
index 0000000..61d41ba
--- /dev/null
+++ b/webrtc/api/objc/RTCDataChannel.h
@@ -0,0 +1,120 @@
+/*
+ *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RTCDataBuffer : NSObject
+
+/** NSData representation of the underlying buffer. */
+@property(nonatomic, readonly) NSData *data;
+
+/** Indicates whether |data| contains UTF-8 or binary data. */
+@property(nonatomic, readonly) BOOL isBinary;
+
+- (instancetype)init NS_UNAVAILABLE;
+
+/**
+ * Initialize an RTCDataBuffer from NSData. |isBinary| indicates whether |data|
+ * contains UTF-8 or binary data.
+ */
+- (instancetype)initWithData:(NSData *)data isBinary:(BOOL)isBinary;
+
+@end
+
+
+@class RTCDataChannel;
+
+@protocol RTCDataChannelDelegate <NSObject>
+
+/** The data channel state changed. */
+- (void)dataChannelDidChangeState:(RTCDataChannel *)dataChannel;
+
+/** The data channel successfully received a data buffer. */
+- (void)dataChannel:(RTCDataChannel *)dataChannel
+    didReceiveMessageWithBuffer:(RTCDataBuffer *)buffer;
+
+@optional
+
+/** The data channel's |bufferedAmount| changed. */
+- (void)dataChannel:(RTCDataChannel *)dataChannel
+    didChangeBufferedAmount:(NSUInteger)amount;
+
+@end
+
+
+/** Represents the state of the data channel. */
+typedef NS_ENUM(NSInteger, RTCDataChannelState) {
+    RTCDataChannelStateConnecting,
+    RTCDataChannelStateOpen,
+    RTCDataChannelStateClosing,
+    RTCDataChannelStateClosed,
+};
+
+@interface RTCDataChannel : NSObject
+
+/**
+ * A label that can be used to distinguish this data channel from other data
+ * channel objects.
+ */
+@property(nonatomic, readonly) NSString *label;
+
+/** Returns whether this data channel is ordered or not. */
+@property(nonatomic, readonly) BOOL isOrdered;
+
+/**
+ * The length of the time window (in milliseconds) during which transmissions
+ * and retransmissions may occur in unreliable mode.
+ */
+@property(nonatomic, readonly) uint16_t maxPacketLifeTime;
+
+/**
+ * The maximum number of retransmissions that are attempted in unreliable mode.
+ */
+@property(nonatomic, readonly) uint16_t maxRetransmits;
+
+/**
+ * The name of the sub-protocol used with this data channel, if any. Otherwise
+ * this returns an empty string.
+ */
+@property(nonatomic, readonly) NSString *protocol;
+
+/**
+ * Returns whether this data channel was negotiated by the application or not.
+ */
+@property(nonatomic, readonly) BOOL isNegotiated;
+
+/** The identifier for this data channel. */
+@property(nonatomic, readonly) int id;
+
+/** The state of the data channel. */
+@property(nonatomic, readonly) RTCDataChannelState readyState;
+
+/**
+ * The number of bytes of application data that have been queued using
+ * |sendData:| but that have not yet been transmitted to the network.
+ */
+@property(nonatomic, readonly) uint64_t bufferedAmount;
+
+/** The delegate for this data channel. */
+@property(nonatomic, weak) id<RTCDataChannelDelegate> delegate;
+
+- (instancetype)init NS_UNAVAILABLE;
+
+/** Closes the data channel. */
+- (void)close;
+
+/** Attempt to send |data| on this data channel's underlying data transport. */
+- (BOOL)sendData:(RTCDataBuffer *)data;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/webrtc/api/objc/RTCDataChannel.mm b/webrtc/api/objc/RTCDataChannel.mm
new file mode 100644
index 0000000..f7ca288
--- /dev/null
+++ b/webrtc/api/objc/RTCDataChannel.mm
@@ -0,0 +1,221 @@
+/*
+ *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import "RTCDataChannel.h"
+
+#import "webrtc/api/objc/RTCDataChannel+Private.h"
+#import "webrtc/base/objc/NSString+StdString.h"
+
+namespace webrtc {
+
+class DataChannelDelegateAdapter : public DataChannelObserver {
+ public:
+  DataChannelDelegateAdapter(RTCDataChannel *channel) { channel_ = channel; }
+
+  void OnStateChange() override {
+    [channel_.delegate dataChannelDidChangeState:channel_];
+  }
+
+  void OnMessage(const DataBuffer& buffer) override {
+    RTCDataBuffer *data_buffer =
+        [[RTCDataBuffer alloc] initWithNativeBuffer:buffer];
+    [channel_.delegate dataChannel:channel_
+       didReceiveMessageWithBuffer:data_buffer];
+  }
+
+  void OnBufferedAmountChange(uint64_t previousAmount) override {
+    id<RTCDataChannelDelegate> delegate = channel_.delegate;
+    if ([delegate
+            respondsToSelector:@selector(channel:didChangeBufferedAmount:)]) {
+      [delegate dataChannel:channel_ didChangeBufferedAmount:previousAmount];
+    }
+  }
+
+ private:
+  __weak RTCDataChannel *channel_;
+};
+}
+
+
+@implementation RTCDataBuffer {
+  rtc::scoped_ptr<webrtc::DataBuffer> _dataBuffer;
+}
+
+- (instancetype)initWithData:(NSData *)data isBinary:(BOOL)isBinary {
+  NSParameterAssert(data);
+  if (self = [super init]) {
+    rtc::Buffer buffer(reinterpret_cast<const uint8_t*>(data.bytes),
+                       data.length);
+    _dataBuffer.reset(new webrtc::DataBuffer(buffer, isBinary));
+  }
+  return self;
+}
+
+- (NSData *)data {
+  return [NSData dataWithBytes:_dataBuffer->data.data()
+                        length:_dataBuffer->data.size()];
+}
+
+- (BOOL)isBinary {
+  return _dataBuffer->binary;
+}
+
+#pragma mark - Private
+
+- (instancetype)initWithNativeBuffer:(const webrtc::DataBuffer&)nativeBuffer {
+  if (self = [super init]) {
+    _dataBuffer.reset(new webrtc::DataBuffer(nativeBuffer));
+  }
+  return self;
+}
+
+- (const webrtc::DataBuffer *)nativeDataBuffer {
+  return _dataBuffer.get();
+}
+
+@end
+
+
+@implementation RTCDataChannel {
+  rtc::scoped_refptr<webrtc::DataChannelInterface> _nativDataChannel;
+  rtc::scoped_ptr<webrtc::DataChannelDelegateAdapter> _observer;
+  BOOL _isObserverRegistered;
+}
+
+@synthesize delegate = _delegate;
+
+- (void)dealloc {
+  // Handles unregistering the observer properly. We need to do this because
+  // there may still be other references to the underlying data channel.
+  self.delegate = nil;
+}
+
+- (NSString *)label {
+  return [NSString stringForStdString:_nativDataChannel->label()];
+}
+
+- (BOOL)isOrdered {
+  return _nativDataChannel->ordered();
+}
+
+- (uint16_t)maxPacketLifeTime {
+  return _nativDataChannel->maxRetransmitTime();
+}
+
+- (uint16_t)maxRetransmits {
+  return _nativDataChannel->maxRetransmits();
+}
+
+- (NSString *)protocol {
+  return [NSString stringForStdString:_nativDataChannel->protocol()];
+}
+
+- (BOOL)isNegotiated {
+  return _nativDataChannel->negotiated();
+}
+
+- (int)id {
+  return _nativDataChannel->id();
+}
+
+- (RTCDataChannelState)readyState {
+  return [[self class] dataChannelStateForNativeState:
+      _nativDataChannel->state()];
+}
+
+- (uint64_t)bufferedAmount {
+  return _nativDataChannel->buffered_amount();
+}
+
+- (void)setDelegate:(id<RTCDataChannelDelegate>)delegate {
+  if (_delegate == delegate) {
+    return;
+  }
+  if (_isObserverRegistered) {
+    _nativDataChannel->UnregisterObserver();
+    _isObserverRegistered = NO;
+  }
+  _delegate = delegate;
+  if (_delegate) {
+    _nativDataChannel->RegisterObserver(_observer.get());
+    _isObserverRegistered = YES;
+  }
+}
+
+- (void)close {
+  _nativDataChannel->Close();
+}
+
+- (BOOL)sendData:(RTCDataBuffer *)data {
+  return _nativDataChannel->Send(*data.nativeDataBuffer);
+}
+
+- (NSString *)description {
+  return [NSString stringWithFormat:@"RTCDataChannel:\n%ld\n%@\n%@",
+                                    (long)self.id,
+                                    self.label,
+                                    [[self class]
+                                        stringForState:self.readyState]];
+}
+
+#pragma mark - Private
+
+- (instancetype)initWithNativeDataChannel:
+    (rtc::scoped_refptr<webrtc::DataChannelInterface>)nativeDataChannel {
+  NSParameterAssert(nativeDataChannel);
+  if (self = [super init]) {
+    _nativDataChannel = nativeDataChannel;
+    _observer.reset(new webrtc::DataChannelDelegateAdapter(self));
+  }
+  return self;
+}
+
++ (webrtc::DataChannelInterface::DataState)
+    nativeDataChannelStateForState:(RTCDataChannelState)state {
+  switch (state) {
+    case RTCDataChannelStateConnecting:
+      return webrtc::DataChannelInterface::DataState::kConnecting;
+    case RTCDataChannelStateOpen:
+      return webrtc::DataChannelInterface::DataState::kOpen;
+    case RTCDataChannelStateClosing:
+      return webrtc::DataChannelInterface::DataState::kClosing;
+    case RTCDataChannelStateClosed:
+      return webrtc::DataChannelInterface::DataState::kClosed;
+  }
+}
+
++ (RTCDataChannelState)dataChannelStateForNativeState:
+    (webrtc::DataChannelInterface::DataState)nativeState {
+  switch (nativeState) {
+    case webrtc::DataChannelInterface::DataState::kConnecting:
+      return RTCDataChannelStateConnecting;
+    case webrtc::DataChannelInterface::DataState::kOpen:
+      return RTCDataChannelStateOpen;
+    case webrtc::DataChannelInterface::DataState::kClosing:
+      return RTCDataChannelStateClosing;
+    case webrtc::DataChannelInterface::DataState::kClosed:
+      return RTCDataChannelStateClosed;
+  }
+}
+
++ (NSString *)stringForState:(RTCDataChannelState)state {
+  switch (state) {
+    case RTCDataChannelStateConnecting:
+      return @"Connecting";
+    case RTCDataChannelStateOpen:
+      return @"Open";
+    case RTCDataChannelStateClosing:
+      return @"Closing";
+    case RTCDataChannelStateClosed:
+      return @"Closed";
+  }
+}
+
+@end