Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / renderer_host / websocket_dispatcher_host.h
blobdebbf584c732138c1e31696b0f22e6f918a0dc3e
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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/compiler_specific.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"
18 #include "content/common/websocket.h"
19 #include "content/public/browser/browser_message_filter.h"
21 namespace net {
22 class URLRequestContext;
23 } // namespace net
25 namespace content {
27 struct WebSocketHandshakeRequest;
28 struct WebSocketHandshakeResponse;
29 class WebSocketHost;
31 // Creates a WebSocketHost object for each WebSocket channel, and dispatches
32 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost.
33 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter {
34 public:
35 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback;
37 // Given a routing_id and delay, WebSocketHostFactory returns a new
38 // instance of WebSocketHost or its subclass.
39 typedef base::Callback<WebSocketHost*(int, base::TimeDelta)>
40 WebSocketHostFactory;
42 // Return value for methods that may delete the WebSocketHost. This enum is
43 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make
44 // conversion cheap. By using a separate enum including net/ header files can
45 // be avoided.
46 enum WebSocketHostState {
47 WEBSOCKET_HOST_ALIVE,
48 WEBSOCKET_HOST_DELETED
51 WebSocketDispatcherHost(
52 int process_id,
53 const GetRequestContextCallback& get_context_callback);
55 // BrowserMessageFilter:
56 bool OnMessageReceived(const IPC::Message& message) override;
58 // 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
60 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure,
61 // leading to the WebSocketChannel and EventInterface also being deleted.
63 // Sends a WebSocketMsg_AddChannelResponse IPC.
64 WebSocketHostState SendAddChannelResponse(
65 int routing_id,
66 const std::string& selected_protocol,
67 const std::string& extensions) WARN_UNUSED_RESULT;
69 // Sends a WebSocketMsg_SendFrame IPC.
70 WebSocketHostState SendFrame(int routing_id,
71 bool fin,
72 WebSocketMessageType type,
73 const std::vector<char>& data);
75 // Sends a WebSocketMsg_FlowControl IPC.
76 WebSocketHostState SendFlowControl(int routing_id,
77 int64 quota) WARN_UNUSED_RESULT;
79 // Sends a WebSocketMsg_NotifyClosing IPC
80 WebSocketHostState NotifyClosingHandshake(int routing_id) WARN_UNUSED_RESULT;
82 // Sends a WebSocketMsg_NotifyStartOpeningHandshake IPC.
83 WebSocketHostState NotifyStartOpeningHandshake(
84 int routing_id,
85 const WebSocketHandshakeRequest& request) WARN_UNUSED_RESULT;
87 // Sends a WebSocketMsg_NotifyFinishOpeningHandshake IPC.
88 WebSocketHostState NotifyFinishOpeningHandshake(
89 int routing_id,
90 const WebSocketHandshakeResponse& response) WARN_UNUSED_RESULT;
92 // Sends a WebSocketMsg_NotifyFailure IPC and deletes and unregisters the
93 // channel.
94 WebSocketHostState NotifyFailure(
95 int routing_id,
96 const std::string& message) WARN_UNUSED_RESULT;
98 // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the
99 // channel.
100 WebSocketHostState DoDropChannel(
101 int routing_id,
102 bool was_clean,
103 uint16 code,
104 const std::string& reason) WARN_UNUSED_RESULT;
106 // Returns whether the associated renderer process can read raw cookies.
107 bool CanReadRawCookies() const;
109 int render_process_id() const { return process_id_; }
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);
118 int num_pending_connections() const { return num_pending_connections_; }
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;
125 ~WebSocketDispatcherHost() override;
127 private:
128 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable;
130 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay);
132 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one
133 // is found, or NULL otherwise.
134 WebSocketHost* GetHost(int routing_id) const;
136 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send()
137 // method. If sending the IPC fails, assumes that this connection is no
138 // longer useable, calls DeleteWebSocketHost(), and returns
139 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types.
140 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT;
142 // Deletes the WebSocketHost object associated with the given |routing_id| and
143 // removes it from the |hosts_| table.
144 void DeleteWebSocketHost(int routing_id);
146 // Calculates the delay for per-renderer WebSocket throttling.
147 base::TimeDelta CalculateDelay() const;
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();
154 // Table of WebSocketHost objects, owned by this object, indexed by
155 // routing_id.
156 WebSocketHostTable hosts_;
158 // The the process ID of the associated renderer process.
159 const int process_id_;
161 // A callback which returns the appropriate net::URLRequestContext for us to
162 // use.
163 GetRequestContextCallback get_context_callback_;
165 WebSocketHostFactory websocket_host_factory_;
167 // Timer and counters for per-renderer WebSocket throttling.
168 base::RepeatingTimer<WebSocketDispatcherHost> throttling_period_timer_;
170 // The current number of pending connections.
171 int num_pending_connections_;
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_;
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_;
183 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost);
186 } // namespace content
188 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_