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 3 // found in the LICENSE file. 4 5 #ifndef WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_ 6 #define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_ 7 8 #include <map> 9 10 #include "base/basictypes.h" 11 #include "base/files/file.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/sequenced_task_runner.h" 16 #include "webkit/browser/webkit_storage_browser_export.h" 17 18 namespace fileapi { 19 20 class TimedTaskHelper; 21 22 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileSystemUsageCache { 23 public: 24 explicit FileSystemUsageCache(base::SequencedTaskRunner* task_runner); 25 ~FileSystemUsageCache(); 26 27 // Gets the size described in the .usage file even if dirty > 0 or 28 // is_valid == false. Returns true if the .usage file is available. 29 bool GetUsage(const base::FilePath& usage_file_path, int64* usage); 30 31 // Gets the dirty count in the .usage file. 32 // Returns true if the .usage file is available. 33 bool GetDirty(const base::FilePath& usage_file_path, uint32* dirty); 34 35 // Increments or decrements the "dirty" entry in the .usage file. 36 // Returns false if no .usage is available. 37 bool IncrementDirty(const base::FilePath& usage_file_path); 38 bool DecrementDirty(const base::FilePath& usage_file_path); 39 40 // Notifies quota system that it needs to recalculate the usage cache of the 41 // origin. Returns false if no .usage is available. 42 bool Invalidate(const base::FilePath& usage_file_path); 43 bool IsValid(const base::FilePath& usage_file_path); 44 45 // Updates the size described in the .usage file. 46 bool UpdateUsage(const base::FilePath& usage_file_path, int64 fs_usage); 47 48 // Updates the size described in the .usage file by delta with keeping dirty 49 // even if dirty > 0. 50 bool AtomicUpdateUsageByDelta(const base::FilePath& usage_file_path, 51 int64 delta); 52 53 bool Exists(const base::FilePath& usage_file_path); 54 bool Delete(const base::FilePath& usage_file_path); 55 56 void CloseCacheFiles(); 57 58 static const base::FilePath::CharType kUsageFileName[]; 59 static const char kUsageFileHeader[]; 60 static const int kUsageFileSize; 61 static const int kUsageFileHeaderSize; 62 63 private: 64 typedef std::map<base::FilePath, base::File*> CacheFiles; 65 66 // Read the size, validity and the "dirty" entry described in the .usage file. 67 // Returns less than zero if no .usage file is available. 68 bool Read(const base::FilePath& usage_file_path, 69 bool* is_valid, 70 uint32* dirty, 71 int64* usage); 72 73 bool Write(const base::FilePath& usage_file_path, 74 bool is_valid, 75 int32 dirty, 76 int64 fs_usage); 77 78 base::File* GetFile(const base::FilePath& file_path); 79 80 bool ReadBytes(const base::FilePath& file_path, 81 char* buffer, 82 int64 buffer_size); 83 bool WriteBytes(const base::FilePath& file_path, 84 const char* buffer, 85 int64 buffer_size); 86 bool FlushFile(const base::FilePath& file_path); 87 void ScheduleCloseTimer(); 88 89 bool HasCacheFileHandle(const base::FilePath& file_path); 90 91 bool CalledOnValidThread(); 92 93 scoped_ptr<TimedTaskHelper> timer_; 94 CacheFiles cache_files_; 95 96 scoped_refptr<base::SequencedTaskRunner> task_runner_; 97 98 base::WeakPtrFactory<FileSystemUsageCache> weak_factory_; 99 100 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCache); 101 }; 102 103 } // namespace fileapi 104 105 #endif // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_ 106