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