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

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

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: rebase Created 3 years, 7 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/chrome_app_icon.h"
6
7 #include <algorithm>
8
9 #include "chrome/browser/extensions/chrome_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 // Rounds the corners of a given image.
27 class RoundedCornersImageSource : public gfx::CanvasImageSource {
28 public:
29 explicit RoundedCornersImageSource(const gfx::ImageSkia& icon)
30 : gfx::CanvasImageSource(icon.size(), false), icon_(icon) {}
31 ~RoundedCornersImageSource() override {}
32
33 private:
34 // gfx::CanvasImageSource overrides:
35 void Draw(gfx::Canvas* canvas) override {
36 // The radius used to round the app icon, based on 2 pixel per 48 pixels
37 // icon size.
38 const int rounding_radius =
39 std::max<int>(std::round(2.0 * icon_.width() / 48.0), 1);
40
41 canvas->DrawImageInt(icon_, 0, 0);
42
43 cc::PaintFlags masking_flags;
44 masking_flags.setBlendMode(SkBlendMode::kDstIn);
45 canvas->SaveLayerWithFlags(masking_flags);
46
47 cc::PaintFlags mask_flags;
48 mask_flags.setAntiAlias(true);
49 mask_flags.setColor(SK_ColorWHITE);
50 canvas->DrawRoundRect(gfx::Rect(icon_.width(), icon_.height()),
51 rounding_radius, mask_flags);
52
53 canvas->Restore();
54 }
55
56 gfx::ImageSkia icon_;
57
58 DISALLOW_COPY_AND_ASSIGN(RoundedCornersImageSource);
59 };
60
61 } // namespace
62
63 ChromeAppIcon::ChromeAppIcon(ChromeAppIconDelegate* delegate,
64 content::BrowserContext* browser_context,
65 DestroyedCallback destroyed_callback,
66 const std::string& app_id,
67 int resource_size_in_dip)
68 : delegate_(delegate),
69 browser_context_(browser_context),
70 destroyed_callback_(std::move(destroyed_callback)),
71 app_id_(app_id),
72 resource_size_in_dip_(resource_size_in_dip) {
73 DCHECK(delegate_);
74 DCHECK(browser_context_);
75 DCHECK(!destroyed_callback_.is_null());
76 DCHECK_GE(resource_size_in_dip, 0);
77 Reload();
78 }
79
80 ChromeAppIcon::~ChromeAppIcon() {
81 std::move(destroyed_callback_).Run(this);
82 }
83
84 const Extension* ChromeAppIcon::GetExtension() {
85 return ExtensionRegistry::Get(browser_context_)
86 ->GetInstalledExtension(app_id_);
87 }
88
89 bool ChromeAppIcon::IsValid() const {
90 return icon_ && icon_->is_valid();
91 }
92
93 void ChromeAppIcon::Reload() {
msw 2017/05/08 19:59:53 nit: function definition order should match declar
khmel 2017/05/09 00:05:04 Done.
94 const Extension* extension = GetExtension();
95 icon_ = base::MakeUnique<IconImage>(
96 browser_context_, extension, IconsInfo::GetIcons(extension),
97 resource_size_in_dip_, util::GetDefaultAppIcon(), this);
98 UpdateIcon();
99 }
100
101 void ChromeAppIcon::UpdateIcon() {
102 DCHECK(icon_);
103
104 image_skia_ = icon_->image_skia();
105 #if defined(OS_CHROMEOS)
106 util::MaybeApplyChromeBadge(browser_context_, app_id_, &image_skia_);
107 #endif
108
109 if (!util::IsAppLaunchable(app_id_, browser_context_)) {
110 const color_utils::HSL shift = {-1, 0, 0.6};
111 image_skia_ =
112 gfx::ImageSkiaOperations::CreateHSLShiftedImage(image_skia_, shift);
113 }
114
115 const Extension* extension = GetExtension();
116 if (extension && extension->from_bookmark())
117 image_skia_ = gfx::ImageSkia(new RoundedCornersImageSource(image_skia_),
118 image_skia_.size());
119
120 delegate_->OnIconUpdated(this);
121 }
122
123 void ChromeAppIcon::EnsureRepsForSupportedScales() {
124 DCHECK(icon_);
125 icon_->image_skia().EnsureRepsForSupportedScales();
126 }
127
128 void ChromeAppIcon::OnExtensionIconImageChanged(IconImage* icon) {
129 DCHECK_EQ(icon_.get(), icon);
130 UpdateIcon();
131 }
132
133 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698