File: image_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 (134 lines) | stat: -rw-r--r-- 5,004 bytes parent folder | download | duplicates (5)
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
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2018 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_IMAGE_FETCHER_CORE_CACHE_IMAGE_CACHE_H_
#define COMPONENTS_IMAGE_FETCHER_CORE_CACHE_IMAGE_CACHE_H_

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "components/image_fetcher/core/cache/image_store_types.h"
#include "components/image_fetcher/core/cache/proto/cached_image_metadata.pb.h"

class PrefRegistrySimple;
class PrefService;

namespace base {
class Clock;
class SequencedTaskRunner;
class TimeTicks;
}  // namespace base

namespace image_fetcher {

class ImageCache;
class ImageDataStore;
class ImageMetadataStore;

// Persist image meta/data via the given implementations of ImageDataStore and
// ImageMetadataStore.
class ImageCache : public base::RefCounted<ImageCache> {
 public:
  static std::string HashUrlToKey(const std::string& input);
  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  ImageCache(std::unique_ptr<ImageDataStore> data_storage,
             std::unique_ptr<ImageMetadataStore> metadata_storage,
             PrefService* pref_service,
             base::Clock* clock,
             scoped_refptr<base::SequencedTaskRunner> task_runner);

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

  // Adds or updates the image data for the `url`. If the class hasn't been
  // initialized yet, the call is queued.
  void SaveImage(std::string url,
                 std::string image_data,
                 bool needs_transcoding,
                 ExpirationInterval expiration_interval);

  // Loads the image data for the `url` and passes it to `callback`. If there's
  // no image in the cache, then an empty string is returned. If `read_only`
  // is true, then the cache metadata won't be updated.
  void LoadImage(bool read_only, std::string url, ImageDataCallback callback);

  // Deletes the image data for the `url`.
  void DeleteImage(std::string url);

 private:
  friend class CachedImageFetcherImageCacheTest;
  friend class base::RefCounted<ImageCache>;
  ~ImageCache();

  // Queue or start `request` depending if the cache is initialized.
  void QueueOrStartRequest(base::OnceClosure request);
  // Start initializing the stores if it hasn't been started already.
  void MaybeStartInitialization();
  // Returns true iff both the stores have been initialized.
  bool AreAllDependenciesInitialized() const;
  // Receives callbacks when stores are initialized.
  void OnDependencyInitialized();

  // Saves the `image_data` for `url`.
  void SaveImageImpl(const std::string& url,
                     std::string image_data,
                     bool needs_transcoding,
                     ExpirationInterval expiration_interval);
  // Loads the data for `url`, calls the user back before updating metadata.
  void LoadImageImpl(bool read_only,
                     const std::string& url,
                     ImageDataCallback callback);
  // Loads the image metadata for the given `url`, used to inspect if there's
  // data available on disk that's in need of transcoding.
  void OnImageMetadataLoadedForLoadImage(
      bool read_only,
      const std::string& key,
      ImageDataCallback callback,
      base::TimeTicks start_time,
      std::optional<CachedImageMetadataProto> metadata);
  // Deletes the data for `url`.
  void DeleteImageImpl(const std::string& url);

  // Runs eviction on the data stores (run on startup).
  void RunEvictionOnStartup();
  // Runs eviction on the data stores (run when cache is full).
  void RunEvictionWhenFull();
  // Catch-all method for eviction, runs reconciliation routine after if
  // `run_reconciliation` is specified. Evicts until there are `bytes_left`
  // left in storage.
  void RunEviction(size_t bytes_left, base::OnceClosure on_completion);
  // Deletes the given keys from the data store.
  void OnKeysEvicted(base::OnceClosure on_completion,
                     std::vector<std::string> keys);
  // Reconcile what's on disk against what's in metadata. Any mismatches will
  // result in eviction.
  void RunReconciliation();
  void ReconcileMetadataKeys(std::vector<std::string> md_keys);
  void ReconcileDataKeys(std::vector<std::string> md_keys,
                         std::vector<std::string> data_keys);

  bool initialization_attempted_;
  std::vector<base::OnceClosure> queued_requests_;

  std::unique_ptr<ImageDataStore> data_store_;
  std::unique_ptr<ImageMetadataStore> metadata_store_;
  raw_ptr<PrefService> pref_service_;

  // Owned by the service which instantiates this.
  raw_ptr<base::Clock> clock_;

  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  base::WeakPtrFactory<ImageCache> weak_ptr_factory_{this};
};

}  // namespace image_fetcher

#endif  // COMPONENTS_IMAGE_FETCHER_CORE_CACHE_IMAGE_CACHE_H_