Pepper_47_C++_interfaces
websocket.cc
Go to the documentation of this file.
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"
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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
void PassRefFromConstructor(PP_Resource resource)
Definition: resource.cc:50
uint16_t GetCloseCode()
Definition: websocket.cc:94
Var GetExtensions()
Definition: websocket.cc:118
bool GetCloseWasClean()
Definition: websocket.cc:109
const PP_CompletionCallback & pp_completion_callback() const
uint64_t GetBufferedAmount()
Definition: websocket.cc:87
PP_WebSocketReadyState GetReadyState()
Definition: websocket.cc:134
const PP_Var & pp_var() const
Definition: var.h:226
WebSocket(const InstanceHandle &instance)
Definition: websocket.cc:25
int32_t Close(uint16_t code, const Var &reason, const CompletionCallback &callback)
Definition: websocket.cc:55
int32_t ReceiveMessage(Var *message, const CompletionCallback &callback)
Definition: websocket.cc:65
int32_t Connect(const Var &url, const Var protocols[], uint32_t protocol_count, const CompletionCallback &callback)
Definition: websocket.cc:35
friend class Var
Definition: resource.h:90
PP_Resource pp_resource() const
Definition: resource.h:47
virtual ~WebSocket()
Destructs a WebSocket object.
Definition: websocket.cc:32
A generic type used for passing data types between the module and the page.
Definition: var.h:21
Var GetProtocol()
Definition: websocket.cc:126
Var GetCloseReason()
Definition: websocket.cc:101
int32_t SendMessage(const Var &message)
Definition: websocket.cc:79
PP_Instance pp_instance() const