1 /* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include "MockWebRTCDataChannelHandler.h" 26 27 #include "public/platform/WebRTCDataChannelHandlerClient.h" 28 #include "public/platform/WebRTCDataChannelInit.h" 29 #include "public/testing/WebTestDelegate.h" 30 #include <assert.h> 31 32 using namespace WebKit; 33 34 namespace WebTestRunner { 35 36 class DataChannelReadyStateTask : public WebMethodTask<MockWebRTCDataChannelHandler> { 37 public: 38 DataChannelReadyStateTask(MockWebRTCDataChannelHandler* object, WebRTCDataChannelHandlerClient* dataChannelClient, WebRTCDataChannelHandlerClient::ReadyState state) 39 : WebMethodTask<MockWebRTCDataChannelHandler>(object) 40 , m_dataChannelClient(dataChannelClient) 41 , m_state(state) 42 { 43 } 44 45 virtual void runIfValid() OVERRIDE 46 { 47 m_dataChannelClient->didChangeReadyState(m_state); 48 } 49 50 private: 51 WebRTCDataChannelHandlerClient* m_dataChannelClient; 52 WebRTCDataChannelHandlerClient::ReadyState m_state; 53 }; 54 55 ///////////////////// 56 57 MockWebRTCDataChannelHandler::MockWebRTCDataChannelHandler(WebString label, const WebRTCDataChannelInit& init, WebTestDelegate* delegate) 58 : m_client(0) 59 , m_label(label) 60 , m_init(init) 61 , m_delegate(delegate) 62 { 63 m_reliable = (init.ordered && init.maxRetransmits == -1 && init.maxRetransmitTime == -1); 64 } 65 66 void MockWebRTCDataChannelHandler::setClient(WebRTCDataChannelHandlerClient* client) 67 { 68 m_client = client; 69 if (m_client) 70 m_delegate->postTask(new DataChannelReadyStateTask(this, m_client, WebRTCDataChannelHandlerClient::ReadyStateOpen)); 71 } 72 73 bool MockWebRTCDataChannelHandler::ordered() const 74 { 75 return m_init.ordered; 76 } 77 78 unsigned short MockWebRTCDataChannelHandler::maxRetransmitTime() const 79 { 80 return m_init.maxRetransmitTime; 81 } 82 83 unsigned short MockWebRTCDataChannelHandler::maxRetransmits() const 84 { 85 return m_init.maxRetransmits; 86 } 87 88 WebString MockWebRTCDataChannelHandler::protocol() const 89 { 90 return m_init.protocol; 91 } 92 93 bool MockWebRTCDataChannelHandler::negotiated() const 94 { 95 return m_init.negotiated; 96 } 97 98 unsigned short MockWebRTCDataChannelHandler::id() const 99 { 100 return m_init.id; 101 } 102 103 unsigned long MockWebRTCDataChannelHandler::bufferedAmount() 104 { 105 return 0; 106 } 107 108 bool MockWebRTCDataChannelHandler::sendStringData(const WebString& data) 109 { 110 assert(m_client); 111 m_client->didReceiveStringData(data); 112 return true; 113 } 114 115 bool MockWebRTCDataChannelHandler::sendRawData(const char* data, size_t size) 116 { 117 assert(m_client); 118 m_client->didReceiveRawData(data, size); 119 return true; 120 } 121 122 void MockWebRTCDataChannelHandler::close() 123 { 124 assert(m_client); 125 m_delegate->postTask(new DataChannelReadyStateTask(this, m_client, WebRTCDataChannelHandlerClient::ReadyStateClosed)); 126 } 127 128 } 129