Index: chrome/browser/ui/app_list/search/arc/arc_playstore_search_result.cc |
diff --git a/chrome/browser/ui/app_list/search/arc/arc_playstore_search_result.cc b/chrome/browser/ui/app_list/search/arc/arc_playstore_search_result.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..59756f4d098916b1252959b09b3027c5a66fa1fa |
--- /dev/null |
+++ b/chrome/browser/ui/app_list/search/arc/arc_playstore_search_result.cc |
@@ -0,0 +1,197 @@ |
+// Copyright 2017 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/arc/arc_playstore_search_result.h" |
+ |
+#include <utility> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "chrome/browser/image_decoder.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/app_list/arc/arc_playstore_app_context_menu.h" |
+#include "chrome/grit/component_extension_resources.h" |
+#include "components/arc/arc_bridge_service.h" |
+#include "components/arc/arc_service_manager.h" |
+#include "components/arc/common/app.mojom.h" |
+#include "components/crx_file/id_util.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "ui/app_list/app_list_constants.h" |
+#include "ui/app_list/vector_icons.h" |
+#include "ui/gfx/codec/png_codec.h" |
+#include "ui/gfx/geometry/size.h" |
+#include "ui/gfx/image/image_skia_operations.h" |
+#include "ui/gfx/image/image_skia_rep.h" |
+#include "ui/gfx/image/image_skia_source.h" |
+#include "ui/gfx/paint_vector_icon.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+// The id prefix to identify a Play Store search result. |
+constexpr char kPlayAppPrefix[] = "play://"; |
+} // namespace |
+ |
+namespace app_list { |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// IconSource |
+ |
+class IconSource : public gfx::ImageSkiaSource { |
+ public: |
+ IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip); |
xiyuan
2017/06/23 19:57:38
Prefer to have only one constructor and add a SetD
Jiaquan He
2017/06/23 21:10:32
Done.
|
+ explicit IconSource(int resource_size_in_dip); |
+ ~IconSource() override = default; |
+ |
+ private: |
+ gfx::ImageSkiaRep GetImageForScale(float scale) override; |
+ |
+ const int resource_size_in_dip_; |
+ std::unique_ptr<gfx::ImageSkia> decoded_icon_; |
xiyuan
2017/06/23 19:57:38
std::unique_ptr<> is not necessary. We can test em
Jiaquan He
2017/06/23 21:10:32
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(IconSource); |
+}; |
+ |
+IconSource::IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip) |
+ : resource_size_in_dip_(resource_size_in_dip) { |
+ decoded_icon_ = base::MakeUnique<gfx::ImageSkia>(); |
+ decoded_icon_->AddRepresentation(gfx::ImageSkiaRep( |
+ decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P))); |
+} |
+ |
+IconSource::IconSource(int resource_size_in_dip) |
+ : resource_size_in_dip_(resource_size_in_dip) {} |
+ |
+gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ // We use the icon if it was decoded successfully, otherwise use the default |
+ // ARC icon. |
+ const gfx::ImageSkia* icon_to_scale; |
+ if (decoded_icon_ != nullptr) { |
+ icon_to_scale = decoded_icon_.get(); |
+ } else { |
+ int resource_id = |
+ scale >= 1.5f ? IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; |
+ icon_to_scale = |
+ ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); |
+ } |
+ DCHECK(icon_to_scale); |
+ |
+ gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( |
+ *icon_to_scale, skia::ImageOperations::RESIZE_BEST, |
+ gfx::Size(resource_size_in_dip_, resource_size_in_dip_)); |
+ return resized_image.GetRepresentation(scale); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// IconDecodeRequest |
+ |
+class ArcPlayStoreSearchResult::IconDecodeRequest |
+ : public ImageDecoder::ImageRequest { |
+ public: |
+ explicit IconDecodeRequest(ArcPlayStoreSearchResult* search_result) |
+ : search_result_(search_result) {} |
+ ~IconDecodeRequest() override = default; |
+ |
+ // ImageDecoder::ImageRequest overrides. |
+ void OnImageDecoded(const SkBitmap& bitmap) override { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ gfx::Size resource_size(app_list::kGridIconDimension, |
+ app_list::kGridIconDimension); |
+ gfx::ImageSkia icon = gfx::ImageSkia( |
+ new IconSource(bitmap, app_list::kGridIconDimension), resource_size); |
+ icon.EnsureRepsForSupportedScales(); |
+ |
+ search_result_->SetIcon(icon); |
+ } |
+ |
+ void OnDecodeImageFailed() override { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ DLOG(ERROR) << "Failed to decode an app icon image."; |
+ |
+ gfx::Size resource_size(app_list::kGridIconDimension, |
+ app_list::kGridIconDimension); |
+ gfx::ImageSkia icon = gfx::ImageSkia( |
+ new IconSource(app_list::kGridIconDimension), resource_size); |
+ icon.EnsureRepsForSupportedScales(); |
+ |
+ search_result_->SetIcon(icon); |
+ } |
+ |
+ private: |
+ // ArcPlayStoreSearchResult owns IconDecodeRequest, so it will outlive this. |
+ ArcPlayStoreSearchResult* const search_result_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(IconDecodeRequest); |
+}; |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// ArcPlayStoreSearchResult |
+ |
+namespace { |
+// Badge icon color, #000 at 54% opacity. |
+constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00); |
xiyuan
2017/06/23 19:57:38
This is used in one place. Move it close to where
Jiaquan He
2017/06/23 21:10:32
Done. Moved under kPlayAppPrefix
|
+} // namespace |
+ |
+ArcPlayStoreSearchResult::ArcPlayStoreSearchResult( |
+ arc::mojom::AppDiscoveryResultPtr data, |
+ Profile* profile, |
+ AppListControllerDelegate* list_controller) |
+ : data_(std::move(data)), |
+ profile_(profile), |
+ list_controller_(list_controller), |
+ weak_ptr_factory_(this) { |
xiyuan
2017/06/23 19:57:38
weak_ptr_factory_ seems not used.
Jiaquan He
2017/06/23 21:10:31
Done. Removed.
|
+ set_title(base::UTF8ToUTF16(label().value())); |
+ set_id(kPlayAppPrefix + |
+ crx_file::id_util::GenerateId(install_intent_uri().value())); |
+ set_display_type(DISPLAY_TILE); |
+ SetBadgeIcon(gfx::CreateVectorIcon( |
+ is_instant_app() ? kIcBadgeInstantIcon : kIcBadgePlayIcon, |
+ kAppBadgeIconSize, kBadgeColor)); |
+ SetFormattedPrice(base::UTF8ToUTF16(formatted_price().value())); |
+ SetRating(review_score()); |
+ |
+ icon_decode_request_ = base::MakeUnique<IconDecodeRequest>(this); |
+ ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data(), |
+ ImageDecoder::DEFAULT_CODEC, true, |
+ gfx::Size()); |
+} |
+ |
+ArcPlayStoreSearchResult::~ArcPlayStoreSearchResult() = default; |
+ |
+std::unique_ptr<SearchResult> ArcPlayStoreSearchResult::Duplicate() const { |
+ std::unique_ptr<ArcPlayStoreSearchResult> result = |
+ base::MakeUnique<ArcPlayStoreSearchResult>(data_.Clone(), profile_, |
+ list_controller_); |
+ result->SetIcon(icon()); |
+ return result; |
+} |
+ |
+void ArcPlayStoreSearchResult::Open(int event_flags) { |
+ arc::mojom::AppInstance* app_instance = |
+ arc::ArcServiceManager::Get() |
+ ? ARC_GET_INSTANCE_FOR_METHOD( |
+ arc::ArcServiceManager::Get()->arc_bridge_service()->app(), |
+ LaunchIntent) |
+ : nullptr; |
+ if (app_instance == nullptr) |
+ return; |
+ |
+ app_instance->LaunchIntent(install_intent_uri().value(), |
+ base::Optional<gfx::Rect>()); |
+} |
+ |
+ui::MenuModel* ArcPlayStoreSearchResult::GetContextMenuModel() { |
+ context_menu_ = base::MakeUnique<ArcPlayStoreAppContextMenu>( |
+ this, profile_, list_controller_); |
+ return context_menu_->GetMenuModel(); |
+} |
+ |
+void ArcPlayStoreSearchResult::ExecuteLaunchCommand(int event_flags) { |
+ Open(event_flags); |
+} |
+ |
+} // namespace app_list |