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..eba4a866f25e3781a32de3fee8cfb2b1ee03c214 |
--- /dev/null |
+++ b/chrome/browser/ui/app_list/search/launcher_search/extension_badged_icon_image.cc |
@@ -0,0 +1,205 @@ |
+// 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/chromeos/launcher_search_provider/test_util.h" |
+#include "chrome/browser/extensions/extension_util.h" |
+#include "extensions/browser/event_router.h" |
+#include "extensions/browser/image_loader.h" |
+#include "extensions/common/api/test.h" |
+#include "extensions/common/constants.h" |
+#include "extensions/common/file_util.h" |
+#include "extensions/common/manifest_handlers/icons_handler.h" |
+#include "extensions/common/value_builder.h" |
+#include "skia/ext/image_operations.h" |
+#include "ui/base/webui/web_ui_util.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" |
+ |
+using extensions::DictionaryBuilder; |
+using chromeos::launcher_search_provider::TestUtil; |
+ |
+namespace { |
+ |
+const int kTruncatedIconUrlMaxSize = 100; |
+const char kUpdateFunctionName[] = "ExtensionBadgedIconImage::Update"; |
+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), |
+ profile_(profile), |
+ 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()) { |
+ 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); |
Matt Giuca
2015/04/30 01:34:08
I think you could make this testable (without havi
yawano
2015/04/30 03:38:52
Okay, I'll extract it as a method. I'm also going
|
+ 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. " + |
+ 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. " + |
+ GetTruncatedIconUrl(kTruncatedIconUrlMaxSize)); |
+ } else { |
+ 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()); |
+ TestUtil::SendMessageToTestExtension( |
+ profile_, DictionaryBuilder() |
+ .Set("functionName", kUpdateFunctionName) |
+ .Set("dataURL", webui::GetBitmapDataUrl( |
+ extension_icon_image_.image_skia() |
+ .GetRepresentation(1.0) |
+ .sk_bitmap())) |
+ .ToJSON()); |
+ 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); |
+ |
+ // Send data url of new icon image for testing. |
+ TestUtil::SendMessageToTestExtension( |
+ profile_, DictionaryBuilder() |
+ .Set("functionName", kUpdateFunctionName) |
+ .Set("dataURL", webui::GetBitmapDataUrl( |
+ badged_icon_image.GetRepresentation(1.0) |
+ .sk_bitmap())) |
+ .ToJSON()); |
+} |
+ |
+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 |