| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 #include "components/browsing_data/content/storage_partition_http_cache_data_rem
over.h" | |
| 6 | |
| 7 #include "base/location.h" | |
| 8 #include "base/single_thread_task_runner.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "content/public/browser/storage_partition.h" | |
| 13 #include "net/base/sdch_manager.h" | |
| 14 #include "net/disk_cache/blockfile/backend_impl.h" | |
| 15 #include "net/disk_cache/disk_cache.h" | |
| 16 #include "net/disk_cache/memory/mem_backend_impl.h" | |
| 17 #include "net/disk_cache/simple/simple_backend_impl.h" | |
| 18 #include "net/http/http_cache.h" | |
| 19 #include "net/url_request/url_request_context.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 | |
| 22 using content::BrowserThread; | |
| 23 | |
| 24 namespace browsing_data { | |
| 25 | |
| 26 StoragePartitionHttpCacheDataRemover::StoragePartitionHttpCacheDataRemover( | |
| 27 base::Callback<bool(const GURL&)> url_predicate, | |
| 28 base::Time delete_begin, | |
| 29 base::Time delete_end, | |
| 30 net::URLRequestContextGetter* main_context_getter, | |
| 31 net::URLRequestContextGetter* media_context_getter) | |
| 32 : url_predicate_(url_predicate), | |
| 33 delete_begin_(delete_begin), | |
| 34 delete_end_(delete_end), | |
| 35 main_context_getter_(main_context_getter), | |
| 36 media_context_getter_(media_context_getter), | |
| 37 next_cache_state_(CacheState::NONE), | |
| 38 cache_(nullptr) {} | |
| 39 | |
| 40 StoragePartitionHttpCacheDataRemover::~StoragePartitionHttpCacheDataRemover() { | |
| 41 } | |
| 42 | |
| 43 // static. | |
| 44 StoragePartitionHttpCacheDataRemover* | |
| 45 StoragePartitionHttpCacheDataRemover::CreateForRange( | |
| 46 content::StoragePartition* storage_partition, | |
| 47 base::Time delete_begin, | |
| 48 base::Time delete_end) { | |
| 49 return new StoragePartitionHttpCacheDataRemover( | |
| 50 base::Callback<bool(const GURL&)>(), // Null callback. | |
| 51 delete_begin, delete_end, | |
| 52 storage_partition->GetURLRequestContext(), | |
| 53 storage_partition->GetMediaURLRequestContext()); | |
| 54 } | |
| 55 | |
| 56 // static. | |
| 57 StoragePartitionHttpCacheDataRemover* | |
| 58 StoragePartitionHttpCacheDataRemover::CreateForURLsAndRange( | |
| 59 content::StoragePartition* storage_partition, | |
| 60 const base::Callback<bool(const GURL&)>& url_predicate, | |
| 61 base::Time delete_begin, | |
| 62 base::Time delete_end) { | |
| 63 return new StoragePartitionHttpCacheDataRemover( | |
| 64 url_predicate, delete_begin, delete_end, | |
| 65 storage_partition->GetURLRequestContext(), | |
| 66 storage_partition->GetMediaURLRequestContext()); | |
| 67 } | |
| 68 | |
| 69 void StoragePartitionHttpCacheDataRemover::Remove( | |
| 70 const base::Closure& done_callback) { | |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 72 DCHECK(!done_callback.is_null()); | |
| 73 done_callback_ = done_callback; | |
| 74 | |
| 75 BrowserThread::PostTask( | |
| 76 BrowserThread::IO, FROM_HERE, | |
| 77 base::Bind( | |
| 78 &StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread, | |
| 79 base::Unretained(this))); | |
| 80 } | |
| 81 | |
| 82 void StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread() { | |
| 83 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 84 next_cache_state_ = CacheState::NONE; | |
| 85 DCHECK_EQ(CacheState::NONE, next_cache_state_); | |
| 86 DCHECK(main_context_getter_.get()); | |
| 87 DCHECK(media_context_getter_.get()); | |
| 88 | |
| 89 next_cache_state_ = CacheState::CREATE_MAIN; | |
| 90 DoClearCache(net::OK); | |
| 91 } | |
| 92 | |
| 93 void StoragePartitionHttpCacheDataRemover::ClearedHttpCache() { | |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 95 done_callback_.Run(); | |
| 96 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
| 97 } | |
| 98 | |
| 99 // The expected state sequence is CacheState::NONE --> CacheState::CREATE_MAIN | |
| 100 // --> CacheState::PROCESS_MAIN --> CacheState::CREATE_MEDIA --> | |
| 101 // CacheState::PROCESS_MEDIA --> CacheState::DONE, and any errors are ignored. | |
| 102 void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) { | |
| 103 DCHECK_NE(CacheState::NONE, next_cache_state_); | |
| 104 | |
| 105 while (rv != net::ERR_IO_PENDING && next_cache_state_ != CacheState::NONE) { | |
| 106 switch (next_cache_state_) { | |
| 107 case CacheState::CREATE_MAIN: | |
| 108 case CacheState::CREATE_MEDIA: { | |
| 109 // Get a pointer to the cache. | |
| 110 net::URLRequestContextGetter* getter = | |
| 111 (next_cache_state_ == CacheState::CREATE_MAIN) | |
| 112 ? main_context_getter_.get() | |
| 113 : media_context_getter_.get(); | |
| 114 net::HttpCache* http_cache = getter->GetURLRequestContext() | |
| 115 ->http_transaction_factory() | |
| 116 ->GetCache(); | |
| 117 | |
| 118 next_cache_state_ = (next_cache_state_ == CacheState::CREATE_MAIN) | |
| 119 ? CacheState::DELETE_MAIN | |
| 120 : CacheState::DELETE_MEDIA; | |
| 121 | |
| 122 // Clear QUIC server information from memory and the disk cache. | |
| 123 http_cache->GetSession() | |
| 124 ->quic_stream_factory() | |
| 125 ->ClearCachedStatesInCryptoConfig(url_predicate_); | |
| 126 | |
| 127 // Clear SDCH dictionary state. | |
| 128 net::SdchManager* sdch_manager = | |
| 129 getter->GetURLRequestContext()->sdch_manager(); | |
| 130 // The test is probably overkill, since chrome should always have an | |
| 131 // SdchManager. But in general the URLRequestContext is *not* | |
| 132 // guaranteed to have an SdchManager, so checking is wise. | |
| 133 if (sdch_manager) | |
| 134 sdch_manager->ClearData(); | |
| 135 | |
| 136 rv = http_cache->GetBackend( | |
| 137 &cache_, | |
| 138 base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, | |
| 139 base::Unretained(this))); | |
| 140 break; | |
| 141 } | |
| 142 case CacheState::DELETE_MAIN: | |
| 143 case CacheState::DELETE_MEDIA: { | |
| 144 next_cache_state_ = (next_cache_state_ == CacheState::DELETE_MAIN) | |
| 145 ? CacheState::CREATE_MEDIA | |
| 146 : CacheState::DONE; | |
| 147 | |
| 148 // |cache_| can be null if it cannot be initialized. | |
| 149 if (cache_) { | |
| 150 if (!url_predicate_.is_null()) { | |
| 151 rv = (new ConditionalCacheDeletionHelper( | |
| 152 cache_, | |
| 153 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( | |
| 154 url_predicate_, | |
| 155 delete_begin_, | |
| 156 delete_end_)))->DeleteAndDestroySelfWhenFinished( | |
| 157 base::Bind( | |
| 158 &StoragePartitionHttpCacheDataRemover::DoClearCache, | |
| 159 base::Unretained(this))); | |
| 160 } else if (delete_begin_.is_null() && delete_end_.is_max()) { | |
| 161 rv = cache_->DoomAllEntries(base::Bind( | |
| 162 &StoragePartitionHttpCacheDataRemover::DoClearCache, | |
| 163 base::Unretained(this))); | |
| 164 } else { | |
| 165 rv = cache_->DoomEntriesBetween( | |
| 166 delete_begin_, delete_end_, | |
| 167 base::Bind( | |
| 168 &StoragePartitionHttpCacheDataRemover::DoClearCache, | |
| 169 base::Unretained(this))); | |
| 170 } | |
| 171 cache_ = NULL; | |
| 172 } | |
| 173 break; | |
| 174 } | |
| 175 case CacheState::DONE: { | |
| 176 cache_ = NULL; | |
| 177 next_cache_state_ = CacheState::NONE; | |
| 178 | |
| 179 // Notify the UI thread that we are done. | |
| 180 BrowserThread::PostTask( | |
| 181 BrowserThread::UI, FROM_HERE, | |
| 182 base::Bind(&StoragePartitionHttpCacheDataRemover::ClearedHttpCache, | |
| 183 base::Unretained(this))); | |
| 184 return; | |
| 185 } | |
| 186 case CacheState::NONE: { | |
| 187 NOTREACHED() << "bad state"; | |
| 188 return; | |
| 189 } | |
| 190 } | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 } // namespace browsing_data | |
| OLD | NEW |