1// Copyright 2014 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/shell/renderer/test_runner/mock_webrtc_data_channel_handler.h"
6
7#include "base/logging.h"
8#include "content/shell/renderer/test_runner/web_test_delegate.h"
9#include "third_party/WebKit/public/platform/WebRTCDataChannelHandlerClient.h"
10
11using namespace blink;
12
13namespace content {
14
15class DataChannelReadyStateTask
16    : public WebMethodTask<MockWebRTCDataChannelHandler> {
17 public:
18  DataChannelReadyStateTask(MockWebRTCDataChannelHandler* object,
19                            WebRTCDataChannelHandlerClient* data_channel_client,
20                            WebRTCDataChannelHandlerClient::ReadyState state)
21      : WebMethodTask<MockWebRTCDataChannelHandler>(object),
22        data_channel_client_(data_channel_client),
23        state_(state) {}
24
25  virtual void RunIfValid() OVERRIDE {
26    data_channel_client_->didChangeReadyState(state_);
27  }
28
29 private:
30  WebRTCDataChannelHandlerClient* data_channel_client_;
31  WebRTCDataChannelHandlerClient::ReadyState state_;
32};
33
34/////////////////////
35
36MockWebRTCDataChannelHandler::MockWebRTCDataChannelHandler(
37    WebString label,
38    const WebRTCDataChannelInit& init,
39    WebTestDelegate* delegate)
40    : client_(0), label_(label), init_(init), delegate_(delegate) {
41  reliable_ = (init.ordered && init.maxRetransmits == -1 &&
42               init.maxRetransmitTime == -1);
43}
44
45void MockWebRTCDataChannelHandler::setClient(
46    WebRTCDataChannelHandlerClient* client) {
47  client_ = client;
48  if (client_)
49    delegate_->PostTask(new DataChannelReadyStateTask(
50        this, client_, WebRTCDataChannelHandlerClient::ReadyStateOpen));
51}
52
53blink::WebString MockWebRTCDataChannelHandler::label() {
54  return label_;
55}
56
57bool MockWebRTCDataChannelHandler::isReliable() {
58  return reliable_;
59}
60
61bool MockWebRTCDataChannelHandler::ordered() const {
62  return init_.ordered;
63}
64
65unsigned short MockWebRTCDataChannelHandler::maxRetransmitTime() const {
66  return init_.maxRetransmitTime;
67}
68
69unsigned short MockWebRTCDataChannelHandler::maxRetransmits() const {
70  return init_.maxRetransmits;
71}
72
73WebString MockWebRTCDataChannelHandler::protocol() const {
74  return init_.protocol;
75}
76
77bool MockWebRTCDataChannelHandler::negotiated() const {
78  return init_.negotiated;
79}
80
81unsigned short MockWebRTCDataChannelHandler::id() const {
82  return init_.id;
83}
84
85unsigned long MockWebRTCDataChannelHandler::bufferedAmount() {
86  return 0;
87}
88
89bool MockWebRTCDataChannelHandler::sendStringData(const WebString& data) {
90  DCHECK(client_);
91  client_->didReceiveStringData(data);
92  return true;
93}
94
95bool MockWebRTCDataChannelHandler::sendRawData(const char* data, size_t size) {
96  DCHECK(client_);
97  client_->didReceiveRawData(data, size);
98  return true;
99}
100
101void MockWebRTCDataChannelHandler::close() {
102  DCHECK(client_);
103  delegate_->PostTask(new DataChannelReadyStateTask(
104      this, client_, WebRTCDataChannelHandlerClient::ReadyStateClosed));
105}
106
107}  // namespace content
108