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 "storage/browser/fileapi/file_system_usage_cache.h" | 5 #include "storage/browser/fileapi/file_system_usage_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/pickle.h" | 15 #include "base/pickle.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
17 #include "storage/browser/fileapi/timed_task_helper.h" | 18 #include "storage/browser/fileapi/timed_task_helper.h" |
18 | 19 |
19 namespace storage { | 20 namespace storage { |
20 | 21 |
21 namespace { | 22 namespace { |
22 const int64_t kCloseDelaySeconds = 5; | 23 const int64_t kCloseDelaySeconds = 5; |
23 const size_t kMaxHandleCacheSize = 2; | 24 const size_t kMaxHandleCacheSize = 2; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 bool FileSystemUsageCache::Delete(const base::FilePath& usage_file_path) { | 155 bool FileSystemUsageCache::Delete(const base::FilePath& usage_file_path) { |
155 TRACE_EVENT0("FileSystem", "UsageCache::Delete"); | 156 TRACE_EVENT0("FileSystem", "UsageCache::Delete"); |
156 DCHECK(CalledOnValidThread()); | 157 DCHECK(CalledOnValidThread()); |
157 CloseCacheFiles(); | 158 CloseCacheFiles(); |
158 return base::DeleteFile(usage_file_path, false); | 159 return base::DeleteFile(usage_file_path, false); |
159 } | 160 } |
160 | 161 |
161 void FileSystemUsageCache::CloseCacheFiles() { | 162 void FileSystemUsageCache::CloseCacheFiles() { |
162 TRACE_EVENT0("FileSystem", "UsageCache::CloseCacheFiles"); | 163 TRACE_EVENT0("FileSystem", "UsageCache::CloseCacheFiles"); |
163 DCHECK(CalledOnValidThread()); | 164 DCHECK(CalledOnValidThread()); |
164 base::STLDeleteValues(&cache_files_); | 165 cache_files_.clear(); |
165 timer_.reset(); | 166 timer_.reset(); |
166 } | 167 } |
167 | 168 |
168 bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path, | 169 bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path, |
169 bool* is_valid, | 170 bool* is_valid, |
170 uint32_t* dirty_out, | 171 uint32_t* dirty_out, |
171 int64_t* usage_out) { | 172 int64_t* usage_out) { |
172 TRACE_EVENT0("FileSystem", "UsageCache::Read"); | 173 TRACE_EVENT0("FileSystem", "UsageCache::Read"); |
173 DCHECK(CalledOnValidThread()); | 174 DCHECK(CalledOnValidThread()); |
174 DCHECK(is_valid); | 175 DCHECK(is_valid); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 } | 222 } |
222 return true; | 223 return true; |
223 } | 224 } |
224 | 225 |
225 base::File* FileSystemUsageCache::GetFile(const base::FilePath& file_path) { | 226 base::File* FileSystemUsageCache::GetFile(const base::FilePath& file_path) { |
226 DCHECK(CalledOnValidThread()); | 227 DCHECK(CalledOnValidThread()); |
227 if (cache_files_.size() >= kMaxHandleCacheSize) | 228 if (cache_files_.size() >= kMaxHandleCacheSize) |
228 CloseCacheFiles(); | 229 CloseCacheFiles(); |
229 ScheduleCloseTimer(); | 230 ScheduleCloseTimer(); |
230 | 231 |
231 base::File* new_file = NULL; | 232 auto& entry = cache_files_[file_path]; |
232 std::pair<CacheFiles::iterator, bool> inserted = | 233 if (entry) |
233 cache_files_.insert(std::make_pair(file_path, new_file)); | 234 return entry.get(); |
234 if (!inserted.second) | |
235 return inserted.first->second; | |
236 | 235 |
237 new_file = new base::File(file_path, | 236 // Because there are no null entries in cache_files_, the [] inserted a blank |
238 base::File::FLAG_OPEN_ALWAYS | | 237 // pointer, so let's populate the cache. |
239 base::File::FLAG_READ | | 238 entry = base::MakeUnique<base::File>(file_path, base::File::FLAG_OPEN_ALWAYS | |
240 base::File::FLAG_WRITE); | 239 base::File::FLAG_READ | |
241 if (!new_file->IsValid()) { | 240 base::File::FLAG_WRITE); |
242 cache_files_.erase(inserted.first); | 241 |
243 delete new_file; | 242 if (!entry->IsValid()) { |
244 return NULL; | 243 cache_files_.erase(file_path); |
| 244 return nullptr; |
245 } | 245 } |
246 | 246 |
247 inserted.first->second = new_file; | 247 return entry.get(); |
248 return new_file; | |
249 } | 248 } |
250 | 249 |
251 bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path, | 250 bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path, |
252 char* buffer, | 251 char* buffer, |
253 int64_t buffer_size) { | 252 int64_t buffer_size) { |
254 DCHECK(CalledOnValidThread()); | 253 DCHECK(CalledOnValidThread()); |
255 base::File* file = GetFile(file_path); | 254 base::File* file = GetFile(file_path); |
256 if (!file) | 255 if (!file) |
257 return false; | 256 return false; |
258 return file->Read(0, buffer, buffer_size) == buffer_size; | 257 return file->Read(0, buffer, buffer_size) == buffer_size; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 return !task_runner_.get() || task_runner_->RunsTasksOnCurrentThread(); | 296 return !task_runner_.get() || task_runner_->RunsTasksOnCurrentThread(); |
298 } | 297 } |
299 | 298 |
300 bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { | 299 bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { |
301 DCHECK(CalledOnValidThread()); | 300 DCHECK(CalledOnValidThread()); |
302 DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); | 301 DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); |
303 return base::ContainsKey(cache_files_, file_path); | 302 return base::ContainsKey(cache_files_, file_path); |
304 } | 303 } |
305 | 304 |
306 } // namespace storage | 305 } // namespace storage |
OLD | NEW |