diff options
author | lazyboy <lazyboy@chromium.org> | 2015-04-01 18:04:58 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-02 01:06:05 +0000 |
commit | 14082d23ec62e040992a82a7e535d2c004874b8a (patch) | |
tree | da4c6d5d34e54d79c426b1899441479f92885832 /components/browsing_data | |
parent | a3ae61c719f620a9d1aa217b07e4099d44e53765 (diff) | |
download | chromium_src-14082d23ec62e040992a82a7e535d2c004874b8a.zip chromium_src-14082d23ec62e040992a82a7e535d2c004874b8a.tar.gz chromium_src-14082d23ec62e040992a82a7e535d2c004874b8a.tar.bz2 |
Move StoragePartitionHttpCacheRemover to browsing_data/ components.
This enables us to use StoragePartitionHttpCacheRemover from extensions/.
SPHCRemover used to live under chrome/.
WebViewGuest, which uses it lives in extensions/, we currently have to install
a chrome/ delegate (ChromeWebViewGuestDelegate) to call into SPHCRemover.
This change will enable us to use SPHCRemover directly from WebViewGuest in
extensions/.
BUG=471287
Test=None, internal only change.
Review URL: https://codereview.chromium.org/1049423002
Cr-Commit-Position: refs/heads/master@{#323385}
Diffstat (limited to 'components/browsing_data')
-rw-r--r-- | components/browsing_data/BUILD.gn | 19 | ||||
-rw-r--r-- | components/browsing_data/DEPS | 4 | ||||
-rw-r--r-- | components/browsing_data/OWNERS | 4 | ||||
-rw-r--r-- | components/browsing_data/storage_partition_http_cache_data_remover.cc | 160 | ||||
-rw-r--r-- | components/browsing_data/storage_partition_http_cache_data_remover.h | 82 |
5 files changed, 269 insertions, 0 deletions
diff --git a/components/browsing_data/BUILD.gn b/components/browsing_data/BUILD.gn new file mode 100644 index 0000000..e8af5b6 --- /dev/null +++ b/components/browsing_data/BUILD.gn @@ -0,0 +1,19 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +static_library("browsing_data") { + output_name = "browsing_data" + sources = [ + "storage_partition_http_cache_data_remover.cc", + "storage_partition_http_cache_data_remover.h", + ] + + configs += [ "//build/config/compiler:no_size_t_to_int_warning" ] + + deps = [ + "//base", + "//content/public/browser", + "//net", + ] +} diff --git a/components/browsing_data/DEPS b/components/browsing_data/DEPS new file mode 100644 index 0000000..8c57389 --- /dev/null +++ b/components/browsing_data/DEPS @@ -0,0 +1,4 @@ +include_rules = [ + "+content/public/browser", + "+net", +] diff --git a/components/browsing_data/OWNERS b/components/browsing_data/OWNERS new file mode 100644 index 0000000..0944d92 --- /dev/null +++ b/components/browsing_data/OWNERS @@ -0,0 +1,4 @@ +markusheintz@chromium.org +mkwst@chromium.org +bauerb@chromium.org +michaeln@chromium.org diff --git a/components/browsing_data/storage_partition_http_cache_data_remover.cc b/components/browsing_data/storage_partition_http_cache_data_remover.cc new file mode 100644 index 0000000..aae4e3b --- /dev/null +++ b/components/browsing_data/storage_partition_http_cache_data_remover.cc @@ -0,0 +1,160 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/browsing_data/storage_partition_http_cache_data_remover.h" + +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/storage_partition.h" +#include "net/disk_cache/disk_cache.h" +#include "net/http/http_cache.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_getter.h" + +using content::BrowserThread; + +namespace browsing_data { + +StoragePartitionHttpCacheDataRemover::StoragePartitionHttpCacheDataRemover( + base::Time delete_begin, + base::Time delete_end, + net::URLRequestContextGetter* main_context_getter, + net::URLRequestContextGetter* media_context_getter) + : delete_begin_(delete_begin), + delete_end_(delete_end), + main_context_getter_(main_context_getter), + media_context_getter_(media_context_getter), + next_cache_state_(STATE_NONE), + cache_(nullptr) { +} + +StoragePartitionHttpCacheDataRemover::~StoragePartitionHttpCacheDataRemover() { +} + +// static. +StoragePartitionHttpCacheDataRemover* +StoragePartitionHttpCacheDataRemover::CreateForRange( + content::StoragePartition* storage_partition, + base::Time delete_begin, + base::Time delete_end) { + return new StoragePartitionHttpCacheDataRemover( + delete_begin, delete_end, storage_partition->GetURLRequestContext(), + storage_partition->GetMediaURLRequestContext()); +} + +void StoragePartitionHttpCacheDataRemover::Remove( + const base::Closure& done_callback) { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + DCHECK(!done_callback.is_null()); + done_callback_ = done_callback; + + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::Bind( + &StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread, + base::Unretained(this))); +} + +void StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread() { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + next_cache_state_ = STATE_NONE; + DCHECK_EQ(STATE_NONE, next_cache_state_); + DCHECK(main_context_getter_.get()); + DCHECK(media_context_getter_.get()); + + next_cache_state_ = STATE_CREATE_MAIN; + DoClearCache(net::OK); +} + +void StoragePartitionHttpCacheDataRemover::ClearedHttpCache() { + DCHECK_CURRENTLY_ON(BrowserThread::UI); + done_callback_.Run(); + base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); +} + +// The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN --> +// STATE_DELETE_MAIN --> STATE_CREATE_MEDIA --> STATE_DELETE_MEDIA --> +// STATE_DONE, and any errors are ignored. +void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) { + DCHECK_NE(STATE_NONE, next_cache_state_); + + while (rv != net::ERR_IO_PENDING && next_cache_state_ != STATE_NONE) { + switch (next_cache_state_) { + case STATE_CREATE_MAIN: + case STATE_CREATE_MEDIA: { + // Get a pointer to the cache. + net::URLRequestContextGetter* getter = + (next_cache_state_ == STATE_CREATE_MAIN) + ? main_context_getter_.get() + : media_context_getter_.get(); + net::HttpCache* http_cache = getter->GetURLRequestContext() + ->http_transaction_factory() + ->GetCache(); + + next_cache_state_ = (next_cache_state_ == STATE_CREATE_MAIN) + ? STATE_DELETE_MAIN + : STATE_DELETE_MEDIA; + + // Clear QUIC server information from memory and the disk cache. + http_cache->GetSession() + ->quic_stream_factory() + ->ClearCachedStatesInCryptoConfig(); + + // Clear SDCH dictionary state. + net::SdchManager* sdch_manager = + getter->GetURLRequestContext()->sdch_manager(); + // The test is probably overkill, since chrome should always have an + // SdchManager. But in general the URLRequestContext is *not* + // guaranteed to have an SdchManager, so checking is wise. + if (sdch_manager) + sdch_manager->ClearData(); + + rv = http_cache->GetBackend( + &cache_, + base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, + base::Unretained(this))); + break; + } + case STATE_DELETE_MAIN: + case STATE_DELETE_MEDIA: { + next_cache_state_ = (next_cache_state_ == STATE_DELETE_MAIN) + ? STATE_CREATE_MEDIA + : STATE_DONE; + + // |cache_| can be null if it cannot be initialized. + if (cache_) { + if (delete_begin_.is_null()) { + rv = cache_->DoomAllEntries( + base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, + base::Unretained(this))); + } else { + rv = cache_->DoomEntriesBetween( + delete_begin_, delete_end_, + base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, + base::Unretained(this))); + } + cache_ = NULL; + } + break; + } + case STATE_DONE: { + cache_ = NULL; + next_cache_state_ = STATE_NONE; + + // Notify the UI thread that we are done. + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + base::Bind(&StoragePartitionHttpCacheDataRemover::ClearedHttpCache, + base::Unretained(this))); + return; + } + default: { + NOTREACHED() << "bad state"; + next_cache_state_ = STATE_NONE; // Stop looping. + return; + } + } + } +} + +} // namespace browsing_data diff --git a/components/browsing_data/storage_partition_http_cache_data_remover.h b/components/browsing_data/storage_partition_http_cache_data_remover.h new file mode 100644 index 0000000..eb9f575 --- /dev/null +++ b/components/browsing_data/storage_partition_http_cache_data_remover.h @@ -0,0 +1,82 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_BROWSING_DATA_STORAGE_PARTITION_HTTP_CACHE_DATA_REMOVER_H_ +#define COMPONENTS_BROWSING_DATA_STORAGE_PARTITION_HTTP_CACHE_DATA_REMOVER_H_ + +#include "base/callback.h" +#include "base/sequenced_task_runner_helpers.h" +#include "base/time/time.h" + +namespace content { +class StoragePartition; +} + +namespace disk_cache { +class Backend; +} + +namespace net { +class URLRequestContextGetter; +} + +namespace browsing_data { + +// Helper to remove http cache data from a StoragePartition. +class StoragePartitionHttpCacheDataRemover { + public: + static StoragePartitionHttpCacheDataRemover* CreateForRange( + content::StoragePartition* storage_partition, + base::Time delete_begin, + base::Time delete_end); + + // Calls |done_callback| upon completion and also destroys itself. + void Remove(const base::Closure& done_callback); + + private: + enum CacheState { + STATE_NONE, + STATE_CREATE_MAIN, + STATE_CREATE_MEDIA, + STATE_DELETE_MAIN, + STATE_DELETE_MEDIA, + STATE_DONE + }; + + StoragePartitionHttpCacheDataRemover( + base::Time delete_begin, + base::Time delete_end, + net::URLRequestContextGetter* main_context_getter, + net::URLRequestContextGetter* media_context_getter); + + // StoragePartitionHttpCacheDataRemover deletes itself (using DeleteHelper) + // and is not supposed to be deleted by other objects so make destructor + // private and DeleteHelper a friend. + friend class base::DeleteHelper<StoragePartitionHttpCacheDataRemover>; + + ~StoragePartitionHttpCacheDataRemover(); + + void ClearHttpCacheOnIOThread(); + void ClearedHttpCache(); + // Performs the actual work to delete the cache. + void DoClearCache(int rv); + + const base::Time delete_begin_; + const base::Time delete_end_; + + const scoped_refptr<net::URLRequestContextGetter> main_context_getter_; + const scoped_refptr<net::URLRequestContextGetter> media_context_getter_; + + base::Closure done_callback_; + + // IO. + int next_cache_state_; + disk_cache::Backend* cache_; + + DISALLOW_COPY_AND_ASSIGN(StoragePartitionHttpCacheDataRemover); +}; + +} // namespace browsing_data + +#endif // COMPONENTS_BROWSING_DATA_STORAGE_PARTITION_HTTP_CACHE_DATA_REMOVER_H_ |