File: websocket.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (154 lines) | stat: -rw-r--r-- 4,217 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ppapi/cpp/websocket.h"

#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_macros.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/module_impl.h"
#include "ppapi/cpp/var.h"

namespace pp {

namespace {

template <> const char* interface_name<PPB_WebSocket_1_0>() {
  return PPB_WEBSOCKET_INTERFACE_1_0;
}

}  // namespace

WebSocket::WebSocket(const InstanceHandle& instance) {
  if (!has_interface<PPB_WebSocket_1_0>())
    return;
  PassRefFromConstructor(get_interface<PPB_WebSocket_1_0>()->Create(
    instance.pp_instance()));
}

WebSocket::~WebSocket() {
}

int32_t WebSocket::Connect(const Var& url, const Var protocols[],
    uint32_t protocol_count, const CompletionCallback& callback) {
  if (!has_interface<PPB_WebSocket_1_0>())
    return PP_ERROR_BADRESOURCE;

  // Convert protocols to C interface.
  PP_Var *c_protocols = NULL;
  if (protocol_count)
    c_protocols = new PP_Var[protocol_count];
  for (uint32_t i = 0; i < protocol_count; ++i)
    c_protocols[i] = protocols[i].pp_var();

  int32_t result = get_interface<PPB_WebSocket_1_0>()->Connect(
      pp_resource(), url.pp_var(), c_protocols, protocol_count,
      callback.pp_completion_callback());
  if (c_protocols)
    delete[] c_protocols;
  return result;
}

int32_t WebSocket::Close(uint16_t code, const Var& reason,
    const CompletionCallback& callback) {
  if (!has_interface<PPB_WebSocket_1_0>())
    return PP_ERROR_BADRESOURCE;

  return get_interface<PPB_WebSocket_1_0>()->Close(
      pp_resource(), code, reason.pp_var(),
      callback.pp_completion_callback());
}

int32_t WebSocket::ReceiveMessage(Var* message,
    const CompletionCallback& callback) {
  if (!has_interface<PPB_WebSocket_1_0>())
    return PP_ERROR_BADRESOURCE;

  // Initialize |message| to release old internal PP_Var of reused |message|.
  if (message)
    *message = Var();

  return get_interface<PPB_WebSocket_1_0>()->ReceiveMessage(
      pp_resource(), const_cast<PP_Var*>(&message->pp_var()),
      callback.pp_completion_callback());
}

int32_t WebSocket::SendMessage(const Var& message) {
  if (!has_interface<PPB_WebSocket_1_0>())
    return PP_ERROR_BADRESOURCE;

  return get_interface<PPB_WebSocket_1_0>()->SendMessage(
      pp_resource(), message.pp_var());
}

uint64_t WebSocket::GetBufferedAmount() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return 0;

  return get_interface<PPB_WebSocket_1_0>()->GetBufferedAmount(pp_resource());
}

uint16_t WebSocket::GetCloseCode() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return 0;

  return get_interface<PPB_WebSocket_1_0>()->GetCloseCode(pp_resource());
}

Var WebSocket::GetCloseReason() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return 0;

  return Var(PASS_REF,
      get_interface<PPB_WebSocket_1_0>()->GetCloseReason(pp_resource()));
}

bool WebSocket::GetCloseWasClean() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return false;

  PP_Bool result =
      get_interface<PPB_WebSocket_1_0>()->GetCloseWasClean(pp_resource());
  return PP_ToBool(result);
}

Var WebSocket::GetExtensions() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return Var();

  return Var(PASS_REF,
             get_interface<PPB_WebSocket_1_0>()->GetExtensions(pp_resource()));
}

Var WebSocket::GetProtocol() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return Var();

  return Var(PASS_REF,
             get_interface<PPB_WebSocket_1_0>()->GetProtocol(pp_resource()));
}

PP_WebSocketReadyState WebSocket::GetReadyState() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return PP_WEBSOCKETREADYSTATE_INVALID;

  return get_interface<PPB_WebSocket_1_0>()->GetReadyState(pp_resource());
}

Var WebSocket::GetURL() {
  if (!has_interface<PPB_WebSocket_1_0>())
    return Var();

  return Var(PASS_REF,
             get_interface<PPB_WebSocket_1_0>()->GetURL(pp_resource()));
}

}  // namespace pp