File: file_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (7)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAINT_PREVIEW_BROWSER_FILE_MANAGER_H_
#define COMPONENTS_PAINT_PREVIEW_BROWSER_FILE_MANAGER_H_

#include <optional>

#include "base/containers/flat_set.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/task/sequenced_task_runner.h"
#include "components/paint_preview/browser/directory_key.h"
#include "components/paint_preview/common/proto/paint_preview.pb.h"
#include "url/gurl.h"

namespace paint_preview {
class PaintPreviewTabService;

// FileManager manages paint preview files associated with a root directory.
// Typically the root directory is <profile_dir>/paint_previews/<feature>.
//
// This class is refcounted so scheduled tasks may continue during shutdown.
class FileManager : public base::RefCountedThreadSafe<FileManager> {
 public:
  enum class ProtoReadStatus : int {
    kOk = 0,
    kNoProto,
    kDeserializationError,
  };

  // Create a file manager for |root_directory|. Top level items in
  // |root_directoy| should be exclusively managed by this class. Items within
  // the subdirectories it creates can be freely modified. All methods will be
  // should be posted to the |io_task_runner| thread.
  FileManager(const base::FilePath& root_directory,
              scoped_refptr<base::SequencedTaskRunner> io_task_runner);

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

  scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() {
    return io_task_runner_;
  }

  // Creates a DirectoryKey from keying material.
  // TODO(crbug.com/40676728): implement collision resolution. At present
  // collisions result in overwriting data.
  DirectoryKey CreateKey(const GURL& url) const;
  DirectoryKey CreateKey(uint64_t tab_id) const;

  // Get statistics about the time of creation and size of artifacts.
  size_t GetSizeOfArtifacts(const DirectoryKey& key) const;
  std::optional<base::File::Info> GetInfo(const DirectoryKey& key) const;

  // Returns the total disk usage of all paint previews.
  size_t GetTotalDiskUsage() const;

  // Returns true if the directory for |key| exists.
  bool DirectoryExists(const DirectoryKey& key) const;

  // Returns true if there is a capture for |key| i.e. there exists a proto or
  // zip file.
  bool CaptureExists(const DirectoryKey& key) const;

  // Creates or gets a subdirectory under |root_directory| for |key| and
  // assigns it to |directory|. The directory will be wiped if |clear| is true.
  // Returns a path on success or nullopt on failure. If the directory was
  // compressed then it will be uncompressed automatically.
  std::optional<base::FilePath> CreateOrGetDirectory(const DirectoryKey& key,
                                                     bool clear) const;

  // Compresses the directory associated with |key|. Returns true on success or
  // if the directory was already compressed. NOTE: an empty directory or a
  // directory containing only empty files/directories will not be compressed.
  bool CompressDirectory(const DirectoryKey& key) const;

  // Deletes artifacts associated with |key| or |keys|.
  void DeleteArtifactSet(const DirectoryKey& key) const;
  void DeleteArtifactSets(const std::vector<DirectoryKey>& keys) const;

  // Deletes all stored paint previews stored in the |root_directory_|.
  void DeleteAll() const;

  // Serializes |proto| to the directory for |key| also compresses is |compress|
  // is true. Returns true on success.
  bool SerializePaintPreviewProto(const DirectoryKey& key,
                                  const PaintPreviewProto& proto,
                                  bool compress) const;

  // Deserializes PaintPreviewProto stored in |key|. Returns a status and the
  // proto. The proto will be nullptr if the ProtoReadStatus != kOk.
  std::pair<ProtoReadStatus, std::unique_ptr<PaintPreviewProto>>
  DeserializePaintPreviewProto(const DirectoryKey& key) const;

  // Lists the current set of in-use DirectoryKeys.
  base::flat_set<DirectoryKey> ListUsedKeys() const;

 private:
  friend class base::RefCountedThreadSafe<FileManager>;
  ~FileManager();

  friend class PaintPreviewTabService;
  base::FilePath GetPath() const { return root_directory_; }

  enum StorageType {
    kNone = 0,
    kDirectory = 1,
    kZip = 2,
  };

  StorageType GetPathForKey(const DirectoryKey& key,
                            base::FilePath* path) const;

  base::FilePath root_directory_;
  scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
};

}  // namespace paint_preview

#endif  // COMPONENTS_PAINT_PREVIEW_BROWSER_FILE_MANAGER_H_