1 // 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
5 #include "chrome/browser/chromeos/arc/icon_decode_request.h"
6
7 #include <memory>
8 #include <utility>
9 #include <vector>
10
11 #include "chrome/browser/ui/app_list/md_icon_normalizer.h"
12 #include "chrome/grit/component_extension_resources.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/base/resource/scale_factor.h"
16 #include "ui/gfx/codec/png_codec.h"
17 #include "ui/gfx/image/image_skia.h"
18 #include "ui/gfx/image/image_skia_operations.h"
19 #include "ui/gfx/image/image_skia_rep.h"
20 #include "ui/gfx/image/image_skia_source.h"
21
22 using content::BrowserThread;
23
24 namespace arc {
25
26 namespace {
27
28 bool disable_safe_decoding_for_testing = false;
29
30 class IconSource : public gfx::ImageSkiaSource {
31 public:
32 IconSource(const SkBitmap& bitmap, int dimension_dip, bool normalize);
33 ~IconSource() override = default;
34
35 private:
36 gfx::ImageSkiaRep GetImageForScale(float scale) override;
37
38 const SkBitmap bitmap_;
39 const int dimension_dip_;
40 const bool normalize_;
41
42 DISALLOW_COPY_AND_ASSIGN(IconSource);
43 };
44
IconSource(const SkBitmap & bitmap,int dimension_dip,bool normalize)45 IconSource::IconSource(const SkBitmap& bitmap,
46 int dimension_dip,
47 bool normalize)
48 : bitmap_(bitmap), dimension_dip_(dimension_dip), normalize_(normalize) {}
49
GetImageForScale(float scale)50 gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) {
51 DCHECK_CURRENTLY_ON(BrowserThread::UI);
52
53 const int dimension_px = static_cast<int>(dimension_dip_ * scale + 0.5);
54 if (bitmap_.isNull()) {
55 const int resource_id =
56 dimension_px <= 32 ? IDR_ARC_SUPPORT_ICON_32 : IDR_ARC_SUPPORT_ICON_192;
57 const gfx::ImageSkia* resource_image =
58 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
59 const gfx::ImageSkia resized_image =
60 gfx::ImageSkiaOperations::CreateResizedImage(
61 *resource_image, skia::ImageOperations::RESIZE_LANCZOS3,
62 gfx::Size(dimension_dip_, dimension_dip_));
63 return resized_image.GetRepresentation(scale);
64 }
65
66 SkBitmap resized_bitmap;
67 if (normalize_) {
68 resized_bitmap = bitmap_;
69 const gfx::Size size_px(dimension_px, dimension_px);
70 const gfx::Size padding_px =
71 app_list::GetMdIconPadding(resized_bitmap, size_px);
72 app_list::MaybeResizeAndPad(size_px, padding_px, &resized_bitmap);
73 } else {
74 resized_bitmap = skia::ImageOperations::Resize(
75 bitmap_, skia::ImageOperations::RESIZE_LANCZOS3, dimension_px,
76 dimension_px);
77 }
78 return gfx::ImageSkiaRep(resized_bitmap, scale);
79 }
80
81 } // namespace
82
83 // static
DisableSafeDecodingForTesting()84 void IconDecodeRequest::DisableSafeDecodingForTesting() {
85 disable_safe_decoding_for_testing = true;
86 }
87
IconDecodeRequest(SetIconCallback set_icon_callback,int dimension_dip)88 IconDecodeRequest::IconDecodeRequest(SetIconCallback set_icon_callback,
89 int dimension_dip)
90 : set_icon_callback_(std::move(set_icon_callback)),
91 dimension_dip_(dimension_dip) {}
92
93 IconDecodeRequest::~IconDecodeRequest() = default;
94
StartWithOptions(const std::vector<uint8_t> & image_data)95 void IconDecodeRequest::StartWithOptions(
96 const std::vector<uint8_t>& image_data) {
97 if (disable_safe_decoding_for_testing) {
98 if (image_data.empty()) {
99 OnDecodeImageFailed();
100 return;
101 }
102 SkBitmap bitmap;
103 if (!gfx::PNGCodec::Decode(
104 reinterpret_cast<const unsigned char*>(image_data.data()),
105 image_data.size(), &bitmap)) {
106 OnDecodeImageFailed();
107 return;
108 }
109 OnImageDecoded(bitmap);
110 return;
111 }
112 ImageDecoder::StartWithOptions(this, image_data, ImageDecoder::DEFAULT_CODEC,
113 true, gfx::Size());
114 }
115
OnImageDecoded(const SkBitmap & bitmap)116 void IconDecodeRequest::OnImageDecoded(const SkBitmap& bitmap) {
117 DCHECK_CURRENTLY_ON(BrowserThread::UI);
118 const gfx::ImageSkia icon(
119 std::make_unique<IconSource>(bitmap, dimension_dip_, normalized_),
120 gfx::Size(dimension_dip_, dimension_dip_));
121 icon.EnsureRepsForSupportedScales();
122 std::move(set_icon_callback_).Run(icon);
123 }
124
OnDecodeImageFailed()125 void IconDecodeRequest::OnDecodeImageFailed() {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127 DLOG(ERROR) << "Failed to decode an icon image.";
128 OnImageDecoded(SkBitmap());
129 }
130
131 } // namespace arc
132