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 |
48 WebSocketDispatcherHost( | 51 WebSocketDispatcherHost( |
49 int process_id, | 52 int process_id, |
50 const GetRequestContextCallback& get_context_callback); | 53 const GetRequestContextCallback& get_context_callback); |
51 | 54 |
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 | |
58 // BrowserMessageFilter: | 55 // BrowserMessageFilter: |
59 bool OnMessageReceived(const IPC::Message& message) override; | 56 bool OnMessageReceived(const IPC::Message& message) override; |
60 | 57 |
61 // The following methods are used by WebSocketHost::EventInterface to send | 58 // The following methods are used by WebSocketHost::EventInterface to send |
62 // IPCs from the browser to the renderer or child process. Any of them may | 59 // IPCs from the browser to the renderer or child process. Any of them may |
63 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure, | 60 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure, |
64 // leading to the WebSocketChannel and EventInterface also being deleted. | 61 // leading to the WebSocketChannel and EventInterface also being deleted. |
65 | 62 |
66 // Sends a WebSocketMsg_AddChannelResponse IPC. | 63 // Sends a WebSocketMsg_AddChannelResponse IPC. |
67 WebSocketHostState SendAddChannelResponse( | 64 WebSocketHostState SendAddChannelResponse( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 int routing_id, | 101 int routing_id, |
105 bool was_clean, | 102 bool was_clean, |
106 uint16 code, | 103 uint16 code, |
107 const std::string& reason) WARN_UNUSED_RESULT; | 104 const std::string& reason) WARN_UNUSED_RESULT; |
108 | 105 |
109 // Returns whether the associated renderer process can read raw cookies. | 106 // Returns whether the associated renderer process can read raw cookies. |
110 bool CanReadRawCookies() const; | 107 bool CanReadRawCookies() const; |
111 | 108 |
112 int render_process_id() const { return process_id_; } | 109 int render_process_id() const { return process_id_; } |
113 | 110 |
| 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 |
114 private: | 127 private: |
115 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; | 128 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; |
116 | 129 |
117 ~WebSocketDispatcherHost() override; | 130 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay); |
118 | |
119 WebSocketHost* CreateWebSocketHost(int routing_id); | |
120 | 131 |
121 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one | 132 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one |
122 // is found, or NULL otherwise. | 133 // is found, or NULL otherwise. |
123 WebSocketHost* GetHost(int routing_id) const; | 134 WebSocketHost* GetHost(int routing_id) const; |
124 | 135 |
125 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() | 136 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() |
126 // method. If sending the IPC fails, assumes that this connection is no | 137 // method. If sending the IPC fails, assumes that this connection is no |
127 // longer useable, calls DeleteWebSocketHost(), and returns | 138 // longer useable, calls DeleteWebSocketHost(), and returns |
128 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. | 139 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. |
129 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; | 140 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; |
130 | 141 |
131 // Deletes the WebSocketHost object associated with the given |routing_id| and | 142 // Deletes the WebSocketHost object associated with the given |routing_id| and |
132 // removes it from the |hosts_| table. | 143 // removes it from the |hosts_| table. |
133 void DeleteWebSocketHost(int routing_id); | 144 void DeleteWebSocketHost(int routing_id); |
134 | 145 |
| 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 |
135 // Table of WebSocketHost objects, owned by this object, indexed by | 154 // Table of WebSocketHost objects, owned by this object, indexed by |
136 // routing_id. | 155 // routing_id. |
137 WebSocketHostTable hosts_; | 156 WebSocketHostTable hosts_; |
138 | 157 |
139 // The the process ID of the associated renderer process. | 158 // The the process ID of the associated renderer process. |
140 const int process_id_; | 159 const int process_id_; |
141 | 160 |
142 // A callback which returns the appropriate net::URLRequestContext for us to | 161 // A callback which returns the appropriate net::URLRequestContext for us to |
143 // use. | 162 // use. |
144 GetRequestContextCallback get_context_callback_; | 163 GetRequestContextCallback get_context_callback_; |
145 | 164 |
146 WebSocketHostFactory websocket_host_factory_; | 165 WebSocketHostFactory websocket_host_factory_; |
147 | 166 |
| 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 |
148 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); | 183 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); |
149 }; | 184 }; |
150 | 185 |
151 } // namespace content | 186 } // namespace content |
152 | 187 |
153 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 188 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
OLD | NEW |