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 <memory> | |
Luis Héctor Chávez
2017/06/22 19:49:17
You don't need to add these here: You can rely on
Jiaquan He
2017/06/22 19:59:36
Done.
| |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/image_decoder.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/app_list/playstore_app_context_menu.h" | |
16 #include "chrome/grit/component_extension_resources.h" | |
17 #include "components/arc/arc_bridge_service.h" | |
18 #include "components/arc/arc_service_manager.h" | |
19 #include "components/arc/common/app.mojom.h" | |
20 #include "components/crx_file/id_util.h" | |
21 #include "content/public/browser/browser_thread.h" | |
22 #include "ui/app_list/app_list_constants.h" | |
23 #include "ui/app_list/vector_icons.h" | |
24 #include "ui/gfx/codec/png_codec.h" | |
25 #include "ui/gfx/geometry/size.h" | |
26 #include "ui/gfx/image/image_skia_operations.h" | |
27 #include "ui/gfx/image/image_skia_rep.h" | |
28 #include "ui/gfx/image/image_skia_source.h" | |
29 #include "ui/gfx/paint_vector_icon.h" | |
30 | |
31 using content::BrowserThread; | |
32 | |
33 namespace { | |
34 // The id prefix to identify a Play Store search result. | |
35 constexpr char kPlayAppPrefix[] = "play://"; | |
36 } // namespace | |
37 | |
38 namespace app_list { | |
39 | |
40 //////////////////////////////////////////////////////////////////////////////// | |
41 // IconSource | |
42 | |
43 class IconSource : public gfx::ImageSkiaSource { | |
44 public: | |
45 IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip); | |
46 explicit IconSource(int resource_size_in_dip); | |
47 ~IconSource() override = default; | |
48 | |
49 private: | |
50 gfx::ImageSkiaRep GetImageForScale(float scale) override; | |
51 | |
52 const int resource_size_in_dip_; | |
53 std::unique_ptr<gfx::ImageSkia> decoded_icon_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(IconSource); | |
56 }; | |
57 | |
58 IconSource::IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip) | |
59 : resource_size_in_dip_(resource_size_in_dip) { | |
60 decoded_icon_ = base::MakeUnique<gfx::ImageSkia>(); | |
61 decoded_icon_->AddRepresentation(gfx::ImageSkiaRep( | |
62 decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P))); | |
63 } | |
64 | |
65 IconSource::IconSource(int resource_size_in_dip) | |
66 : resource_size_in_dip_(resource_size_in_dip) {} | |
67 | |
68 gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) { | |
69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
70 | |
71 // We use the icon if it was decoded successfully, otherwise use the default | |
72 // ARC icon. | |
73 const gfx::ImageSkia* icon_to_scale; | |
74 if (decoded_icon_ != nullptr) { | |
75 icon_to_scale = decoded_icon_.get(); | |
76 } else { | |
77 int resource_id = | |
78 scale >= 1.5f ? IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; | |
79 icon_to_scale = | |
80 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); | |
81 } | |
82 DCHECK(icon_to_scale); | |
83 | |
84 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( | |
85 *icon_to_scale, skia::ImageOperations::RESIZE_BEST, | |
86 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)); | |
87 return resized_image.GetRepresentation(scale); | |
88 } | |
89 | |
90 //////////////////////////////////////////////////////////////////////////////// | |
91 // IconDecodeRequest | |
92 | |
93 class PlayStoreSearchResult::IconDecodeRequest | |
94 : public ImageDecoder::ImageRequest { | |
95 public: | |
96 explicit IconDecodeRequest(PlayStoreSearchResult* search_result) | |
97 : search_result_(search_result) {} | |
98 ~IconDecodeRequest() override = default; | |
99 | |
100 // ImageDecoder::ImageRequest overrides. | |
101 void OnImageDecoded(const SkBitmap& bitmap) override { | |
102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
103 | |
104 gfx::Size resource_size(app_list::kGridIconDimension, | |
105 app_list::kGridIconDimension); | |
106 gfx::ImageSkia icon = gfx::ImageSkia( | |
107 new IconSource(bitmap, app_list::kGridIconDimension), resource_size); | |
108 icon.EnsureRepsForSupportedScales(); | |
109 | |
110 search_result_->SetIcon(icon); | |
111 } | |
112 | |
113 void OnDecodeImageFailed() override { | |
114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
115 DLOG(ERROR) << "Failed to decode an app icon image."; | |
116 | |
117 gfx::Size resource_size(app_list::kGridIconDimension, | |
118 app_list::kGridIconDimension); | |
119 gfx::ImageSkia icon = gfx::ImageSkia( | |
120 new IconSource(app_list::kGridIconDimension), resource_size); | |
121 icon.EnsureRepsForSupportedScales(); | |
122 | |
123 search_result_->SetIcon(icon); | |
124 } | |
125 | |
126 private: | |
127 // PlayStoreSearchResult owns IconDecodeRequest, so it will outlive this. | |
128 PlayStoreSearchResult* const search_result_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(IconDecodeRequest); | |
131 }; | |
132 | |
133 //////////////////////////////////////////////////////////////////////////////// | |
134 // PlayStoreSearchResult | |
135 | |
136 namespace { | |
137 // Badge icon color, #000 at 54% opacity. | |
138 constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00); | |
139 } // namespace | |
140 | |
141 PlayStoreSearchResult::PlayStoreSearchResult( | |
142 const base::Optional<std::string>& launch_intent_uri, | |
143 const base::Optional<std::string>& install_intent_uri, | |
144 const base::Optional<std::string>& label, | |
145 bool is_instant_app, | |
146 bool is_recent, | |
147 const base::Optional<std::string>& publisher_name, | |
148 const base::Optional<std::string>& formatted_price, | |
149 float review_score, | |
150 const std::vector<uint8_t>& icon_png_data) | |
151 : launch_intent_uri_(launch_intent_uri), | |
152 install_intent_uri_(install_intent_uri), | |
153 label_(label), | |
154 is_instant_app_(is_instant_app), | |
155 is_recent_(is_recent), | |
156 publisher_name_(publisher_name), | |
157 formatted_price_(formatted_price), | |
158 review_score_(review_score), | |
159 icon_png_data_(icon_png_data), | |
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 | |
169 icon_decode_request_ = base::MakeUnique<IconDecodeRequest>(this); | |
170 ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data_, | |
171 ImageDecoder::DEFAULT_CODEC, true, | |
172 gfx::Size()); | |
173 } | |
174 | |
175 PlayStoreSearchResult::~PlayStoreSearchResult() = default; | |
176 | |
177 std::unique_ptr<SearchResult> PlayStoreSearchResult::Duplicate() const { | |
178 std::unique_ptr<PlayStoreSearchResult> result = | |
179 base::MakeUnique<PlayStoreSearchResult>( | |
180 launch_intent_uri_, install_intent_uri_, label_, is_instant_app_, | |
181 is_recent_, publisher_name_, formatted_price_, review_score_, | |
182 icon_png_data_); | |
183 result->SetIcon(icon()); | |
184 result->set_profile(profile()); | |
185 result->set_list_controller(list_controller()); | |
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 |