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" | |
|
xiyuan
2017/04/20 17:52:46
remove ?
khmel
2017/04/20 18:01:11
Done.
| |
| 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(ExtensionAppIconDelegate* delegate, | |
| 70 content::BrowserContext* context, | |
| 71 Closure close_callback, | |
| 72 const std::string& app_id, | |
| 73 int resource_size_in_dip) | |
| 74 : delegate_(delegate), | |
| 75 context_(context), | |
| 76 close_callback_(close_callback), | |
| 77 app_id_(app_id), | |
| 78 resource_size_in_dip_(resource_size_in_dip) { | |
| 79 DCHECK(delegate_); | |
| 80 DCHECK(context_); | |
| 81 DCHECK(!close_callback_.is_null()); | |
| 82 DCHECK_GE(resource_size_in_dip, 0); | |
| 83 Reload(); | |
| 84 } | |
| 85 | |
| 86 ExtensionAppIcon::~ExtensionAppIcon() { | |
| 87 close_callback_.Run(this); | |
| 88 } | |
| 89 | |
| 90 const extensions::Extension* ExtensionAppIcon::GetExtension() { | |
| 91 const extensions::Extension* extension = | |
| 92 extensions::ExtensionRegistry::Get(context_)->GetInstalledExtension( | |
| 93 app_id_); | |
| 94 return extension; | |
| 95 } | |
| 96 | |
| 97 bool ExtensionAppIcon::IsValid() const { | |
| 98 return icon_ && icon_->is_valid(); | |
| 99 } | |
| 100 | |
| 101 void ExtensionAppIcon::Reload() { | |
| 102 const extensions::Extension* extension = GetExtension(); | |
| 103 icon_ = base::MakeUnique<extensions::IconImage>( | |
| 104 context_, extension, extensions::IconsInfo::GetIcons(extension), | |
| 105 resource_size_in_dip_, extensions::util::GetDefaultAppIcon(), this); | |
| 106 UpdateIcon(); | |
| 107 } | |
| 108 | |
| 109 void ExtensionAppIcon::UpdateIcon() { | |
| 110 DCHECK(icon_); | |
| 111 | |
| 112 image_skia_ = icon_->image_skia(); | |
| 113 #if defined(OS_CHROMEOS) | |
| 114 extensions::util::MaybeApplyChromeBadge(context_, app_id_, &image_skia_); | |
| 115 #endif | |
| 116 | |
| 117 const bool enabled = extensions::util::IsAppLaunchable(app_id_, context_); | |
| 118 if (!enabled) | |
| 119 image_skia_ = CreateDisabledIcon(image_skia_); | |
| 120 | |
| 121 const extensions::Extension* extension = GetExtension(); | |
| 122 if (extension && extension->from_bookmark()) | |
| 123 image_skia_ = gfx::ImageSkia(new RoundedCornersImageSource(image_skia_), | |
| 124 image_skia_.size()); | |
| 125 | |
| 126 delegate_->OnIconUpdated(this); | |
| 127 } | |
| 128 | |
| 129 void ExtensionAppIcon::EnsureRepsForSupportedScales() { | |
| 130 DCHECK(icon_); | |
| 131 icon_->image_skia().EnsureRepsForSupportedScales(); | |
| 132 } | |
| 133 | |
| 134 void ExtensionAppIcon::OnExtensionIconImageChanged( | |
| 135 extensions::IconImage* icon) { | |
| 136 DCHECK_EQ(icon_.get(), icon); | |
| 137 UpdateIcon(); | |
| 138 } | |
| 139 | |
| 140 } // namespace extensions | |
| OLD | NEW |