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