OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/app_list/search/playstore/playstore_search_result.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/image_decoder.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/app_list/playstore_app_context_menu.h" |
| 12 #include "chrome/grit/component_extension_resources.h" |
| 13 #include "components/arc/arc_bridge_service.h" |
| 14 #include "components/arc/arc_service_manager.h" |
| 15 #include "components/arc/common/app.mojom.h" |
| 16 #include "components/crx_file/id_util.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "ui/app_list/app_list_constants.h" |
| 19 #include "ui/app_list/vector_icons.h" |
| 20 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/geometry/size.h" |
| 22 #include "ui/gfx/image/image_skia_operations.h" |
| 23 #include "ui/gfx/image/image_skia_rep.h" |
| 24 #include "ui/gfx/image/image_skia_source.h" |
| 25 #include "ui/gfx/paint_vector_icon.h" |
| 26 |
| 27 using content::BrowserThread; |
| 28 |
| 29 namespace { |
| 30 // The id prefix to identify a Play Store search result. |
| 31 constexpr char kPlayAppPrefix[] = "play://"; |
| 32 } // namespace |
| 33 |
| 34 namespace app_list { |
| 35 |
| 36 //////////////////////////////////////////////////////////////////////////////// |
| 37 // IconSource |
| 38 |
| 39 class IconSource : public gfx::ImageSkiaSource { |
| 40 public: |
| 41 IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip); |
| 42 explicit IconSource(int resource_size_in_dip); |
| 43 ~IconSource() override = default; |
| 44 |
| 45 private: |
| 46 gfx::ImageSkiaRep GetImageForScale(float scale) override; |
| 47 |
| 48 const int resource_size_in_dip_; |
| 49 std::unique_ptr<gfx::ImageSkia> decoded_icon_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(IconSource); |
| 52 }; |
| 53 |
| 54 IconSource::IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip) |
| 55 : resource_size_in_dip_(resource_size_in_dip) { |
| 56 decoded_icon_ = base::MakeUnique<gfx::ImageSkia>(); |
| 57 decoded_icon_->AddRepresentation(gfx::ImageSkiaRep( |
| 58 decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P))); |
| 59 } |
| 60 |
| 61 IconSource::IconSource(int resource_size_in_dip) |
| 62 : resource_size_in_dip_(resource_size_in_dip) {} |
| 63 |
| 64 gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) { |
| 65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 66 |
| 67 // We use the icon if it was decoded successfully, otherwise use the default |
| 68 // ARC icon. |
| 69 const gfx::ImageSkia* icon_to_scale; |
| 70 if (decoded_icon_ != nullptr) { |
| 71 icon_to_scale = decoded_icon_.get(); |
| 72 } else { |
| 73 int resource_id = |
| 74 scale >= 1.5f ? IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; |
| 75 icon_to_scale = |
| 76 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); |
| 77 } |
| 78 DCHECK(icon_to_scale); |
| 79 |
| 80 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( |
| 81 *icon_to_scale, skia::ImageOperations::RESIZE_BEST, |
| 82 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)); |
| 83 return resized_image.GetRepresentation(scale); |
| 84 } |
| 85 |
| 86 //////////////////////////////////////////////////////////////////////////////// |
| 87 // IconDecodeRequest |
| 88 |
| 89 class PlayStoreSearchResult::IconDecodeRequest |
| 90 : public ImageDecoder::ImageRequest { |
| 91 public: |
| 92 explicit IconDecodeRequest(PlayStoreSearchResult* search_result) |
| 93 : search_result_(search_result) {} |
| 94 ~IconDecodeRequest() override = default; |
| 95 |
| 96 // ImageDecoder::ImageRequest overrides. |
| 97 void OnImageDecoded(const SkBitmap& bitmap) override { |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 99 |
| 100 gfx::Size resource_size(app_list::kGridIconDimension, |
| 101 app_list::kGridIconDimension); |
| 102 gfx::ImageSkia icon = gfx::ImageSkia( |
| 103 new IconSource(bitmap, app_list::kGridIconDimension), resource_size); |
| 104 icon.EnsureRepsForSupportedScales(); |
| 105 |
| 106 search_result_->SetIcon(icon); |
| 107 } |
| 108 |
| 109 void OnDecodeImageFailed() override { |
| 110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 111 DLOG(ERROR) << "Failed to decode an app icon image."; |
| 112 |
| 113 gfx::Size resource_size(app_list::kGridIconDimension, |
| 114 app_list::kGridIconDimension); |
| 115 gfx::ImageSkia icon = gfx::ImageSkia( |
| 116 new IconSource(app_list::kGridIconDimension), resource_size); |
| 117 icon.EnsureRepsForSupportedScales(); |
| 118 |
| 119 search_result_->SetIcon(icon); |
| 120 } |
| 121 |
| 122 private: |
| 123 // PlayStoreSearchResult owns IconDecodeRequest, so it will outlive this. |
| 124 PlayStoreSearchResult* const search_result_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(IconDecodeRequest); |
| 127 }; |
| 128 |
| 129 //////////////////////////////////////////////////////////////////////////////// |
| 130 // PlayStoreSearchResult |
| 131 |
| 132 namespace { |
| 133 // Badge icon color, #000 at 54% opacity. |
| 134 constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00); |
| 135 } // namespace |
| 136 |
| 137 PlayStoreSearchResult::PlayStoreSearchResult( |
| 138 const base::Optional<std::string>& launch_intent_uri, |
| 139 const base::Optional<std::string>& install_intent_uri, |
| 140 const base::Optional<std::string>& label, |
| 141 bool is_instant_app, |
| 142 bool is_recent, |
| 143 const base::Optional<std::string>& publisher_name, |
| 144 const base::Optional<std::string>& formatted_price, |
| 145 float review_score, |
| 146 const std::vector<uint8_t>& icon_png_data, |
| 147 Profile* const profile, |
| 148 AppListControllerDelegate* const list_controller) |
| 149 : launch_intent_uri_(launch_intent_uri), |
| 150 install_intent_uri_(install_intent_uri), |
| 151 label_(label), |
| 152 is_instant_app_(is_instant_app), |
| 153 is_recent_(is_recent), |
| 154 publisher_name_(publisher_name), |
| 155 formatted_price_(formatted_price), |
| 156 review_score_(review_score), |
| 157 icon_png_data_(icon_png_data), |
| 158 profile_(profile), |
| 159 list_controller_(list_controller), |
| 160 weak_ptr_factory_(this) { |
| 161 set_title(base::UTF8ToUTF16(label_.value())); |
| 162 set_id(kPlayAppPrefix + |
| 163 crx_file::id_util::GenerateId(install_intent_uri_.value())); |
| 164 set_display_type(DISPLAY_TILE); |
| 165 SetBadgeIcon(gfx::CreateVectorIcon( |
| 166 is_instant_app_ ? kIcBadgeInstantIcon : kIcBadgePlayIcon, |
| 167 kAppBadgeIconSize, kBadgeColor)); |
| 168 SetFormattedPrice(base::UTF8ToUTF16(formatted_price_.value())); |
| 169 SetRating(review_score_); |
| 170 |
| 171 icon_decode_request_ = base::MakeUnique<IconDecodeRequest>(this); |
| 172 ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data_, |
| 173 ImageDecoder::DEFAULT_CODEC, true, |
| 174 gfx::Size()); |
| 175 } |
| 176 |
| 177 PlayStoreSearchResult::~PlayStoreSearchResult() = default; |
| 178 |
| 179 std::unique_ptr<SearchResult> PlayStoreSearchResult::Duplicate() const { |
| 180 std::unique_ptr<PlayStoreSearchResult> result = |
| 181 base::MakeUnique<PlayStoreSearchResult>( |
| 182 launch_intent_uri_, install_intent_uri_, label_, is_instant_app_, |
| 183 is_recent_, publisher_name_, formatted_price_, review_score_, |
| 184 icon_png_data_, profile_, list_controller_); |
| 185 result->SetIcon(icon()); |
| 186 return result; |
| 187 } |
| 188 |
| 189 void PlayStoreSearchResult::Open(int event_flags) { |
| 190 arc::mojom::AppInstance* app_instance = |
| 191 arc::ArcServiceManager::Get() |
| 192 ? ARC_GET_INSTANCE_FOR_METHOD( |
| 193 arc::ArcServiceManager::Get()->arc_bridge_service()->app(), |
| 194 LaunchIntent) |
| 195 : nullptr; |
| 196 if (app_instance == nullptr) |
| 197 return; |
| 198 |
| 199 app_instance->LaunchIntent(install_intent_uri_.value(), |
| 200 base::Optional<gfx::Rect>()); |
| 201 } |
| 202 |
| 203 ui::MenuModel* PlayStoreSearchResult::GetContextMenuModel() { |
| 204 context_menu_ = base::MakeUnique<PlayStoreAppContextMenu>(this, profile_, |
| 205 list_controller_); |
| 206 return context_menu_->GetMenuModel(); |
| 207 } |
| 208 |
| 209 void PlayStoreSearchResult::ExecuteLaunchCommand(int event_flags) { |
| 210 Open(event_flags); |
| 211 } |
| 212 |
| 213 } // namespace app_list |
OLD | NEW |