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_HOST_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
18 #include "content/common/websocket.h" | 18 #include "content/common/websocket.h" |
| 19 #include "net/websockets/websocket_event_interface.h" |
| 20 #include "net/websockets/websocket_frame.h" |
| 21 #include "storage/browser/blob/blob_data_handle.h" |
19 | 22 |
20 class GURL; | 23 class GURL; |
21 | 24 |
22 namespace url { | 25 namespace url { |
23 class Origin; | 26 class Origin; |
24 } // namespace url | 27 } // namespace url |
25 | 28 |
26 namespace net { | 29 namespace net { |
27 class WebSocketChannel; | 30 class WebSocketChannel; |
28 class URLRequestContext; | 31 class URLRequestContext; |
29 } // namespace net | 32 } // namespace net |
30 | 33 |
31 namespace IPC { | 34 namespace IPC { |
32 class Message; | 35 class Message; |
33 } // namespace IPC | 36 } // namespace IPC |
34 | 37 |
35 namespace content { | 38 namespace content { |
36 | 39 |
| 40 class WebSocketBlobReceiver; |
37 class WebSocketBlobSender; | 41 class WebSocketBlobSender; |
38 class WebSocketDispatcherHost; | 42 class WebSocketDispatcherHost; |
39 | 43 |
40 // Host of net::WebSocketChannel. The lifetime of an instance of this class is | 44 // Host of net::WebSocketChannel. The lifetime of an instance of this class is |
41 // completely controlled by the WebSocketDispatcherHost object. | 45 // completely controlled by the WebSocketDispatcherHost object. |
42 class CONTENT_EXPORT WebSocketHost { | 46 class CONTENT_EXPORT WebSocketHost { |
43 public: | 47 public: |
44 WebSocketHost(int routing_id, | 48 WebSocketHost(int routing_id, |
45 WebSocketDispatcherHost* dispatcher, | 49 WebSocketDispatcherHost* dispatcher, |
46 net::URLRequestContext* url_request_context, | 50 net::URLRequestContext* url_request_context, |
47 base::TimeDelta delay); | 51 base::TimeDelta delay); |
48 virtual ~WebSocketHost(); | 52 virtual ~WebSocketHost(); |
49 | 53 |
50 // The renderer process is going away. | 54 // The renderer process is going away. |
51 // This function is virtual for testing. | 55 // This function is virtual for testing. |
52 virtual void GoAway(); | 56 virtual void GoAway(); |
53 | 57 |
54 // General message dispatch. WebSocketDispatcherHost::OnMessageReceived | 58 // General message dispatch. WebSocketDispatcherHost::OnMessageReceived |
55 // delegates to this method after looking up the |routing_id|. | 59 // delegates to this method after looking up the |routing_id|. |
56 virtual bool OnMessageReceived(const IPC::Message& message); | 60 virtual bool OnMessageReceived(const IPC::Message& message); |
57 | 61 |
58 int routing_id() const { return routing_id_; } | 62 int routing_id() const { return routing_id_; } |
59 | 63 |
60 bool handshake_succeeded() const { return handshake_succeeded_; } | 64 bool handshake_succeeded() const { return handshake_succeeded_; } |
61 void OnHandshakeSucceeded() { handshake_succeeded_ = true; } | 65 void OnHandshakeSucceeded() { handshake_succeeded_ = true; } |
62 | 66 |
63 private: | 67 private: |
64 class WebSocketEventHandler; | 68 class WebSocketEventHandler; |
| 69 using ReceiveQuotaProvider = size_t; |
| 70 using ReceiveQuotaConsumer = size_t; |
| 71 |
| 72 // This class provides an abstraction for when there are multiple active |
| 73 // providers of receive quota, but only one can actual consume receive quota |
| 74 // (ie. receive messages) at any one time. |
| 75 class ReceiveQuotaMultiplexer { |
| 76 public: |
| 77 ReceiveQuotaMultiplexer(); |
| 78 |
| 79 // Sets the channel It must be called before any of the other |
| 80 // methods. SetChannel(nullptr) can be used to prevent any further quota |
| 81 // from being supplied to the channel. |
| 82 void SetChannel(net::WebSocketChannel* channel); |
| 83 |
| 84 // Sets the current consumer. The pointer remains owned by the caller, and |
| 85 // must remain valid until this method is called again, or the class is |
| 86 // destroyed. The consumer must be initially set before any quota-related |
| 87 // methods are called. |
| 88 void SetConsumer(ReceiveQuotaConsumer* consumer); |
| 89 |
| 90 // Adds quota for ReceiveQuotaProvider |provider|. The addition is performed |
| 91 // by this method. If |provider| is also the current quota consumer, returns |
| 92 // true. |
| 93 bool AddQuota(ReceiveQuotaProvider* provider, size_t quota); |
| 94 |
| 95 // Returns the quota available from the currently active quota consumer. In |
| 96 // other words, how many bytes of data may be sent to the consumer right |
| 97 // now. |
| 98 size_t AvailableQuota() const; |
| 99 |
| 100 // Marks some quota as consumed. Call immediately before sending some data |
| 101 // to the current consumer. |
| 102 void ConsumedQuota(size_t quota); |
| 103 |
| 104 // Called when we receive a frame from the channel, so that we can track |
| 105 // the channel's view of available quota correctly. |
| 106 void ReceivedFrame(size_t size); |
| 107 |
| 108 // Calls channel_->SendFlowControl() if appropriate. |
| 109 void PublishMoreQuotaIfAvailable(); |
| 110 |
| 111 private: |
| 112 net::WebSocketChannel* channel_ = nullptr; |
| 113 size_t channel_quota_ = 0; |
| 114 ReceiveQuotaConsumer* current_consumer_ = nullptr; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(ReceiveQuotaMultiplexer); |
| 117 }; |
| 118 |
| 119 struct QueuedFrame { |
| 120 QueuedFrame(bool fin, |
| 121 net::WebSocketFrameHeader::OpCode type, |
| 122 const std::vector<char>& data); |
| 123 QueuedFrame(QueuedFrame&& rhs); |
| 124 ~QueuedFrame(); |
| 125 QueuedFrame& operator=(QueuedFrame&& rhs); |
| 126 bool fin = false; |
| 127 net::WebSocketFrameHeader::OpCode type = |
| 128 net::WebSocketFrameHeader::kOpCodeText; |
| 129 std::vector<char> data; |
| 130 }; |
| 131 |
| 132 struct DropChannelParameters; |
| 133 |
| 134 class BlobReceiverClient; |
65 | 135 |
66 // Handlers for each message type, dispatched by OnMessageReceived(), as | 136 // Handlers for each message type, dispatched by OnMessageReceived(), as |
67 // defined in content/common/websocket_messages.h | 137 // defined in content/common/websocket_messages.h |
68 | 138 |
69 void OnAddChannelRequest(const GURL& socket_url, | 139 void OnAddChannelRequest(const GURL& socket_url, |
70 const std::vector<std::string>& requested_protocols, | 140 const std::vector<std::string>& requested_protocols, |
71 const url::Origin& origin, | 141 const url::Origin& origin, |
72 int render_frame_id); | 142 int render_frame_id); |
73 | 143 |
74 void AddChannel(const GURL& socket_url, | 144 void AddChannel(const GURL& socket_url, |
75 const std::vector<std::string>& requested_protocols, | 145 const std::vector<std::string>& requested_protocols, |
76 const url::Origin& origin, | 146 const url::Origin& origin, |
77 int render_frame_id); | 147 int render_frame_id); |
78 | 148 |
79 void OnSendBlob(const std::string& uuid, uint64_t expected_size); | 149 void OnSendBlob(const std::string& uuid, uint64_t expected_size); |
80 | 150 |
81 void OnSendFrame(bool fin, | 151 void OnSendFrame(bool fin, |
82 WebSocketMessageType type, | 152 WebSocketMessageType type, |
83 const std::vector<char>& data); | 153 const std::vector<char>& data); |
84 | 154 |
85 void OnFlowControl(int64_t quota); | 155 void OnFlowControl(int64_t quota); |
86 | 156 |
87 void OnDropChannel(bool was_clean, uint16_t code, const std::string& reason); | 157 void OnDropChannel(bool was_clean, uint16_t code, const std::string& reason); |
88 | 158 |
| 159 void OnBinaryTypeChanged(WebSocketBinaryType new_type); |
| 160 |
| 161 void OnBlobConfirmed(); |
| 162 |
89 void BlobSendComplete(int result); | 163 void BlobSendComplete(int result); |
90 | 164 |
| 165 // Returns true if the message should be queued. |
| 166 bool ShouldQueue(size_t data_size); |
| 167 |
| 168 // Adds a message to the end of the queue. |
| 169 void AppendToQueue(bool fin, |
| 170 net::WebSocketFrameHeader::OpCode type, |
| 171 const std::vector<char>& data); |
| 172 |
| 173 // Attempts to send queued messages, and publish quota to WebSocketChannel if |
| 174 // there is any left. May destroy |this| as a side-effect. |
| 175 void FlushQueueAndPublishQuotaIfAvailable(); |
| 176 |
| 177 // Either passes the frame to the renderer or, if a Blob is being constructed, |
| 178 // appends the data to the Blob. May start writing a new Blob, in which case |
| 179 // |started_blob_receive| is set to true and the same frame will need to be |
| 180 // processed again. In this case the caller has to ensure that the frame is |
| 181 // queued, or, if it is already in the queue, not removed. |
| 182 net::WebSocketEventInterface::ChannelState SendFrameInternal( |
| 183 bool fin, |
| 184 net::WebSocketFrameHeader::OpCode type, |
| 185 const std::vector<char>& data, |
| 186 bool* started_blob_receive); |
| 187 |
| 188 // Starts receiving a blob. |
| 189 void StartReceivingBlob(bool bin, const std::vector<char>& data); |
| 190 |
| 191 // Finishes receiving a blob. |
| 192 void FinishReceivingBlob(scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 193 uint64_t size); |
| 194 |
| 195 // Blob creation has failed. Kills the channel and destroys |this|. |
| 196 void BlobReceiveFailed(int net_error_code); |
| 197 |
| 198 // Returns true if a "DropChannel" message should be delayed. |
| 199 bool ShouldDelayDropChannel() const; |
| 200 |
| 201 void SetPendingDropChannel(bool was_clean, |
| 202 uint16_t code, |
| 203 const std::string& reason); |
| 204 |
| 205 // Sends a DropChannel IPC, resulting in |this| being deleted. |
| 206 void DoDelayedDropChannel(); |
| 207 |
91 // non-NULL if and only if this object is currently in "blob sending mode". | 208 // non-NULL if and only if this object is currently in "blob sending mode". |
92 scoped_ptr<WebSocketBlobSender> blob_sender_; | 209 scoped_ptr<WebSocketBlobSender> blob_sender_; |
93 | 210 |
94 // The channel we use to send events to the network. | 211 // The channel we use to send events to the network. |
95 scoped_ptr<net::WebSocketChannel> channel_; | 212 scoped_ptr<net::WebSocketChannel> channel_; |
96 | 213 |
97 // The WebSocketHostDispatcher that created this object. | 214 // The WebSocketHostDispatcher that created this object. |
98 WebSocketDispatcherHost* const dispatcher_; | 215 WebSocketDispatcherHost* const dispatcher_; |
99 | 216 |
100 // The URL request context for the channel. | 217 // The URL request context for the channel. |
101 net::URLRequestContext* const url_request_context_; | 218 net::URLRequestContext* const url_request_context_; |
102 | 219 |
103 // The ID used to route messages. | 220 // The ID used to route messages. |
104 const int routing_id_; | 221 const int routing_id_; |
105 | 222 |
106 // Delay used for per-renderer WebSocket throttling. | 223 // Delay used for per-renderer WebSocket throttling. |
107 base::TimeDelta delay_; | 224 base::TimeDelta delay_; |
108 | 225 |
109 // SendFlowControl() is delayed when OnFlowControl() is called before | 226 // SendFlowControl() is delayed when OnFlowControl() is called before |
110 // AddChannel() is called. | 227 // AddChannel() is called. |
111 // Zero indicates there is no pending SendFlowControl(). | 228 // Zero indicates there is no pending SendFlowControl(). |
112 int64_t pending_flow_control_quota_; | 229 int64_t pending_flow_control_quota_; |
113 | 230 |
114 // handshake_succeeded_ is set and used by WebSocketDispatcherHost | 231 // handshake_succeeded_ is set and used by WebSocketDispatcherHost |
115 // to manage counters for per-renderer WebSocket throttling. | 232 // to manage counters for per-renderer WebSocket throttling. |
116 bool handshake_succeeded_; | 233 bool handshake_succeeded_; |
117 | 234 |
| 235 WebSocketBinaryType binary_type_; |
| 236 |
| 237 std::queue<QueuedFrame> data_frame_queue_; |
| 238 std::queue<scoped_ptr<storage::BlobDataHandle>> unconfirmed_blob_queue_; |
| 239 ReceiveQuotaMultiplexer receive_quota_multiplexer_; |
| 240 ReceiveQuotaProvider renderer_quota_; |
| 241 ReceiveQuotaProvider blob_receiver_quota_; |
| 242 |
| 243 scoped_ptr<WebSocketBlobReceiver> blob_receiver_; |
| 244 scoped_ptr<DropChannelParameters> pending_drop_channel_; |
| 245 |
118 base::WeakPtrFactory<WebSocketHost> weak_ptr_factory_; | 246 base::WeakPtrFactory<WebSocketHost> weak_ptr_factory_; |
119 | 247 |
120 DISALLOW_COPY_AND_ASSIGN(WebSocketHost); | 248 DISALLOW_COPY_AND_ASSIGN(WebSocketHost); |
121 }; | 249 }; |
122 | 250 |
123 } // namespace content | 251 } // namespace content |
124 | 252 |
125 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ | 253 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ |
OLD | NEW |