| 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 namespace { | 11 namespace { |
| 12 // The maximum size of memory each StreamRegistry instance is allowed to use | 12 // The maximum size of memory each StreamRegistry instance is allowed to use |
| 13 // for its Stream instances. | 13 // for its Stream instances. |
| 14 const size_t kDefaultMaxMemoryUsage = 1024 * 1024 * 1024U; // 1GiB | 14 const size_t kDefaultMaxMemoryUsage = 1024 * 1024 * 1024U; // 1GiB |
| 15 } | 15 } |
| 16 | 16 |
| 17 StreamRegistry::StreamRegistry() | 17 StreamRegistry::StreamRegistry() |
| 18 : total_memory_usage_(0), | 18 : total_memory_usage_(0), |
| 19 max_memory_usage_(kDefaultMaxMemoryUsage) { | 19 max_memory_usage_(kDefaultMaxMemoryUsage) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 StreamRegistry::~StreamRegistry() { | 22 StreamRegistry::~StreamRegistry() { |
| 23 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 23 DCHECK(register_observers_.empty()); | 24 DCHECK(register_observers_.empty()); |
| 24 } | 25 } |
| 25 | 26 |
| 26 void StreamRegistry::RegisterStream(Stream* stream) { | 27 void StreamRegistry::RegisterStream(Stream* stream) { |
| 27 DCHECK(CalledOnValidThread()); | 28 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 28 DCHECK(stream); | 29 DCHECK(stream); |
| 29 DCHECK(!stream->url().is_empty()); | 30 DCHECK(!stream->url().is_empty()); |
| 30 | 31 |
| 31 auto aborted_url_itr = reader_aborted_urls_.find(stream->url()); | 32 auto aborted_url_itr = reader_aborted_urls_.find(stream->url()); |
| 32 if (aborted_url_itr != reader_aborted_urls_.end()) { | 33 if (aborted_url_itr != reader_aborted_urls_.end()) { |
| 33 reader_aborted_urls_.erase(aborted_url_itr); | 34 reader_aborted_urls_.erase(aborted_url_itr); |
| 34 return; | 35 return; |
| 35 } | 36 } |
| 36 streams_[stream->url()] = stream; | 37 streams_[stream->url()] = stream; |
| 37 | 38 |
| 38 auto itr = register_observers_.find(stream->url()); | 39 auto itr = register_observers_.find(stream->url()); |
| 39 if (itr != register_observers_.end()) | 40 if (itr != register_observers_.end()) |
| 40 itr->second->OnStreamRegistered(stream); | 41 itr->second->OnStreamRegistered(stream); |
| 41 } | 42 } |
| 42 | 43 |
| 43 scoped_refptr<Stream> StreamRegistry::GetStream(const GURL& url) { | 44 scoped_refptr<Stream> StreamRegistry::GetStream(const GURL& url) { |
| 44 DCHECK(CalledOnValidThread()); | 45 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 45 StreamMap::const_iterator stream = streams_.find(url); | 46 StreamMap::const_iterator stream = streams_.find(url); |
| 46 if (stream != streams_.end()) | 47 if (stream != streams_.end()) |
| 47 return stream->second; | 48 return stream->second; |
| 48 | 49 |
| 49 return NULL; | 50 return NULL; |
| 50 } | 51 } |
| 51 | 52 |
| 52 bool StreamRegistry::CloneStream(const GURL& url, const GURL& src_url) { | 53 bool StreamRegistry::CloneStream(const GURL& url, const GURL& src_url) { |
| 53 DCHECK(CalledOnValidThread()); | 54 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 54 scoped_refptr<Stream> stream(GetStream(src_url)); | 55 scoped_refptr<Stream> stream(GetStream(src_url)); |
| 55 if (stream.get()) { | 56 if (stream.get()) { |
| 56 streams_[url] = stream; | 57 streams_[url] = stream; |
| 57 return true; | 58 return true; |
| 58 } | 59 } |
| 59 return false; | 60 return false; |
| 60 } | 61 } |
| 61 | 62 |
| 62 void StreamRegistry::UnregisterStream(const GURL& url) { | 63 void StreamRegistry::UnregisterStream(const GURL& url) { |
| 63 DCHECK(CalledOnValidThread()); | 64 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 64 | 65 |
| 65 StreamMap::iterator iter = streams_.find(url); | 66 StreamMap::iterator iter = streams_.find(url); |
| 66 if (iter == streams_.end()) | 67 if (iter == streams_.end()) |
| 67 return; | 68 return; |
| 68 | 69 |
| 69 // Only update |total_memory_usage_| if |url| is NOT a Stream clone because | 70 // Only update |total_memory_usage_| if |url| is NOT a Stream clone because |
| 70 // cloned streams do not update |total_memory_usage_|. | 71 // cloned streams do not update |total_memory_usage_|. |
| 71 if (iter->second->url() == url) { | 72 if (iter->second->url() == url) { |
| 72 size_t buffered_bytes = iter->second->last_total_buffered_bytes(); | 73 size_t buffered_bytes = iter->second->last_total_buffered_bytes(); |
| 73 DCHECK_LE(buffered_bytes, total_memory_usage_); | 74 DCHECK_LE(buffered_bytes, total_memory_usage_); |
| 74 total_memory_usage_ -= buffered_bytes; | 75 total_memory_usage_ -= buffered_bytes; |
| 75 } | 76 } |
| 76 | 77 |
| 77 streams_.erase(url); | 78 streams_.erase(url); |
| 78 } | 79 } |
| 79 | 80 |
| 80 bool StreamRegistry::UpdateMemoryUsage(const GURL& url, | 81 bool StreamRegistry::UpdateMemoryUsage(const GURL& url, |
| 81 size_t current_size, | 82 size_t current_size, |
| 82 size_t increase) { | 83 size_t increase) { |
| 83 DCHECK(CalledOnValidThread()); | 84 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 84 | 85 |
| 85 StreamMap::iterator iter = streams_.find(url); | 86 StreamMap::iterator iter = streams_.find(url); |
| 86 // A Stream must be registered with its parent registry to get memory. | 87 // A Stream must be registered with its parent registry to get memory. |
| 87 if (iter == streams_.end()) | 88 if (iter == streams_.end()) |
| 88 return false; | 89 return false; |
| 89 | 90 |
| 90 size_t last_size = iter->second->last_total_buffered_bytes(); | 91 size_t last_size = iter->second->last_total_buffered_bytes(); |
| 91 DCHECK_LE(last_size, total_memory_usage_); | 92 DCHECK_LE(last_size, total_memory_usage_); |
| 92 size_t usage_of_others = total_memory_usage_ - last_size; | 93 size_t usage_of_others = total_memory_usage_ - last_size; |
| 93 DCHECK_LE(current_size, last_size); | 94 DCHECK_LE(current_size, last_size); |
| 94 size_t current_total_memory_usage = usage_of_others + current_size; | 95 size_t current_total_memory_usage = usage_of_others + current_size; |
| 95 | 96 |
| 96 if (increase > max_memory_usage_ - current_total_memory_usage) | 97 if (increase > max_memory_usage_ - current_total_memory_usage) |
| 97 return false; | 98 return false; |
| 98 | 99 |
| 99 total_memory_usage_ = current_total_memory_usage + increase; | 100 total_memory_usage_ = current_total_memory_usage + increase; |
| 100 return true; | 101 return true; |
| 101 } | 102 } |
| 102 | 103 |
| 103 | 104 |
| 104 void StreamRegistry::SetRegisterObserver(const GURL& url, | 105 void StreamRegistry::SetRegisterObserver(const GURL& url, |
| 105 StreamRegisterObserver* observer) { | 106 StreamRegisterObserver* observer) { |
| 106 DCHECK(CalledOnValidThread()); | 107 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 107 DCHECK(register_observers_.find(url) == register_observers_.end()); | 108 DCHECK(register_observers_.find(url) == register_observers_.end()); |
| 108 register_observers_[url] = observer; | 109 register_observers_[url] = observer; |
| 109 } | 110 } |
| 110 | 111 |
| 111 void StreamRegistry::RemoveRegisterObserver(const GURL& url) { | 112 void StreamRegistry::RemoveRegisterObserver(const GURL& url) { |
| 112 DCHECK(CalledOnValidThread()); | 113 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 113 register_observers_.erase(url); | 114 register_observers_.erase(url); |
| 114 } | 115 } |
| 115 | 116 |
| 116 void StreamRegistry::AbortPendingStream(const GURL& url) { | 117 void StreamRegistry::AbortPendingStream(const GURL& url) { |
| 117 DCHECK(CalledOnValidThread()); | 118 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 118 reader_aborted_urls_.insert(url); | 119 reader_aborted_urls_.insert(url); |
| 119 } | 120 } |
| 120 | 121 |
| 121 } // namespace content | 122 } // namespace content |
| OLD | NEW |