OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/metrics/field_trial.h" | 6 #include "base/metrics/field_trial.h" |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/disk_cache/backend_impl.h" | 9 #include "net/disk_cache/backend_impl.h" |
10 #include "net/disk_cache/cache_util.h" | 10 #include "net/disk_cache/cache_util.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 bool force_; | 38 bool force_; |
39 bool retry_; | 39 bool retry_; |
40 int max_bytes_; | 40 int max_bytes_; |
41 net::CacheType type_; | 41 net::CacheType type_; |
42 uint32 flags_; | 42 uint32 flags_; |
43 scoped_refptr<base::MessageLoopProxy> thread_; | 43 scoped_refptr<base::MessageLoopProxy> thread_; |
44 disk_cache::Backend** backend_; | 44 disk_cache::Backend** backend_; |
45 net::CompletionCallback callback_; | 45 net::CompletionCallback callback_; |
46 disk_cache::Backend* created_cache_; | 46 disk_cache::Backend* created_cache_; |
47 net::NetLog* net_log_; | 47 net::NetLog* net_log_; |
48 bool use_simple_cache_backend_; | |
49 | 48 |
50 DISALLOW_COPY_AND_ASSIGN(CacheCreator); | 49 DISALLOW_COPY_AND_ASSIGN(CacheCreator); |
51 }; | 50 }; |
52 | 51 |
53 CacheCreator::CacheCreator( | 52 CacheCreator::CacheCreator( |
54 const base::FilePath& path, bool force, int max_bytes, | 53 const base::FilePath& path, bool force, int max_bytes, |
55 net::CacheType type, uint32 flags, | 54 net::CacheType type, uint32 flags, |
56 base::MessageLoopProxy* thread, net::NetLog* net_log, | 55 base::MessageLoopProxy* thread, net::NetLog* net_log, |
57 disk_cache::Backend** backend, | 56 disk_cache::Backend** backend, |
58 const net::CompletionCallback& callback) | 57 const net::CompletionCallback& callback) |
59 : path_(path), | 58 : path_(path), |
60 force_(force), | 59 force_(force), |
61 retry_(false), | 60 retry_(false), |
62 max_bytes_(max_bytes), | 61 max_bytes_(max_bytes), |
63 type_(type), | 62 type_(type), |
64 flags_(flags), | 63 flags_(flags), |
65 thread_(thread), | 64 thread_(thread), |
66 backend_(backend), | 65 backend_(backend), |
67 callback_(callback), | 66 callback_(callback), |
68 created_cache_(NULL), | 67 created_cache_(NULL), |
69 net_log_(net_log), | 68 net_log_(net_log) { |
70 use_simple_cache_backend_(false) { | |
71 } | 69 } |
72 | 70 |
73 CacheCreator::~CacheCreator() { | 71 CacheCreator::~CacheCreator() { |
74 } | 72 } |
75 | 73 |
76 int CacheCreator::Run() { | 74 int CacheCreator::Run() { |
77 // TODO(pasko): The two caches should never coexist on disk. Each individual | 75 // TODO(pasko): The two caches should never coexist on disk. Each individual |
78 // cache backend should fail to initialize if it observes the index that does | 76 // cache backend should fail to initialize if it observes the index that does |
79 // not belong to it. | 77 // not belong to it. |
80 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") == "Yes") { | 78 if (base::FieldTrialList::FindFullName("SimpleCacheTrial") == "Yes") { |
81 // TODO(gavinp,pasko): While simple backend development proceeds, we're only | 79 // TODO(gavinp,pasko): While simple backend development proceeds, we're only |
82 // testing it against net::DISK_CACHE. Turn it on for more cache types as | 80 // testing it against net::DISK_CACHE. Turn it on for more cache types as |
83 // appropriate. | 81 // appropriate. |
84 if (type_ == net::DISK_CACHE) { | 82 if (type_ == net::DISK_CACHE) { |
85 VLOG(1) << "Using the Simple Cache Backend."; | 83 disk_cache::SimpleBackendImpl* simple_cache = |
86 use_simple_cache_backend_ = true; | 84 new disk_cache::SimpleBackendImpl(path_, max_bytes_, type_, thread_, |
87 return disk_cache::SimpleBackendImpl::CreateBackend( | 85 net_log_); |
88 path_, max_bytes_, type_, disk_cache::kNone, thread_, net_log_, | 86 created_cache_ = simple_cache; |
89 backend_, base::Bind(&CacheCreator::OnIOComplete, | 87 return simple_cache->Init( |
90 base::Unretained(this))); | 88 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); |
91 } | 89 } |
92 } | 90 } |
93 disk_cache::BackendImpl* new_cache = | 91 disk_cache::BackendImpl* new_cache = |
94 new disk_cache::BackendImpl(path_, thread_, net_log_); | 92 new disk_cache::BackendImpl(path_, thread_, net_log_); |
95 created_cache_ = new_cache; | 93 created_cache_ = new_cache; |
96 new_cache->SetMaxSize(max_bytes_); | 94 new_cache->SetMaxSize(max_bytes_); |
97 new_cache->SetType(type_); | 95 new_cache->SetType(type_); |
98 new_cache->SetFlags(flags_); | 96 new_cache->SetFlags(flags_); |
99 int rv = new_cache->Init( | 97 int rv = new_cache->Init( |
100 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); | 98 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); |
101 DCHECK_EQ(net::ERR_IO_PENDING, rv); | 99 DCHECK_EQ(net::ERR_IO_PENDING, rv); |
102 return rv; | 100 return rv; |
103 } | 101 } |
104 | 102 |
105 void CacheCreator::DoCallback(int result) { | 103 void CacheCreator::DoCallback(int result) { |
106 DCHECK_NE(net::ERR_IO_PENDING, result); | 104 DCHECK_NE(net::ERR_IO_PENDING, result); |
107 if (result == net::OK) { | 105 if (result == net::OK) |
108 // TODO(pasko): Separate creation of the Simple Backend from its | 106 *backend_ = created_cache_; |
109 // initialization, eliminate unnecessary use_simple_cache_backend_. | 107 else { |
110 if (use_simple_cache_backend_) | |
111 created_cache_ = *backend_; | |
112 else | |
113 *backend_ = created_cache_; | |
114 } else { | |
115 LOG(ERROR) << "Unable to create cache"; | 108 LOG(ERROR) << "Unable to create cache"; |
116 *backend_ = NULL; | 109 *backend_ = NULL; |
117 delete created_cache_; | 110 delete created_cache_; |
118 } | 111 } |
119 callback_.Run(result); | 112 callback_.Run(result); |
120 delete this; | 113 delete this; |
121 } | 114 } |
122 | 115 |
123 // If the initialization of the cache fails, and |force| is true, we will | 116 // If the initialization of the cache fails, and |force| is true, we will |
124 // discard the whole cache and create a new one. | 117 // discard the whole cache and create a new one. |
(...skipping 28 matching lines...) Expand all Loading... |
153 if (type == net::MEMORY_CACHE) { | 146 if (type == net::MEMORY_CACHE) { |
154 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log); | 147 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log); |
155 return *backend ? net::OK : net::ERR_FAILED; | 148 return *backend ? net::OK : net::ERR_FAILED; |
156 } | 149 } |
157 DCHECK(thread); | 150 DCHECK(thread); |
158 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, kNone, | 151 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, kNone, |
159 thread, net_log, backend, callback); | 152 thread, net_log, backend, callback); |
160 return creator->Run(); | 153 return creator->Run(); |
161 } | 154 } |
162 | 155 |
163 | |
164 } // namespace disk_cache | 156 } // namespace disk_cache |
OLD | NEW |