1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/chrome_app_icon.h"
#include <algorithm>
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/chrome_app_icon_delegate.h"
#include "chrome/browser/extensions/extension_util.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia_operations.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/extensions/gfx_utils.h"
#endif
namespace extensions {
namespace {
// Rounds the corners of a given image.
// TODO(khmel): avoid sub-classing CanvasImageSource.
class RoundedCornersImageSource : public gfx::CanvasImageSource {
public:
explicit RoundedCornersImageSource(const gfx::ImageSkia& icon)
: gfx::CanvasImageSource(icon.size()), icon_(icon) {}
RoundedCornersImageSource(const RoundedCornersImageSource&) = delete;
RoundedCornersImageSource& operator=(const RoundedCornersImageSource&) =
delete;
~RoundedCornersImageSource() override = default;
private:
// gfx::CanvasImageSource overrides:
void Draw(gfx::Canvas* canvas) override {
// The radius used to round the app icon, based on 2 pixel per 48 pixels
// icon size.
const int rounding_radius =
std::max<int>(std::round(2.0 * icon_.width() / 48.0), 1);
canvas->DrawImageInt(icon_, 0, 0);
cc::PaintFlags masking_flags;
masking_flags.setBlendMode(SkBlendMode::kDstIn);
canvas->SaveLayerWithFlags(masking_flags);
cc::PaintFlags mask_flags;
mask_flags.setAntiAlias(true);
mask_flags.setColor(SK_ColorWHITE);
canvas->DrawRoundRect(gfx::Rect(icon_.width(), icon_.height()),
rounding_radius, mask_flags);
canvas->Restore();
}
gfx::ImageSkia icon_;
};
} // namespace
// static
void ChromeAppIcon::ApplyEffects(int resource_size_in_dip,
const ResizeFunction& resize_function,
bool app_launchable,
bool rounded_corners,
Badge badge_type,
gfx::ImageSkia* image_skia) {
if (!resize_function.is_null()) {
resize_function.Run(gfx::Size(resource_size_in_dip, resource_size_in_dip),
image_skia);
}
if (!app_launchable) {
constexpr color_utils::HSL shift = {-1, 0, 0.6};
*image_skia =
gfx::ImageSkiaOperations::CreateHSLShiftedImage(*image_skia, shift);
}
#if BUILDFLAG(IS_CHROMEOS)
// Badge should be added after graying out the icon to have a crisp look.
if (badge_type != Badge::kNone) {
util::ApplyBadge(image_skia, badge_type);
}
#endif
if (rounded_corners) {
*image_skia =
gfx::ImageSkia(std::make_unique<RoundedCornersImageSource>(*image_skia),
image_skia->size());
}
}
ChromeAppIcon::ChromeAppIcon(ChromeAppIconDelegate* delegate,
content::BrowserContext* browser_context,
DestroyedCallback destroyed_callback,
const std::string& app_id,
int resource_size_in_dip,
const ResizeFunction& resize_function)
: delegate_(delegate),
browser_context_(browser_context),
destroyed_callback_(std::move(destroyed_callback)),
app_id_(app_id),
resource_size_in_dip_(resource_size_in_dip),
resize_function_(resize_function) {
DCHECK(delegate_);
DCHECK(browser_context_);
DCHECK(!destroyed_callback_.is_null());
DCHECK_GE(resource_size_in_dip, 0);
Reload();
}
ChromeAppIcon::~ChromeAppIcon() {
std::move(destroyed_callback_).Run(this);
}
const Extension* ChromeAppIcon::GetExtension() {
return ExtensionRegistry::Get(browser_context_)
->GetInstalledExtension(app_id_);
}
void ChromeAppIcon::Reload() {
const Extension* extension = GetExtension();
const gfx::ImageSkia default_icon = extension && extension->is_app()
? util::GetDefaultAppIcon()
: util::GetDefaultExtensionIcon();
icon_ = std::make_unique<IconImage>(
browser_context_, extension,
extension ? IconsInfo::GetIcons(extension) : ExtensionIconSet(),
resource_size_in_dip_, !resize_function_.is_null(), default_icon, this);
UpdateIcon();
}
bool ChromeAppIcon::IsValid() const {
DCHECK(icon_);
return icon_->is_valid();
}
void ChromeAppIcon::UpdateIcon() {
DCHECK(icon_);
image_skia_ = icon_->image_skia();
Badge badge_type = Badge::kNone;
bool app_launchable = util::IsAppLaunchable(app_id_, browser_context_);
#if BUILDFLAG(IS_CHROMEOS)
has_chrome_badge_ = util::ShouldApplyChromeBadge(browser_context_, app_id_);
if (!app_launchable) {
badge_type = Badge::kBlocked;
} else if (has_chrome_badge_) {
badge_type = Badge::kChrome;
}
#endif
ApplyEffects(resource_size_in_dip_, resize_function_, app_launchable,
/*rounded_corners=*/false, badge_type, &image_skia_);
delegate_->OnIconUpdated(this);
}
void ChromeAppIcon::OnExtensionIconImageChanged(IconImage* icon) {
DCHECK_EQ(icon_.get(), icon);
UpdateIcon();
}
} // namespace extensions
|