OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_BROWSING_DATA_CONTENT_CONDITIONAL_CACHE_COUNTING_HELPER_H_ |
| 6 #define COMPONENTS_BROWSING_DATA_CONTENT_CONDITIONAL_CACHE_COUNTING_HELPER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "net/base/net_errors.h" |
| 12 #include "net/disk_cache/disk_cache.h" |
| 13 |
| 14 namespace content { |
| 15 class StoragePartition; |
| 16 } |
| 17 |
| 18 namespace net { |
| 19 class URLRequestContextGetter; |
| 20 } |
| 21 |
| 22 namespace browsing_data { |
| 23 |
| 24 // Helper to count the size of the http cache data from a StoragePartition. |
| 25 class ConditionalCacheCountingHelper { |
| 26 public: |
| 27 // Returns the number bytes in the selected range. |
| 28 typedef base::Callback<void(int64_t)> CacheCountCallback; |
| 29 |
| 30 static ConditionalCacheCountingHelper* CreateForRange( |
| 31 content::StoragePartition* storage_partition, |
| 32 base::Time begin_time, |
| 33 base::Time end_time); |
| 34 |
| 35 // Count the cache entries according to the specified time range. Destroys |
| 36 // this instance of ConditionalCacheCountingHelper when finished. |
| 37 // The returned WeakPtr can be used to cancel the operation if necessary. |
| 38 // Must be called on the UI thread! |
| 39 // |
| 40 // The |completion_callback| will be invoked when the operation completes. |
| 41 base::WeakPtr<ConditionalCacheCountingHelper> CountAndDestroySelfWhenFinished( |
| 42 const CacheCountCallback& result_callback); |
| 43 |
| 44 // Cancel the currently counting process. The task on the IO thread will stop |
| 45 // and the callback will return net::ERR_ABORTED. |
| 46 void CancelCounting(); |
| 47 |
| 48 bool IsFinished(); |
| 49 |
| 50 private: |
| 51 enum class CacheState { |
| 52 NONE, |
| 53 CREATE_MAIN, |
| 54 CREATE_MEDIA, |
| 55 COUNT_MAIN, |
| 56 COUNT_MEDIA, |
| 57 DONE |
| 58 }; |
| 59 |
| 60 friend class base::DeleteHelper<ConditionalCacheCountingHelper>; |
| 61 |
| 62 ConditionalCacheCountingHelper( |
| 63 base::Time begin_time, |
| 64 base::Time end_time, |
| 65 net::URLRequestContextGetter* main_context_getter, |
| 66 net::URLRequestContextGetter* media_context_getter); |
| 67 ~ConditionalCacheCountingHelper(); |
| 68 |
| 69 void Finished(); |
| 70 |
| 71 void CountHttpCacheOnIOThread(); |
| 72 void DoCountCache(int rv); |
| 73 |
| 74 int CountEntries(disk_cache::Backend* backend); |
| 75 void IterateOverEntries(int error); |
| 76 |
| 77 // Stores the cache size computation result before it can be returned |
| 78 // via a callback. This is either the sum of size of the the two cache |
| 79 // backends, or an error code if the calculation failed. |
| 80 int64_t calculation_result_; |
| 81 |
| 82 CacheCountCallback result_callback_; |
| 83 const base::Time begin_time_; |
| 84 const base::Time end_time_; |
| 85 |
| 86 // This flag is used to tell the IO thread to stop counting if the user |
| 87 // called CancelCounting(). |
| 88 volatile bool is_cancelled_; |
| 89 bool is_finished_; |
| 90 |
| 91 const scoped_refptr<net::URLRequestContextGetter> main_context_getter_; |
| 92 const scoped_refptr<net::URLRequestContextGetter> media_context_getter_; |
| 93 |
| 94 CacheState next_cache_state_; |
| 95 disk_cache::Backend* cache_; |
| 96 |
| 97 std::unique_ptr<disk_cache::Backend::Iterator> iterator_; |
| 98 disk_cache::Entry* current_entry_; |
| 99 |
| 100 base::WeakPtrFactory<ConditionalCacheCountingHelper> weak_ptr_factory_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(ConditionalCacheCountingHelper); |
| 103 }; |
| 104 |
| 105 } // namespace browsing_data |
| 106 |
| 107 #endif // COMPONENTS_BROWSING_DATA_CONTENT_CONDITIONAL_CACHE_COUNTING_HELPER_H_ |
OLD | NEW |