OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_backend_impl.h" | 5 #include "net/disk_cache/simple/simple_backend_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 namespace disk_cache { | 38 namespace disk_cache { |
39 | 39 |
40 SimpleBackendImpl::SimpleBackendImpl( | 40 SimpleBackendImpl::SimpleBackendImpl( |
41 const FilePath& path, | 41 const FilePath& path, |
42 int max_bytes, | 42 int max_bytes, |
43 net::CacheType type, | 43 net::CacheType type, |
44 const scoped_refptr<base::TaskRunner>& cache_thread, | 44 const scoped_refptr<base::TaskRunner>& cache_thread, |
45 net::NetLog* net_log) | 45 net::NetLog* net_log) |
46 : path_(path), | 46 : path_(path), |
47 cache_thread_(cache_thread) { | 47 index_(new SimpleIndex(cache_thread, |
48 index_.reset(new SimpleIndex(cache_thread, path)); | 48 MessageLoopProxy::current(), // io_thread |
| 49 path)), |
| 50 cache_thread_(cache_thread) {} |
| 51 |
| 52 SimpleBackendImpl::~SimpleBackendImpl() { |
| 53 index_->WriteToDisk(); |
49 } | 54 } |
50 | 55 |
51 int SimpleBackendImpl::Init(const CompletionCallback& callback) { | 56 int SimpleBackendImpl::Init(const CompletionCallback& completion_callback) { |
| 57 InitializeIndexCallback initialize_index_callback = |
| 58 base::Bind(&SimpleBackendImpl::InitializeIndex, |
| 59 base::Unretained(this), |
| 60 completion_callback); |
52 cache_thread_->PostTask(FROM_HERE, | 61 cache_thread_->PostTask(FROM_HERE, |
53 base::Bind(&SimpleBackendImpl::InitializeIndex, | 62 base::Bind(&SimpleBackendImpl::CreateDirectory, |
54 base::Unretained(this), | 63 MessageLoopProxy::current(), // io_thread |
55 MessageLoopProxy::current(), | 64 path_, |
56 callback)); | 65 initialize_index_callback)); |
57 return net::ERR_IO_PENDING; | 66 return net::ERR_IO_PENDING; |
58 } | 67 } |
59 | 68 |
60 SimpleBackendImpl::~SimpleBackendImpl() { | |
61 index_->Cleanup(); | |
62 } | |
63 | |
64 net::CacheType SimpleBackendImpl::GetCacheType() const { | 69 net::CacheType SimpleBackendImpl::GetCacheType() const { |
65 return net::DISK_CACHE; | 70 return net::DISK_CACHE; |
66 } | 71 } |
67 | 72 |
68 int32 SimpleBackendImpl::GetEntryCount() const { | 73 int32 SimpleBackendImpl::GetEntryCount() const { |
69 NOTIMPLEMENTED(); | 74 NOTIMPLEMENTED(); |
70 return 0; | 75 return 0; |
71 } | 76 } |
72 | 77 |
73 int SimpleBackendImpl::OpenEntry(const std::string& key, | 78 int SimpleBackendImpl::OpenEntry(const std::string& key, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 127 |
123 void SimpleBackendImpl::GetStats( | 128 void SimpleBackendImpl::GetStats( |
124 std::vector<std::pair<std::string, std::string> >* stats) { | 129 std::vector<std::pair<std::string, std::string> >* stats) { |
125 NOTIMPLEMENTED(); | 130 NOTIMPLEMENTED(); |
126 } | 131 } |
127 | 132 |
128 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { | 133 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { |
129 NOTIMPLEMENTED(); | 134 NOTIMPLEMENTED(); |
130 } | 135 } |
131 | 136 |
132 void SimpleBackendImpl::InitializeIndex(MessageLoopProxy* io_thread, | 137 void SimpleBackendImpl::InitializeIndex( |
133 const CompletionCallback& callback) { | 138 const CompletionCallback& callback, int result) { |
| 139 if (result == net::OK) |
| 140 index_->Initialize(); |
| 141 callback.Run(result); |
| 142 } |
| 143 |
| 144 // static |
| 145 void SimpleBackendImpl::CreateDirectory( |
| 146 MessageLoopProxy* io_thread, |
| 147 const base::FilePath& path, |
| 148 const InitializeIndexCallback& initialize_index_callback) { |
134 int rv = net::OK; | 149 int rv = net::OK; |
135 if (!file_util::PathExists(path_) && !file_util::CreateDirectory(path_)) { | 150 if (!file_util::PathExists(path) && !file_util::CreateDirectory(path)) { |
136 LOG(ERROR) << "Simple Cache Backend: failed to create: " << path_.value(); | 151 LOG(ERROR) << "Simple Cache Backend: failed to create: " << path.value(); |
137 rv = net::ERR_FAILED; | 152 rv = net::ERR_FAILED; |
138 } else | 153 } |
139 rv = index_->Initialize() ? net::OK : net::ERR_FAILED; | 154 |
140 io_thread->PostTask(FROM_HERE, base::Bind(callback, rv)); | 155 io_thread->PostTask(FROM_HERE, base::Bind(initialize_index_callback, rv)); |
141 } | 156 } |
142 | 157 |
143 } // namespace disk_cache | 158 } // namespace disk_cache |
OLD | NEW |