OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
7 | 7 |
8 #include <stdint.h> | |
9 #include <string> | 8 #include <string> |
10 #include <vector> | 9 #include <vector> |
11 | 10 |
| 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/time/time.h" | |
16 #include "base/timer/timer.h" | |
17 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
18 #include "content/common/websocket.h" | 16 #include "content/common/websocket.h" |
19 #include "content/public/browser/browser_message_filter.h" | 17 #include "content/public/browser/browser_message_filter.h" |
20 | 18 |
21 namespace net { | 19 namespace net { |
22 class URLRequestContext; | 20 class URLRequestContext; |
23 } // namespace net | 21 } // namespace net |
24 | 22 |
25 namespace content { | 23 namespace content { |
26 | 24 |
27 struct WebSocketHandshakeRequest; | 25 struct WebSocketHandshakeRequest; |
28 struct WebSocketHandshakeResponse; | 26 struct WebSocketHandshakeResponse; |
29 class WebSocketHost; | 27 class WebSocketHost; |
30 | 28 |
31 // Creates a WebSocketHost object for each WebSocket channel, and dispatches | 29 // Creates a WebSocketHost object for each WebSocket channel, and dispatches |
32 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. | 30 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. |
33 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { | 31 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { |
34 public: | 32 public: |
35 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; | 33 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; |
36 | 34 |
37 // Given a routing_id and delay, WebSocketHostFactory returns a new | 35 // Given a routing_id, WebSocketHostFactory returns a new instance of |
38 // instance of WebSocketHost or its subclass. | 36 // WebSocketHost or its subclass. |
39 typedef base::Callback<WebSocketHost*(int, base::TimeDelta)> | 37 typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory; // NOLINT |
40 WebSocketHostFactory; | |
41 | 38 |
42 // Return value for methods that may delete the WebSocketHost. This enum is | 39 // Return value for methods that may delete the WebSocketHost. This enum is |
43 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make | 40 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make |
44 // conversion cheap. By using a separate enum including net/ header files can | 41 // conversion cheap. By using a separate enum including net/ header files can |
45 // be avoided. | 42 // be avoided. |
46 enum WebSocketHostState { | 43 enum WebSocketHostState { |
47 WEBSOCKET_HOST_ALIVE, | 44 WEBSOCKET_HOST_ALIVE, |
48 WEBSOCKET_HOST_DELETED | 45 WEBSOCKET_HOST_DELETED |
49 }; | 46 }; |
50 | 47 |
51 WebSocketDispatcherHost( | 48 WebSocketDispatcherHost( |
52 int process_id, | 49 int process_id, |
53 const GetRequestContextCallback& get_context_callback); | 50 const GetRequestContextCallback& get_context_callback); |
54 | 51 |
| 52 // For testing. Specify a factory method that creates mock version of |
| 53 // WebSocketHost. |
| 54 WebSocketDispatcherHost(int process_id, |
| 55 const GetRequestContextCallback& get_context_callback, |
| 56 const WebSocketHostFactory& websocket_host_factory); |
| 57 |
55 // BrowserMessageFilter: | 58 // BrowserMessageFilter: |
56 bool OnMessageReceived(const IPC::Message& message) override; | 59 bool OnMessageReceived(const IPC::Message& message) override; |
57 | 60 |
58 // The following methods are used by WebSocketHost::EventInterface to send | 61 // The following methods are used by WebSocketHost::EventInterface to send |
59 // IPCs from the browser to the renderer or child process. Any of them may | 62 // IPCs from the browser to the renderer or child process. Any of them may |
60 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure, | 63 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure, |
61 // leading to the WebSocketChannel and EventInterface also being deleted. | 64 // leading to the WebSocketChannel and EventInterface also being deleted. |
62 | 65 |
63 // Sends a WebSocketMsg_AddChannelResponse IPC. | 66 // Sends a WebSocketMsg_AddChannelResponse IPC. |
64 WebSocketHostState SendAddChannelResponse( | 67 WebSocketHostState SendAddChannelResponse( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 int routing_id, | 104 int routing_id, |
102 bool was_clean, | 105 bool was_clean, |
103 uint16 code, | 106 uint16 code, |
104 const std::string& reason) WARN_UNUSED_RESULT; | 107 const std::string& reason) WARN_UNUSED_RESULT; |
105 | 108 |
106 // Returns whether the associated renderer process can read raw cookies. | 109 // Returns whether the associated renderer process can read raw cookies. |
107 bool CanReadRawCookies() const; | 110 bool CanReadRawCookies() const; |
108 | 111 |
109 int render_process_id() const { return process_id_; } | 112 int render_process_id() const { return process_id_; } |
110 | 113 |
111 protected: | |
112 // For testing. Specify a factory method that creates mock version of | |
113 // WebSocketHost. | |
114 WebSocketDispatcherHost(int process_id, | |
115 const GetRequestContextCallback& get_context_callback, | |
116 const WebSocketHostFactory& websocket_host_factory); | |
117 | |
118 int num_pending_connections() const { return num_pending_connections_; } | |
119 | |
120 // The number of handshakes that failed/succeeded in the current and | |
121 // previous time period, respectively. | |
122 int64_t num_failed_connections() const; | |
123 int64_t num_succeeded_connections() const; | |
124 | |
125 ~WebSocketDispatcherHost() override; | |
126 | |
127 private: | 114 private: |
128 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; | 115 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; |
129 | 116 |
130 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay); | 117 ~WebSocketDispatcherHost() override; |
| 118 |
| 119 WebSocketHost* CreateWebSocketHost(int routing_id); |
131 | 120 |
132 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one | 121 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one |
133 // is found, or NULL otherwise. | 122 // is found, or NULL otherwise. |
134 WebSocketHost* GetHost(int routing_id) const; | 123 WebSocketHost* GetHost(int routing_id) const; |
135 | 124 |
136 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() | 125 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() |
137 // method. If sending the IPC fails, assumes that this connection is no | 126 // method. If sending the IPC fails, assumes that this connection is no |
138 // longer useable, calls DeleteWebSocketHost(), and returns | 127 // longer useable, calls DeleteWebSocketHost(), and returns |
139 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. | 128 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. |
140 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; | 129 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; |
141 | 130 |
142 // Deletes the WebSocketHost object associated with the given |routing_id| and | 131 // Deletes the WebSocketHost object associated with the given |routing_id| and |
143 // removes it from the |hosts_| table. | 132 // removes it from the |hosts_| table. |
144 void DeleteWebSocketHost(int routing_id); | 133 void DeleteWebSocketHost(int routing_id); |
145 | 134 |
146 // Calculates the delay for per-renderer WebSocket throttling. | |
147 base::TimeDelta CalculateDelay() const; | |
148 | |
149 // Rotates the counts of successful and failed connections for current | |
150 // and previous time periods. Called every two minutes while the counts | |
151 // are non-zero. | |
152 void ThrottlingPeriodTimerCallback(); | |
153 | |
154 // Table of WebSocketHost objects, owned by this object, indexed by | 135 // Table of WebSocketHost objects, owned by this object, indexed by |
155 // routing_id. | 136 // routing_id. |
156 WebSocketHostTable hosts_; | 137 WebSocketHostTable hosts_; |
157 | 138 |
158 // The the process ID of the associated renderer process. | 139 // The the process ID of the associated renderer process. |
159 const int process_id_; | 140 const int process_id_; |
160 | 141 |
161 // A callback which returns the appropriate net::URLRequestContext for us to | 142 // A callback which returns the appropriate net::URLRequestContext for us to |
162 // use. | 143 // use. |
163 GetRequestContextCallback get_context_callback_; | 144 GetRequestContextCallback get_context_callback_; |
164 | 145 |
165 WebSocketHostFactory websocket_host_factory_; | 146 WebSocketHostFactory websocket_host_factory_; |
166 | 147 |
167 // Timer and counters for per-renderer WebSocket throttling. | |
168 base::RepeatingTimer<WebSocketDispatcherHost> throttling_period_timer_; | |
169 | |
170 // The current number of pending connections. | |
171 int num_pending_connections_; | |
172 | |
173 // The number of handshakes that failed in the current and previous time | |
174 // period. | |
175 int64_t num_current_succeeded_connections_; | |
176 int64_t num_previous_succeeded_connections_; | |
177 | |
178 // The number of handshakes that succeeded in the current and previous time | |
179 // period. | |
180 int64_t num_current_failed_connections_; | |
181 int64_t num_previous_failed_connections_; | |
182 | |
183 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); | 148 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); |
184 }; | 149 }; |
185 | 150 |
186 } // namespace content | 151 } // namespace content |
187 | 152 |
188 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 153 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
OLD | NEW |