OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "content/browser/streams/stream_registry.h" | 5 #include "content/browser/streams/stream_registry.h" |
6 | 6 |
7 #include "content/browser/streams/stream.h" | 7 #include "content/browser/streams/stream.h" |
8 | 8 |
9 namespace content { | 9 namespace content { |
10 | 10 |
11 StreamRegistry::StreamRegistry() { | 11 namespace { |
| 12 // The maximum size of memory each StreamRegistry instance is allowed to use |
| 13 // for its Stream instances. |
| 14 const size_t kDefaultMaxMemoryUsage = 1024 * 1024 * 1024U; // 1GiB |
| 15 } |
| 16 |
| 17 StreamRegistry::StreamRegistry() |
| 18 : total_memory_usage_(0), |
| 19 max_memory_usage_(kDefaultMaxMemoryUsage) { |
12 } | 20 } |
13 | 21 |
14 StreamRegistry::~StreamRegistry() { | 22 StreamRegistry::~StreamRegistry() { |
15 } | 23 } |
16 | 24 |
17 void StreamRegistry::RegisterStream(scoped_refptr<Stream> stream) { | 25 void StreamRegistry::RegisterStream(scoped_refptr<Stream> stream) { |
18 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
19 DCHECK(stream.get()); | 27 DCHECK(stream.get()); |
20 DCHECK(!stream->url().is_empty()); | 28 DCHECK(!stream->url().is_empty()); |
21 streams_[stream->url()] = stream; | 29 streams_[stream->url()] = stream; |
(...skipping 13 matching lines...) Expand all Loading... |
35 scoped_refptr<Stream> stream(GetStream(src_url)); | 43 scoped_refptr<Stream> stream(GetStream(src_url)); |
36 if (stream.get()) { | 44 if (stream.get()) { |
37 streams_[url] = stream; | 45 streams_[url] = stream; |
38 return true; | 46 return true; |
39 } | 47 } |
40 return false; | 48 return false; |
41 } | 49 } |
42 | 50 |
43 void StreamRegistry::UnregisterStream(const GURL& url) { | 51 void StreamRegistry::UnregisterStream(const GURL& url) { |
44 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
| 53 |
| 54 StreamMap::iterator iter = streams_.find(url); |
| 55 if (iter == streams_.end()) |
| 56 return; |
| 57 |
| 58 size_t buffered_bytes = iter->second->last_total_buffered_bytes(); |
| 59 DCHECK_LE(buffered_bytes, total_memory_usage_); |
| 60 total_memory_usage_ -= buffered_bytes; |
45 streams_.erase(url); | 61 streams_.erase(url); |
46 } | 62 } |
47 | 63 |
| 64 bool StreamRegistry::UpdateMemoryUsage(const GURL& url, |
| 65 size_t current_size, |
| 66 size_t increase) { |
| 67 DCHECK(CalledOnValidThread()); |
| 68 |
| 69 StreamMap::iterator iter = streams_.find(url); |
| 70 // A Stream must be registered with its parent registry to get memory. |
| 71 if (iter == streams_.end()) |
| 72 return false; |
| 73 |
| 74 size_t last_size = iter->second->last_total_buffered_bytes(); |
| 75 DCHECK_LE(last_size, total_memory_usage_); |
| 76 size_t usage_of_others = total_memory_usage_ - last_size; |
| 77 DCHECK_LE(current_size, last_size); |
| 78 size_t current_total_memory_usage = usage_of_others + current_size; |
| 79 |
| 80 if (increase > max_memory_usage_ - current_total_memory_usage) |
| 81 return false; |
| 82 |
| 83 total_memory_usage_ = current_total_memory_usage + increase; |
| 84 return true; |
| 85 } |
| 86 |
48 } // namespace content | 87 } // namespace content |
OLD | NEW |