File: file_system_usage_cache.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_USAGE_CACHE_H_
#define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_USAGE_CACHE_H_

#include <stdint.h>

#include <map>
#include <memory>

#include "base/component_export.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/sequence_checker.h"
#include "base/timer/timer.h"

namespace storage {

class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemUsageCache {
 public:
  FileSystemUsageCache(bool is_incognito);

  FileSystemUsageCache(const FileSystemUsageCache&) = delete;
  FileSystemUsageCache& operator=(const FileSystemUsageCache&) = delete;

  ~FileSystemUsageCache();

  // Gets the size described in the .usage file even if dirty > 0 or
  // is_valid == false.  Returns true if the .usage file is available.
  bool GetUsage(const base::FilePath& usage_file_path, int64_t* usage);

  // Gets the dirty count in the .usage file.
  // Returns true if the .usage file is available.
  bool GetDirty(const base::FilePath& usage_file_path, uint32_t* dirty);

  // Increments or decrements the "dirty" entry in the .usage file.
  // Returns false if no .usage is available.
  bool IncrementDirty(const base::FilePath& usage_file_path);
  bool DecrementDirty(const base::FilePath& usage_file_path);

  // Notifies quota system that it needs to recalculate the usage cache of the
  // origin.  Returns false if no .usage is available.
  bool Invalidate(const base::FilePath& usage_file_path);
  bool IsValid(const base::FilePath& usage_file_path);

  // Updates the size described in the .usage file.
  bool UpdateUsage(const base::FilePath& usage_file_path, int64_t fs_usage);

  // Updates the size described in the .usage file by delta with keeping dirty
  // even if dirty > 0.
  bool AtomicUpdateUsageByDelta(const base::FilePath& usage_file_path,
                                int64_t delta);

  bool Exists(const base::FilePath& usage_file_path);
  bool Delete(const base::FilePath& usage_file_path);

  void CloseCacheFiles();

  static const base::FilePath::CharType kUsageFileName[];
  static const char kUsageFileHeader[];
  static const int kUsageFileSize;
  static const size_t kUsageFileHeaderSize;

 private:
  // Read the size, validity and the "dirty" entry described in the .usage file.
  // Returns less than zero if no .usage file is available.
  bool Read(const base::FilePath& usage_file_path,
            bool* is_valid,
            uint32_t* dirty,
            int64_t* usage);

  bool Write(const base::FilePath& usage_file_path,
             bool is_valid,
             int32_t dirty,
             int64_t fs_usage);

  base::File* GetFile(const base::FilePath& file_path);

  bool ReadBytes(const base::FilePath& file_path, base::span<uint8_t> buffer);
  bool WriteBytes(const base::FilePath& file_path,
                  base::span<const uint8_t> buffer);
  bool FlushFile(const base::FilePath& file_path);
  void ScheduleCloseTimer();

  bool HasCacheFileHandle(const base::FilePath& file_path);

  // Used to verify that this is used from a single sequence.
  SEQUENCE_CHECKER(sequence_checker_);

  // Used to scheduled delayed calls to CloseCacheFiles().
  base::OneShotTimer timer_;

  // Incognito usages are kept in memory and are not written to disk.
  bool is_incognito_;
  // TODO(crbug.com/41454722): Stop using base::FilePath as the key in
  // this API as the paths are not necessarily actual on-disk locations.
  std::map<base::FilePath, std::vector<uint8_t>> incognito_usages_;

  std::map<base::FilePath, std::unique_ptr<base::File>> cache_files_;
};

}  // namespace storage

#endif  // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_USAGE_CACHE_H_