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 #ifndef CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ | 5 #ifndef CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ |
6 #define CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ | 6 #define CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <set> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
13 #include "content/browser/streams/stream_register_observer.h" | 14 #include "content/browser/streams/stream_register_observer.h" |
14 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 class Stream; | 20 class Stream; |
20 | 21 |
21 // Maintains a mapping of blob: URLs to active streams. | 22 // Maintains a mapping of blob: URLs to active streams. |
22 class CONTENT_EXPORT StreamRegistry : public base::NonThreadSafe { | 23 class CONTENT_EXPORT StreamRegistry : public base::NonThreadSafe { |
23 public: | 24 public: |
24 StreamRegistry(); | 25 StreamRegistry(); |
25 virtual ~StreamRegistry(); | 26 virtual ~StreamRegistry(); |
26 | 27 |
27 // Registers a stream, and sets its URL. | 28 // Registers a stream, and sets its URL. |
28 void RegisterStream(scoped_refptr<Stream> stream); | 29 void RegisterStream(Stream* stream); |
29 | 30 |
30 // Clones a stream. Returns true on success, or false if |src_url| doesn't | 31 // Clones a stream. Returns true on success, or false if |src_url| doesn't |
31 // exist. | 32 // exist. |
32 bool CloneStream(const GURL& url, const GURL& src_url); | 33 bool CloneStream(const GURL& url, const GURL& src_url); |
33 | 34 |
34 void UnregisterStream(const GURL& url); | 35 void UnregisterStream(const GURL& url); |
35 | 36 |
36 // Called by Stream instances to request increase of memory usage. If the | 37 // Called by Stream instances to request increase of memory usage. If the |
37 // total memory usage for this registry is going to exceed the limit, | 38 // total memory usage for this registry is going to exceed the limit, |
38 // returns false. Otherwise, updates |total_memory_usage_| and returns true. | 39 // returns false. Otherwise, updates |total_memory_usage_| and returns true. |
39 // | 40 // |
40 // |current_size| is the up-to-date size of ByteStream of the Stream instance | 41 // |current_size| is the up-to-date size of ByteStream of the Stream instance |
41 // and |increase| must be the amount of data going to be added to the Stream | 42 // and |increase| must be the amount of data going to be added to the Stream |
42 // instance. | 43 // instance. |
43 bool UpdateMemoryUsage(const GURL& url, size_t current_size, size_t increase); | 44 bool UpdateMemoryUsage(const GURL& url, size_t current_size, size_t increase); |
44 | 45 |
45 // Gets the stream associated with |url|. Returns NULL if there is no such | 46 // Gets the stream associated with |url|. Returns NULL if there is no such |
46 // stream. | 47 // stream. |
47 scoped_refptr<Stream> GetStream(const GURL& url); | 48 scoped_refptr<Stream> GetStream(const GURL& url); |
48 | 49 |
49 void set_max_memory_usage_for_testing(size_t size) { | 50 void set_max_memory_usage_for_testing(size_t size) { |
50 max_memory_usage_ = size; | 51 max_memory_usage_ = size; |
51 } | 52 } |
52 | 53 |
53 void SetRegisterObserver(const GURL& url, StreamRegisterObserver* observer); | 54 void SetRegisterObserver(const GURL& url, StreamRegisterObserver* observer); |
54 void RemoveRegisterObserver(const GURL& url); | 55 void RemoveRegisterObserver(const GURL& url); |
55 | 56 |
| 57 // If the reader is aborted before the stream is registered, call this method |
| 58 // to reduce the memory consumption. After this method is called, |
| 59 // RegisterStream doesn't register the stream of the URL. |
| 60 void AbortPendingStream(const GURL& url); |
| 61 |
56 private: | 62 private: |
57 typedef std::map<GURL, scoped_refptr<Stream> > StreamMap; | 63 typedef std::map<GURL, scoped_refptr<Stream> > StreamMap; |
58 | 64 |
59 StreamMap streams_; | 65 StreamMap streams_; |
60 std::map<GURL, StreamRegisterObserver*> register_observers_; | 66 std::map<GURL, StreamRegisterObserver*> register_observers_; |
| 67 std::set<GURL> reader_aborted_urls_; |
61 | 68 |
62 size_t total_memory_usage_; | 69 size_t total_memory_usage_; |
63 | 70 |
64 // Maximum amount of memory allowed to use for Stream instances registered | 71 // Maximum amount of memory allowed to use for Stream instances registered |
65 // with this registry. | 72 // with this registry. |
66 size_t max_memory_usage_; | 73 size_t max_memory_usage_; |
67 | 74 |
68 DISALLOW_COPY_AND_ASSIGN(StreamRegistry); | 75 DISALLOW_COPY_AND_ASSIGN(StreamRegistry); |
69 }; | 76 }; |
70 | 77 |
71 } // namespace content | 78 } // namespace content |
72 | 79 |
73 #endif // CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ | 80 #endif // CONTENT_BROWSER_STREAMS_STREAM_REGISTRY_H_ |
OLD | NEW |