| 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" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/disk_cache/simple/simple_entry_impl.h" | 14 #include "net/disk_cache/simple/simple_entry_impl.h" |
| 15 #include "net/disk_cache/simple/simple_index.h" | 15 #include "net/disk_cache/simple/simple_index.h" |
| 16 | 16 |
| 17 using base::FilePath; | 17 using base::FilePath; |
| 18 using base::MessageLoopProxy; | 18 using base::MessageLoopProxy; |
| 19 using base::Time; | 19 using base::Time; |
| 20 using base::WorkerPool; | 20 using base::WorkerPool; |
| 21 using file_util::DirectoryExists; | 21 using file_util::DirectoryExists; |
| 22 using file_util::CreateDirectory; | 22 using file_util::CreateDirectory; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const char* kSimpleBackendSubdirectory = "Simple"; | |
| 27 | |
| 28 // Must run on IO Thread. | 26 // Must run on IO Thread. |
| 29 void DeleteBackendImpl(disk_cache::Backend** backend, | 27 void DeleteBackendImpl(disk_cache::Backend** backend, |
| 30 const net::CompletionCallback& callback, | 28 const net::CompletionCallback& callback, |
| 31 int result) { | 29 int result) { |
| 32 DCHECK(*backend); | 30 DCHECK(*backend); |
| 33 delete *backend; | 31 delete *backend; |
| 34 *backend = NULL; | 32 *backend = NULL; |
| 35 callback.Run(result); | 33 callback.Run(result); |
| 36 } | 34 } |
| 37 | 35 |
| 38 } // namespace | 36 } // namespace |
| 39 | 37 |
| 40 namespace disk_cache { | 38 namespace disk_cache { |
| 41 | 39 |
| 42 // static | 40 SimpleBackendImpl::SimpleBackendImpl( |
| 43 int SimpleBackendImpl::CreateBackend( | 41 const FilePath& path, |
| 44 const FilePath& full_path, | |
| 45 int max_bytes, | 42 int max_bytes, |
| 46 net::CacheType type, | 43 net::CacheType type, |
| 47 uint32 flags, | 44 const scoped_refptr<base::TaskRunner>& cache_thread, |
| 48 scoped_refptr<base::TaskRunner> cache_thread, | 45 net::NetLog* net_log) |
| 49 net::NetLog* net_log, | 46 : path_(path), |
| 50 Backend** backend, | 47 cache_thread_(cache_thread) { |
| 51 const CompletionCallback& callback) { | 48 index_.reset(new SimpleIndex(cache_thread, path)); |
| 52 // TODO(gavinp): Use the |net_log|. | 49 } |
| 53 DCHECK_EQ(net::DISK_CACHE, type); | |
| 54 FilePath simple_cache_path = | |
| 55 full_path.AppendASCII(kSimpleBackendSubdirectory); | |
| 56 | 50 |
| 57 // In order to not leak when the EnsureCachePathExists fails, we need to | 51 int SimpleBackendImpl::Init(const CompletionCallback& callback) { |
| 58 // delete this in DeleteBackendImpl on the IO Thread. | 52 cache_thread_->PostTask(FROM_HERE, |
| 59 *backend = new SimpleBackendImpl(cache_thread, simple_cache_path); | 53 base::Bind(&SimpleBackendImpl::InitializeIndex, |
| 60 | 54 base::Unretained(this), |
| 61 cache_thread->PostTask(FROM_HERE, | 55 MessageLoopProxy::current(), |
| 62 base::Bind(&SimpleBackendImpl::EnsureCachePathExists, | 56 callback)); |
| 63 simple_cache_path, | |
| 64 cache_thread, | |
| 65 MessageLoopProxy::current(), | |
| 66 callback, | |
| 67 backend)); | |
| 68 return net::ERR_IO_PENDING; | 57 return net::ERR_IO_PENDING; |
| 69 } | 58 } |
| 70 | 59 |
| 71 SimpleBackendImpl::~SimpleBackendImpl() { | 60 SimpleBackendImpl::~SimpleBackendImpl() { |
| 72 index_->Cleanup(); | 61 index_->Cleanup(); |
| 73 } | 62 } |
| 74 | 63 |
| 75 net::CacheType SimpleBackendImpl::GetCacheType() const { | 64 net::CacheType SimpleBackendImpl::GetCacheType() const { |
| 76 return net::DISK_CACHE; | 65 return net::DISK_CACHE; |
| 77 } | 66 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 122 |
| 134 void SimpleBackendImpl::GetStats( | 123 void SimpleBackendImpl::GetStats( |
| 135 std::vector<std::pair<std::string, std::string> >* stats) { | 124 std::vector<std::pair<std::string, std::string> >* stats) { |
| 136 NOTIMPLEMENTED(); | 125 NOTIMPLEMENTED(); |
| 137 } | 126 } |
| 138 | 127 |
| 139 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { | 128 void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { |
| 140 NOTIMPLEMENTED(); | 129 NOTIMPLEMENTED(); |
| 141 } | 130 } |
| 142 | 131 |
| 143 SimpleBackendImpl::SimpleBackendImpl( | 132 void SimpleBackendImpl::InitializeIndex(MessageLoopProxy* io_thread, |
| 144 const scoped_refptr<base::TaskRunner>& cache_thread, | 133 const CompletionCallback& callback) { |
| 145 const FilePath& path) : path_(path) { | 134 int rv = net::OK; |
| 146 index_.reset(new SimpleIndex(cache_thread, path)); | 135 if (!file_util::PathExists(path_) && !file_util::CreateDirectory(path_)) { |
| 147 } | 136 LOG(ERROR) << "Simple Cache Backend: failed to create: " << path_.value(); |
| 148 | 137 rv = net::ERR_FAILED; |
| 149 void SimpleBackendImpl::Initialize() { | 138 } else |
| 150 index_->Initialize(); | 139 rv = index_->Initialize() ? net::OK : net::ERR_FAILED; |
| 151 } | 140 io_thread->PostTask(FROM_HERE, base::Bind(callback, rv)); |
| 152 | |
| 153 // static | |
| 154 void SimpleBackendImpl::EnsureCachePathExists( | |
| 155 const FilePath& path, | |
| 156 const scoped_refptr<base::TaskRunner>& cache_thread, | |
| 157 const scoped_refptr<base::TaskRunner>& io_thread, | |
| 158 const CompletionCallback& callback, | |
| 159 Backend** backend) { | |
| 160 int result = net::OK; | |
| 161 if (!DirectoryExists(path) && !CreateDirectory(path)) | |
| 162 result = net::ERR_FAILED; | |
| 163 | |
| 164 if (result == net::OK) { | |
| 165 DCHECK(*backend); | |
| 166 // TODO(pasko): Move the object creation and initalization out of | |
| 167 // CreateBackend and fix this downcast. | |
| 168 static_cast<SimpleBackendImpl*>(*backend)->Initialize(); | |
| 169 io_thread->PostTask(FROM_HERE, | |
| 170 base::Bind(callback, result)); | |
| 171 } else { | |
| 172 io_thread->PostTask(FROM_HERE, | |
| 173 base::Bind( | |
| 174 &DeleteBackendImpl, | |
| 175 backend, callback, result)); | |
| 176 } | |
| 177 } | 141 } |
| 178 | 142 |
| 179 } // namespace disk_cache | 143 } // namespace disk_cache |
| OLD | NEW |