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

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

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

Powered by Google App Engine
This is Rietveld 408576698