| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 COMPONENTS_PRECACHE_CORE_FETCHER_POOL_H_ | 5 #ifndef COMPONENTS_PRECACHE_CORE_FETCHER_POOL_H_ |
| 6 #define COMPONENTS_PRECACHE_CORE_FETCHER_POOL_H_ | 6 #define COMPONENTS_PRECACHE_CORE_FETCHER_POOL_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 8 #include <unordered_map> | 9 #include <unordered_map> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 | 12 |
| 13 namespace precache { | 13 namespace precache { |
| 14 | 14 |
| 15 // FetcherPool that accepts a limited number of elements. | 15 // FetcherPool that accepts a limited number of elements. |
| 16 // | 16 // |
| 17 // FetcherPool is particularly suited for having multiple URLFetchers running | 17 // FetcherPool is particularly suited for having multiple URLFetchers running |
| 18 // in parallel. | 18 // in parallel. |
| 19 // | 19 // |
| 20 // It doesn't enqueue the elements above a defined capacity. The callsite must | 20 // It doesn't enqueue the elements above a defined capacity. The callsite must |
| 21 // check for IsAvailable before calling Start. | 21 // check for IsAvailable before calling Start. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 35 // // OnURLFetchComplete. | 35 // // OnURLFetchComplete. |
| 36 // start_next_batch(); | 36 // start_next_batch(); |
| 37 template <typename T> | 37 template <typename T> |
| 38 class FetcherPool { | 38 class FetcherPool { |
| 39 public: | 39 public: |
| 40 explicit FetcherPool(size_t max_size) : max_size_(max_size){}; | 40 explicit FetcherPool(size_t max_size) : max_size_(max_size){}; |
| 41 virtual ~FetcherPool(){}; | 41 virtual ~FetcherPool(){}; |
| 42 | 42 |
| 43 // Takes ownership and adds the given |element| to the pool. | 43 // Takes ownership and adds the given |element| to the pool. |
| 44 // The element will live until its deletion. | 44 // The element will live until its deletion. |
| 45 void Add(scoped_ptr<T> element) { | 45 void Add(std::unique_ptr<T> element) { |
| 46 DCHECK(IsAvailable()) << "FetcherPool size exceeded. " | 46 DCHECK(IsAvailable()) << "FetcherPool size exceeded. " |
| 47 "Did you check IsAvailable?"; | 47 "Did you check IsAvailable?"; |
| 48 DCHECK(element) << "The element cannot be null."; | 48 DCHECK(element) << "The element cannot be null."; |
| 49 DCHECK(elements_.find(element.get()) == elements_.end()) | 49 DCHECK(elements_.find(element.get()) == elements_.end()) |
| 50 << "The pool already contains the given element."; | 50 << "The pool already contains the given element."; |
| 51 elements_[element.get()].reset(element.release()); | 51 elements_[element.get()].reset(element.release()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Deletes the given |element| from the pool. | 54 // Deletes the given |element| from the pool. |
| 55 void Delete(const T& element) { | 55 void Delete(const T& element) { |
| 56 DCHECK(elements_.find(&element) != elements_.end()) | 56 DCHECK(elements_.find(&element) != elements_.end()) |
| 57 << "The pool doesn't contain the given element."; | 57 << "The pool doesn't contain the given element."; |
| 58 elements_.erase(&element); | 58 elements_.erase(&element); |
| 59 } | 59 } |
| 60 | 60 |
| 61 // Deletes all the elements in the pool. | 61 // Deletes all the elements in the pool. |
| 62 void DeleteAll() { elements_.clear(); } | 62 void DeleteAll() { elements_.clear(); } |
| 63 | 63 |
| 64 // Returns true iff the pool is empty. | 64 // Returns true iff the pool is empty. |
| 65 bool IsEmpty() const { return elements_.empty(); } | 65 bool IsEmpty() const { return elements_.empty(); } |
| 66 | 66 |
| 67 // Returns true iff the pool can accept a new element. | 67 // Returns true iff the pool can accept a new element. |
| 68 bool IsAvailable() const { return max_size_ > elements_.size(); } | 68 bool IsAvailable() const { return max_size_ > elements_.size(); } |
| 69 | 69 |
| 70 private: | 70 private: |
| 71 const size_t max_size_; | 71 const size_t max_size_; |
| 72 std::unordered_map<const T*, scoped_ptr<T>> elements_; | 72 std::unordered_map<const T*, std::unique_ptr<T>> elements_; |
| 73 | 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(FetcherPool); | 74 DISALLOW_COPY_AND_ASSIGN(FetcherPool); |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 } // namespace precache | 77 } // namespace precache |
| 78 | 78 |
| 79 #endif // COMPONENTS_PRECACHE_CORE_FETCHER_POOL_H_ | 79 #endif // COMPONENTS_PRECACHE_CORE_FETCHER_POOL_H_ |
| OLD | NEW |