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"
19 template <> const char* interface_name
<PPB_WebSocket_1_0
>() {
20 return PPB_WEBSOCKET_INTERFACE_1_0
;
25 WebSocket::WebSocket(const InstanceHandle
& instance
) {
26 if (!has_interface
<PPB_WebSocket_1_0
>())
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
;
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();
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());
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
;
60 return get_interface
<PPB_WebSocket_1_0
>()->Close(
61 pp_resource(), code
, reason
.pp_var(),
62 callback
.pp_completion_callback());
65 int32_t WebSocket::ReceiveMessage(Var
* message
,
66 const CompletionCallback
& callback
) {
67 if (!has_interface
<PPB_WebSocket_1_0
>())
68 return PP_ERROR_BADRESOURCE
;
70 // Initialize |message| to release old internal PP_Var of reused |message|.
74 return get_interface
<PPB_WebSocket_1_0
>()->ReceiveMessage(
75 pp_resource(), const_cast<PP_Var
*>(&message
->pp_var()),
76 callback
.pp_completion_callback());
79 int32_t WebSocket::SendMessage(const Var
& message
) {
80 if (!has_interface
<PPB_WebSocket_1_0
>())
81 return PP_ERROR_BADRESOURCE
;
83 return get_interface
<PPB_WebSocket_1_0
>()->SendMessage(
84 pp_resource(), message
.pp_var());
87 uint64_t WebSocket::GetBufferedAmount() {
88 if (!has_interface
<PPB_WebSocket_1_0
>())
91 return get_interface
<PPB_WebSocket_1_0
>()->GetBufferedAmount(pp_resource());
94 uint16_t WebSocket::GetCloseCode() {
95 if (!has_interface
<PPB_WebSocket_1_0
>())
98 return get_interface
<PPB_WebSocket_1_0
>()->GetCloseCode(pp_resource());
101 Var
WebSocket::GetCloseReason() {
102 if (!has_interface
<PPB_WebSocket_1_0
>())
106 get_interface
<PPB_WebSocket_1_0
>()->GetCloseReason(pp_resource()));
109 bool WebSocket::GetCloseWasClean() {
110 if (!has_interface
<PPB_WebSocket_1_0
>())
114 get_interface
<PPB_WebSocket_1_0
>()->GetCloseWasClean(pp_resource());
115 return PP_ToBool(result
);
118 Var
WebSocket::GetExtensions() {
119 if (!has_interface
<PPB_WebSocket_1_0
>())
123 get_interface
<PPB_WebSocket_1_0
>()->GetExtensions(pp_resource()));
126 Var
WebSocket::GetProtocol() {
127 if (!has_interface
<PPB_WebSocket_1_0
>())
131 get_interface
<PPB_WebSocket_1_0
>()->GetProtocol(pp_resource()));
134 PP_WebSocketReadyState
WebSocket::GetReadyState() {
135 if (!has_interface
<PPB_WebSocket_1_0
>())
136 return PP_WEBSOCKETREADYSTATE_INVALID
;
138 return get_interface
<PPB_WebSocket_1_0
>()->GetReadyState(pp_resource());
141 Var
WebSocket::GetURL() {
142 if (!has_interface
<PPB_WebSocket_1_0
>())
146 get_interface
<PPB_WebSocket_1_0
>()->GetURL(pp_resource()));