Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / streams / stream_registry.cc
blobf722f57d03cb21f07a9860a61b12b28dd8be2853
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
3 // found in the LICENSE file.
5 #include "content/browser/streams/stream_registry.h"
7 #include "content/browser/streams/stream.h"
9 namespace content {
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
17 StreamRegistry::StreamRegistry()
18 : total_memory_usage_(0),
19 max_memory_usage_(kDefaultMaxMemoryUsage) {
22 StreamRegistry::~StreamRegistry() {
25 void StreamRegistry::RegisterStream(scoped_refptr<Stream> stream) {
26 DCHECK(CalledOnValidThread());
27 DCHECK(stream.get());
28 DCHECK(!stream->url().is_empty());
29 streams_[stream->url()] = stream;
32 scoped_refptr<Stream> StreamRegistry::GetStream(const GURL& url) {
33 DCHECK(CalledOnValidThread());
34 StreamMap::const_iterator stream = streams_.find(url);
35 if (stream != streams_.end())
36 return stream->second;
38 return NULL;
41 bool StreamRegistry::CloneStream(const GURL& url, const GURL& src_url) {
42 DCHECK(CalledOnValidThread());
43 scoped_refptr<Stream> stream(GetStream(src_url));
44 if (stream.get()) {
45 streams_[url] = stream;
46 return true;
48 return false;
51 void StreamRegistry::UnregisterStream(const GURL& url) {
52 DCHECK(CalledOnValidThread());
54 StreamMap::iterator iter = streams_.find(url);
55 if (iter == streams_.end())
56 return;
58 // Only update |total_memory_usage_| if |url| is NOT a Stream clone because
59 // cloned streams do not update |total_memory_usage_|.
60 if (iter->second->url() == url) {
61 size_t buffered_bytes = iter->second->last_total_buffered_bytes();
62 DCHECK_LE(buffered_bytes, total_memory_usage_);
63 total_memory_usage_ -= buffered_bytes;
66 streams_.erase(url);
69 bool StreamRegistry::UpdateMemoryUsage(const GURL& url,
70 size_t current_size,
71 size_t increase) {
72 DCHECK(CalledOnValidThread());
74 StreamMap::iterator iter = streams_.find(url);
75 // A Stream must be registered with its parent registry to get memory.
76 if (iter == streams_.end())
77 return false;
79 size_t last_size = iter->second->last_total_buffered_bytes();
80 DCHECK_LE(last_size, total_memory_usage_);
81 size_t usage_of_others = total_memory_usage_ - last_size;
82 DCHECK_LE(current_size, last_size);
83 size_t current_total_memory_usage = usage_of_others + current_size;
85 if (increase > max_memory_usage_ - current_total_memory_usage)
86 return false;
88 total_memory_usage_ = current_total_memory_usage + increase;
89 return true;
92 } // namespace content