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 "ppapi/cpp/websocket.h" 6 7 #include "ppapi/c/pp_errors.h" 8 #include "ppapi/c/pp_macros.h" 9 #include "ppapi/cpp/completion_callback.h" 10 #include "ppapi/cpp/instance_handle.h" 11 #include "ppapi/cpp/module.h" 12 #include "ppapi/cpp/module_impl.h" 13 #include "ppapi/cpp/var.h" 14 15 namespace pp { 16 17 namespace { 18 19 template <> const char* interface_name<PPB_WebSocket_1_0>() { 20 return PPB_WEBSOCKET_INTERFACE_1_0; 21 } 22 23 } // namespace 24 25 WebSocket::WebSocket(const InstanceHandle& instance) { 26 if (!has_interface<PPB_WebSocket_1_0>()) 27 return; 28 PassRefFromConstructor(get_interface<PPB_WebSocket_1_0>()->Create( 29 instance.pp_instance())); 30 } 31 32 WebSocket::~WebSocket() { 33 } 34 35 int32_t WebSocket::Connect(const Var& url, const Var protocols[], 36 uint32_t protocol_count, const CompletionCallback& callback) { 37 if (!has_interface<PPB_WebSocket_1_0>()) 38 return PP_ERROR_BADRESOURCE; 39 40 // Convert protocols to C interface. 41 PP_Var *c_protocols = NULL; 42 if (protocol_count) 43 c_protocols = new PP_Var[protocol_count]; 44 for (uint32_t i = 0; i < protocol_count; ++i) 45 c_protocols[i] = protocols[i].pp_var(); 46 47 int32_t result = get_interface<PPB_WebSocket_1_0>()->Connect( 48 pp_resource(), url.pp_var(), c_protocols, protocol_count, 49 callback.pp_completion_callback()); 50 if (c_protocols) 51 delete[] c_protocols; 52 return result; 53 } 54 55 int32_t WebSocket::Close(uint16_t code, const Var& reason, 56 const CompletionCallback& callback) { 57 if (!has_interface<PPB_WebSocket_1_0>()) 58 return PP_ERROR_BADRESOURCE; 59 60 return get_interface<PPB_WebSocket_1_0>()->Close( 61 pp_resource(), code, reason.pp_var(), 62 callback.pp_completion_callback()); 63 } 64 65 int32_t WebSocket::ReceiveMessage(Var* message, 66 const CompletionCallback& callback) { 67 if (!has_interface<PPB_WebSocket_1_0>()) 68 return PP_ERROR_BADRESOURCE; 69 70 // Initialize |message| to release old internal PP_Var of reused |message|. 71 if (message) 72 *message = Var(); 73 74 return get_interface<PPB_WebSocket_1_0>()->ReceiveMessage( 75 pp_resource(), const_cast<PP_Var*>(&message->pp_var()), 76 callback.pp_completion_callback()); 77 } 78 79 int32_t WebSocket::SendMessage(const Var& message) { 80 if (!has_interface<PPB_WebSocket_1_0>()) 81 return PP_ERROR_BADRESOURCE; 82 83 return get_interface<PPB_WebSocket_1_0>()->SendMessage( 84 pp_resource(), message.pp_var()); 85 } 86 87 uint64_t WebSocket::GetBufferedAmount() { 88 if (!has_interface<PPB_WebSocket_1_0>()) 89 return 0; 90 91 return get_interface<PPB_WebSocket_1_0>()->GetBufferedAmount(pp_resource()); 92 } 93 94 uint16_t WebSocket::GetCloseCode() { 95 if (!has_interface<PPB_WebSocket_1_0>()) 96 return 0; 97 98 return get_interface<PPB_WebSocket_1_0>()->GetCloseCode(pp_resource()); 99 } 100 101 Var WebSocket::GetCloseReason() { 102 if (!has_interface<PPB_WebSocket_1_0>()) 103 return 0; 104 105 return Var(PASS_REF, 106 get_interface<PPB_WebSocket_1_0>()->GetCloseReason(pp_resource())); 107 } 108 109 bool WebSocket::GetCloseWasClean() { 110 if (!has_interface<PPB_WebSocket_1_0>()) 111 return false; 112 113 PP_Bool result = 114 get_interface<PPB_WebSocket_1_0>()->GetCloseWasClean(pp_resource()); 115 return PP_ToBool(result); 116 } 117 118 Var WebSocket::GetExtensions() { 119 if (!has_interface<PPB_WebSocket_1_0>()) 120 return Var(); 121 122 return Var(PASS_REF, 123 get_interface<PPB_WebSocket_1_0>()->GetExtensions(pp_resource())); 124 } 125 126 Var WebSocket::GetProtocol() { 127 if (!has_interface<PPB_WebSocket_1_0>()) 128 return Var(); 129 130 return Var(PASS_REF, 131 get_interface<PPB_WebSocket_1_0>()->GetProtocol(pp_resource())); 132 } 133 134 PP_WebSocketReadyState WebSocket::GetReadyState() { 135 if (!has_interface<PPB_WebSocket_1_0>()) 136 return PP_WEBSOCKETREADYSTATE_INVALID; 137 138 return get_interface<PPB_WebSocket_1_0>()->GetReadyState(pp_resource()); 139 } 140 141 Var WebSocket::GetURL() { 142 if (!has_interface<PPB_WebSocket_1_0>()) 143 return Var(); 144 145 return Var(PASS_REF, 146 get_interface<PPB_WebSocket_1_0>()->GetURL(pp_resource())); 147 } 148 149 } // namespace pp 150