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/service_worker/cache_storage_context_impl.h"
8 #include "base/files/file_path.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "content/browser/fileapi/chrome_blob_storage_context.h"
11 #include "content/browser/service_worker/service_worker_cache_storage_manager.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 ServiceWorkerCacheStorageManager
* CacheStorageContextImpl::cache_manager()
70 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
71 return cache_manager_
.get();
74 void CacheStorageContextImpl::SetBlobParametersForCache(
75 net::URLRequestContextGetter
* request_context
,
76 ChromeBlobStorageContext
* blob_storage_context
) {
77 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
79 if (cache_manager_
&& request_context
&& blob_storage_context
) {
80 cache_manager_
->SetBlobParametersForCache(
81 request_context
->GetURLRequestContext(),
82 blob_storage_context
->context()->AsWeakPtr());
86 void CacheStorageContextImpl::CreateCacheStorageManager(
87 const base::FilePath
& user_data_directory
,
88 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
89 storage::QuotaManagerProxy
* quota_manager_proxy
,
90 storage::SpecialStoragePolicy
* special_storage_policy
) {
91 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
93 DCHECK(!cache_manager_
);
94 cache_manager_
= ServiceWorkerCacheStorageManager::Create(
95 user_data_directory
, cache_task_runner
.get(),
96 make_scoped_refptr(quota_manager_proxy
));
99 void CacheStorageContextImpl::ShutdownOnIO() {
100 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
102 cache_manager_
.reset();
105 } // namespace content