| 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..98013dd1b30ce7c97f77dca72f9ad119669f8d37
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/app_list/search/arc/arc_playstore_search_result.cc
|
| @@ -0,0 +1,194 @@
|
| +// 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/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://";
|
| +// Badge icon color, #000 at 54% opacity.
|
| +constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00);
|
| +} // namespace
|
| +
|
| +namespace app_list {
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// IconSource
|
| +
|
| +class IconSource : public gfx::ImageSkiaSource {
|
| + public:
|
| + IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip);
|
| + explicit IconSource(int resource_size_in_dip);
|
| + ~IconSource() override = default;
|
| +
|
| + void SetDecodedImage(const SkBitmap& decoded_bitmap);
|
| +
|
| + private:
|
| + gfx::ImageSkiaRep GetImageForScale(float scale) override;
|
| +
|
| + const int resource_size_in_dip_;
|
| + gfx::ImageSkia decoded_icon_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(IconSource);
|
| +};
|
| +
|
| +IconSource::IconSource(int resource_size_in_dip)
|
| + : resource_size_in_dip_(resource_size_in_dip) {}
|
| +
|
| +void IconSource::SetDecodedImage(const SkBitmap& decoded_bitmap) {
|
| + decoded_icon_.AddRepresentation(gfx::ImageSkiaRep(
|
| + decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P)));
|
| +}
|
| +
|
| +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_.isNull()) {
|
| + int resource_id =
|
| + scale >= 1.5f ? IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48;
|
| + icon_to_scale =
|
| + ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
|
| + } else {
|
| + icon_to_scale = &decoded_icon_;
|
| + }
|
| + 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);
|
| +
|
| + const gfx::Size resource_size(app_list::kGridIconDimension,
|
| + app_list::kGridIconDimension);
|
| + IconSource* icon_source = new IconSource(app_list::kGridIconDimension);
|
| + icon_source->SetDecodedImage(bitmap);
|
| + const gfx::ImageSkia icon = gfx::ImageSkia(icon_source, 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.";
|
| +
|
| + const gfx::Size resource_size(app_list::kGridIconDimension,
|
| + app_list::kGridIconDimension);
|
| + IconSource* icon_source = new IconSource(app_list::kGridIconDimension);
|
| + const gfx::ImageSkia icon = gfx::ImageSkia(icon_source, 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
|
| +
|
| +ArcPlayStoreSearchResult::ArcPlayStoreSearchResult(
|
| + arc::mojom::AppDiscoveryResultPtr data,
|
| + Profile* profile,
|
| + AppListControllerDelegate* list_controller)
|
| + : data_(std::move(data)),
|
| + profile_(profile),
|
| + list_controller_(list_controller) {
|
| + 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());
|
| + set_result_type(is_instant_app() ? RESULT_INSTANT_APP : RESULT_PLAYSTORE_APP);
|
| +
|
| + 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::nullopt);
|
| +}
|
| +
|
| +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
|
|
|