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