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

Side by Side Diff: content/browser/byte_stream.cc

Issue 22908008: Limit the total memory usage for Stream instances (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "content/browser/byte_stream.h" 5 #include "content/browser/byte_stream.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 void SetPeer(ByteStreamReaderImpl* peer, 54 void SetPeer(ByteStreamReaderImpl* peer,
55 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, 55 scoped_refptr<base::SequencedTaskRunner> peer_task_runner,
56 scoped_refptr<LifetimeFlag> peer_lifetime_flag); 56 scoped_refptr<LifetimeFlag> peer_lifetime_flag);
57 57
58 // Overridden from ByteStreamWriter. 58 // Overridden from ByteStreamWriter.
59 virtual bool Write(scoped_refptr<net::IOBuffer> buffer, 59 virtual bool Write(scoped_refptr<net::IOBuffer> buffer,
60 size_t byte_count) OVERRIDE; 60 size_t byte_count) OVERRIDE;
61 virtual void Flush() OVERRIDE; 61 virtual void Flush() OVERRIDE;
62 virtual void Close(int status) OVERRIDE; 62 virtual void Close(int status) OVERRIDE;
63 virtual void RegisterCallback(const base::Closure& source_callback) OVERRIDE; 63 virtual void RegisterCallback(const base::Closure& source_callback) OVERRIDE;
64 virtual size_t TotalBufferedBytes() const OVERRIDE;
64 65
65 // PostTask target from |ByteStreamReaderImpl::MaybeUpdateInput|. 66 // PostTask target from |ByteStreamReaderImpl::MaybeUpdateInput|.
66 static void UpdateWindow(scoped_refptr<LifetimeFlag> lifetime_flag, 67 static void UpdateWindow(scoped_refptr<LifetimeFlag> lifetime_flag,
67 ByteStreamWriterImpl* target, 68 ByteStreamWriterImpl* target,
68 size_t bytes_consumed); 69 size_t bytes_consumed);
69 70
70 private: 71 private:
71 // Called from UpdateWindow when object existence has been validated. 72 // Called from UpdateWindow when object existence has been validated.
72 void UpdateWindowInternal(size_t bytes_consumed); 73 void UpdateWindowInternal(size_t bytes_consumed);
73 74
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 my_task_runner_(task_runner), 187 my_task_runner_(task_runner),
187 my_lifetime_flag_(lifetime_flag), 188 my_lifetime_flag_(lifetime_flag),
188 input_contents_size_(0), 189 input_contents_size_(0),
189 output_size_used_(0), 190 output_size_used_(0),
190 peer_(NULL) { 191 peer_(NULL) {
191 DCHECK(my_lifetime_flag_.get()); 192 DCHECK(my_lifetime_flag_.get());
192 my_lifetime_flag_->is_alive = true; 193 my_lifetime_flag_->is_alive = true;
193 } 194 }
194 195
195 ByteStreamWriterImpl::~ByteStreamWriterImpl() { 196 ByteStreamWriterImpl::~ByteStreamWriterImpl() {
197 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
196 my_lifetime_flag_->is_alive = false; 198 my_lifetime_flag_->is_alive = false;
197 } 199 }
198 200
199 void ByteStreamWriterImpl::SetPeer( 201 void ByteStreamWriterImpl::SetPeer(
200 ByteStreamReaderImpl* peer, 202 ByteStreamReaderImpl* peer,
201 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, 203 scoped_refptr<base::SequencedTaskRunner> peer_task_runner,
202 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { 204 scoped_refptr<LifetimeFlag> peer_lifetime_flag) {
203 peer_ = peer; 205 peer_ = peer;
204 peer_task_runner_ = peer_task_runner; 206 peer_task_runner_ = peer_task_runner;
205 peer_lifetime_flag_ = peer_lifetime_flag; 207 peer_lifetime_flag_ = peer_lifetime_flag;
206 } 208 }
207 209
208 bool ByteStreamWriterImpl::Write( 210 bool ByteStreamWriterImpl::Write(
209 scoped_refptr<net::IOBuffer> buffer, size_t byte_count) { 211 scoped_refptr<net::IOBuffer> buffer, size_t byte_count) {
210 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); 212 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
211 213
212 input_contents_.push_back(std::make_pair(buffer, byte_count)); 214 input_contents_.push_back(std::make_pair(buffer, byte_count));
213 input_contents_size_ += byte_count; 215 input_contents_size_ += byte_count;
214 216
215 // Arbitrarily, we buffer to a third of the total size before sending. 217 // Arbitrarily, we buffer to a third of the total size before sending.
216 if (input_contents_size_ > total_buffer_size_ / kFractionBufferBeforeSending) 218 if (input_contents_size_ > total_buffer_size_ / kFractionBufferBeforeSending)
217 PostToPeer(false, 0); 219 PostToPeer(false, 0);
218 220
219 return (input_contents_size_ + output_size_used_ <= total_buffer_size_); 221 return (TotalBufferedBytes() <= total_buffer_size_);
220 } 222 }
221 223
222 void ByteStreamWriterImpl::Flush() { 224 void ByteStreamWriterImpl::Flush() {
223 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); 225 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
224 if (input_contents_size_ > 0) 226 if (input_contents_size_ > 0)
225 PostToPeer(false, 0); 227 PostToPeer(false, 0);
226 } 228 }
227 229
228 void ByteStreamWriterImpl::Close(int status) { 230 void ByteStreamWriterImpl::Close(int status) {
229 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); 231 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
230 PostToPeer(true, status); 232 PostToPeer(true, status);
231 } 233 }
232 234
233 void ByteStreamWriterImpl::RegisterCallback( 235 void ByteStreamWriterImpl::RegisterCallback(
234 const base::Closure& source_callback) { 236 const base::Closure& source_callback) {
235 DCHECK(my_task_runner_->RunsTasksOnCurrentThread()); 237 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
236 space_available_callback_ = source_callback; 238 space_available_callback_ = source_callback;
237 } 239 }
238 240
241 size_t ByteStreamWriterImpl::TotalBufferedBytes() const {
242 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
243 return input_contents_size_ + output_size_used_;
kinuko 2013/08/14 09:32:23 Could this overflow?
tyoshino (SeeGerritForStatus) 2013/08/15 04:15:33 Added overflow check in Write()
244 }
245
239 // static 246 // static
240 void ByteStreamWriterImpl::UpdateWindow( 247 void ByteStreamWriterImpl::UpdateWindow(
241 scoped_refptr<LifetimeFlag> lifetime_flag, ByteStreamWriterImpl* target, 248 scoped_refptr<LifetimeFlag> lifetime_flag, ByteStreamWriterImpl* target,
242 size_t bytes_consumed) { 249 size_t bytes_consumed) {
243 // If the target object isn't alive anymore, we do nothing. 250 // If the target object isn't alive anymore, we do nothing.
244 if (!lifetime_flag->is_alive) return; 251 if (!lifetime_flag->is_alive) return;
245 252
246 target->UpdateWindowInternal(bytes_consumed); 253 target->UpdateWindowInternal(bytes_consumed);
247 } 254 }
248 255
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 my_lifetime_flag_(lifetime_flag), 302 my_lifetime_flag_(lifetime_flag),
296 received_status_(false), 303 received_status_(false),
297 status_(0), 304 status_(0),
298 unreported_consumed_bytes_(0), 305 unreported_consumed_bytes_(0),
299 peer_(NULL) { 306 peer_(NULL) {
300 DCHECK(my_lifetime_flag_.get()); 307 DCHECK(my_lifetime_flag_.get());
301 my_lifetime_flag_->is_alive = true; 308 my_lifetime_flag_->is_alive = true;
302 } 309 }
303 310
304 ByteStreamReaderImpl::~ByteStreamReaderImpl() { 311 ByteStreamReaderImpl::~ByteStreamReaderImpl() {
312 DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
305 my_lifetime_flag_->is_alive = false; 313 my_lifetime_flag_->is_alive = false;
306 } 314 }
307 315
308 void ByteStreamReaderImpl::SetPeer( 316 void ByteStreamReaderImpl::SetPeer(
309 ByteStreamWriterImpl* peer, 317 ByteStreamWriterImpl* peer,
310 scoped_refptr<base::SequencedTaskRunner> peer_task_runner, 318 scoped_refptr<base::SequencedTaskRunner> peer_task_runner,
311 scoped_refptr<LifetimeFlag> peer_lifetime_flag) { 319 scoped_refptr<LifetimeFlag> peer_lifetime_flag) {
312 peer_ = peer; 320 peer_ = peer;
313 peer_task_runner_ = peer_task_runner; 321 peer_task_runner_ = peer_task_runner;
314 peer_lifetime_flag_ = peer_lifetime_flag; 322 peer_lifetime_flag_ = peer_lifetime_flag;
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 ByteStreamReaderImpl* out = new ByteStreamReaderImpl( 440 ByteStreamReaderImpl* out = new ByteStreamReaderImpl(
433 output_task_runner, output_flag, buffer_size); 441 output_task_runner, output_flag, buffer_size);
434 442
435 in->SetPeer(out, output_task_runner, output_flag); 443 in->SetPeer(out, output_task_runner, output_flag);
436 out->SetPeer(in, input_task_runner, input_flag); 444 out->SetPeer(in, input_task_runner, input_flag);
437 input->reset(in); 445 input->reset(in);
438 output->reset(out); 446 output->reset(out);
439 } 447 }
440 448
441 } // namespace content 449 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698