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.
5 #ifndef STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_
6 #define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_
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 "storage/browser/storage_browser_export.h"
20 class TimedTaskHelper
;
22 class STORAGE_EXPORT_PRIVATE FileSystemUsageCache
{
24 explicit FileSystemUsageCache(base::SequencedTaskRunner
* task_runner
);
25 ~FileSystemUsageCache();
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
);
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
);
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
);
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
);
45 // Updates the size described in the .usage file.
46 bool UpdateUsage(const base::FilePath
& usage_file_path
, int64 fs_usage
);
48 // Updates the size described in the .usage file by delta with keeping dirty
50 bool AtomicUpdateUsageByDelta(const base::FilePath
& usage_file_path
,
53 bool Exists(const base::FilePath
& usage_file_path
);
54 bool Delete(const base::FilePath
& usage_file_path
);
56 void CloseCacheFiles();
58 static const base::FilePath::CharType kUsageFileName
[];
59 static const char kUsageFileHeader
[];
60 static const int kUsageFileSize
;
61 static const int kUsageFileHeaderSize
;
64 typedef std::map
<base::FilePath
, base::File
*> CacheFiles
;
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
,
73 bool Write(const base::FilePath
& usage_file_path
,
78 base::File
* GetFile(const base::FilePath
& file_path
);
80 bool ReadBytes(const base::FilePath
& file_path
,
83 bool WriteBytes(const base::FilePath
& file_path
,
86 bool FlushFile(const base::FilePath
& file_path
);
87 void ScheduleCloseTimer();
89 bool HasCacheFileHandle(const base::FilePath
& file_path
);
91 bool CalledOnValidThread();
93 scoped_ptr
<TimedTaskHelper
> timer_
;
94 CacheFiles cache_files_
;
96 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
98 base::WeakPtrFactory
<FileSystemUsageCache
> weak_factory_
;
100 DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCache
);
103 } // namespace storage
105 #endif // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_