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() {
23 DCHECK(register_observers_
.empty());
26 void StreamRegistry::RegisterStream(Stream
* stream
) {
27 DCHECK(CalledOnValidThread());
29 DCHECK(!stream
->url().is_empty());
31 auto aborted_url_itr
= reader_aborted_urls_
.find(stream
->url());
32 if (aborted_url_itr
!= reader_aborted_urls_
.end()) {
33 reader_aborted_urls_
.erase(aborted_url_itr
);
36 streams_
[stream
->url()] = stream
;
38 auto itr
= register_observers_
.find(stream
->url());
39 if (itr
!= register_observers_
.end())
40 itr
->second
->OnStreamRegistered(stream
);
43 scoped_refptr
<Stream
> StreamRegistry::GetStream(const GURL
& url
) {
44 DCHECK(CalledOnValidThread());
45 StreamMap::const_iterator stream
= streams_
.find(url
);
46 if (stream
!= streams_
.end())
47 return stream
->second
;
52 bool StreamRegistry::CloneStream(const GURL
& url
, const GURL
& src_url
) {
53 DCHECK(CalledOnValidThread());
54 scoped_refptr
<Stream
> stream(GetStream(src_url
));
56 streams_
[url
] = stream
;
62 void StreamRegistry::UnregisterStream(const GURL
& url
) {
63 DCHECK(CalledOnValidThread());
65 StreamMap::iterator iter
= streams_
.find(url
);
66 if (iter
== streams_
.end())
69 // Only update |total_memory_usage_| if |url| is NOT a Stream clone because
70 // cloned streams do not update |total_memory_usage_|.
71 if (iter
->second
->url() == url
) {
72 size_t buffered_bytes
= iter
->second
->last_total_buffered_bytes();
73 DCHECK_LE(buffered_bytes
, total_memory_usage_
);
74 total_memory_usage_
-= buffered_bytes
;
80 bool StreamRegistry::UpdateMemoryUsage(const GURL
& url
,
83 DCHECK(CalledOnValidThread());
85 StreamMap::iterator iter
= streams_
.find(url
);
86 // A Stream must be registered with its parent registry to get memory.
87 if (iter
== streams_
.end())
90 size_t last_size
= iter
->second
->last_total_buffered_bytes();
91 DCHECK_LE(last_size
, total_memory_usage_
);
92 size_t usage_of_others
= total_memory_usage_
- last_size
;
93 DCHECK_LE(current_size
, last_size
);
94 size_t current_total_memory_usage
= usage_of_others
+ current_size
;
96 if (increase
> max_memory_usage_
- current_total_memory_usage
)
99 total_memory_usage_
= current_total_memory_usage
+ increase
;
104 void StreamRegistry::SetRegisterObserver(const GURL
& url
,
105 StreamRegisterObserver
* observer
) {
106 DCHECK(CalledOnValidThread());
107 DCHECK(register_observers_
.find(url
) == register_observers_
.end());
108 register_observers_
[url
] = observer
;
111 void StreamRegistry::RemoveRegisterObserver(const GURL
& url
) {
112 DCHECK(CalledOnValidThread());
113 register_observers_
.erase(url
);
116 void StreamRegistry::AbortPendingStream(const GURL
& url
) {
117 DCHECK(CalledOnValidThread());
118 reader_aborted_urls_
.insert(url
);
121 } // namespace content