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 155 156 157 158 159
|
// 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.
#ifndef PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
#define PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
#include <stdint.h>
#include "base/containers/queue.h"
#include "ppapi/c/ppb_websocket.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/shared_impl/tracked_callback.h"
#include "ppapi/thunk/ppb_websocket_api.h"
namespace ppapi {
class StringVar;
class Var;
namespace proxy {
// This class contains protocol checks which doesn't affect security when it
// run with untrusted code.
class PPAPI_PROXY_EXPORT WebSocketResource : public PluginResource,
public thunk::PPB_WebSocket_API {
public:
WebSocketResource(Connection connection, PP_Instance instance);
WebSocketResource(const WebSocketResource&) = delete;
WebSocketResource& operator=(const WebSocketResource&) = delete;
~WebSocketResource() override;
// PluginResource implementation.
thunk::PPB_WebSocket_API* AsPPB_WebSocket_API() override;
// PPB_WebSocket_API implementation.
int32_t Connect(const PP_Var& url,
const PP_Var protocols[],
uint32_t protocol_count,
scoped_refptr<TrackedCallback> callback) override;
int32_t Close(uint16_t code,
const PP_Var& reason,
scoped_refptr<TrackedCallback> callback) override;
int32_t ReceiveMessage(
PP_Var* message,
scoped_refptr<TrackedCallback> callback) override;
int32_t SendMessage(const PP_Var& message) override;
uint64_t GetBufferedAmount() override;
uint16_t GetCloseCode() override;
PP_Var GetCloseReason() override;
PP_Bool GetCloseWasClean() override;
PP_Var GetExtensions() override;
PP_Var GetProtocol() override;
PP_WebSocketReadyState GetReadyState() override;
PP_Var GetURL() override;
private:
// PluginResource override.
void OnReplyReceived(const ResourceMessageReplyParams& params,
const IPC::Message& msg) override;
// IPC message handlers.
void OnPluginMsgConnectReply(const ResourceMessageReplyParams& params,
const std::string& url,
const std::string& protocol);
void OnPluginMsgCloseReply(const ResourceMessageReplyParams& params,
uint64_t buffered_amount,
bool was_clean,
uint16_t code,
const std::string& reason);
void OnPluginMsgReceiveTextReply(const ResourceMessageReplyParams& params,
const std::string& message);
void OnPluginMsgReceiveBinaryReply(const ResourceMessageReplyParams& params,
const std::vector<uint8_t>& message);
void OnPluginMsgErrorReply(const ResourceMessageReplyParams& params);
void OnPluginMsgBufferedAmountReply(const ResourceMessageReplyParams& params,
uint64_t buffered_amount);
void OnPluginMsgStateReply(const ResourceMessageReplyParams& params,
int32_t state);
void OnPluginMsgClosedReply(const ResourceMessageReplyParams& params,
uint64_t buffered_amount,
bool was_clean,
uint16_t code,
const std::string& reason);
// Picks up a received message and moves it to user receiving buffer. This
// function is used in both ReceiveMessage for fast returning path, and
// OnPluginMsgReceiveTextReply and OnPluginMsgReceiveBinaryReply for delayed
// callback invocations.
int32_t DoReceive();
// Holds user callbacks to invoke later.
scoped_refptr<TrackedCallback> connect_callback_;
scoped_refptr<TrackedCallback> close_callback_;
scoped_refptr<TrackedCallback> receive_callback_;
// Represents readyState described in the WebSocket API specification. It can
// be read via GetReadyState().
PP_WebSocketReadyState state_;
// Becomes true if any error is detected. Incoming data will be disposed
// if this variable is true, then ReceiveMessage() returns PP_ERROR_FAILED
// after returning all received data.
bool error_was_received_;
// Keeps a pointer to PP_Var which is provided via ReceiveMessage().
// Received data will be copied to this PP_Var on ready.
PP_Var* receive_callback_var_;
// Keeps received data until ReceiveMessage() requests.
base::queue<scoped_refptr<Var>> received_messages_;
// Keeps empty string for functions to return empty string.
scoped_refptr<StringVar> empty_string_;
// Keeps the status code field of closing handshake. It can be read via
// GetCloseCode().
uint16_t close_code_;
// Keeps the reason field of closing handshake. It can be read via
// GetCloseReason().
scoped_refptr<StringVar> close_reason_;
// Becomes true when closing handshake is performed successfully. It can be
// read via GetCloseWasClean().
PP_Bool close_was_clean_;
// Represents extensions described in the WebSocket API specification. It can
// be read via GetExtensions().
scoped_refptr<StringVar> extensions_;
// Represents protocol described in the WebSocket API specification. It can be
// read via GetProtocol().
scoped_refptr<StringVar> protocol_;
// Represents url described in the WebSocket API specification. It can be
// read via GetURL().
scoped_refptr<StringVar> url_;
// Keeps the number of bytes of application data that have been queued using
// SendMessage(). WebKit side implementation calculates the actual amount.
// This is a cached value which is notified through a WebKit callback.
// This value is used to calculate bufferedAmount in the WebSocket API
// specification. The calculated value can be read via GetBufferedAmount().
uint64_t buffered_amount_;
// Keeps the number of bytes of application data that have been ignored
// because the connection was already closed.
// This value is used to calculate bufferedAmount in the WebSocket API
// specification. The calculated value can be read via GetBufferedAmount().
uint64_t buffered_amount_after_close_;
};
} // namespace proxy
} // namespace ppapi
#endif // PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
|