archive_manager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2016 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 COMPONENTS_OFFLINE_PAGES_CORE_ARCHIVE_MANAGER_H_
  5. #define COMPONENTS_OFFLINE_PAGES_CORE_ARCHIVE_MANAGER_H_
  6. #include "base/callback_forward.h"
  7. #include "base/files/file_path.h"
  8. #include "base/memory/ref_counted.h"
  9. namespace base {
  10. class SequencedTaskRunner;
  11. } // namespace base
  12. namespace offline_pages {
  13. // Class that manages all archive files for offline pages. They are stored in
  14. // different archive directories based on their lifetime types (persistent or
  15. // temporary).
  16. // All tasks are performed using |task_runner_|.
  17. class ArchiveManager {
  18. public:
  19. // Used by metrics collection and clearing storage of temporary pages.
  20. // - |internal_free_disk_space| is the free space of the volume which
  21. // contains temporary and private archive directories.
  22. // - |external_free_disk_space| is the free space of the volume which contains
  23. // public archives directory, in most cases it should be Download directory
  24. // in /sdcard/.
  25. // - |{temporary/private}_archives_size| is the size of the directory. Since
  26. // these are inside app directory and are fully controlled, it's unnecessary
  27. // to calculate the size by iterating and counting.
  28. // - |public_archives_size| will enumerate all mhtml files in the public
  29. // directory and records the total file size. This will include mhtml files
  30. // shared into the public directory through other approaches.
  31. struct StorageStats {
  32. int64_t internal_archives_size() const {
  33. return temporary_archives_size + private_archives_size;
  34. }
  35. int64_t internal_free_disk_space;
  36. int64_t external_free_disk_space;
  37. int64_t temporary_archives_size;
  38. int64_t private_archives_size;
  39. int64_t public_archives_size;
  40. };
  41. typedef base::OnceCallback<void(
  42. const ArchiveManager::StorageStats& storage_stats)>
  43. StorageStatsCallback;
  44. ArchiveManager(const base::FilePath& temporary_archives_dir,
  45. const base::FilePath& private_archives_dir_,
  46. const base::FilePath& public_archives_dir,
  47. const scoped_refptr<base::SequencedTaskRunner>& task_runner);
  48. ArchiveManager(const ArchiveManager&) = delete;
  49. ArchiveManager& operator=(const ArchiveManager&) = delete;
  50. virtual ~ArchiveManager();
  51. // Creates archives directory if one does not exist yet;
  52. virtual void EnsureArchivesDirCreated(base::OnceCallback<void()> callback);
  53. // Gets stats about archive storage, i.e. sizes of all archive directories
  54. // and free disk spaces.
  55. virtual void GetStorageStats(StorageStatsCallback callback) const;
  56. // Gets the archive directories.
  57. const base::FilePath& GetTemporaryArchivesDir() const;
  58. const base::FilePath& GetPrivateArchivesDir() const;
  59. virtual const base::FilePath& GetPublicArchivesDir();
  60. protected:
  61. // Used for testing.
  62. ArchiveManager();
  63. private:
  64. // Path under which all of the temporary archives should be stored.
  65. base::FilePath temporary_archives_dir_;
  66. // Path under which all of the persistent archives should be saved initially.
  67. base::FilePath private_archives_dir_;
  68. // Publically accessible path where archives should be moved once ready.
  69. base::FilePath public_archives_dir_;
  70. // Task runner for running file operations.
  71. // Since the task_runner is a SequencedTaskRunner, it's guaranteed that the
  72. // second task will start after the first one. This is an important assumption
  73. // for |ArchiveManager::EnsureArchivesDirCreated|.
  74. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  75. };
  76. } // namespace offline_pages
  77. #endif // COMPONENTS_OFFLINE_PAGES_CORE_ARCHIVE_MANAGER_H_