file_system_usage_cache.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_USAGE_CACHE_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_USAGE_CACHE_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include "base/component_export.h"
  10. #include "base/files/file.h"
  11. #include "base/files/file_path.h"
  12. #include "base/sequence_checker.h"
  13. #include "base/timer/timer.h"
  14. namespace storage {
  15. class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemUsageCache {
  16. public:
  17. FileSystemUsageCache(bool is_incognito);
  18. FileSystemUsageCache(const FileSystemUsageCache&) = delete;
  19. FileSystemUsageCache& operator=(const FileSystemUsageCache&) = delete;
  20. ~FileSystemUsageCache();
  21. // Gets the size described in the .usage file even if dirty > 0 or
  22. // is_valid == false. Returns true if the .usage file is available.
  23. bool GetUsage(const base::FilePath& usage_file_path, int64_t* usage);
  24. // Gets the dirty count in the .usage file.
  25. // Returns true if the .usage file is available.
  26. bool GetDirty(const base::FilePath& usage_file_path, uint32_t* dirty);
  27. // Increments or decrements the "dirty" entry in the .usage file.
  28. // Returns false if no .usage is available.
  29. bool IncrementDirty(const base::FilePath& usage_file_path);
  30. bool DecrementDirty(const base::FilePath& usage_file_path);
  31. // Notifies quota system that it needs to recalculate the usage cache of the
  32. // origin. Returns false if no .usage is available.
  33. bool Invalidate(const base::FilePath& usage_file_path);
  34. bool IsValid(const base::FilePath& usage_file_path);
  35. // Updates the size described in the .usage file.
  36. bool UpdateUsage(const base::FilePath& usage_file_path, int64_t fs_usage);
  37. // Updates the size described in the .usage file by delta with keeping dirty
  38. // even if dirty > 0.
  39. bool AtomicUpdateUsageByDelta(const base::FilePath& usage_file_path,
  40. int64_t delta);
  41. bool Exists(const base::FilePath& usage_file_path);
  42. bool Delete(const base::FilePath& usage_file_path);
  43. void CloseCacheFiles();
  44. static const base::FilePath::CharType kUsageFileName[];
  45. static const char kUsageFileHeader[];
  46. static const int kUsageFileSize;
  47. static const size_t kUsageFileHeaderSize;
  48. private:
  49. // Read the size, validity and the "dirty" entry described in the .usage file.
  50. // Returns less than zero if no .usage file is available.
  51. bool Read(const base::FilePath& usage_file_path,
  52. bool* is_valid,
  53. uint32_t* dirty,
  54. int64_t* usage);
  55. bool Write(const base::FilePath& usage_file_path,
  56. bool is_valid,
  57. int32_t dirty,
  58. int64_t fs_usage);
  59. base::File* GetFile(const base::FilePath& file_path);
  60. bool ReadBytes(const base::FilePath& file_path,
  61. char* buffer,
  62. int64_t buffer_size);
  63. bool WriteBytes(const base::FilePath& file_path,
  64. const char* buffer,
  65. int64_t buffer_size);
  66. bool FlushFile(const base::FilePath& file_path);
  67. void ScheduleCloseTimer();
  68. bool HasCacheFileHandle(const base::FilePath& file_path);
  69. // Used to verify that this is used from a single sequence.
  70. SEQUENCE_CHECKER(sequence_checker_);
  71. // Used to scheduled delayed calls to CloseCacheFiles().
  72. base::OneShotTimer timer_;
  73. // Incognito usages are kept in memory and are not written to disk.
  74. bool is_incognito_;
  75. // TODO(https://crbug.com/955905): Stop using base::FilePath as the key in
  76. // this API as the paths are not necessarily actual on-disk locations.
  77. std::map<base::FilePath, std::vector<uint8_t>> incognito_usages_;
  78. std::map<base::FilePath, std::unique_ptr<base::File>> cache_files_;
  79. };
  80. } // namespace storage
  81. #endif // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_USAGE_CACHE_H_