Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: content/browser/websocket/websocket_manager.cc

Issue 2119973002: Port WebSockets to Mojo IPC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/browser/websocket/websocket_manager.h"
6
7 #include <stddef.h>
8
9 #include <algorithm>
10 #include <set>
11 #include <string>
12 #include <vector>
13
14 #include "base/callback.h"
15 #include "base/logging.h"
16 #include "base/numerics/safe_conversions.h"
17 #include "base/rand_util.h"
18 #include "base/stl_util.h"
19 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
20 #include "content/browser/child_process_security_policy_impl.h"
21 #include "content/browser/websocket/websocket_impl.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "net/url_request/url_request_context_getter.h"
24
25 namespace content {
26
27 namespace {
28
29 // Max number of pending connections per WebSocketManager used for per-renderer
30 // WebSocket throttling.
31 const int kMaxPendingWebSocketConnections = 255;
32
33 } // namespace
34
35 class WebSocketManager::IOThreadState : public WebSocketImpl::Delegate {
36 public:
37 IOThreadState(
38 int process_id,
39 StoragePartition* storage_partition,
40 const scoped_refptr<ChromeBlobStorageContext>& blob_storage_context)
41 : process_id_(process_id),
42 storage_partition_(storage_partition),
43 blob_storage_context_(blob_storage_context),
44 num_pending_connections_(0),
45 num_current_succeeded_connections_(0),
46 num_previous_succeeded_connections_(0),
47 num_current_failed_connections_(0),
48 num_previous_failed_connections_(0) {}
49
50 ~IOThreadState() override {
51 for (auto impl : impls_) {
52 impl->GoAway();
53 delete impl;
54 }
55 }
56
57 void CreateWebSocket(int render_frame_id, mojom::WebSocketRequest request) {
58 if (num_pending_connections_ >= kMaxPendingWebSocketConnections) {
59 // XXX
60 #if 0
61 if (!Send(new WebSocketMsg_NotifyFailure(
62 routing_id,
63 "Error in connection establishment: "
64 "net::ERR_INSUFFICIENT_RESOURCES"))) {
65 DVLOG(1) << "Sending of message type "
66 << "WebSocketMsg_NotifyFailure failed.";
67 }
68 #endif
69 return;
70 }
71
72 // We keep all WebSocketImpls alive until we either observe a connection
73 // error (the client dropped its connection) or we need to shutdown.
74
75 impls_.insert(new WebSocketImpl(this,
76 std::move(request),
77 render_frame_id,
78 CalculateDelay()));
79 ++num_pending_connections_;
80
81 if (!throttling_period_timer_.IsRunning()) {
82 throttling_period_timer_.Start(
83 FROM_HERE,
84 base::TimeDelta::FromMinutes(2),
85 this,
86 &IOThreadState::ThrottlingPeriodTimerCallback);
87 }
88 }
89
90 private:
91 // Calculate delay as described in the per-renderer WebSocket throttling
92 // design doc:
93 // https://docs.google.com/document/d/1aw2oN5PKfk-1gLnBrlv1OwLA8K3-ykM2ckwX2lu bTg4/edit?usp=sharing
94 base::TimeDelta CalculateDelay() const {
95 int64_t f = num_previous_failed_connections_ +
96 num_current_failed_connections_;
97 int64_t s = num_previous_succeeded_connections_ +
98 num_current_succeeded_connections_;
99 int p = num_pending_connections_;
100 return base::TimeDelta::FromMilliseconds(
101 base::RandInt(1000, 5000) *
102 (1 << std::min(p + f / (s + 1), INT64_C(16))) / 65536);
103 }
104
105 void ThrottlingPeriodTimerCallback() {
106 num_previous_failed_connections_ = num_current_failed_connections_;
107 num_current_failed_connections_ = 0;
108
109 num_previous_succeeded_connections_ = num_current_succeeded_connections_;
110 num_current_succeeded_connections_ = 0;
111
112 if (num_pending_connections_ == 0 &&
113 num_previous_failed_connections_ == 0 &&
114 num_previous_succeeded_connections_ == 0) {
115 throttling_period_timer_.Stop();
116 }
117 }
118
119 // WebSocketImpl::Delegate methods:
120
121 int GetClientProcessId() override {
122 return process_id_;
123 }
124
125 StoragePartition* GetStoragePartition() override {
126 return storage_partition_;
127 }
128
129 storage::BlobStorageContext* GetBlobStorageContext() override {
130 return blob_storage_context_->context();
131 }
132
133 void OnReceivedResponseFromServer(WebSocketImpl* impl) override {
134 // XXX
135 impl->OnHandshakeSucceeded();
136 --num_pending_connections_;
137 DCHECK_GE(num_pending_connections_, 0);
138 ++num_current_succeeded_connections_;
139 }
140
141 void OnLostConnectionToClient(WebSocketImpl* impl) override {
142 if (!impl->handshake_succeeded()) {
143 // Update throttling counters (failure).
144 --num_pending_connections_;
145 DCHECK_GE(num_pending_connections_, 0);
146 ++num_current_failed_connections_;
147 }
148 impls_.erase(impl);
149 delete impl;
150 }
151
152 int process_id_;
153 StoragePartition* storage_partition_;
154 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
155
156 std::set<WebSocketImpl*> impls_;
157
158 // Timer and counters for per-renderer WebSocket throttling.
159 base::RepeatingTimer throttling_period_timer_;
160
161 // The current number of pending connections.
162 int num_pending_connections_;
163
164 // The number of handshakes that failed in the current and previous time
165 // period.
166 int64_t num_current_succeeded_connections_;
167 int64_t num_previous_succeeded_connections_;
168
169 // The number of handshakes that succeeded in the current and previous time
170 // period.
171 int64_t num_current_failed_connections_;
172 int64_t num_previous_failed_connections_;
173 };
174
175 WebSocketManager::WebSocketManager(
176 int process_id,
177 StoragePartition* storage_partition,
178 const scoped_refptr<ChromeBlobStorageContext>& blob_storage_context)
179 : io_thread_state_(new IOThreadState(process_id,
180 storage_partition,
181 blob_storage_context)) {}
182
183 WebSocketManager::~WebSocketManager() {
184 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, io_thread_state_);
185 }
186
187 void WebSocketManager::CreateWebSocket(
188 int render_frame_id,
189 mojom::WebSocketRequest request) {
190 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO));
191
192 BrowserThread::PostTask(
193 BrowserThread::IO,
194 FROM_HERE,
195 base::Bind(&IOThreadState::CreateWebSocket,
196 base::Unretained(io_thread_state_),
197 render_frame_id,
198 base::Passed(&request)));
199 }
200
201 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698