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_path.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/platform_file.h"
15#include "base/sequenced_task_runner.h"
16#include "webkit/browser/webkit_storage_browser_export.h"
17
18namespace fileapi {
19
20class TimedTaskHelper;
21
22class 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::PlatformFile> 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  bool GetPlatformFile(const base::FilePath& file_path,
79                       base::PlatformFile* file);
80
81  bool ReadBytes(const base::FilePath& file_path,
82                 char* buffer,
83                 int64 buffer_size);
84  bool WriteBytes(const base::FilePath& file_path,
85                  const char* buffer,
86                  int64 buffer_size);
87  bool FlushFile(const base::FilePath& file_path);
88  void ScheduleCloseTimer();
89
90  bool HasCacheFileHandle(const base::FilePath& file_path);
91
92  bool CalledOnValidThread();
93
94  scoped_ptr<TimedTaskHelper> timer_;
95  std::map<base::FilePath, base::PlatformFile> cache_files_;
96  base::WeakPtrFactory<FileSystemUsageCache> weak_factory_;
97
98  scoped_refptr<base::SequencedTaskRunner> task_runner_;
99
100  DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCache);
101};
102
103}  // namespace fileapi
104
105#endif  // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_USAGE_CACHE_H_
106