Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ppapi / proxy / websocket_resource.h
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 #ifndef PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
6 #define PPAPI_PROXY_WEBSOCKET_RESOURCE_H_
7
8 #include <queue>
9
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"
14
15 namespace ppapi {
16
17 class StringVar;
18 class Var;
19
20 namespace proxy {
21
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   virtual ~WebSocketResource();
30
31   // PluginResource implementation.
32   virtual thunk::PPB_WebSocket_API* AsPPB_WebSocket_API() override;
33
34   // PPB_WebSocket_API implementation.
35   virtual int32_t Connect(const PP_Var& url,
36                           const PP_Var protocols[],
37                           uint32_t protocol_count,
38                           scoped_refptr<TrackedCallback> callback) override;
39   virtual int32_t Close(uint16_t code,
40                         const PP_Var& reason,
41                         scoped_refptr<TrackedCallback> callback) override;
42   virtual int32_t ReceiveMessage(
43       PP_Var* message,
44       scoped_refptr<TrackedCallback> callback) override;
45   virtual int32_t SendMessage(const PP_Var& message) override;
46   virtual uint64_t GetBufferedAmount() override;
47   virtual uint16_t GetCloseCode() override;
48   virtual PP_Var GetCloseReason() override;
49   virtual PP_Bool GetCloseWasClean() override;
50   virtual PP_Var GetExtensions() override;
51   virtual PP_Var GetProtocol() override;
52   virtual PP_WebSocketReadyState GetReadyState() override;
53   virtual PP_Var GetURL() override;
54
55  private:
56   // PluginResource override.
57   virtual void OnReplyReceived(const ResourceMessageReplyParams& params,
58                                const IPC::Message& msg) override;
59
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);
83
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();
89
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_;
94
95   // Represents readyState described in the WebSocket API specification. It can
96   // be read via GetReadyState().
97   PP_WebSocketReadyState state_;
98
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_;
103
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_;
107
108   // Keeps received data until ReceiveMessage() requests.
109   std::queue<scoped_refptr<Var> > received_messages_;
110
111   // Keeps empty string for functions to return empty string.
112   scoped_refptr<StringVar> empty_string_;
113
114   // Keeps the status code field of closing handshake. It can be read via
115   // GetCloseCode().
116   uint16_t close_code_;
117
118   // Keeps the reason field of closing handshake. It can be read via
119   // GetCloseReason().
120   scoped_refptr<StringVar> close_reason_;
121
122   // Becomes true when closing handshake is performed successfully. It can be
123   // read via GetCloseWasClean().
124   PP_Bool close_was_clean_;
125
126   // Represents extensions described in the WebSocket API specification. It can
127   // be read via GetExtensions().
128   scoped_refptr<StringVar> extensions_;
129
130   // Represents protocol described in the WebSocket API specification. It can be
131   // read via GetProtocol().
132   scoped_refptr<StringVar> protocol_;
133
134   // Represents url described in the WebSocket API specification. It can be
135   // read via GetURL().
136   scoped_refptr<StringVar> url_;
137
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_;
144
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_;
150
151   DISALLOW_COPY_AND_ASSIGN(WebSocketResource);
152 };
153
154 }  // namespace proxy
155 }  // namespace ppapi
156
157 #endif  // PPAPI_PROXY_WEBSOCKET_RESOURCE_H_