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> | |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
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" | |
15 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
16 #include "content/common/websocket.h" | 18 #include "content/common/websocket.h" |
17 #include "content/public/browser/browser_message_filter.h" | 19 #include "content/public/browser/browser_message_filter.h" |
18 | 20 |
19 namespace net { | 21 namespace net { |
20 class URLRequestContext; | 22 class URLRequestContext; |
21 } // namespace net | 23 } // namespace net |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 | 26 |
25 struct WebSocketHandshakeRequest; | 27 struct WebSocketHandshakeRequest; |
26 struct WebSocketHandshakeResponse; | 28 struct WebSocketHandshakeResponse; |
27 class WebSocketHost; | 29 class WebSocketHost; |
28 | 30 |
29 // Creates a WebSocketHost object for each WebSocket channel, and dispatches | 31 // Creates a WebSocketHost object for each WebSocket channel, and dispatches |
30 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. | 32 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. |
31 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { | 33 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { |
32 public: | 34 public: |
33 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; | 35 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; |
34 | 36 |
35 // Given a routing_id, WebSocketHostFactory returns a new instance of | 37 // Given a routing_id and delay, WebSocketHostFactory returns a new |
36 // WebSocketHost or its subclass. | 38 // instance of WebSocketHost or its subclass. |
37 typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory; // NOLINT | 39 typedef base::Callback<WebSocketHost*(int, base::TimeDelta)> |
40 WebSocketHostFactory; | |
38 | 41 |
39 // Return value for methods that may delete the WebSocketHost. This enum is | 42 // Return value for methods that may delete the WebSocketHost. This enum is |
40 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make | 43 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make |
41 // conversion cheap. By using a separate enum including net/ header files can | 44 // conversion cheap. By using a separate enum including net/ header files can |
42 // be avoided. | 45 // be avoided. |
43 enum WebSocketHostState { | 46 enum WebSocketHostState { |
44 WEBSOCKET_HOST_ALIVE, | 47 WEBSOCKET_HOST_ALIVE, |
45 WEBSOCKET_HOST_DELETED | 48 WEBSOCKET_HOST_DELETED |
46 }; | 49 }; |
47 | 50 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 // Returns whether the associated renderer process can read raw cookies. | 112 // Returns whether the associated renderer process can read raw cookies. |
110 bool CanReadRawCookies() const; | 113 bool CanReadRawCookies() const; |
111 | 114 |
112 int render_process_id() const { return process_id_; } | 115 int render_process_id() const { return process_id_; } |
113 | 116 |
114 private: | 117 private: |
115 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; | 118 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; |
116 | 119 |
117 ~WebSocketDispatcherHost() override; | 120 ~WebSocketDispatcherHost() override; |
118 | 121 |
119 WebSocketHost* CreateWebSocketHost(int routing_id); | 122 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay); |
120 | 123 |
121 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one | 124 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one |
122 // is found, or NULL otherwise. | 125 // is found, or NULL otherwise. |
123 WebSocketHost* GetHost(int routing_id) const; | 126 WebSocketHost* GetHost(int routing_id) const; |
124 | 127 |
125 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() | 128 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() |
126 // method. If sending the IPC fails, assumes that this connection is no | 129 // method. If sending the IPC fails, assumes that this connection is no |
127 // longer useable, calls DeleteWebSocketHost(), and returns | 130 // longer useable, calls DeleteWebSocketHost(), and returns |
128 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. | 131 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. |
129 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; | 132 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; |
130 | 133 |
131 // Deletes the WebSocketHost object associated with the given |routing_id| and | 134 // Deletes the WebSocketHost object associated with the given |routing_id| and |
132 // removes it from the |hosts_| table. | 135 // removes it from the |hosts_| table. |
133 void DeleteWebSocketHost(int routing_id); | 136 void DeleteWebSocketHost(int routing_id); |
134 | 137 |
138 // Calculate delay for per-renderer WebSocket throttling. | |
Adam Rice
2015/03/06 11:01:06
Should be "Calculates the delay" as mentioned at h
hiroshige
2015/03/11 06:21:24
Done.
| |
139 base::TimeDelta CalculateDelay() const; | |
140 | |
141 // Rotates the counts of successful and failed connections for current | |
142 // and previous time periods. Called every two minutes while the counts | |
143 // are non-zero. | |
144 void ThrottlingPeriodTimerCallback(); | |
145 | |
135 // Table of WebSocketHost objects, owned by this object, indexed by | 146 // Table of WebSocketHost objects, owned by this object, indexed by |
136 // routing_id. | 147 // routing_id. |
137 WebSocketHostTable hosts_; | 148 WebSocketHostTable hosts_; |
138 | 149 |
139 // The the process ID of the associated renderer process. | 150 // The the process ID of the associated renderer process. |
140 const int process_id_; | 151 const int process_id_; |
141 | 152 |
142 // A callback which returns the appropriate net::URLRequestContext for us to | 153 // A callback which returns the appropriate net::URLRequestContext for us to |
143 // use. | 154 // use. |
144 GetRequestContextCallback get_context_callback_; | 155 GetRequestContextCallback get_context_callback_; |
145 | 156 |
146 WebSocketHostFactory websocket_host_factory_; | 157 WebSocketHostFactory websocket_host_factory_; |
147 | 158 |
159 // Timer and counters for per-renderer WebSocket throttling. | |
160 base::RepeatingTimer<WebSocketDispatcherHost> throttling_period_timer_; | |
161 // The current number of pending connections. | |
162 int num_pending_connections_; | |
163 // The number of handshakes that failed in the current and previous time | |
164 // period. | |
165 int64_t num_current_succeeded_connections_; | |
166 int64_t num_previous_succeeded_connections_; | |
167 // The number of handshakes that succeeded in the current and previous time | |
168 // period. | |
169 int64_t num_current_failed_connections_; | |
170 int64_t num_previous_failed_connections_; | |
171 | |
148 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); | 172 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); |
149 }; | 173 }; |
150 | 174 |
151 } // namespace content | 175 } // namespace content |
152 | 176 |
153 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 177 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
OLD | NEW |