blob: 040c3a57cd881bb796224052b2351b20670b7bf3 [file] [log] [blame]
Qiang Xu139f86532018-03-06 22:14:431// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Henrique Ferreiroe52f59412021-03-25 13:33:165#include "chrome/browser/ash/arc/icon_decode_request.h"
Qiang Xu139f86532018-03-06 22:14:436
Qiang Xu1bbbdca2018-03-15 22:50:407#include <memory>
8#include <utility>
9#include <vector>
10
Nancy Wanga75ebc02021-03-30 05:13:2411#include "base/no_destructor.h"
Vladislav Kaznacheev84de1c52018-11-01 14:38:5812#include "chrome/browser/ui/app_list/md_icon_normalizer.h"
Qiang Xu139f86532018-03-06 22:14:4313#include "chrome/grit/component_extension_resources.h"
14#include "content/public/browser/browser_thread.h"
Nancy Wanga75ebc02021-03-30 05:13:2415#include "services/data_decoder/public/cpp/data_decoder.h"
Qiang Xu1b2a62ded2018-04-17 18:55:3716#include "ui/base/resource/resource_bundle.h"
Mitsuru Oshima15732bc92021-08-16 22:59:3117#include "ui/base/resource/resource_scale_factor.h"
Qiang Xu1bbbdca2018-03-15 22:50:4018#include "ui/gfx/codec/png_codec.h"
19#include "ui/gfx/image/image_skia.h"
Qiang Xu139f86532018-03-06 22:14:4320#include "ui/gfx/image/image_skia_operations.h"
21#include "ui/gfx/image/image_skia_rep.h"
22#include "ui/gfx/image/image_skia_source.h"
23
24using content::BrowserThread;
25
Qiang Xu1b2a62ded2018-04-17 18:55:3726namespace arc {
Qiang Xu139f86532018-03-06 22:14:4327
28namespace {
29
Qiang Xu1bbbdca2018-03-15 22:50:4030bool disable_safe_decoding_for_testing = false;
31
Qiang Xu139f86532018-03-06 22:14:4332class IconSource : public gfx::ImageSkiaSource {
33 public:
Vladislav Kaznacheev84de1c52018-11-01 14:38:5834 IconSource(const SkBitmap& bitmap, int dimension_dip, bool normalize);
Peter Boström53c6c5952021-09-17 09:41:2635
36 IconSource(const IconSource&) = delete;
37 IconSource& operator=(const IconSource&) = delete;
38
Qiang Xu139f86532018-03-06 22:14:4339 ~IconSource() override = default;
40
Qiang Xu139f86532018-03-06 22:14:4341 private:
42 gfx::ImageSkiaRep GetImageForScale(float scale) override;
43
Vladislav Kaznacheev84de1c52018-11-01 14:38:5844 const SkBitmap bitmap_;
45 const int dimension_dip_;
46 const bool normalize_;
Qiang Xu139f86532018-03-06 22:14:4347};
48
Vladislav Kaznacheev84de1c52018-11-01 14:38:5849IconSource::IconSource(const SkBitmap& bitmap,
50 int dimension_dip,
51 bool normalize)
52 : bitmap_(bitmap), dimension_dip_(dimension_dip), normalize_(normalize) {}
Qiang Xu139f86532018-03-06 22:14:4353
54gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI);
56
Vladislav Kaznacheev84de1c52018-11-01 14:38:5857 const int dimension_px = static_cast<int>(dimension_dip_ * scale + 0.5);
58 if (bitmap_.isNull()) {
59 const int resource_id =
60 dimension_px <= 32 ? IDR_ARC_SUPPORT_ICON_32 : IDR_ARC_SUPPORT_ICON_192;
61 const gfx::ImageSkia* resource_image =
Qiang Xu139f86532018-03-06 22:14:4362 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
Vladislav Kaznacheev84de1c52018-11-01 14:38:5863 const gfx::ImageSkia resized_image =
64 gfx::ImageSkiaOperations::CreateResizedImage(
65 *resource_image, skia::ImageOperations::RESIZE_LANCZOS3,
66 gfx::Size(dimension_dip_, dimension_dip_));
67 return resized_image.GetRepresentation(scale);
Qiang Xu139f86532018-03-06 22:14:4368 }
Qiang Xu139f86532018-03-06 22:14:4369
Vladislav Kaznacheev84de1c52018-11-01 14:38:5870 SkBitmap resized_bitmap;
71 if (normalize_) {
72 resized_bitmap = bitmap_;
73 const gfx::Size size_px(dimension_px, dimension_px);
74 const gfx::Size padding_px =
75 app_list::GetMdIconPadding(resized_bitmap, size_px);
76 app_list::MaybeResizeAndPad(size_px, padding_px, &resized_bitmap);
77 } else {
78 resized_bitmap = skia::ImageOperations::Resize(
79 bitmap_, skia::ImageOperations::RESIZE_LANCZOS3, dimension_px,
80 dimension_px);
81 }
82 return gfx::ImageSkiaRep(resized_bitmap, scale);
Qiang Xu139f86532018-03-06 22:14:4383}
84
Nancy Wanga75ebc02021-03-30 05:13:2485data_decoder::DataDecoder& GetDataDecoder() {
86 static base::NoDestructor<data_decoder::DataDecoder> data_decoder;
87 return *data_decoder;
88}
89
Qiang Xu139f86532018-03-06 22:14:4390} // namespace
91
Qiang Xu1bbbdca2018-03-15 22:50:4092// static
93void IconDecodeRequest::DisableSafeDecodingForTesting() {
94 disable_safe_decoding_for_testing = true;
95}
96
Qiang Xu1b2a62ded2018-04-17 18:55:3797IconDecodeRequest::IconDecodeRequest(SetIconCallback set_icon_callback,
Vladislav Kaznacheev84de1c52018-11-01 14:38:5898 int dimension_dip)
Nancy Wanga75ebc02021-03-30 05:13:2499 : ImageRequest(&GetDataDecoder()),
100 set_icon_callback_(std::move(set_icon_callback)),
Vladislav Kaznacheev84de1c52018-11-01 14:38:58101 dimension_dip_(dimension_dip) {}
Qiang Xu139f86532018-03-06 22:14:43102
103IconDecodeRequest::~IconDecodeRequest() = default;
104
Qiang Xu1bbbdca2018-03-15 22:50:40105void IconDecodeRequest::StartWithOptions(
106 const std::vector<uint8_t>& image_data) {
107 if (disable_safe_decoding_for_testing) {
108 if (image_data.empty()) {
109 OnDecodeImageFailed();
110 return;
111 }
112 SkBitmap bitmap;
113 if (!gfx::PNGCodec::Decode(
114 reinterpret_cast<const unsigned char*>(image_data.data()),
115 image_data.size(), &bitmap)) {
116 OnDecodeImageFailed();
117 return;
118 }
119 OnImageDecoded(bitmap);
120 return;
121 }
122 ImageDecoder::StartWithOptions(this, image_data, ImageDecoder::DEFAULT_CODEC,
123 true, gfx::Size());
124}
125
Qiang Xu139f86532018-03-06 22:14:43126void IconDecodeRequest::OnImageDecoded(const SkBitmap& bitmap) {
127 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Vladislav Kaznacheev84de1c52018-11-01 14:38:58128 const gfx::ImageSkia icon(
129 std::make_unique<IconSource>(bitmap, dimension_dip_, normalized_),
130 gfx::Size(dimension_dip_, dimension_dip_));
Qiang Xu139f86532018-03-06 22:14:43131 icon.EnsureRepsForSupportedScales();
Qiang Xu139f86532018-03-06 22:14:43132 std::move(set_icon_callback_).Run(icon);
133}
134
135void IconDecodeRequest::OnDecodeImageFailed() {
136 DCHECK_CURRENTLY_ON(BrowserThread::UI);
137 DLOG(ERROR) << "Failed to decode an icon image.";
Vladislav Kaznacheev84de1c52018-11-01 14:38:58138 OnImageDecoded(SkBitmap());
Qiang Xu139f86532018-03-06 22:14:43139}
140
Qiang Xu1b2a62ded2018-04-17 18:55:37141} // namespace arc