Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 if (!icon_) | |
| 112 return; | |
| 113 | |
| 114 gfx::ImageSkia image = icon_->image_skia(); | |
| 115 #if defined(OS_CHROMEOS) | |
| 116 extensions::util::MaybeApplyChromeBadge(context_, app_id_, &image); | |
| 117 #endif | |
| 118 | |
| 119 const bool enabled = extensions::util::IsAppLaunchable(app_id_, context_); | |
| 120 if (!enabled) | |
| 121 image = CreateDisabledIcon(image); | |
| 122 | |
| 123 const extensions::Extension* extension = GetExtension(); | |
| 124 if (extension && extension->from_bookmark()) | |
| 125 image = gfx::ImageSkia(new RoundedCornersImageSource(image), image.size()); | |
| 126 | |
| 127 delegate_->UpdateIcon(this, image); | |
| 128 } | |
| 129 | |
| 130 void ExtensionAppIcon::EnsureRepsForSupportedScales() { | |
| 131 if (icon_) | |
| 132 icon_->image_skia().EnsureRepsForSupportedScales(); | |
| 133 } | |
| 134 | |
| 135 void ExtensionAppIcon::OnExtensionIconImageChanged( | |
| 136 extensions::IconImage* image) { | |
| 137 DCHECK_EQ(icon_.get(), image); | |
| 138 UpdateIcon(); | |
| 139 } | |
| 140 | |
| 141 void ExtensionAppIcon::OnExtensionIconImageDestroyed( | |
| 142 extensions::IconImage* image) { | |
| 143 // 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.
| |
| 144 } | |
| 145 | |
| 146 } // namespace extensions | |
| OLD | NEW |