File: almanac_app_icon_loader.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 (175 lines) | stat: -rw-r--r-- 6,142 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/almanac_api_client/almanac_app_icon_loader.h"

#include "base/functional/bind.h"
#include "base/one_shot_event.h"
#include "base/strings/string_util.h"
#include "chrome/browser/apps/almanac_api_client/almanac_icon_cache.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_factory.h"
#include "chrome/browser/image_fetcher/image_fetcher_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "components/services/app_service/public/cpp/icon_effects.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"

namespace apps {

namespace {

constexpr int kSvgRasterSize = 192;

bool IsSvgExtension(const GURL& icon_url, std::string_view icon_mime_type) {
  std::string url_string = icon_url.spec();
  return icon_mime_type.empty()
             ? base::EndsWith(url_string, ".svg", base::CompareCase::SENSITIVE)
             : icon_mime_type == "image/svg+xml";
}

}  // namespace

// Creates a background WebContents for downloading and rendering an SVG image.
class AlmanacAppIconLoader::SvgLoader : public content::WebContentsObserver {
 public:
  explicit SvgLoader(Profile& profile) {
    // SVG icons require a full renderer to rasterize to a bitmap.
    web_contents_ = content::WebContents::Create(
        content::WebContents::CreateParams(&profile));

    // Load about:blank to initialise the WebContents before using it.
    Observe(web_contents_.get());
    content::NavigationController::LoadURLParams load_params{
        GURL(url::kAboutBlankURL)};
    load_params.transition_type = ui::PAGE_TRANSITION_GENERATED;
    web_contents_->GetController().LoadURLWithParams(load_params);
    // Logic continues in DidStopLoading().
  }

  ~SvgLoader() override = default;

  // content::WebContentsObserver:
  void DidStopLoading() override {
    if (!web_contents_ready_.is_signaled()) {
      web_contents_ready_.Signal();
    }
  }

  void GetSvg(GURL svg_url,
              base::OnceCallback<void(std::optional<SkBitmap>)> callback) {
    web_contents_ready_.Post(
        FROM_HERE,
        base::BindOnce(base::IgnoreResult(&content::WebContents::DownloadImage),
                       web_contents_->GetWeakPtr(), svg_url,
                       /*is_favicon=*/false,
                       gfx::Size(kSvgRasterSize, kSvgRasterSize),
                       /*max_bitmap_size=*/0,  // No max size.
                       /*bypass_cache=*/false,
                       base::BindOnce(&SvgLoader::OnImageDownloaded,
                                      std::move(callback))));
  }

  void WebContentsDestroyed() override { Observe(nullptr); }

 private:
  static void OnImageDownloaded(
      base::OnceCallback<void(std::optional<SkBitmap>)> callback,
      int id,
      int http_status_code,
      const GURL& image_url,
      const std::vector<SkBitmap>& bitmaps,
      const std::vector<gfx::Size>& sizes) {
    if (!bitmaps.empty()) {
      std::move(callback).Run(bitmaps.front());
      return;
    }
    std::move(callback).Run(std::nullopt);
  }

  std::unique_ptr<content::WebContents> web_contents_;
  base::OneShotEvent web_contents_ready_;
};

AlmanacAppIconLoader::AlmanacAppIconLoader(Profile& profile)
    : profile_(profile.GetWeakPtr()) {}

AlmanacAppIconLoader::~AlmanacAppIconLoader() = default;

void AlmanacAppIconLoader::GetAppIcon(
    const GURL& icon_url,
    std::string_view icon_mime_type,
    bool icon_masking_allowed,
    base::OnceCallback<void(apps::IconValuePtr)> callback) {
  if (!profile_) {
    std::move(callback).Run(nullptr);
  }

  if (IsSvgExtension(icon_url, icon_mime_type)) {
    if (!svg_loader_) {
      svg_loader_ = std::make_unique<SvgLoader>(*profile_);
    }
    svg_loader_->GetSvg(
        icon_url, base::BindOnce(&AlmanacAppIconLoader::OnSvgLoaded,
                                 weak_ptr_factory_.GetWeakPtr(),
                                 icon_masking_allowed, std::move(callback)));
    return;
  }

  if (!icon_cache_) {
    icon_cache_ = std::make_unique<AlmanacIconCache>(profile_->GetProfileKey());
  }
  icon_cache_->GetIcon(
      icon_url, base::BindOnce(&AlmanacAppIconLoader::OnBitmapLoaded,
                               weak_ptr_factory_.GetWeakPtr(),
                               icon_masking_allowed, std::move(callback)));
}

void AlmanacAppIconLoader::OnSvgLoaded(
    bool icon_masking_allowed,
    base::OnceCallback<void(apps::IconValuePtr)> callback,
    std::optional<SkBitmap> bitmap) {
  if (!bitmap.has_value()) {
    std::move(callback).Run(nullptr);
    return;
  }

  ApplyIconEffects(icon_masking_allowed,
                   gfx::Image::CreateFrom1xBitmap(bitmap.value()),
                   std::move(callback));
}

void AlmanacAppIconLoader::OnBitmapLoaded(
    bool icon_masking_allowed,
    base::OnceCallback<void(apps::IconValuePtr)> callback,
    const gfx::Image& icon_bitmap) {
  ApplyIconEffects(icon_masking_allowed, icon_bitmap, std::move(callback));
}

void AlmanacAppIconLoader::ApplyIconEffects(
    bool icon_masking_allowed,
    gfx::Image icon_bitmap,
    base::OnceCallback<void(apps::IconValuePtr)> callback) {
  if (!profile_ || icon_bitmap.IsEmpty()) {
    std::move(callback).Run(nullptr);
    return;
  }

  apps::IconValuePtr icon_value = std::make_unique<apps::IconValue>();
  icon_value->icon_type = apps::IconType::kStandard;
  icon_value->is_placeholder_icon = false;
  icon_value->is_maskable_icon = icon_masking_allowed;
  icon_value->uncompressed = icon_bitmap.AsImageSkia();

  apps::ApplyIconEffects(
      profile_.get(), /*app_id=*/std::nullopt,
      icon_masking_allowed ? apps::IconEffects::kCrOsStandardMask
                           : apps::IconEffects::kCrOsStandardIcon,
      icon_bitmap.Width(), std::move(icon_value), std::move(callback));
}

}  // namespace apps