OLD | NEW |
(Empty) | |
| 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.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>() { |
| 20 return PPB_WEBSOCKET_INTERFACE; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 WebSocket::WebSocket(Instance* instance) { |
| 26 if (!has_interface<PPB_WebSocket>()) |
| 27 return; |
| 28 PassRefFromConstructor(get_interface<PPB_WebSocket>()->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>()) |
| 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 if (!c_protocols) |
| 45 return PP_ERROR_NOMEMORY; |
| 46 } |
| 47 for (uint32_t i = 0; i < protocol_count; ++i) |
| 48 c_protocols[i] = protocols[i].pp_var(); |
| 49 |
| 50 int32_t result = get_interface<PPB_WebSocket>()->Connect( |
| 51 pp_resource(), url.pp_var(), c_protocols, protocol_count, |
| 52 callback.pp_completion_callback()); |
| 53 if (c_protocols) |
| 54 delete[] c_protocols; |
| 55 return result; |
| 56 } |
| 57 |
| 58 int32_t WebSocket::Close(uint16_t code, const Var& reason, |
| 59 const CompletionCallback& callback) { |
| 60 if (!has_interface<PPB_WebSocket>()) |
| 61 return PP_ERROR_BADRESOURCE; |
| 62 |
| 63 return get_interface<PPB_WebSocket>()->Close( |
| 64 pp_resource(), code, reason.pp_var(), |
| 65 callback.pp_completion_callback()); |
| 66 } |
| 67 |
| 68 int32_t WebSocket::ReceiveMessage(Var* message, |
| 69 const CompletionCallback& callback) { |
| 70 if (!has_interface<PPB_WebSocket>()) |
| 71 return PP_ERROR_BADRESOURCE; |
| 72 |
| 73 return get_interface<PPB_WebSocket>()->ReceiveMessage( |
| 74 pp_resource(), const_cast<PP_Var*>(&message->pp_var()), |
| 75 callback.pp_completion_callback()); |
| 76 } |
| 77 |
| 78 int32_t WebSocket::SendMessage(const Var& message) { |
| 79 if (!has_interface<PPB_WebSocket>()) |
| 80 return PP_ERROR_BADRESOURCE; |
| 81 |
| 82 return get_interface<PPB_WebSocket>()->SendMessage( |
| 83 pp_resource(), message.pp_var()); |
| 84 } |
| 85 |
| 86 uint64_t WebSocket::GetBufferedAmount() { |
| 87 if (!has_interface<PPB_WebSocket>()) |
| 88 return 0; |
| 89 |
| 90 return get_interface<PPB_WebSocket>()->GetBufferedAmount(pp_resource()); |
| 91 } |
| 92 |
| 93 uint16_t WebSocket::GetCloseCode() { |
| 94 if (!has_interface<PPB_WebSocket>()) |
| 95 return 0; |
| 96 |
| 97 return get_interface<PPB_WebSocket>()->GetCloseCode(pp_resource()); |
| 98 } |
| 99 |
| 100 Var WebSocket::GetCloseReason() { |
| 101 if (!has_interface<PPB_WebSocket>()) |
| 102 return 0; |
| 103 |
| 104 return Var(Var::PassRef(), |
| 105 get_interface<PPB_WebSocket>()->GetCloseReason(pp_resource())); |
| 106 } |
| 107 |
| 108 bool WebSocket::GetCloseWasClean() { |
| 109 if (!has_interface<PPB_WebSocket>()) |
| 110 return false; |
| 111 |
| 112 PP_Bool result = |
| 113 get_interface<PPB_WebSocket>()->GetCloseWasClean(pp_resource()); |
| 114 return PP_ToBool(result); |
| 115 } |
| 116 |
| 117 Var WebSocket::GetExtensions() { |
| 118 if (!has_interface<PPB_WebSocket>()) |
| 119 return Var(); |
| 120 |
| 121 return Var(Var::PassRef(), |
| 122 get_interface<PPB_WebSocket>()->GetExtensions(pp_resource())); |
| 123 } |
| 124 |
| 125 Var WebSocket::GetProtocol() { |
| 126 if (!has_interface<PPB_WebSocket>()) |
| 127 return Var(); |
| 128 |
| 129 return Var(Var::PassRef(), |
| 130 get_interface<PPB_WebSocket>()->GetProtocol(pp_resource())); |
| 131 } |
| 132 |
| 133 PP_WebSocketReadyState WebSocket::GetReadyState() { |
| 134 if (!has_interface<PPB_WebSocket>()) |
| 135 return PP_WEBSOCKETREADYSTATE_INVALID; |
| 136 |
| 137 return get_interface<PPB_WebSocket>()->GetReadyState(pp_resource()); |
| 138 } |
| 139 |
| 140 Var WebSocket::GetURL() { |
| 141 if (!has_interface<PPB_WebSocket>()) |
| 142 return Var(); |
| 143 |
| 144 return Var(Var::PassRef(), |
| 145 get_interface<PPB_WebSocket>()->GetURL(pp_resource())); |
| 146 } |
| 147 |
| 148 } // namespace pp |
OLD | NEW |