Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(668)

Side by Side Diff: chrome/browser/extensions/extension_app_icon.cc

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: nits Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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/extensions/extension_app_icon.h"
6
7 #include <algorithm>
8
9 #include "chrome/browser/extensions/extension_app_icon_delegate.h"
10 #include "chrome/browser/extensions/extension_util.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/common/manifest_handlers/icons_handler.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/image/canvas_image_source.h"
16 #include "ui/gfx/image/image_skia_operations.h"
17
18 #if defined(OS_CHROMEOS)
19 #include "chrome/browser/chromeos/extensions/gfx_utils.h"
20 #endif
21
22 namespace extensions {
23
24 namespace {
25
26 gfx::ImageSkia CreateDisabledIcon(const gfx::ImageSkia& icon) {
27 const color_utils::HSL shift = {-1, 0, 0.6};
28 return gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon, shift);
29 }
30
31 // Rounds the corners of a given image.
32 class RoundedCornersImageSource : public gfx::CanvasImageSource {
33 public:
34 explicit RoundedCornersImageSource(const gfx::ImageSkia& icon)
35 : gfx::CanvasImageSource(icon.size(), false), icon_(icon) {}
36 ~RoundedCornersImageSource() override {}
37
38 private:
39 // gfx::CanvasImageSource overrides:
40 void Draw(gfx::Canvas* canvas) override {
41 // The radius used to round the app icon, based on 2 pixel per 48 pixels
42 // icon size.
43 const int rounding_radius =
44 std::max((int)std::round(2.0 * icon_.width() / 48.0), 1);
45
46 canvas->DrawImageInt(icon_, 0, 0);
47
48 cc::PaintFlags masking_flags;
49 masking_flags.setBlendMode(SkBlendMode::kDstIn);
50 canvas->SaveLayerWithFlags(masking_flags);
51
52 cc::PaintFlags mask_flags;
53 mask_flags.setAntiAlias(true);
54 mask_flags.setColor(SK_ColorWHITE);
55 canvas->DrawRoundRect(gfx::Rect(icon_.width(), icon_.height()),
56 rounding_radius, mask_flags);
57
58 canvas->Restore();
59 }
60
61 gfx::ImageSkia icon_;
62
63 DISALLOW_COPY_AND_ASSIGN(RoundedCornersImageSource);
64 };
65
66 } // namespace
67
68 ExtensionAppIcon::ExtensionAppIcon(ExtensionAppIconDelegate* delegate,
69 content::BrowserContext* context,
70 DestroyCallback destroy_callback,
71 const std::string& app_id,
72 int resource_size_in_dip)
73 : delegate_(delegate),
74 context_(context),
75 destroy_callback_(destroy_callback),
76 app_id_(app_id),
77 resource_size_in_dip_(resource_size_in_dip) {
78 DCHECK(delegate_);
79 DCHECK(context_);
80 DCHECK(!destroy_callback_.is_null());
81 DCHECK_GE(resource_size_in_dip, 0);
82 Reload();
83 }
84
85 ExtensionAppIcon::~ExtensionAppIcon() {
86 destroy_callback_.Run(this);
87 }
88
89 const extensions::Extension* ExtensionAppIcon::GetExtension() {
90 const extensions::Extension* extension =
91 extensions::ExtensionRegistry::Get(context_)->GetInstalledExtension(
92 app_id_);
93 return extension;
94 }
95
96 bool ExtensionAppIcon::IsValid() const {
97 return icon_ && icon_->is_valid();
98 }
99
100 void ExtensionAppIcon::Reload() {
101 const extensions::Extension* extension = GetExtension();
102 icon_ = base::MakeUnique<extensions::IconImage>(
103 context_, extension, extensions::IconsInfo::GetIcons(extension),
104 resource_size_in_dip_, extensions::util::GetDefaultAppIcon(), this);
105 UpdateIcon();
106 }
107
108 void ExtensionAppIcon::UpdateIcon() {
109 DCHECK(icon_);
110
111 image_skia_ = icon_->image_skia();
112 #if defined(OS_CHROMEOS)
113 extensions::util::MaybeApplyChromeBadge(context_, app_id_, &image_skia_);
114 #endif
115
116 const bool enabled = extensions::util::IsAppLaunchable(app_id_, context_);
117 if (!enabled)
118 image_skia_ = CreateDisabledIcon(image_skia_);
119
120 const extensions::Extension* extension = GetExtension();
121 if (extension && extension->from_bookmark())
122 image_skia_ = gfx::ImageSkia(new RoundedCornersImageSource(image_skia_),
123 image_skia_.size());
124
125 delegate_->OnIconUpdated(this);
126 }
127
128 void ExtensionAppIcon::EnsureRepsForSupportedScales() {
129 DCHECK(icon_);
130 icon_->image_skia().EnsureRepsForSupportedScales();
131 }
132
133 void ExtensionAppIcon::OnExtensionIconImageChanged(
134 extensions::IconImage* icon) {
135 DCHECK_EQ(icon_.get(), icon);
136 UpdateIcon();
137 }
138
139 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698