OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | |
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/containers/hash_tables.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/time/time.h" | |
18 #include "base/timer/timer.h" | |
19 #include "content/common/content_export.h" | |
20 #include "content/common/websocket.h" | |
21 #include "content/public/browser/browser_message_filter.h" | |
22 | |
23 namespace net { | |
24 class URLRequestContext; | |
25 } // namespace net | |
26 | |
27 namespace storage { | |
28 class BlobStorageContext; | |
29 } | |
30 | |
31 namespace content { | |
32 | |
33 class ChromeBlobStorageContext; | |
34 class StoragePartition; | |
35 struct WebSocketHandshakeRequest; | |
36 struct WebSocketHandshakeResponse; | |
37 class WebSocketHost; | |
38 | |
39 // Creates a WebSocketHost object for each WebSocket channel, and dispatches | |
40 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. | |
41 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { | |
42 public: | |
43 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; | |
44 | |
45 // Given a routing_id and delay, WebSocketHostFactory returns a new | |
46 // instance of WebSocketHost or its subclass. | |
47 typedef base::Callback<WebSocketHost*(int, base::TimeDelta)> | |
48 WebSocketHostFactory; | |
49 | |
50 // Return value for methods that may delete the WebSocketHost. This enum is | |
51 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make | |
52 // conversion cheap. By using a separate enum including net/ header files can | |
53 // be avoided. | |
54 enum WebSocketHostState { | |
55 WEBSOCKET_HOST_ALIVE, | |
56 WEBSOCKET_HOST_DELETED | |
57 }; | |
58 | |
59 WebSocketDispatcherHost(int process_id, | |
60 const GetRequestContextCallback& get_context_callback, | |
61 ChromeBlobStorageContext* blob_storage_context, | |
62 StoragePartition* storage_partition); | |
63 | |
64 // BrowserMessageFilter: | |
65 bool OnMessageReceived(const IPC::Message& message) override; | |
66 | |
67 // The following methods are used by WebSocketHost::EventInterface to send | |
68 // IPCs from the browser to the renderer or child process. Any of them may | |
69 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure, | |
70 // leading to the WebSocketChannel and EventInterface also being deleted. | |
71 | |
72 // Sends a WebSocketMsg_AddChannelResponse IPC. | |
73 WebSocketHostState SendAddChannelResponse( | |
74 int routing_id, | |
75 const std::string& selected_protocol, | |
76 const std::string& extensions) WARN_UNUSED_RESULT; | |
77 | |
78 // Sends a WebSocketMsg_SendFrame IPC. | |
79 WebSocketHostState SendFrame(int routing_id, | |
80 bool fin, | |
81 WebSocketMessageType type, | |
82 const std::vector<char>& data); | |
83 | |
84 // Sends a WebSocketMsg_FlowControl IPC. | |
85 WebSocketHostState SendFlowControl(int routing_id, | |
86 int64_t quota) WARN_UNUSED_RESULT; | |
87 | |
88 // Sends a WebSocketMsg_NotifyClosing IPC | |
89 WebSocketHostState NotifyClosingHandshake(int routing_id) WARN_UNUSED_RESULT; | |
90 | |
91 // Sends a WebSocketMsg_NotifyStartOpeningHandshake IPC. | |
92 WebSocketHostState NotifyStartOpeningHandshake( | |
93 int routing_id, | |
94 const WebSocketHandshakeRequest& request) WARN_UNUSED_RESULT; | |
95 | |
96 // Sends a WebSocketMsg_NotifyFinishOpeningHandshake IPC. | |
97 WebSocketHostState NotifyFinishOpeningHandshake( | |
98 int routing_id, | |
99 const WebSocketHandshakeResponse& response) WARN_UNUSED_RESULT; | |
100 | |
101 // Sends a WebSocketMsg_NotifyFailure IPC and deletes and unregisters the | |
102 // channel. | |
103 WebSocketHostState NotifyFailure( | |
104 int routing_id, | |
105 const std::string& message) WARN_UNUSED_RESULT; | |
106 | |
107 WebSocketHostState BlobSendComplete(int routing_id); | |
108 | |
109 // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the | |
110 // channel. | |
111 WebSocketHostState DoDropChannel(int routing_id, | |
112 bool was_clean, | |
113 uint16_t code, | |
114 const std::string& reason) | |
115 WARN_UNUSED_RESULT; | |
116 | |
117 // Returns whether the associated renderer process can read raw cookies. | |
118 bool CanReadRawCookies() const; | |
119 | |
120 int render_process_id() const { return process_id_; } | |
121 | |
122 // Returns a BlobStorageContext associated with this object's render process. | |
123 // The pointer will be valid for as long this object is. | |
124 storage::BlobStorageContext* blob_storage_context() const; | |
125 | |
126 // Returns the StoragePartition associated with this render process. | |
127 StoragePartition* storage_partition() const { return storage_partition_; } | |
128 | |
129 protected: | |
130 // For testing. Specify a factory method that creates mock version of | |
131 // WebSocketHost. | |
132 WebSocketDispatcherHost(int process_id, | |
133 const GetRequestContextCallback& get_context_callback, | |
134 const WebSocketHostFactory& websocket_host_factory); | |
135 | |
136 int num_pending_connections() const { return num_pending_connections_; } | |
137 | |
138 // The number of handshakes that failed/succeeded in the current and | |
139 // previous time period, respectively. | |
140 int64_t num_failed_connections() const; | |
141 int64_t num_succeeded_connections() const; | |
142 | |
143 ~WebSocketDispatcherHost() override; | |
144 | |
145 private: | |
146 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; | |
147 | |
148 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay); | |
149 | |
150 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one | |
151 // is found, or NULL otherwise. | |
152 WebSocketHost* GetHost(int routing_id) const; | |
153 | |
154 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() | |
155 // method. If sending the IPC fails, assumes that this connection is no | |
156 // longer useable, calls DeleteWebSocketHost(), and returns | |
157 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. | |
158 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; | |
159 | |
160 // Deletes the WebSocketHost object associated with the given |routing_id| and | |
161 // removes it from the |hosts_| table. | |
162 void DeleteWebSocketHost(int routing_id); | |
163 | |
164 // Calculates the delay for per-renderer WebSocket throttling. | |
165 base::TimeDelta CalculateDelay() const; | |
166 | |
167 // Rotates the counts of successful and failed connections for current | |
168 // and previous time periods. Called every two minutes while the counts | |
169 // are non-zero. | |
170 void ThrottlingPeriodTimerCallback(); | |
171 | |
172 // Table of WebSocketHost objects, owned by this object, indexed by | |
173 // routing_id. | |
174 WebSocketHostTable hosts_; | |
175 | |
176 // The the process ID of the associated renderer process. | |
177 const int process_id_; | |
178 | |
179 // A callback which returns the appropriate net::URLRequestContext for us to | |
180 // use. | |
181 GetRequestContextCallback get_context_callback_; | |
182 | |
183 WebSocketHostFactory websocket_host_factory_; | |
184 | |
185 // Timer and counters for per-renderer WebSocket throttling. | |
186 base::RepeatingTimer throttling_period_timer_; | |
187 | |
188 // The current number of pending connections. | |
189 int num_pending_connections_; | |
190 | |
191 // The number of handshakes that failed in the current and previous time | |
192 // period. | |
193 int64_t num_current_succeeded_connections_; | |
194 int64_t num_previous_succeeded_connections_; | |
195 | |
196 // The number of handshakes that succeeded in the current and previous time | |
197 // period. | |
198 int64_t num_current_failed_connections_; | |
199 int64_t num_previous_failed_connections_; | |
200 | |
201 // Needed to read from blobs for browser-side blob sending. | |
202 const scoped_refptr<const ChromeBlobStorageContext> blob_storage_context_; | |
203 | |
204 // Needed to access to the StoragePartition for browser-side blob sending. | |
205 StoragePartition* const storage_partition_; | |
206 | |
207 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); | |
208 }; | |
209 | |
210 } // namespace content | |
211 | |
212 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | |
OLD | NEW |