Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / ppapi / cpp / websocket.cc
blobf0ae5fa0c30075ddef20cfc14a2f0b65983b0833
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.
5 #include "ppapi/cpp/websocket.h"
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"
15 namespace pp {
17 namespace {
19 template <> const char* interface_name<PPB_WebSocket_1_0>() {
20 return PPB_WEBSOCKET_INTERFACE_1_0;
23 } // namespace
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()));
32 WebSocket::~WebSocket() {
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;
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;
47 for (uint32_t i = 0; i < protocol_count; ++i)
48 c_protocols[i] = protocols[i].pp_var();
50 int32_t result = get_interface<PPB_WebSocket_1_0>()->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;
58 int32_t WebSocket::Close(uint16_t code, const Var& reason,
59 const CompletionCallback& callback) {
60 if (!has_interface<PPB_WebSocket_1_0>())
61 return PP_ERROR_BADRESOURCE;
63 return get_interface<PPB_WebSocket_1_0>()->Close(
64 pp_resource(), code, reason.pp_var(),
65 callback.pp_completion_callback());
68 int32_t WebSocket::ReceiveMessage(Var* message,
69 const CompletionCallback& callback) {
70 if (!has_interface<PPB_WebSocket_1_0>())
71 return PP_ERROR_BADRESOURCE;
73 // Initialize |message| to release old internal PP_Var of reused |message|.
74 if (message)
75 *message = Var();
77 return get_interface<PPB_WebSocket_1_0>()->ReceiveMessage(
78 pp_resource(), const_cast<PP_Var*>(&message->pp_var()),
79 callback.pp_completion_callback());
82 int32_t WebSocket::SendMessage(const Var& message) {
83 if (!has_interface<PPB_WebSocket_1_0>())
84 return PP_ERROR_BADRESOURCE;
86 return get_interface<PPB_WebSocket_1_0>()->SendMessage(
87 pp_resource(), message.pp_var());
90 uint64_t WebSocket::GetBufferedAmount() {
91 if (!has_interface<PPB_WebSocket_1_0>())
92 return 0;
94 return get_interface<PPB_WebSocket_1_0>()->GetBufferedAmount(pp_resource());
97 uint16_t WebSocket::GetCloseCode() {
98 if (!has_interface<PPB_WebSocket_1_0>())
99 return 0;
101 return get_interface<PPB_WebSocket_1_0>()->GetCloseCode(pp_resource());
104 Var WebSocket::GetCloseReason() {
105 if (!has_interface<PPB_WebSocket_1_0>())
106 return 0;
108 return Var(PASS_REF,
109 get_interface<PPB_WebSocket_1_0>()->GetCloseReason(pp_resource()));
112 bool WebSocket::GetCloseWasClean() {
113 if (!has_interface<PPB_WebSocket_1_0>())
114 return false;
116 PP_Bool result =
117 get_interface<PPB_WebSocket_1_0>()->GetCloseWasClean(pp_resource());
118 return PP_ToBool(result);
121 Var WebSocket::GetExtensions() {
122 if (!has_interface<PPB_WebSocket_1_0>())
123 return Var();
125 return Var(PASS_REF,
126 get_interface<PPB_WebSocket_1_0>()->GetExtensions(pp_resource()));
129 Var WebSocket::GetProtocol() {
130 if (!has_interface<PPB_WebSocket_1_0>())
131 return Var();
133 return Var(PASS_REF,
134 get_interface<PPB_WebSocket_1_0>()->GetProtocol(pp_resource()));
137 PP_WebSocketReadyState WebSocket::GetReadyState() {
138 if (!has_interface<PPB_WebSocket_1_0>())
139 return PP_WEBSOCKETREADYSTATE_INVALID;
141 return get_interface<PPB_WebSocket_1_0>()->GetReadyState(pp_resource());
144 Var WebSocket::GetURL() {
145 if (!has_interface<PPB_WebSocket_1_0>())
146 return Var();
148 return Var(PASS_REF,
149 get_interface<PPB_WebSocket_1_0>()->GetURL(pp_resource()));
152 } // namespace pp