| Index: chrome/browser/ui/app_list/search/playstore/playstore_search_result.cc
|
| diff --git a/chrome/browser/ui/app_list/search/playstore/playstore_search_result.cc b/chrome/browser/ui/app_list/search/playstore/playstore_search_result.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8b4eed0d42c8dc490d2d3b5f5405e8b4fa1c4c53
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/app_list/search/playstore/playstore_search_result.cc
|
| @@ -0,0 +1,213 @@
|
| +// 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/playstore/playstore_search_result.h"
|
| +
|
| +#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/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);
|
| + 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_;
|
| +
|
| + 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 PlayStoreSearchResult::IconDecodeRequest
|
| + : public ImageDecoder::ImageRequest {
|
| + public:
|
| + explicit IconDecodeRequest(PlayStoreSearchResult* 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:
|
| + // PlayStoreSearchResult owns IconDecodeRequest, so it will outlive this.
|
| + PlayStoreSearchResult* const search_result_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(IconDecodeRequest);
|
| +};
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +// PlayStoreSearchResult
|
| +
|
| +namespace {
|
| +// Badge icon color, #000 at 54% opacity.
|
| +constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00);
|
| +} // namespace
|
| +
|
| +PlayStoreSearchResult::PlayStoreSearchResult(
|
| + const base::Optional<std::string>& launch_intent_uri,
|
| + const base::Optional<std::string>& install_intent_uri,
|
| + const base::Optional<std::string>& label,
|
| + bool is_instant_app,
|
| + bool is_recent,
|
| + const base::Optional<std::string>& publisher_name,
|
| + const base::Optional<std::string>& formatted_price,
|
| + float review_score,
|
| + const std::vector<uint8_t>& icon_png_data,
|
| + Profile* const profile,
|
| + AppListControllerDelegate* const list_controller)
|
| + : launch_intent_uri_(launch_intent_uri),
|
| + install_intent_uri_(install_intent_uri),
|
| + label_(label),
|
| + is_instant_app_(is_instant_app),
|
| + is_recent_(is_recent),
|
| + publisher_name_(publisher_name),
|
| + formatted_price_(formatted_price),
|
| + review_score_(review_score),
|
| + icon_png_data_(icon_png_data),
|
| + profile_(profile),
|
| + list_controller_(list_controller),
|
| + weak_ptr_factory_(this) {
|
| + 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());
|
| +}
|
| +
|
| +PlayStoreSearchResult::~PlayStoreSearchResult() = default;
|
| +
|
| +std::unique_ptr<SearchResult> PlayStoreSearchResult::Duplicate() const {
|
| + std::unique_ptr<PlayStoreSearchResult> result =
|
| + base::MakeUnique<PlayStoreSearchResult>(
|
| + launch_intent_uri_, install_intent_uri_, label_, is_instant_app_,
|
| + is_recent_, publisher_name_, formatted_price_, review_score_,
|
| + icon_png_data_, profile_, list_controller_);
|
| + result->SetIcon(icon());
|
| + return result;
|
| +}
|
| +
|
| +void PlayStoreSearchResult::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* PlayStoreSearchResult::GetContextMenuModel() {
|
| + context_menu_ = base::MakeUnique<PlayStoreAppContextMenu>(this, profile_,
|
| + list_controller_);
|
| + return context_menu_->GetMenuModel();
|
| +}
|
| +
|
| +void PlayStoreSearchResult::ExecuteLaunchCommand(int event_flags) {
|
| + Open(event_flags);
|
| +}
|
| +
|
| +} // namespace app_list
|
|
|