Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_context_impl.cc
blob7e43070909d30921ac685c5504bd87d1ec883895
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"
7 #include "base/bind.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"
19 namespace content {
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);
49 return;
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,
75 ChromeBlobStorageContext* blob_storage_context) {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO);
78 if (cache_manager_ && request_context && blob_storage_context) {
79 cache_manager_->SetBlobParametersForCache(
80 request_context->GetURLRequestContext(),
81 blob_storage_context->context()->AsWeakPtr());
85 void CacheStorageContextImpl::CreateCacheStorageManager(
86 const base::FilePath& user_data_directory,
87 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner,
88 storage::QuotaManagerProxy* quota_manager_proxy,
89 storage::SpecialStoragePolicy* special_storage_policy) {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO);
92 DCHECK(!cache_manager_);
93 cache_manager_ =
94 CacheStorageManager::Create(user_data_directory, cache_task_runner.get(),
95 make_scoped_refptr(quota_manager_proxy));
98 void CacheStorageContextImpl::ShutdownOnIO() {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO);
101 cache_manager_.reset();
104 } // namespace content