Commit 044f3ca9 authored by lazyboy's avatar lazyboy Committed by Commit bot

Refactor http cache removing from a StoragePartition into separate class:

  StoragePartitionHttpCacheDataRemover.

This will enable us to to clear http cache from a StoragePartition from
  chrome/ layer. <webview>s with their own storage partitions can use
  this to clear patitions' cache.

This will be used to implement clear cache for <webview>, complete
  CL is: https://chromiumcodereview.appspot.com/1021073002/

BUG=406437
Test=None, internal only change.

Review URL: https://codereview.chromium.org/1033013003

Cr-Commit-Position: refs/heads/master@{#322428}
parent db167e36
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "chrome/browser/autofill/personal_data_manager_factory.h" #include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_data/browsing_data_helper.h" #include "chrome/browser/browsing_data/browsing_data_helper.h"
#include "chrome/browser/browsing_data/storage_partition_http_cache_data_remover.h"
#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/domain_reliability/service_factory.h" #include "chrome/browser/domain_reliability/service_factory.h"
#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/download/download_prefs.h"
...@@ -54,28 +55,20 @@ ...@@ -54,28 +55,20 @@
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "components/web_cache/browser/web_cache_manager.h" #include "components/web_cache/browser/web_cache_manager.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/download_manager.h" #include "content/public/browser/download_manager.h"
#include "content/public/browser/local_storage_usage_info.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "content/public/browser/plugin_data_remover.h" #include "content/public/browser/plugin_data_remover.h"
#include "content/public/browser/session_storage_usage_info.h"
#include "content/public/browser/ssl_host_state_delegate.h" #include "content/public/browser/ssl_host_state_delegate.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/user_metrics.h" #include "content/public/browser/user_metrics.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/sdch_manager.h"
#include "net/cookies/cookie_store.h" #include "net/cookies/cookie_store.h"
#include "net/disk_cache/disk_cache.h"
#include "net/http/http_cache.h"
#include "net/http/transport_security_state.h" #include "net/http/transport_security_state.h"
#include "net/ssl/channel_id_service.h" #include "net/ssl/channel_id_service.h"
#include "net/ssl/channel_id_store.h" #include "net/ssl/channel_id_store.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "storage/browser/quota/quota_manager.h"
#include "storage/browser/quota/special_storage_policy.h" #include "storage/browser/quota/special_storage_policy.h"
#include "storage/common/quota/quota_types.h"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
...@@ -208,8 +201,6 @@ BrowsingDataRemover::BrowsingDataRemover(Profile* profile, ...@@ -208,8 +201,6 @@ BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
: profile_(profile), : profile_(profile),
delete_begin_(delete_begin), delete_begin_(delete_begin),
delete_end_(delete_end), delete_end_(delete_end),
next_cache_state_(STATE_NONE),
cache_(NULL),
main_context_getter_(profile->GetRequestContext()), main_context_getter_(profile->GetRequestContext()),
media_context_getter_(profile->GetMediaRequestContext()), media_context_getter_(profile->GetMediaRequestContext()),
deauthorize_content_licenses_request_id_(0), deauthorize_content_licenses_request_id_(0),
...@@ -614,14 +605,15 @@ void BrowsingDataRemover::RemoveImpl(int remove_mask, ...@@ -614,14 +605,15 @@ void BrowsingDataRemover::RemoveImpl(int remove_mask,
// Tell the renderers to clear their cache. // Tell the renderers to clear their cache.
web_cache::WebCacheManager::GetInstance()->ClearCache(); web_cache::WebCacheManager::GetInstance()->ClearCache();
// Invoke DoClearCache on the IO thread.
waiting_for_clear_cache_ = true;
content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache")); content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
BrowserThread::PostTask( waiting_for_clear_cache_ = true;
BrowserThread::IO, FROM_HERE, // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
base::Bind(&BrowsingDataRemover::ClearCacheOnIOThread, StoragePartitionHttpCacheDataRemover::CreateForRange(
base::Unretained(this))); BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
delete_end_)
->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
base::Unretained(this)));
#if !defined(DISABLE_NACL) #if !defined(DISABLE_NACL)
waiting_for_clear_nacl_cache_ = true; waiting_for_clear_nacl_cache_ = true;
...@@ -963,98 +955,6 @@ void BrowsingDataRemover::ClearedCache() { ...@@ -963,98 +955,6 @@ void BrowsingDataRemover::ClearedCache() {
NotifyAndDeleteIfDone(); NotifyAndDeleteIfDone();
} }
void BrowsingDataRemover::ClearCacheOnIOThread() {
// This function should be called on the IO thread.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
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);
}
// 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 BrowsingDataRemover::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(&BrowsingDataRemover::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(&BrowsingDataRemover::DoClearCache,
base::Unretained(this)));
} else {
rv = cache_->DoomEntriesBetween(
delete_begin_, delete_end_,
base::Bind(&BrowsingDataRemover::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(&BrowsingDataRemover::ClearedCache,
base::Unretained(this)));
return;
}
default: {
NOTREACHED() << "bad state";
next_cache_state_ = STATE_NONE; // Stop looping.
return;
}
}
}
}
#if !defined(DISABLE_NACL) #if !defined(DISABLE_NACL)
void BrowsingDataRemover::ClearedNaClCache() { void BrowsingDataRemover::ClearedNaClCache() {
// This function should be called on the UI thread. // This function should be called on the UI thread.
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include "storage/common/quota/quota_types.h" #include "storage/common/quota/quota_types.h"
#include "url/gurl.h" #include "url/gurl.h"
class ExtensionSpecialStoragePolicy;
class IOThread; class IOThread;
class Profile; class Profile;
...@@ -36,24 +35,10 @@ class PluginDataRemover; ...@@ -36,24 +35,10 @@ class PluginDataRemover;
class StoragePartition; class StoragePartition;
} }
namespace disk_cache {
class Backend;
}
namespace net { namespace net {
class URLRequestContextGetter; class URLRequestContextGetter;
} }
namespace storage {
class QuotaManager;
}
namespace content {
class DOMStorageContext;
struct LocalStorageUsageInfo;
struct SessionStorageUsageInfo;
}
// BrowsingDataRemover is responsible for removing data related to browsing: // BrowsingDataRemover is responsible for removing data related to browsing:
// visits in url database, downloads, cookies ... // visits in url database, downloads, cookies ...
...@@ -237,15 +222,6 @@ class BrowsingDataRemover ...@@ -237,15 +222,6 @@ class BrowsingDataRemover
// TODO(mkwst): See http://crbug.com/113621 // TODO(mkwst): See http://crbug.com/113621
friend class BrowsingDataRemoverTest; friend class BrowsingDataRemoverTest;
enum CacheState {
STATE_NONE,
STATE_CREATE_MAIN,
STATE_CREATE_MEDIA,
STATE_DELETE_MAIN,
STATE_DELETE_MEDIA,
STATE_DONE
};
// Setter for |is_removing_|; DCHECKs that we can only start removing if we're // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
// not already removing, and vice-versa. // not already removing, and vice-versa.
static void set_removing(bool is_removing); static void set_removing(bool is_removing);
...@@ -332,13 +308,6 @@ class BrowsingDataRemover ...@@ -332,13 +308,6 @@ class BrowsingDataRemover
// Callback for when the cache has been deleted. Invokes // Callback for when the cache has been deleted. Invokes
// NotifyAndDeleteIfDone. // NotifyAndDeleteIfDone.
void ClearedCache(); void ClearedCache();
// Invoked on the IO thread to delete from the cache.
void ClearCacheOnIOThread();
// Performs the actual work to delete the cache.
void DoClearCache(int rv);
#if !defined(DISABLE_NACL) #if !defined(DISABLE_NACL)
// Callback for when the NaCl cache has been deleted. Invokes // Callback for when the NaCl cache has been deleted. Invokes
// NotifyAndDeleteIfDone. // NotifyAndDeleteIfDone.
...@@ -417,9 +386,6 @@ class BrowsingDataRemover ...@@ -417,9 +386,6 @@ class BrowsingDataRemover
// to artificially delay completion. Used for testing. // to artificially delay completion. Used for testing.
static CompletionInhibitor* completion_inhibitor_; static CompletionInhibitor* completion_inhibitor_;
CacheState next_cache_state_;
disk_cache::Backend* cache_;
// Used to delete data from HTTP cache. // Used to delete data from HTTP cache.
scoped_refptr<net::URLRequestContextGetter> main_context_getter_; scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
scoped_refptr<net::URLRequestContextGetter> media_context_getter_; scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
......
// 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 "chrome/browser/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;
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;
}
}
}
}
// 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 CHROME_BROWSER_BROWSING_DATA_STORAGE_PARTITION_HTTP_CACHE_DATA_REMOVER_H_
#define CHROME_BROWSER_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;
}
// 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);
};
#endif // CHROME_BROWSER_BROWSING_DATA_STORAGE_PARTITION_HTTP_CACHE_DATA_REMOVER_H_
...@@ -251,6 +251,8 @@ ...@@ -251,6 +251,8 @@
'browser/browsing_data/cookies_tree_model.h', 'browser/browsing_data/cookies_tree_model.h',
'browser/browsing_data/local_data_container.cc', 'browser/browsing_data/local_data_container.cc',
'browser/browsing_data/local_data_container.h', 'browser/browsing_data/local_data_container.h',
'browser/browsing_data/storage_partition_http_cache_data_remover.cc',
'browser/browsing_data/storage_partition_http_cache_data_remover.h',
'browser/caps/generate_state_json.h', 'browser/caps/generate_state_json.h',
'browser/caps/generate_state_json.cc', 'browser/caps/generate_state_json.cc',
'browser/character_encoding.cc', 'browser/character_encoding.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment