Chromium Code Reviews| Index: chrome/browser/extensions/extension_app_icon.cc |
| diff --git a/chrome/browser/extensions/extension_app_icon.cc b/chrome/browser/extensions/extension_app_icon.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85f92cef2f3c3adb0b0d1781eed266975efb25cb |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_app_icon.cc |
| @@ -0,0 +1,146 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/extension_app_icon.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "chrome/browser/extensions/extension_app_icon_delegate.h" |
| +#include "chrome/browser/extensions/extension_app_icon_service.h" |
| +#include "chrome/browser/extensions/extension_util.h" |
| +#include "extensions/browser/extension_registry.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 defined(OS_CHROMEOS) |
| +#include "chrome/browser/chromeos/extensions/gfx_utils.h" |
| +#endif |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +gfx::ImageSkia CreateDisabledIcon(const gfx::ImageSkia& icon) { |
| + const color_utils::HSL shift = {-1, 0, 0.6}; |
| + return gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon, shift); |
| +} |
| + |
| +// Rounds the corners of a given image. |
| +class RoundedCornersImageSource : public gfx::CanvasImageSource { |
| + public: |
| + explicit RoundedCornersImageSource(const gfx::ImageSkia& icon) |
| + : gfx::CanvasImageSource(icon.size(), false), icon_(icon) {} |
| + ~RoundedCornersImageSource() override {} |
| + |
| + 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_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RoundedCornersImageSource); |
| +}; |
| + |
| +} // namespace |
| + |
| +ExtensionAppIcon::ExtensionAppIcon( |
| + const base::WeakPtr<ExtensionAppIconService>& host, |
| + ExtensionAppIconDelegate* delegate, |
| + content::BrowserContext* context, |
| + const std::string& app_id, |
| + int resource_size_in_dip) |
| + : host_(host), |
| + delegate_(delegate), |
| + context_(context), |
| + app_id_(app_id), |
| + resource_size_in_dip_(resource_size_in_dip) { |
| + DCHECK(delegate_); |
| + DCHECK(context_); |
| + DCHECK_GE(resource_size_in_dip, 0); |
| + Reload(); |
| +} |
| + |
| +ExtensionAppIcon::~ExtensionAppIcon() { |
| + if (host_) |
| + host_->OnIconDestroyed(this); |
| +} |
| + |
| +const extensions::Extension* ExtensionAppIcon::GetExtension() { |
| + const extensions::Extension* extension = |
| + extensions::ExtensionRegistry::Get(context_)->GetInstalledExtension( |
| + app_id_); |
| + return extension; |
| +} |
| + |
| +bool ExtensionAppIcon::IsValid() const { |
| + return icon_ && icon_->is_valid(); |
| +} |
| + |
| +void ExtensionAppIcon::Reload() { |
| + const extensions::Extension* extension = GetExtension(); |
| + icon_ = base::MakeUnique<extensions::IconImage>( |
| + context_, extension, extensions::IconsInfo::GetIcons(extension), |
| + resource_size_in_dip_, extensions::util::GetDefaultAppIcon(), this); |
| + UpdateIcon(); |
| +} |
| + |
| +void ExtensionAppIcon::UpdateIcon() { |
| + if (!icon_) |
| + return; |
| + |
| + gfx::ImageSkia image = icon_->image_skia(); |
| +#if defined(OS_CHROMEOS) |
| + extensions::util::MaybeApplyChromeBadge(context_, app_id_, &image); |
| +#endif |
| + |
| + const bool enabled = extensions::util::IsAppLaunchable(app_id_, context_); |
| + if (!enabled) |
| + image = CreateDisabledIcon(image); |
| + |
| + const extensions::Extension* extension = GetExtension(); |
| + if (extension && extension->from_bookmark()) |
| + image = gfx::ImageSkia(new RoundedCornersImageSource(image), image.size()); |
| + |
| + delegate_->UpdateIcon(this, image); |
| +} |
| + |
| +void ExtensionAppIcon::EnsureRepsForSupportedScales() { |
| + if (icon_) |
| + icon_->image_skia().EnsureRepsForSupportedScales(); |
| +} |
| + |
| +void ExtensionAppIcon::OnExtensionIconImageChanged( |
| + extensions::IconImage* image) { |
| + DCHECK_EQ(icon_.get(), image); |
| + UpdateIcon(); |
| +} |
| + |
| +void ExtensionAppIcon::OnExtensionIconImageDestroyed( |
| + extensions::IconImage* image) { |
| + // We own this object. Do nothing. |
|
xiyuan
2017/04/18 20:43:00
How about just get rid of it?
khmel
2017/04/20 00:13:37
True, this is optional callback.
|
| +} |
| + |
| +} // namespace extensions |