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"
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());
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
;
41 bool StreamRegistry::CloneStream(const GURL
& url
, const GURL
& src_url
) {
42 DCHECK(CalledOnValidThread());
43 scoped_refptr
<Stream
> stream(GetStream(src_url
));
45 streams_
[url
] = stream
;
51 void StreamRegistry::UnregisterStream(const GURL
& url
) {
52 DCHECK(CalledOnValidThread());
54 StreamMap::iterator iter
= streams_
.find(url
);
55 if (iter
== streams_
.end())
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
;
69 bool StreamRegistry::UpdateMemoryUsage(const GURL
& url
,
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())
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
)
88 total_memory_usage_
= current_total_memory_usage
+ increase
;
92 } // namespace content