Chromium Code Reviews| Index: chrome/browser/ui/app_list/search/launcher_search/extension_badged_icon_image.cc |
| diff --git a/chrome/browser/ui/app_list/search/launcher_search/extension_badged_icon_image.cc b/chrome/browser/ui/app_list/search/launcher_search/extension_badged_icon_image.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f915644182c8e0b3a56621ecfbcf4e461d3506d1 |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/search/launcher_search/extension_badged_icon_image.cc |
| @@ -0,0 +1,178 @@ |
| +// Copyright 2015 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/ui/app_list/search/launcher_search/extension_badged_icon_image.h" |
| + |
| +#include "base/strings/string_util.h" |
| +#include "chrome/browser/chromeos/launcher_search_provider/error_reporter.h" |
| +#include "chrome/browser/extensions/extension_util.h" |
| +#include "extensions/browser/image_loader.h" |
| +#include "extensions/common/constants.h" |
| +#include "extensions/common/file_util.h" |
| +#include "extensions/common/manifest_handlers/icons_handler.h" |
| +#include "skia/ext/image_operations.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/geometry/size.h" |
| +#include "ui/gfx/image/canvas_image_source.h" |
| +#include "ui/gfx/image/image_skia_operations.h" |
| + |
| +namespace { |
| + |
| +const int kTruncatedIconUrlMaxSize = 100; |
| +const char kWarningMessagePrefix[] = |
| + "[chrome.launcherSearchProvider.setSearchResults] "; |
| + |
| +class BadgedIconSource : public gfx::CanvasImageSource { |
| + public: |
| + BadgedIconSource(const gfx::ImageSkia& custom_icon, |
| + const gfx::ImageSkia& extension_icon, |
| + const gfx::Size& icon_size) |
| + : CanvasImageSource(icon_size, false), custom_icon_(custom_icon) { |
| + // Badged icon size is 2/3 of custom icon. |
| + int badge_dimension = size().width() * 2 / 3; |
| + gfx::Size badge_size = gfx::Size(badge_dimension, badge_dimension); |
| + resized_extension_icon_ = gfx::ImageSkiaOperations::CreateResizedImage( |
| + extension_icon, skia::ImageOperations::ResizeMethod::RESIZE_GOOD, |
| + badge_size); |
| + } |
| + |
| + void Draw(gfx::Canvas* canvas) override { |
| + canvas->DrawImageInt(custom_icon_, 0, 0); |
| + canvas->DrawImageInt( |
| + resized_extension_icon_, |
| + size().width() - resized_extension_icon_.size().width(), |
| + size().height() - resized_extension_icon_.size().height()); |
| + } |
| + |
| + private: |
| + gfx::ImageSkia custom_icon_; |
| + gfx::ImageSkia resized_extension_icon_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BadgedIconSource); |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace app_list { |
| + |
| +ExtensionBadgedIconImage::ExtensionBadgedIconImage( |
| + const GURL& icon_url, |
| + Profile* profile, |
| + const extensions::Extension* extension, |
| + const int icon_dimension, |
| + const chromeos::launcher_search_provider::ErrorReporter& error_reporter) |
| + : extension_icon_image_(profile, |
| + extension, |
| + extensions::IconsInfo::GetIcons(extension), |
| + icon_dimension, |
| + extensions::util::GetDefaultExtensionIcon(), |
| + this), |
| + icon_url_(icon_url), |
| + icon_size_(icon_dimension, icon_dimension), |
| + error_reporter_(error_reporter), |
| + weak_ptr_factory_(this) { |
| + // If valid icon_url is provided as chrome-extension scheme with the host of |
| + // |extension|, load custom icon. |
| + if (!icon_url.is_empty()) { |
|
Matt Giuca
2015/04/24 04:26:46
Could you move this entire function *except* for U
yawano
2015/05/01 06:09:55
Moved this to LoadResources. We needed to extract
|
| + if (icon_url.is_valid() && |
| + icon_url.SchemeIs(extensions::kExtensionScheme) && |
| + icon_url.host() == extension->id()) { |
| + const base::FilePath& file_path = |
| + extensions::file_util::ExtensionURLToRelativeFilePath(icon_url); |
| + const extensions::ExtensionResource& resource = |
| + extension->GetResource(file_path); |
| + |
| + // Load image with ImageLoader. ImageLoader resizes image to |icon_size|. |
| + std::vector<extensions::ImageLoader::ImageRepresentation> info_list; |
| + info_list.push_back(extensions::ImageLoader::ImageRepresentation( |
| + resource, extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
| + icon_size_, ui::SCALE_FACTOR_100P)); |
| + extensions::ImageLoader::Get(profile)->LoadImagesAsync( |
| + extension, info_list, |
| + base::Bind(&ExtensionBadgedIconImage::OnCustomIconLoaded, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } else { |
| + error_reporter_.Warn( |
| + std::string(kWarningMessagePrefix) + |
| + "Invalid url is provided. Only chrome-extension:// url is valid. " + |
|
Matt Giuca
2015/04/24 04:26:46
This message is a bit hard to read.
What about "I
yawano
2015/05/01 06:09:54
Done.
|
| + GetTruncatedIconUrl(kTruncatedIconUrlMaxSize)); |
| + } |
| + } |
| + |
| + Update(); |
| +} |
| + |
| +ExtensionBadgedIconImage::~ExtensionBadgedIconImage() { |
| +} |
| + |
| +void ExtensionBadgedIconImage::AddObserver(Observer* observer) { |
| + observers_.insert(observer); |
| +} |
| + |
| +void ExtensionBadgedIconImage::RemoveObserver(Observer* observer) { |
| + observers_.erase(observer); |
| +} |
| + |
| +void ExtensionBadgedIconImage::OnExtensionIconImageChanged( |
| + extensions::IconImage* image) { |
| + Update(); |
| +} |
| + |
| +const gfx::ImageSkia& ExtensionBadgedIconImage::GetIconImage() const { |
| + return badged_icon_image_; |
| +} |
| + |
| +void ExtensionBadgedIconImage::OnCustomIconLoaded(const gfx::Image& image) { |
| + if (image.IsEmpty()) { |
| + error_reporter_.Warn(std::string(kWarningMessagePrefix) + |
| + "Failed to load icon url. " + |
|
Matt Giuca
2015/04/24 04:26:46
nit: "Failed to load icon URL: "
yawano
2015/05/01 06:09:54
Done.
|
| + GetTruncatedIconUrl(kTruncatedIconUrlMaxSize)); |
| + } else { |
|
Matt Giuca
2015/04/24 04:26:46
No else; just return early in the error case.
yawano
2015/05/01 06:09:55
Done.
|
| + custom_icon_image_ = image.AsImageSkia(); |
| + Update(); |
| + } |
| +} |
| + |
| +void ExtensionBadgedIconImage::Update() { |
| + // If extension_icon_image is not available, return immediately. |
| + if (extension_icon_image_.image_skia().isNull()) |
| + return; |
| + |
| + // When custom icon image is not available, simply use extension icon image |
| + // without badge. |
| + if (custom_icon_image_.isNull()) { |
| + SetIconImage(extension_icon_image_.image_skia()); |
| + return; |
| + } |
| + |
| + // Create badged icon image. |
| + gfx::ImageSkia badged_icon_image( |
| + new BadgedIconSource(custom_icon_image_, |
| + extension_icon_image_.image_skia(), icon_size_), |
| + icon_size_); |
| + SetIconImage(badged_icon_image); |
| +} |
| + |
| +void ExtensionBadgedIconImage::SetIconImage(const gfx::ImageSkia& icon_image) { |
| + badged_icon_image_ = icon_image; |
| + |
| + for (auto* observer : observers_) { |
| + observer->OnIconImageChanged(this); |
| + } |
| +} |
| + |
| +std::string ExtensionBadgedIconImage::GetTruncatedIconUrl( |
| + const uint32 max_size) { |
| + CHECK(max_size > 3); |
| + |
| + if (icon_url_.spec().size() <= max_size) |
| + return icon_url_.spec(); |
| + |
| + std::string truncated_url; |
| + base::TruncateUTF8ToByteSize(icon_url_.spec(), max_size - 3, &truncated_url); |
| + truncated_url.append("..."); |
| + return truncated_url; |
| +} |
| + |
| +} // namespace app_list |