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 |
10 #include <string> | 10 #include <string> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 // General message dispatch. WebSocketDispatcherHost::OnMessageReceived | 53 // General message dispatch. WebSocketDispatcherHost::OnMessageReceived |
54 // delegates to this method after looking up the |routing_id|. | 54 // delegates to this method after looking up the |routing_id|. |
55 virtual bool OnMessageReceived(const IPC::Message& message); | 55 virtual bool OnMessageReceived(const IPC::Message& message); |
56 | 56 |
57 int routing_id() const { return routing_id_; } | 57 int routing_id() const { return routing_id_; } |
58 | 58 |
59 bool handshake_succeeded() const { return handshake_succeeded_; } | 59 bool handshake_succeeded() const { return handshake_succeeded_; } |
60 void OnHandshakeSucceeded() { handshake_succeeded_ = true; } | 60 void OnHandshakeSucceeded() { handshake_succeeded_ = true; } |
61 | 61 |
62 private: | 62 private: |
| 63 using ReceiveQuotaProvider = int64_t; |
| 64 using ReceiveQuotaConsumer = int64_t; |
| 65 |
| 66 // This class provides an abstraction for when there are multiple active |
| 67 // providers of receive quota, but only one can actual consume receive quota |
| 68 // (ie. receive messages) at any one time. |
| 69 class ReceiveQuotaMultiplexer { |
| 70 public: |
| 71 ReceiveQuotaMultiplexer(); |
| 72 |
| 73 // set_channel must be called before any other methods. |
| 74 void set_channel(net::WebSocketChannel* channel) { channel_ = channel; } |
| 75 |
| 76 // Adds quota for ReceiveQuotaProvider |provider|. The amount is actually |
| 77 // updated by this method. If |provider| is also the current quota consumer, |
| 78 // returns true. May provide |channel_| with more quota. |
| 79 bool AddQuota(ReceiveQuotaProvider* provider, int64_t quota); |
| 80 |
| 81 // Returns the quota available from the currently active quota consumer. In |
| 82 // other words, how many bytes of data may be sent to the consumer right |
| 83 // now. |
| 84 int64_t AvailableQuota() const; |
| 85 |
| 86 // Marks some quota as consumed. To be called this after sending some data |
| 87 // to the current consumer. |
| 88 void ConsumedQuota(int64_t quota); |
| 89 |
| 90 // Set the current consumer. The pointer remains owned by the caller, and |
| 91 // must remain valid until this method is called again, or the class is |
| 92 // destroyed. |
| 93 void SetConsumer(ReceiveQuotaConsumer* consumer); |
| 94 |
| 95 private: |
| 96 // Calls channel_->SendFlowControl() if appropriate. |
| 97 void PublishMoreQuotaIfNeeded(); |
| 98 |
| 99 net::WebSocketChannel* channel_; |
| 100 int64_t published_quota_; |
| 101 ReceiveQuotaConsumer* current_consumer_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(ReceiveQuotaMultiplexer); |
| 104 }; |
| 105 |
63 // Handlers for each message type, dispatched by OnMessageReceived(), as | 106 // Handlers for each message type, dispatched by OnMessageReceived(), as |
64 // defined in content/common/websocket_messages.h | 107 // defined in content/common/websocket_messages.h |
65 | 108 |
66 void OnAddChannelRequest(const GURL& socket_url, | 109 void OnAddChannelRequest(const GURL& socket_url, |
67 const std::vector<std::string>& requested_protocols, | 110 const std::vector<std::string>& requested_protocols, |
68 const url::Origin& origin, | 111 const url::Origin& origin, |
69 int render_frame_id); | 112 int render_frame_id); |
70 | 113 |
71 void AddChannel(const GURL& socket_url, | 114 void AddChannel(const GURL& socket_url, |
72 const std::vector<std::string>& requested_protocols, | 115 const std::vector<std::string>& requested_protocols, |
(...skipping 25 matching lines...) Expand all Loading... |
98 | 141 |
99 // SendFlowControl() is delayed when OnFlowControl() is called before | 142 // SendFlowControl() is delayed when OnFlowControl() is called before |
100 // AddChannel() is called. | 143 // AddChannel() is called. |
101 // Zero indicates there is no pending SendFlowControl(). | 144 // Zero indicates there is no pending SendFlowControl(). |
102 int64_t pending_flow_control_quota_; | 145 int64_t pending_flow_control_quota_; |
103 | 146 |
104 // handshake_succeeded_ is set and used by WebSocketDispatcherHost | 147 // handshake_succeeded_ is set and used by WebSocketDispatcherHost |
105 // to manage counters for per-renderer WebSocket throttling. | 148 // to manage counters for per-renderer WebSocket throttling. |
106 bool handshake_succeeded_; | 149 bool handshake_succeeded_; |
107 | 150 |
| 151 ReceiveQuotaMultiplexer receive_quota_multiplexer_; |
| 152 |
108 base::WeakPtrFactory<WebSocketHost> weak_ptr_factory_; | 153 base::WeakPtrFactory<WebSocketHost> weak_ptr_factory_; |
109 | 154 |
110 DISALLOW_COPY_AND_ASSIGN(WebSocketHost); | 155 DISALLOW_COPY_AND_ASSIGN(WebSocketHost); |
111 }; | 156 }; |
112 | 157 |
113 } // namespace content | 158 } // namespace content |
114 | 159 |
115 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ | 160 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_ |
OLD | NEW |