File: icon_cache.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (177 lines) | stat: -rw-r--r-- 5,495 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 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.

#include "components/services/app_service/public/cpp/icon_cache.h"

#include <utility>

#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"

namespace apps {

void RecordIconLoadMethodMetrics(IconLoadingMethod icon_loading_method) {
  base::UmaHistogramEnumeration("Apps.IconLoadingMethod", icon_loading_method);
}

IconCache::Value::Value()
    : image_(), is_placeholder_icon_(false), ref_count_(0) {}

IconValuePtr IconCache::Value::AsIconValue(IconType icon_type) {
  auto icon_value = std::make_unique<IconValue>();
  icon_value->icon_type = icon_type;
  icon_value->uncompressed = image_;
  icon_value->is_placeholder_icon = is_placeholder_icon_;
  return icon_value;
}

IconCache::IconCache(IconLoader* wrapped_loader,
                     GarbageCollectionPolicy gc_policy)
    : wrapped_loader_(wrapped_loader), gc_policy_(gc_policy) {}

IconCache::~IconCache() = default;

std::optional<IconKey> IconCache::GetIconKey(const std::string& id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return wrapped_loader_ ? wrapped_loader_->GetIconKey(id) : std::nullopt;
}

std::unique_ptr<IconLoader::Releaser> IconCache::LoadIconFromIconKey(
    const std::string& id,
    const IconKey& icon_key,
    IconType icon_type,
    int32_t size_hint_in_dip,
    bool allow_placeholder_icon,
    apps::LoadIconCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  IconLoader::Key key(
      id, icon_key, icon_type, size_hint_in_dip,
      // We pass false instead of allow_placeholder_icon, as the Value
      // already records placeholder-ness. If the allow_placeholder_icon
      // arg to this function is true, we can re-use a cache hit regardless
      // of whether the previous call to the underlying wrapped_loader_
      // returned the placeholder icon or the real icon, so we don't want
      // to restrict our map lookup to only one flavor.
      false);
  Value* cache_hit = nullptr;
  bool ref_count_incremented = false;

  if (icon_type == IconType::kUncompressed ||
      icon_type == IconType::kStandard) {
    auto iter = map_.find(key);
    if (iter == map_.end()) {
      iter = map_.insert(std::make_pair(key, Value())).first;
    } else if (!iter->second.image_.isNull() &&
               (allow_placeholder_icon || !iter->second.is_placeholder_icon_)) {
      cache_hit = &iter->second;
    }

    auto new_ref_count = ++iter->second.ref_count_;
    CHECK(new_ref_count != std::numeric_limits<decltype(new_ref_count)>::max());
    ref_count_incremented = true;
  }

  std::unique_ptr<IconLoader::Releaser> releaser(nullptr);
  if (cache_hit) {
    RecordIconLoadMethodMetrics(IconLoadingMethod::kFromCache);
    std::move(callback).Run(cache_hit->AsIconValue(icon_type));
  } else if (wrapped_loader_) {
    releaser = wrapped_loader_->LoadIconFromIconKey(
        id, icon_key, icon_type, size_hint_in_dip, allow_placeholder_icon,
        base::BindOnce(&IconCache::OnLoadIcon, weak_ptr_factory_.GetWeakPtr(),
                       key, std::move(callback)));
  } else {
    std::move(callback).Run(std::make_unique<IconValue>());
  }

  return ref_count_incremented
             ? std::make_unique<IconLoader::Releaser>(
                   std::move(releaser),
                   base::BindOnce(&IconCache::OnRelease,
                                  weak_ptr_factory_.GetWeakPtr(),
                                  std::move(key)))
             : std::move(releaser);
}

void IconCache::SweepReleasedIcons() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (gc_policy_ != GarbageCollectionPolicy::kExplicit) {
    return;
  }

  auto iter = map_.begin();
  while (iter != map_.end()) {
    if (iter->second.ref_count_ == 0) {
      iter = map_.erase(iter);
    } else {
      ++iter;
    }
  }
}

void IconCache::RemoveIcon(const std::string& id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (gc_policy_ != GarbageCollectionPolicy::kExplicit) {
    return;
  }

  auto iter = map_.begin();
  while (iter != map_.end()) {
    if (iter->first.id_ == id) {
      iter = map_.erase(iter);
    } else {
      ++iter;
    }
  }
}

void IconCache::Update(const IconLoader::Key& key,
                       const IconValue& icon_value) {
  if (icon_value.icon_type != IconType::kUncompressed &&
      icon_value.icon_type != IconType::kStandard) {
    return;
  }

  auto iter = map_.find(key);
  if (iter == map_.end()) {
    return;
  }

  // Don't let a placeholder overwrite a real icon.
  if (icon_value.is_placeholder_icon && !iter->second.is_placeholder_icon_) {
    return;
  }

  iter->second.image_ = icon_value.uncompressed;
}

void IconCache::OnLoadIcon(const IconLoader::Key& key,
                           apps::LoadIconCallback callback,
                           IconValuePtr icon_value) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  Update(key, *icon_value);
  std::move(callback).Run(std::move(icon_value));
}

void IconCache::OnRelease(IconLoader::Key key) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto iter = map_.find(key);
  if (iter == map_.end()) {
    return;
  }

  auto n = iter->second.ref_count_;
  if (n > 0) {
    n--;
  }
  iter->second.ref_count_ = n;

  if ((n == 0) && (gc_policy_ == GarbageCollectionPolicy::kEager)) {
    map_.erase(iter);
  }
}

}  // namespace apps