Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7127)

Unified Diff: chrome/browser/ui/app_list/search/playstore/playstore_search_result.cc

Issue 2929273002: Add the Play Store app search to the launcher. (Closed)
Patch Set: Finished comments and merged with the context menu CL. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..aa7d4bcac9e11575f52c432780e52e670986d52d
--- /dev/null
+++ b/chrome/browser/ui/app_list/search/playstore/playstore_search_result.cc
@@ -0,0 +1,214 @@
+// 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;
+
+#if defined(OS_CHROMEOS)
Luis Héctor Chávez 2017/06/22 15:42:10 Remove. Only add this file to the build if Chrome
Jiaquan He 2017/06/22 19:12:41 Done.
+
+namespace {
+// The id prefix to identify a Play Store search result.
+constexpr char kPlayAppPrefix[] = "play://";
+} // namespace
+
+namespace app_list {
+
+////////////////////////////////////////////////////////////////////////////////
+// IconSource
+
+class PlayStoreSearchResult::IconSource : public gfx::ImageSkiaSource {
+ public:
+ IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip);
+ 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);
+};
+
+PlayStoreSearchResult::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)));
+}
+
+PlayStoreSearchResult::IconSource::IconSource(int resource_size_in_dip)
+ : resource_size_in_dip_(resource_size_in_dip) {}
+
+gfx::ImageSkiaRep PlayStoreSearchResult::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_ != NULL) {
Luis Héctor Chávez 2017/06/22 15:42:10 nullptr
Jiaquan He 2017/06/22 19:12:41 Done.
+ 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:
+ 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)
+ : 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),
+ 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));
+
+ icon_decode_request_ = base::MakeUnique<IconDecodeRequest>(this);
+ ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data_,
+ ImageDecoder::DEFAULT_CODEC, true,
+ gfx::Size());
+}
+
+PlayStoreSearchResult::~PlayStoreSearchResult() {}
Luis Héctor Chávez 2017/06/22 15:42:10 nit: = default;
Jiaquan He 2017/06/22 19:12:41 Done.
+
+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_);
+ result->SetIcon(icon());
+ result->set_profile(profile());
+ result->set_list_controller(list_controller());
+ 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
+
+#endif

Powered by Google App Engine
This is Rietveld 408576698