- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / streams / stream_registry.cc
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.
4
5 #include "content/browser/streams/stream_registry.h"
6
7 #include "content/browser/streams/stream.h"
8
9 namespace content {
10
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
15 }
16
17 StreamRegistry::StreamRegistry()
18     : total_memory_usage_(0),
19       max_memory_usage_(kDefaultMaxMemoryUsage) {
20 }
21
22 StreamRegistry::~StreamRegistry() {
23 }
24
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;
30 }
31
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;
37
38   return NULL;
39 }
40
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;
47   }
48   return false;
49 }
50
51 void StreamRegistry::UnregisterStream(const GURL& url) {
52   DCHECK(CalledOnValidThread());
53
54   StreamMap::iterator iter = streams_.find(url);
55   if (iter == streams_.end())
56     return;
57
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;
64   }
65
66   streams_.erase(url);
67 }
68
69 bool StreamRegistry::UpdateMemoryUsage(const GURL& url,
70                                        size_t current_size,
71                                        size_t increase) {
72   DCHECK(CalledOnValidThread());
73
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;
78
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;
84
85   if (increase > max_memory_usage_ - current_total_memory_usage)
86     return false;
87
88   total_memory_usage_ = current_total_memory_usage + increase;
89   return true;
90 }
91
92 }  // namespace content