OLD | NEW |
| (Empty) |
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 WEBKIT_PLUGINS_PPAPI_PPB_WEBSOCKET_IMPL_H_ | |
6 #define WEBKIT_PLUGINS_PPAPI_PPB_WEBSOCKET_IMPL_H_ | |
7 | |
8 #include <queue> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "ppapi/shared_impl/resource.h" | |
13 #include "ppapi/shared_impl/tracked_callback.h" | |
14 #include "ppapi/thunk/ppb_websocket_api.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocket.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocketClient.h" | |
17 | |
18 namespace ppapi { | |
19 class StringVar; | |
20 class Var; | |
21 } // namespace ppapi | |
22 | |
23 namespace webkit { | |
24 namespace ppapi { | |
25 | |
26 // All implementation is in this class for now. We should move some common | |
27 // implementation to shared_impl when we implement proxy interfaces. | |
28 class PPB_WebSocket_Impl : public ::ppapi::Resource, | |
29 public ::ppapi::thunk::PPB_WebSocket_API, | |
30 public ::WebKit::WebSocketClient { | |
31 public: | |
32 explicit PPB_WebSocket_Impl(PP_Instance instance); | |
33 virtual ~PPB_WebSocket_Impl(); | |
34 | |
35 static PP_Resource Create(PP_Instance instance); | |
36 | |
37 // Resource overrides. | |
38 // Returns the pointer to the thunk::PPB_WebSocket_API if it's supported. | |
39 virtual ::ppapi::thunk::PPB_WebSocket_API* AsPPB_WebSocket_API() OVERRIDE; | |
40 | |
41 // PPB_WebSocket_API implementation. | |
42 virtual int32_t Connect( | |
43 PP_Var url, | |
44 const PP_Var protocols[], | |
45 uint32_t protocol_count, | |
46 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
47 virtual int32_t Close( | |
48 uint16_t code, | |
49 PP_Var reason, | |
50 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
51 virtual int32_t ReceiveMessage( | |
52 PP_Var* message, | |
53 scoped_refptr< ::ppapi::TrackedCallback> callback) OVERRIDE; | |
54 virtual int32_t SendMessage(PP_Var message) OVERRIDE; | |
55 virtual uint64_t GetBufferedAmount() OVERRIDE; | |
56 virtual uint16_t GetCloseCode() OVERRIDE; | |
57 virtual PP_Var GetCloseReason() OVERRIDE; | |
58 virtual PP_Bool GetCloseWasClean() OVERRIDE; | |
59 virtual PP_Var GetExtensions() OVERRIDE; | |
60 virtual PP_Var GetProtocol() OVERRIDE; | |
61 virtual PP_WebSocketReadyState GetReadyState() OVERRIDE; | |
62 virtual PP_Var GetURL() OVERRIDE; | |
63 | |
64 // WebSocketClient implementation. | |
65 virtual void didConnect(); | |
66 virtual void didReceiveMessage(const WebKit::WebString& message); | |
67 virtual void didReceiveArrayBuffer(const WebKit::WebArrayBuffer& binaryData); | |
68 virtual void didReceiveMessageError(); | |
69 virtual void didUpdateBufferedAmount(unsigned long buffered_amount); | |
70 virtual void didStartClosingHandshake(); | |
71 virtual void didClose(unsigned long unhandled_buffered_amount, | |
72 ClosingHandshakeCompletionStatus status, | |
73 unsigned short code, | |
74 const WebKit::WebString& reason); | |
75 private: | |
76 // Picks up a received message and moves it to user receiving buffer. This | |
77 // function is used in both ReceiveMessage for fast returning path and | |
78 // didReceiveMessage callback. | |
79 int32_t DoReceive(); | |
80 | |
81 // Keeps the WebKit side WebSocket object. This is used for calling WebKit | |
82 // side functions via WebKit API. | |
83 scoped_ptr<WebKit::WebSocket> websocket_; | |
84 | |
85 // Represents readyState described in the WebSocket API specification. It can | |
86 // be read via GetReadyState(). | |
87 PP_WebSocketReadyState state_; | |
88 | |
89 // Becomes true if any error is detected. Incoming data will be disposed | |
90 // if this variable is true, then ReceiveMessage() returns PP_ERROR_FAILED | |
91 // after returning all received data. | |
92 bool error_was_received_; | |
93 | |
94 // Keeps a completion callback for asynchronous Connect(). | |
95 scoped_refptr< ::ppapi::TrackedCallback> connect_callback_; | |
96 | |
97 // Keeps a completion callback for asynchronous ReceiveMessage(). | |
98 scoped_refptr< ::ppapi::TrackedCallback> receive_callback_; | |
99 | |
100 // Keeps a pointer to PP_Var which is provided via ReceiveMessage(). | |
101 // Received data will be copied to this PP_Var on ready. | |
102 PP_Var* receive_callback_var_; | |
103 | |
104 // Becomes true when asynchronous ReceiveMessage() is processed. | |
105 bool wait_for_receive_; | |
106 | |
107 // Keeps received data until ReceiveMessage() requests. | |
108 std::queue< scoped_refptr< ::ppapi::Var> > received_messages_; | |
109 | |
110 // Keeps a completion callback for asynchronous Close(). | |
111 scoped_refptr< ::ppapi::TrackedCallback> close_callback_; | |
112 | |
113 // Keeps the status code field of closing handshake. It can be read via | |
114 // GetCloseCode(). | |
115 uint16_t close_code_; | |
116 | |
117 // Keeps the reason field of closing handshake. It can be read via | |
118 // GetCloseReason(). | |
119 scoped_refptr< ::ppapi::StringVar> close_reason_; | |
120 | |
121 // Becomes true when closing handshake is performed successfully. It can be | |
122 // read via GetCloseWasClean(). | |
123 PP_Bool close_was_clean_; | |
124 | |
125 // Keeps empty string for functions to return empty string. | |
126 scoped_refptr< ::ppapi::StringVar> empty_string_; | |
127 | |
128 // Represents extensions described in the WebSocket API specification. It can | |
129 // be read via GetExtensions(). | |
130 scoped_refptr< ::ppapi::StringVar> extensions_; | |
131 | |
132 // Represents url described in the WebSocket API specification. It can be | |
133 // read via GetURL(). | |
134 scoped_refptr< ::ppapi::StringVar> url_; | |
135 | |
136 // Keeps the number of bytes of application data that have been queued using | |
137 // SendMessage(). WebKit side implementation calculates the actual amount. | |
138 // This is a cached value which is notified through a WebKit callback. | |
139 // This value is used to calculate bufferedAmount in the WebSocket API | |
140 // specification. The calculated value can be read via GetBufferedAmount(). | |
141 uint64_t buffered_amount_; | |
142 | |
143 // Keeps the number of bytes of application data that have been ignored | |
144 // because the connection was already closed. | |
145 // This value is used to calculate bufferedAmount in the WebSocket API | |
146 // specification. The calculated value can be read via GetBufferedAmount(). | |
147 uint64_t buffered_amount_after_close_; | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(PPB_WebSocket_Impl); | |
150 }; | |
151 | |
152 } // namespace ppapi | |
153 } // namespace webkit | |
154 | |
155 #endif // WEBKIT_PLUGINS_PPAPI_PPB_WEBSOCKET_IMPL_H_ | |
OLD | NEW |