Roll src/third_party/WebKit 88cc93e:f6ae725 (svn 200125:200141)
[chromium-blink-merge.git] / ppapi / proxy / websocket_resource.h
blob51fb52d0626762f92660e3f887a3f5c26cbcfc17
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 #ifndef PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
6 #define PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
8 #include <queue>
10 #include "ppapi/c/ppb_websocket.h"
11 #include "ppapi/proxy/plugin_resource.h"
12 #include "ppapi/shared_impl/tracked_callback.h"
13 #include "ppapi/thunk/ppb_websocket_api.h"
15 namespace ppapi {
17 class StringVar;
18 class Var;
20 namespace proxy {
22 // This class contains protocol checks which doesn't affect security when it
23 // run with untrusted code.
24 class PPAPI_PROXY_EXPORT WebSocketResource
25 : public PluginResource,
26 public NON_EXPORTED_BASE(thunk::PPB_WebSocket_API) {
27 public:
28 WebSocketResource(Connection connection, PP_Instance instance);
29 ~WebSocketResource() override;
31 // PluginResource implementation.
32 thunk::PPB_WebSocket_API* AsPPB_WebSocket_API() override;
34 // PPB_WebSocket_API implementation.
35 int32_t Connect(const PP_Var& url,
36 const PP_Var protocols[],
37 uint32_t protocol_count,
38 scoped_refptr<TrackedCallback> callback) override;
39 int32_t Close(uint16_t code,
40 const PP_Var& reason,
41 scoped_refptr<TrackedCallback> callback) override;
42 int32_t ReceiveMessage(
43 PP_Var* message,
44 scoped_refptr<TrackedCallback> callback) override;
45 int32_t SendMessage(const PP_Var& message) override;
46 uint64_t GetBufferedAmount() override;
47 uint16_t GetCloseCode() override;
48 PP_Var GetCloseReason() override;
49 PP_Bool GetCloseWasClean() override;
50 PP_Var GetExtensions() override;
51 PP_Var GetProtocol() override;
52 PP_WebSocketReadyState GetReadyState() override;
53 PP_Var GetURL() override;
55 private:
56 // PluginResource override.
57 void OnReplyReceived(const ResourceMessageReplyParams& params,
58 const IPC::Message& msg) override;
60 // IPC message handlers.
61 void OnPluginMsgConnectReply(const ResourceMessageReplyParams& params,
62 const std::string& url,
63 const std::string& protocol);
64 void OnPluginMsgCloseReply(const ResourceMessageReplyParams& params,
65 unsigned long buffered_amount,
66 bool was_clean,
67 unsigned short code,
68 const std::string& reason);
69 void OnPluginMsgReceiveTextReply(const ResourceMessageReplyParams& params,
70 const std::string& message);
71 void OnPluginMsgReceiveBinaryReply(const ResourceMessageReplyParams& params,
72 const std::vector<uint8_t>& message);
73 void OnPluginMsgErrorReply(const ResourceMessageReplyParams& params);
74 void OnPluginMsgBufferedAmountReply(const ResourceMessageReplyParams& params,
75 unsigned long buffered_amount);
76 void OnPluginMsgStateReply(const ResourceMessageReplyParams& params,
77 int32_t state);
78 void OnPluginMsgClosedReply(const ResourceMessageReplyParams& params,
79 unsigned long buffered_amount,
80 bool was_clean,
81 unsigned short code,
82 const std::string& reason);
84 // Picks up a received message and moves it to user receiving buffer. This
85 // function is used in both ReceiveMessage for fast returning path, and
86 // OnPluginMsgReceiveTextReply and OnPluginMsgReceiveBinaryReply for delayed
87 // callback invocations.
88 int32_t DoReceive();
90 // Holds user callbacks to invoke later.
91 scoped_refptr<TrackedCallback> connect_callback_;
92 scoped_refptr<TrackedCallback> close_callback_;
93 scoped_refptr<TrackedCallback> receive_callback_;
95 // Represents readyState described in the WebSocket API specification. It can
96 // be read via GetReadyState().
97 PP_WebSocketReadyState state_;
99 // Becomes true if any error is detected. Incoming data will be disposed
100 // if this variable is true, then ReceiveMessage() returns PP_ERROR_FAILED
101 // after returning all received data.
102 bool error_was_received_;
104 // Keeps a pointer to PP_Var which is provided via ReceiveMessage().
105 // Received data will be copied to this PP_Var on ready.
106 PP_Var* receive_callback_var_;
108 // Keeps received data until ReceiveMessage() requests.
109 std::queue<scoped_refptr<Var> > received_messages_;
111 // Keeps empty string for functions to return empty string.
112 scoped_refptr<StringVar> empty_string_;
114 // Keeps the status code field of closing handshake. It can be read via
115 // GetCloseCode().
116 uint16_t close_code_;
118 // Keeps the reason field of closing handshake. It can be read via
119 // GetCloseReason().
120 scoped_refptr<StringVar> close_reason_;
122 // Becomes true when closing handshake is performed successfully. It can be
123 // read via GetCloseWasClean().
124 PP_Bool close_was_clean_;
126 // Represents extensions described in the WebSocket API specification. It can
127 // be read via GetExtensions().
128 scoped_refptr<StringVar> extensions_;
130 // Represents protocol described in the WebSocket API specification. It can be
131 // read via GetProtocol().
132 scoped_refptr<StringVar> protocol_;
134 // Represents url described in the WebSocket API specification. It can be
135 // read via GetURL().
136 scoped_refptr<StringVar> url_;
138 // Keeps the number of bytes of application data that have been queued using
139 // SendMessage(). WebKit side implementation calculates the actual amount.
140 // This is a cached value which is notified through a WebKit callback.
141 // This value is used to calculate bufferedAmount in the WebSocket API
142 // specification. The calculated value can be read via GetBufferedAmount().
143 uint64_t buffered_amount_;
145 // Keeps the number of bytes of application data that have been ignored
146 // because the connection was already closed.
147 // This value is used to calculate bufferedAmount in the WebSocket API
148 // specification. The calculated value can be read via GetBufferedAmount().
149 uint64_t buffered_amount_after_close_;
151 DISALLOW_COPY_AND_ASSIGN(WebSocketResource);
154 } // namespace proxy
155 } // namespace ppapi
157 #endif // PPAPI_PROXY_WEBSOCKET_RESOURCE_H_