Chromium Code Reviews| Index: content/browser/streams/stream_registry.cc |
| diff --git a/content/browser/streams/stream_registry.cc b/content/browser/streams/stream_registry.cc |
| index 39d24b39f5f7b505484a472b67a2a490892a3151..3fac83553f3a8d7a5f6e75e9b6a10ed43c9569d2 100644 |
| --- a/content/browser/streams/stream_registry.cc |
| +++ b/content/browser/streams/stream_registry.cc |
| @@ -8,7 +8,15 @@ |
| namespace content { |
| -StreamRegistry::StreamRegistry() { |
| +namespace { |
| +// The maximum size of memory each StreamRegistry instance is allowed to use |
| +// for its Stream instances. |
| +const size_t kDefaultMaxMemoryUsage = 1024 * 1024 * 1024U; // 1GiB |
| +} |
| + |
| +StreamRegistry::StreamRegistry() |
| + : total_memory_usage_(0), |
| + max_memory_usage_(kDefaultMaxMemoryUsage) { |
| } |
| StreamRegistry::~StreamRegistry() { |
| @@ -42,7 +50,35 @@ bool StreamRegistry::CloneStream(const GURL& url, const GURL& src_url) { |
| void StreamRegistry::UnregisterStream(const GURL& url) { |
| DCHECK(CalledOnValidThread()); |
| + |
| + StreamMap::iterator iter = streams_.find(url); |
| + if (iter == streams_.end()) |
| + return; |
| + |
| + size_t buffered_bytes = iter->second->last_total_buffered_bytes(); |
| + DCHECK_LE(buffered_bytes, total_memory_usage_); |
| + total_memory_usage_ -= buffered_bytes; |
| streams_.erase(url); |
| } |
| +bool StreamRegistry::UpdateMemoryUsage(const GURL& url, size_t new_size) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + StreamMap::iterator iter = streams_.find(url); |
| + // A Stream must be registered with its parent registry to get memory. |
| + if (iter == streams_.end()) |
| + return false; |
| + |
| + size_t last_size = iter->second->last_total_buffered_bytes(); |
| + DCHECK_LE(last_size, total_memory_usage_); |
| + size_t usage_of_others = total_memory_usage_ - last_size; |
| + |
| + if (new_size > std::numeric_limits<size_t>::max() - usage_of_others || |
| + usage_of_others + new_size > max_memory_usage_) |
|
Charlie Reis
2013/08/20 22:24:52
nit: Please change the second half of the check to
tyoshino (SeeGerritForStatus)
2013/08/21 05:37:28
Oh.. right. Removed numeric_limits check from Stre
|
| + return false; |
| + |
| + total_memory_usage_ = usage_of_others + new_size; |
| + return true; |
| +} |
| + |
| } // namespace content |