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 "chrome/browser/browsing_data/storage_partition_http_cache_data_remover
.h" |
| 6 |
| 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "content/public/browser/storage_partition.h" |
| 9 #include "net/disk_cache/disk_cache.h" |
| 10 #include "net/http/http_cache.h" |
| 11 #include "net/url_request/url_request_context.h" |
| 12 #include "net/url_request/url_request_context_getter.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 StoragePartitionHttpCacheDataRemover::StoragePartitionHttpCacheDataRemover( |
| 17 base::Time delete_begin, |
| 18 base::Time delete_end, |
| 19 net::URLRequestContextGetter* main_context_getter, |
| 20 net::URLRequestContextGetter* media_context_getter) |
| 21 : delete_begin_(delete_begin), |
| 22 delete_end_(delete_end), |
| 23 main_context_getter_(main_context_getter), |
| 24 media_context_getter_(media_context_getter), |
| 25 next_cache_state_(STATE_NONE), |
| 26 cache_(nullptr) { |
| 27 } |
| 28 |
| 29 StoragePartitionHttpCacheDataRemover::~StoragePartitionHttpCacheDataRemover() { |
| 30 } |
| 31 |
| 32 // static. |
| 33 StoragePartitionHttpCacheDataRemover* |
| 34 StoragePartitionHttpCacheDataRemover::CreateForRange( |
| 35 content::StoragePartition* storage_partition, |
| 36 base::Time delete_begin, |
| 37 base::Time delete_end) { |
| 38 return new StoragePartitionHttpCacheDataRemover( |
| 39 delete_begin, delete_end, storage_partition->GetURLRequestContext(), |
| 40 storage_partition->GetMediaURLRequestContext()); |
| 41 } |
| 42 |
| 43 void StoragePartitionHttpCacheDataRemover::Remove( |
| 44 const base::Closure& done_callback) { |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 46 DCHECK(!done_callback.is_null()); |
| 47 done_callback_ = done_callback; |
| 48 |
| 49 BrowserThread::PostTask( |
| 50 BrowserThread::IO, FROM_HERE, |
| 51 base::Bind( |
| 52 &StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread, |
| 53 base::Unretained(this))); |
| 54 } |
| 55 |
| 56 void StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread() { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 58 next_cache_state_ = STATE_NONE; |
| 59 DCHECK_EQ(STATE_NONE, next_cache_state_); |
| 60 DCHECK(main_context_getter_.get()); |
| 61 DCHECK(media_context_getter_.get()); |
| 62 |
| 63 next_cache_state_ = STATE_CREATE_MAIN; |
| 64 DoClearCache(net::OK); |
| 65 } |
| 66 |
| 67 void StoragePartitionHttpCacheDataRemover::ClearedHttpCache() { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 69 done_callback_.Run(); |
| 70 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 71 } |
| 72 |
| 73 // The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN --> |
| 74 // STATE_DELETE_MAIN --> STATE_CREATE_MEDIA --> STATE_DELETE_MEDIA --> |
| 75 // STATE_DONE, and any errors are ignored. |
| 76 void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) { |
| 77 DCHECK_NE(STATE_NONE, next_cache_state_); |
| 78 |
| 79 while (rv != net::ERR_IO_PENDING && next_cache_state_ != STATE_NONE) { |
| 80 switch (next_cache_state_) { |
| 81 case STATE_CREATE_MAIN: |
| 82 case STATE_CREATE_MEDIA: { |
| 83 // Get a pointer to the cache. |
| 84 net::URLRequestContextGetter* getter = |
| 85 (next_cache_state_ == STATE_CREATE_MAIN) |
| 86 ? main_context_getter_.get() |
| 87 : media_context_getter_.get(); |
| 88 net::HttpCache* http_cache = getter->GetURLRequestContext() |
| 89 ->http_transaction_factory() |
| 90 ->GetCache(); |
| 91 |
| 92 next_cache_state_ = (next_cache_state_ == STATE_CREATE_MAIN) |
| 93 ? STATE_DELETE_MAIN |
| 94 : STATE_DELETE_MEDIA; |
| 95 |
| 96 // Clear QUIC server information from memory and the disk cache. |
| 97 http_cache->GetSession() |
| 98 ->quic_stream_factory() |
| 99 ->ClearCachedStatesInCryptoConfig(); |
| 100 |
| 101 // Clear SDCH dictionary state. |
| 102 net::SdchManager* sdch_manager = |
| 103 getter->GetURLRequestContext()->sdch_manager(); |
| 104 // The test is probably overkill, since chrome should always have an |
| 105 // SdchManager. But in general the URLRequestContext is *not* |
| 106 // guaranteed to have an SdchManager, so checking is wise. |
| 107 if (sdch_manager) |
| 108 sdch_manager->ClearData(); |
| 109 |
| 110 rv = http_cache->GetBackend( |
| 111 &cache_, |
| 112 base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, |
| 113 base::Unretained(this))); |
| 114 break; |
| 115 } |
| 116 case STATE_DELETE_MAIN: |
| 117 case STATE_DELETE_MEDIA: { |
| 118 next_cache_state_ = (next_cache_state_ == STATE_DELETE_MAIN) |
| 119 ? STATE_CREATE_MEDIA |
| 120 : STATE_DONE; |
| 121 |
| 122 // |cache_| can be null if it cannot be initialized. |
| 123 if (cache_) { |
| 124 if (delete_begin_.is_null()) { |
| 125 rv = cache_->DoomAllEntries( |
| 126 base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, |
| 127 base::Unretained(this))); |
| 128 } else { |
| 129 rv = cache_->DoomEntriesBetween( |
| 130 delete_begin_, delete_end_, |
| 131 base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, |
| 132 base::Unretained(this))); |
| 133 } |
| 134 cache_ = NULL; |
| 135 } |
| 136 break; |
| 137 } |
| 138 case STATE_DONE: { |
| 139 cache_ = NULL; |
| 140 next_cache_state_ = STATE_NONE; |
| 141 |
| 142 // Notify the UI thread that we are done. |
| 143 BrowserThread::PostTask( |
| 144 BrowserThread::UI, FROM_HERE, |
| 145 base::Bind(&StoragePartitionHttpCacheDataRemover::ClearedHttpCache, |
| 146 base::Unretained(this))); |
| 147 return; |
| 148 } |
| 149 default: { |
| 150 NOTREACHED() << "bad state"; |
| 151 next_cache_state_ = STATE_NONE; // Stop looping. |
| 152 return; |
| 153 } |
| 154 } |
| 155 } |
| 156 } |
OLD | NEW |