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/arc/arc_playstore_search_result.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "chrome/browser/image_decoder.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/ui/app_list/arc/arc_playstore_app_context_menu.h" | |
14 #include "chrome/grit/component_extension_resources.h" | |
15 #include "components/arc/arc_bridge_service.h" | |
16 #include "components/arc/arc_service_manager.h" | |
17 #include "components/arc/common/app.mojom.h" | |
18 #include "components/crx_file/id_util.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "ui/app_list/app_list_constants.h" | |
21 #include "ui/app_list/vector_icons.h" | |
22 #include "ui/gfx/codec/png_codec.h" | |
23 #include "ui/gfx/geometry/size.h" | |
24 #include "ui/gfx/image/image_skia_operations.h" | |
25 #include "ui/gfx/image/image_skia_rep.h" | |
26 #include "ui/gfx/image/image_skia_source.h" | |
27 #include "ui/gfx/paint_vector_icon.h" | |
28 | |
29 using content::BrowserThread; | |
30 | |
31 namespace { | |
32 // The id prefix to identify a Play Store search result. | |
33 constexpr char kPlayAppPrefix[] = "play://"; | |
34 // Badge icon color, #000 at 54% opacity. | |
35 constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00); | |
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 void SetDecodedImage(const SkBitmap& decoded_bitmap); | |
50 | |
51 private: | |
52 gfx::ImageSkiaRep GetImageForScale(float scale) override; | |
53 | |
54 const int resource_size_in_dip_; | |
55 gfx::ImageSkia decoded_icon_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(IconSource); | |
58 }; | |
59 | |
60 IconSource::IconSource(int resource_size_in_dip) | |
61 : resource_size_in_dip_(resource_size_in_dip) {} | |
62 | |
63 void IconSource::SetDecodedImage(const SkBitmap& decoded_bitmap) { | |
64 decoded_icon_.AddRepresentation(gfx::ImageSkiaRep( | |
65 decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P))); | |
66 } | |
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_.isNull()) { | |
75 int resource_id = | |
76 scale >= 1.5f ? IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; | |
77 icon_to_scale = | |
78 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); | |
79 } else { | |
80 icon_to_scale = &decoded_icon_; | |
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 ArcPlayStoreSearchResult::IconDecodeRequest | |
94 : public ImageDecoder::ImageRequest { | |
95 public: | |
96 explicit IconDecodeRequest(ArcPlayStoreSearchResult* 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 const gfx::Size resource_size(app_list::kGridIconDimension, | |
105 app_list::kGridIconDimension); | |
106 IconSource* icon_source = new IconSource(app_list::kGridIconDimension); | |
107 icon_source->SetDecodedImage(bitmap); | |
108 const gfx::ImageSkia icon = gfx::ImageSkia(icon_source, resource_size); | |
109 icon.EnsureRepsForSupportedScales(); | |
110 | |
111 search_result_->SetIcon(icon); | |
112 } | |
113 | |
114 void OnDecodeImageFailed() override { | |
115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
116 DLOG(ERROR) << "Failed to decode an app icon image."; | |
117 | |
118 const gfx::Size resource_size(app_list::kGridIconDimension, | |
119 app_list::kGridIconDimension); | |
120 IconSource* icon_source = new IconSource(app_list::kGridIconDimension); | |
121 const gfx::ImageSkia icon = gfx::ImageSkia(icon_source, resource_size); | |
122 icon.EnsureRepsForSupportedScales(); | |
123 | |
124 search_result_->SetIcon(icon); | |
125 } | |
126 | |
127 private: | |
128 // ArcPlayStoreSearchResult owns IconDecodeRequest, so it will outlive this. | |
129 ArcPlayStoreSearchResult* const search_result_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(IconDecodeRequest); | |
132 }; | |
133 | |
134 //////////////////////////////////////////////////////////////////////////////// | |
135 // ArcPlayStoreSearchResult | |
136 | |
137 ArcPlayStoreSearchResult::ArcPlayStoreSearchResult( | |
138 arc::mojom::AppDiscoveryResultPtr data, | |
139 Profile* profile, | |
140 AppListControllerDelegate* list_controller) | |
141 : data_(std::move(data)), | |
142 profile_(profile), | |
143 list_controller_(list_controller) { | |
144 set_title(base::UTF8ToUTF16(label().value())); | |
145 set_id(kPlayAppPrefix + | |
146 crx_file::id_util::GenerateId(install_intent_uri().value())); | |
147 set_display_type(DISPLAY_TILE); | |
148 SetBadgeIcon(gfx::CreateVectorIcon( | |
149 is_instant_app() ? kIcBadgeInstantIcon : kIcBadgePlayIcon, | |
150 kAppBadgeIconSize, kBadgeColor)); | |
151 SetFormattedPrice(base::UTF8ToUTF16(formatted_price().value())); | |
152 SetRating(review_score()); | |
153 set_result_type(is_instant_app() ? RESULT_INSTANT_APP : RESULT_PLAYSTORE_APP); | |
154 | |
155 icon_decode_request_ = base::MakeUnique<IconDecodeRequest>(this); | |
156 ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data(), | |
157 ImageDecoder::DEFAULT_CODEC, true, | |
158 gfx::Size()); | |
159 } | |
160 | |
161 ArcPlayStoreSearchResult::~ArcPlayStoreSearchResult() = default; | |
162 | |
163 std::unique_ptr<SearchResult> ArcPlayStoreSearchResult::Duplicate() const { | |
164 std::unique_ptr<ArcPlayStoreSearchResult> result = | |
165 base::MakeUnique<ArcPlayStoreSearchResult>(data_.Clone(), profile_, | |
166 list_controller_); | |
167 result->SetIcon(icon()); | |
168 return result; | |
169 } | |
170 | |
171 void ArcPlayStoreSearchResult::Open(int event_flags) { | |
172 arc::mojom::AppInstance* app_instance = | |
173 arc::ArcServiceManager::Get() | |
174 ? ARC_GET_INSTANCE_FOR_METHOD( | |
175 arc::ArcServiceManager::Get()->arc_bridge_service()->app(), | |
176 LaunchIntent) | |
177 : nullptr; | |
178 if (app_instance == nullptr) | |
179 return; | |
180 | |
181 app_instance->LaunchIntent(install_intent_uri().value(), | |
182 base::Optional<gfx::Rect>()); | |
dcheng
2017/06/24 00:16:47
Nit: base::nullopt
Jiaquan He
2017/06/24 00:48:08
Done.
| |
183 } | |
184 | |
185 ui::MenuModel* ArcPlayStoreSearchResult::GetContextMenuModel() { | |
186 context_menu_ = base::MakeUnique<ArcPlayStoreAppContextMenu>( | |
187 this, profile_, list_controller_); | |
188 return context_menu_->GetMenuModel(); | |
189 } | |
190 | |
191 void ArcPlayStoreSearchResult::ExecuteLaunchCommand(int event_flags) { | |
192 Open(event_flags); | |
193 } | |
194 | |
195 } // namespace app_list | |
OLD | NEW |