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 "webkit/browser/fileapi/file_system_usage_cache.h" | 5 #include "storage/browser/fileapi/file_system_usage_cache.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "webkit/browser/fileapi/timed_task_helper.h" | 15 #include "storage/browser/fileapi/timed_task_helper.h" |
16 | 16 |
17 namespace fileapi { | 17 namespace storage { |
18 | 18 |
19 namespace { | 19 namespace { |
20 const int64 kCloseDelaySeconds = 5; | 20 const int64 kCloseDelaySeconds = 5; |
21 const size_t kMaxHandleCacheSize = 2; | 21 const size_t kMaxHandleCacheSize = 2; |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 FileSystemUsageCache::FileSystemUsageCache( | 24 FileSystemUsageCache::FileSystemUsageCache( |
25 base::SequencedTaskRunner* task_runner) | 25 base::SequencedTaskRunner* task_runner) |
26 : task_runner_(task_runner), | 26 : task_runner_(task_runner), weak_factory_(this) { |
27 weak_factory_(this) { | |
28 } | 27 } |
29 | 28 |
30 FileSystemUsageCache::~FileSystemUsageCache() { | 29 FileSystemUsageCache::~FileSystemUsageCache() { |
31 task_runner_ = NULL; | 30 task_runner_ = NULL; |
32 CloseCacheFiles(); | 31 CloseCacheFiles(); |
33 } | 32 } |
34 | 33 |
35 const base::FilePath::CharType FileSystemUsageCache::kUsageFileName[] = | 34 const base::FilePath::CharType FileSystemUsageCache::kUsageFileName[] = |
36 FILE_PATH_LITERAL(".usage"); | 35 FILE_PATH_LITERAL(".usage"); |
37 const char FileSystemUsageCache::kUsageFileHeader[] = "FSU5"; | 36 const char FileSystemUsageCache::kUsageFileHeader[] = "FSU5"; |
38 const int FileSystemUsageCache::kUsageFileHeaderSize = 4; | 37 const int FileSystemUsageCache::kUsageFileHeaderSize = 4; |
39 | 38 |
40 // Pickle::{Read,Write}Bool treat bool as int | 39 // Pickle::{Read,Write}Bool treat bool as int |
41 const int FileSystemUsageCache::kUsageFileSize = | 40 const int FileSystemUsageCache::kUsageFileSize = |
42 sizeof(Pickle::Header) + | 41 sizeof(Pickle::Header) + FileSystemUsageCache::kUsageFileHeaderSize + |
43 FileSystemUsageCache::kUsageFileHeaderSize + | |
44 sizeof(int) + sizeof(int32) + sizeof(int64); // NOLINT | 42 sizeof(int) + sizeof(int32) + sizeof(int64); // NOLINT |
45 | 43 |
46 bool FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path, | 44 bool FileSystemUsageCache::GetUsage(const base::FilePath& usage_file_path, |
47 int64* usage_out) { | 45 int64* usage_out) { |
48 TRACE_EVENT0("FileSystem", "UsageCache::GetUsage"); | 46 TRACE_EVENT0("FileSystem", "UsageCache::GetUsage"); |
49 DCHECK(CalledOnValidThread()); | 47 DCHECK(CalledOnValidThread()); |
50 DCHECK(usage_out); | 48 DCHECK(usage_out); |
51 bool is_valid = true; | 49 bool is_valid = true; |
52 uint32 dirty = 0; | 50 uint32 dirty = 0; |
53 int64 usage = 0; | 51 int64 usage = 0; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 DCHECK(CalledOnValidThread()); | 119 DCHECK(CalledOnValidThread()); |
122 bool is_valid = true; | 120 bool is_valid = true; |
123 uint32 dirty = 0; | 121 uint32 dirty = 0; |
124 int64 usage = 0; | 122 int64 usage = 0; |
125 if (!Read(usage_file_path, &is_valid, &dirty, &usage)) | 123 if (!Read(usage_file_path, &is_valid, &dirty, &usage)) |
126 return false; | 124 return false; |
127 return is_valid; | 125 return is_valid; |
128 } | 126 } |
129 | 127 |
130 bool FileSystemUsageCache::AtomicUpdateUsageByDelta( | 128 bool FileSystemUsageCache::AtomicUpdateUsageByDelta( |
131 const base::FilePath& usage_file_path, int64 delta) { | 129 const base::FilePath& usage_file_path, |
| 130 int64 delta) { |
132 TRACE_EVENT0("FileSystem", "UsageCache::AtomicUpdateUsageByDelta"); | 131 TRACE_EVENT0("FileSystem", "UsageCache::AtomicUpdateUsageByDelta"); |
133 DCHECK(CalledOnValidThread()); | 132 DCHECK(CalledOnValidThread()); |
134 bool is_valid = true; | 133 bool is_valid = true; |
135 uint32 dirty = 0; | 134 uint32 dirty = 0; |
136 int64 usage = 0;; | 135 int64 usage = 0; |
| 136 ; |
137 if (!Read(usage_file_path, &is_valid, &dirty, &usage)) | 137 if (!Read(usage_file_path, &is_valid, &dirty, &usage)) |
138 return false; | 138 return false; |
139 return Write(usage_file_path, is_valid, dirty, usage + delta); | 139 return Write(usage_file_path, is_valid, dirty, usage + delta); |
140 } | 140 } |
141 | 141 |
142 bool FileSystemUsageCache::UpdateUsage(const base::FilePath& usage_file_path, | 142 bool FileSystemUsageCache::UpdateUsage(const base::FilePath& usage_file_path, |
143 int64 fs_usage) { | 143 int64 fs_usage) { |
144 TRACE_EVENT0("FileSystem", "UsageCache::UpdateUsage"); | 144 TRACE_EVENT0("FileSystem", "UsageCache::UpdateUsage"); |
145 DCHECK(CalledOnValidThread()); | 145 DCHECK(CalledOnValidThread()); |
146 return Write(usage_file_path, true, 0, fs_usage); | 146 return Write(usage_file_path, true, 0, fs_usage); |
(...skipping 13 matching lines...) Expand all Loading... |
160 } | 160 } |
161 | 161 |
162 void FileSystemUsageCache::CloseCacheFiles() { | 162 void FileSystemUsageCache::CloseCacheFiles() { |
163 TRACE_EVENT0("FileSystem", "UsageCache::CloseCacheFiles"); | 163 TRACE_EVENT0("FileSystem", "UsageCache::CloseCacheFiles"); |
164 DCHECK(CalledOnValidThread()); | 164 DCHECK(CalledOnValidThread()); |
165 STLDeleteValues(&cache_files_); | 165 STLDeleteValues(&cache_files_); |
166 timer_.reset(); | 166 timer_.reset(); |
167 } | 167 } |
168 | 168 |
169 bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path, | 169 bool FileSystemUsageCache::Read(const base::FilePath& usage_file_path, |
170 bool* is_valid, | 170 bool* is_valid, |
171 uint32* dirty_out, | 171 uint32* dirty_out, |
172 int64* usage_out) { | 172 int64* usage_out) { |
173 TRACE_EVENT0("FileSystem", "UsageCache::Read"); | 173 TRACE_EVENT0("FileSystem", "UsageCache::Read"); |
174 DCHECK(CalledOnValidThread()); | 174 DCHECK(CalledOnValidThread()); |
175 DCHECK(is_valid); | 175 DCHECK(is_valid); |
176 DCHECK(dirty_out); | 176 DCHECK(dirty_out); |
177 DCHECK(usage_out); | 177 DCHECK(usage_out); |
178 char buffer[kUsageFileSize]; | 178 char buffer[kUsageFileSize]; |
179 const char *header; | 179 const char* header; |
180 if (usage_file_path.empty() || | 180 if (usage_file_path.empty() || |
181 !ReadBytes(usage_file_path, buffer, kUsageFileSize)) | 181 !ReadBytes(usage_file_path, buffer, kUsageFileSize)) |
182 return false; | 182 return false; |
183 Pickle read_pickle(buffer, kUsageFileSize); | 183 Pickle read_pickle(buffer, kUsageFileSize); |
184 PickleIterator iter(read_pickle); | 184 PickleIterator iter(read_pickle); |
185 uint32 dirty = 0; | 185 uint32 dirty = 0; |
186 int64 usage = 0; | 186 int64 usage = 0; |
187 | 187 |
188 if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) || | 188 if (!read_pickle.ReadBytes(&iter, &header, kUsageFileHeaderSize) || |
189 !read_pickle.ReadBool(&iter, is_valid) || | 189 !read_pickle.ReadBool(&iter, is_valid) || |
190 !read_pickle.ReadUInt32(&iter, &dirty) || | 190 !read_pickle.ReadUInt32(&iter, &dirty) || |
191 !read_pickle.ReadInt64(&iter, &usage)) | 191 !read_pickle.ReadInt64(&iter, &usage)) |
192 return false; | 192 return false; |
193 | 193 |
194 if (header[0] != kUsageFileHeader[0] || | 194 if (header[0] != kUsageFileHeader[0] || header[1] != kUsageFileHeader[1] || |
195 header[1] != kUsageFileHeader[1] || | 195 header[2] != kUsageFileHeader[2] || header[3] != kUsageFileHeader[3]) |
196 header[2] != kUsageFileHeader[2] || | |
197 header[3] != kUsageFileHeader[3]) | |
198 return false; | 196 return false; |
199 | 197 |
200 *dirty_out = dirty; | 198 *dirty_out = dirty; |
201 *usage_out = usage; | 199 *usage_out = usage; |
202 return true; | 200 return true; |
203 } | 201 } |
204 | 202 |
205 bool FileSystemUsageCache::Write(const base::FilePath& usage_file_path, | 203 bool FileSystemUsageCache::Write(const base::FilePath& usage_file_path, |
206 bool is_valid, | 204 bool is_valid, |
207 int32 dirty, | 205 int32 dirty, |
(...skipping 22 matching lines...) Expand all Loading... |
230 ScheduleCloseTimer(); | 228 ScheduleCloseTimer(); |
231 | 229 |
232 base::File* new_file = NULL; | 230 base::File* new_file = NULL; |
233 std::pair<CacheFiles::iterator, bool> inserted = | 231 std::pair<CacheFiles::iterator, bool> inserted = |
234 cache_files_.insert(std::make_pair(file_path, new_file)); | 232 cache_files_.insert(std::make_pair(file_path, new_file)); |
235 if (!inserted.second) | 233 if (!inserted.second) |
236 return inserted.first->second; | 234 return inserted.first->second; |
237 | 235 |
238 new_file = new base::File(file_path, | 236 new_file = new base::File(file_path, |
239 base::File::FLAG_OPEN_ALWAYS | | 237 base::File::FLAG_OPEN_ALWAYS | |
240 base::File::FLAG_READ | | 238 base::File::FLAG_READ | base::File::FLAG_WRITE); |
241 base::File::FLAG_WRITE); | |
242 if (!new_file->IsValid()) { | 239 if (!new_file->IsValid()) { |
243 cache_files_.erase(inserted.first); | 240 cache_files_.erase(inserted.first); |
244 delete new_file; | 241 delete new_file; |
245 return NULL; | 242 return NULL; |
246 } | 243 } |
247 | 244 |
248 inserted.first->second = new_file; | 245 inserted.first->second = new_file; |
249 return new_file; | 246 return new_file; |
250 } | 247 } |
251 | 248 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 bool FileSystemUsageCache::CalledOnValidThread() { | 294 bool FileSystemUsageCache::CalledOnValidThread() { |
298 return !task_runner_.get() || task_runner_->RunsTasksOnCurrentThread(); | 295 return !task_runner_.get() || task_runner_->RunsTasksOnCurrentThread(); |
299 } | 296 } |
300 | 297 |
301 bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { | 298 bool FileSystemUsageCache::HasCacheFileHandle(const base::FilePath& file_path) { |
302 DCHECK(CalledOnValidThread()); | 299 DCHECK(CalledOnValidThread()); |
303 DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); | 300 DCHECK_LE(cache_files_.size(), kMaxHandleCacheSize); |
304 return ContainsKey(cache_files_, file_path); | 301 return ContainsKey(cache_files_, file_path); |
305 } | 302 } |
306 | 303 |
307 } // namespace fileapi | 304 } // namespace storage |
OLD | NEW |