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 "content/browser/cache_storage/cache_storage_context_impl.h"
8 #include "base/files/file_path.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "content/browser/cache_storage/cache_storage_manager.h"
11 #include "content/browser/fileapi/chrome_blob_storage_context.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/url_request/url_request_context_getter.h"
15 #include "storage/browser/blob/blob_storage_context.h"
16 #include "storage/browser/quota/quota_manager_proxy.h"
17 #include "storage/browser/quota/special_storage_policy.h"
21 CacheStorageContextImpl::CacheStorageContextImpl(
22 BrowserContext
* browser_context
) {
23 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
26 CacheStorageContextImpl::~CacheStorageContextImpl() {
29 void CacheStorageContextImpl::Init(
30 const base::FilePath
& user_data_directory
,
31 storage::QuotaManagerProxy
* quota_manager_proxy
,
32 storage::SpecialStoragePolicy
* special_storage_policy
) {
33 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
35 is_incognito_
= user_data_directory
.empty();
36 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
37 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner
=
38 pool
->GetSequencedTaskRunnerWithShutdownBehavior(
39 BrowserThread::GetBlockingPool()->GetSequenceToken(),
40 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
42 // This thread-hopping antipattern is needed here for some unit tests, where
43 // browser threads are collapsed the quota manager is initialized before the
44 // posted task can register the quota client.
45 // TODO: Fix the tests to let the quota manager initialize normally.
46 if (BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
47 CreateCacheStorageManager(user_data_directory
, cache_task_runner
,
48 quota_manager_proxy
, special_storage_policy
);
52 BrowserThread::PostTask(
53 BrowserThread::IO
, FROM_HERE
,
54 base::Bind(&CacheStorageContextImpl::CreateCacheStorageManager
, this,
55 user_data_directory
, cache_task_runner
,
56 make_scoped_refptr(quota_manager_proxy
),
57 make_scoped_refptr(special_storage_policy
)));
60 void CacheStorageContextImpl::Shutdown() {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
63 BrowserThread::PostTask(
64 BrowserThread::IO
, FROM_HERE
,
65 base::Bind(&CacheStorageContextImpl::ShutdownOnIO
, this));
68 CacheStorageManager
* CacheStorageContextImpl::cache_manager() const {
69 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
70 return cache_manager_
.get();
73 void CacheStorageContextImpl::SetBlobParametersForCache(
74 net::URLRequestContextGetter
* request_context_getter
,
75 ChromeBlobStorageContext
* blob_storage_context
) {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
78 if (cache_manager_
&& request_context_getter
&& blob_storage_context
) {
79 cache_manager_
->SetBlobParametersForCache(
80 request_context_getter
, blob_storage_context
->context()->AsWeakPtr());
84 void CacheStorageContextImpl::GetAllOriginsInfo(
85 const CacheStorageContext::GetUsageInfoCallback
& callback
) {
86 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
88 if (!cache_manager_
) {
89 BrowserThread::PostTask(
90 BrowserThread::IO
, FROM_HERE
,
91 base::Bind(callback
, std::vector
<CacheStorageUsageInfo
>()));
95 cache_manager_
->GetAllOriginsUsage(callback
);
98 void CacheStorageContextImpl::DeleteForOrigin(const GURL
& origin
) {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
101 cache_manager_
->DeleteOriginData(origin
);
104 void CacheStorageContextImpl::CreateCacheStorageManager(
105 const base::FilePath
& user_data_directory
,
106 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
107 storage::QuotaManagerProxy
* quota_manager_proxy
,
108 storage::SpecialStoragePolicy
* special_storage_policy
) {
109 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
111 DCHECK(!cache_manager_
);
113 CacheStorageManager::Create(user_data_directory
, cache_task_runner
.get(),
114 make_scoped_refptr(quota_manager_proxy
));
117 void CacheStorageContextImpl::ShutdownOnIO() {
118 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
120 cache_manager_
.reset();
123 } // namespace content