Chromium Code Reviews| 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) |
|
danakj
2016/10/03 22:51:49
This is slightly different but it looks like we ne
Avi (use Gerrit)
2016/10/04 15:56:22
The old code is weird. It does an insert with a nu
danakj
2016/10/04 19:55:52
I just think it would be helpful when reading this
Avi (use Gerrit)
2016/10/04 22:06:07
Done.
| |
| 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 entry = base::MakeUnique<base::File>(file_path, base::File::FLAG_OPEN_ALWAYS | |
| 238 base::File::FLAG_OPEN_ALWAYS | | 237 base::File::FLAG_READ | |
| 239 base::File::FLAG_READ | | 238 base::File::FLAG_WRITE); |
| 240 base::File::FLAG_WRITE); | 239 |
| 241 if (!new_file->IsValid()) { | 240 if (!entry->IsValid()) { |
| 242 cache_files_.erase(inserted.first); | 241 cache_files_.erase(file_path); |
| 243 delete new_file; | 242 return nullptr; |
| 244 return NULL; | |
| 245 } | 243 } |
| 246 | 244 |
| 247 inserted.first->second = new_file; | 245 return entry.get(); |
| 248 return new_file; | |
| 249 } | 246 } |
| 250 | 247 |
| 251 bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path, | 248 bool FileSystemUsageCache::ReadBytes(const base::FilePath& file_path, |
| 252 char* buffer, | 249 char* buffer, |
| 253 int64_t buffer_size) { | 250 int64_t buffer_size) { |
| 254 DCHECK(CalledOnValidThread()); | 251 DCHECK(CalledOnValidThread()); |
| 255 base::File* file = GetFile(file_path); | 252 base::File* file = GetFile(file_path); |
| 256 if (!file) | 253 if (!file) |
| 257 return false; | 254 return false; |
| 258 return file->Read(0, buffer, buffer_size) == buffer_size; | 255 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(); | 294 return !task_runner_.get() || task_runner_->RunsTasksOnCurrentThread(); |
| 298 } | 295 } |
| 299 | 296 |
| 300 bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { | 297 bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { |
| 301 DCHECK(CalledOnValidThread()); | 298 DCHECK(CalledOnValidThread()); |
| 302 DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); | 299 DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); |
| 303 return base::ContainsKey(cache_files_, file_path); | 300 return base::ContainsKey(cache_files_, file_path); |
| 304 } | 301 } |
| 305 | 302 |
| 306 } // namespace storage | 303 } // namespace storage |
| OLD | NEW |