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