File: crx_cache.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (103 lines) | stat: -rw-r--r-- 3,825 bytes parent folder | download | duplicates (3)
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
// Copyright 2022 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_UPDATE_CLIENT_CRX_CACHE_H_
#define COMPONENTS_UPDATE_CLIENT_CRX_CACHE_H_

#include <map>
#include <optional>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_checker.h"
#include "base/threading/sequence_bound.h"
#include "base/types/expected.h"

namespace base {
class FilePath;
}

namespace update_client {

enum class UnpackerError;
class CrxCacheSynchronous;

// A CRX cache is a filesystem-backed cache of files. It keeps files inside a
// particular directory, and uses a metadata file within that directory to
// track data about the entries.
//
// When constructed, the cache loads the existing metadata file in the provided
// directory and immediately deletes any files in the directory that don't have
// a corresponding entry in the metadata file. If there is no metadata file,
// the cache does not delete anything. The metadata is created after the first
// Put.
//
// It is unsafe to instantiate multiple CrxCaches with a shared `cache_root`.
//
// The cache maintains only a single cached element per app ID.
class CrxCache : public base::RefCountedThreadSafe<CrxCache> {
 public:
  CrxCache(const CrxCache&) = delete;
  CrxCache& operator=(const CrxCache&) = delete;

  // Constructs a CrxCache that operates in `cache_root`. If `cache_root` is
  // not provided, the CrxCache returns errors from most methods. Deletes any
  // contents in `cache_root` that do not belong to the cache.
  explicit CrxCache(std::optional<base::FilePath> cache_root);

  // Returns a multimap of app IDs to hashes that are present in the cache.
  void ListHashesByAppId(
      base::OnceCallback<void(const std::multimap<std::string, std::string>&)>
          callback) const;

  // Returns a cached element with a matching hash. If there is no element
  // cached, returns base::unexpected(kCrxCacheFileNotCached). O(1) lookup.
  void GetByHash(
      const std::string& hash,
      base::OnceCallback<void(base::expected<base::FilePath, UnpackerError>)>
          callback) const;

  // Similar to GetByHash, but looks up an item by fingerprint. O(N) lookup.
  void GetByFp(
      const std::string& fp,
      base::OnceCallback<void(base::expected<base::FilePath, UnpackerError>)>
          callback) const;

  // Adds `file` to the cache. Any entries with the same `app_id` are first
  // evicted. The file is moved into the cache, and the new path to the
  // file is returned. Hashes should be in ASCII. O(N) time.
  void Put(
      const base::FilePath& file,
      const std::string& app_id,
      const std::string& hash,
      const std::string& fp,
      base::OnceCallback<void(base::expected<base::FilePath, UnpackerError>)>
          callback);

  // Removes all entries associated with a particular app ID. O(N) time.
  void RemoveAll(const std::string& app_id, base::OnceClosure callback);

  // Removes all entries that are not associated with any of the listed
  // app IDs. O(N+M) time.
  void RemoveIfNot(const std::vector<std::string>& app_ids,
                   base::OnceClosure callback);

 private:
  friend class base::RefCountedThreadSafe<CrxCache>;

  // Destroying the CrxCache schedules a write of the metadata to disk. The
  // write will block shutdown, but callers can't depend on it happening before
  // the destructor returns, so it is unsafe to construct a new CrxCache with
  // the same cache_root before the process shuts down.
  virtual ~CrxCache();

  SEQUENCE_CHECKER(sequence_checker_);
  base::SequenceBound<CrxCacheSynchronous> delegate_;
};

}  // namespace update_client

#endif  // COMPONENTS_UPDATE_CLIENT_CRX_CACHE_H_