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 } // namespace | |
35 | |
36 namespace app_list { | |
37 | |
38 //////////////////////////////////////////////////////////////////////////////// | |
39 // IconSource | |
40 | |
41 class IconSource : public gfx::ImageSkiaSource { | |
42 public: | |
43 IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip); | |
xiyuan
2017/06/23 19:57:38
Prefer to have only one constructor and add a SetD
Jiaquan He
2017/06/23 21:10:32
Done.
| |
44 explicit IconSource(int resource_size_in_dip); | |
45 ~IconSource() override = default; | |
46 | |
47 private: | |
48 gfx::ImageSkiaRep GetImageForScale(float scale) override; | |
49 | |
50 const int resource_size_in_dip_; | |
51 std::unique_ptr<gfx::ImageSkia> decoded_icon_; | |
xiyuan
2017/06/23 19:57:38
std::unique_ptr<> is not necessary. We can test em
Jiaquan He
2017/06/23 21:10:32
Done.
| |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(IconSource); | |
54 }; | |
55 | |
56 IconSource::IconSource(const SkBitmap& decoded_bitmap, int resource_size_in_dip) | |
57 : resource_size_in_dip_(resource_size_in_dip) { | |
58 decoded_icon_ = base::MakeUnique<gfx::ImageSkia>(); | |
59 decoded_icon_->AddRepresentation(gfx::ImageSkiaRep( | |
60 decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P))); | |
61 } | |
62 | |
63 IconSource::IconSource(int resource_size_in_dip) | |
64 : resource_size_in_dip_(resource_size_in_dip) {} | |
65 | |
66 gfx::ImageSkiaRep IconSource::GetImageForScale(float scale) { | |
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
68 | |
69 // We use the icon if it was decoded successfully, otherwise use the default | |
70 // ARC icon. | |
71 const gfx::ImageSkia* icon_to_scale; | |
72 if (decoded_icon_ != nullptr) { | |
73 icon_to_scale = decoded_icon_.get(); | |
74 } else { | |
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 } | |
80 DCHECK(icon_to_scale); | |
81 | |
82 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( | |
83 *icon_to_scale, skia::ImageOperations::RESIZE_BEST, | |
84 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)); | |
85 return resized_image.GetRepresentation(scale); | |
86 } | |
87 | |
88 //////////////////////////////////////////////////////////////////////////////// | |
89 // IconDecodeRequest | |
90 | |
91 class ArcPlayStoreSearchResult::IconDecodeRequest | |
92 : public ImageDecoder::ImageRequest { | |
93 public: | |
94 explicit IconDecodeRequest(ArcPlayStoreSearchResult* search_result) | |
95 : search_result_(search_result) {} | |
96 ~IconDecodeRequest() override = default; | |
97 | |
98 // ImageDecoder::ImageRequest overrides. | |
99 void OnImageDecoded(const SkBitmap& bitmap) override { | |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
101 | |
102 gfx::Size resource_size(app_list::kGridIconDimension, | |
103 app_list::kGridIconDimension); | |
104 gfx::ImageSkia icon = gfx::ImageSkia( | |
105 new IconSource(bitmap, app_list::kGridIconDimension), resource_size); | |
106 icon.EnsureRepsForSupportedScales(); | |
107 | |
108 search_result_->SetIcon(icon); | |
109 } | |
110 | |
111 void OnDecodeImageFailed() override { | |
112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
113 DLOG(ERROR) << "Failed to decode an app icon image."; | |
114 | |
115 gfx::Size resource_size(app_list::kGridIconDimension, | |
116 app_list::kGridIconDimension); | |
117 gfx::ImageSkia icon = gfx::ImageSkia( | |
118 new IconSource(app_list::kGridIconDimension), resource_size); | |
119 icon.EnsureRepsForSupportedScales(); | |
120 | |
121 search_result_->SetIcon(icon); | |
122 } | |
123 | |
124 private: | |
125 // ArcPlayStoreSearchResult owns IconDecodeRequest, so it will outlive this. | |
126 ArcPlayStoreSearchResult* const search_result_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(IconDecodeRequest); | |
129 }; | |
130 | |
131 //////////////////////////////////////////////////////////////////////////////// | |
132 // ArcPlayStoreSearchResult | |
133 | |
134 namespace { | |
135 // Badge icon color, #000 at 54% opacity. | |
136 constexpr SkColor kBadgeColor = SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00); | |
xiyuan
2017/06/23 19:57:38
This is used in one place. Move it close to where
Jiaquan He
2017/06/23 21:10:32
Done. Moved under kPlayAppPrefix
| |
137 } // namespace | |
138 | |
139 ArcPlayStoreSearchResult::ArcPlayStoreSearchResult( | |
140 arc::mojom::AppDiscoveryResultPtr data, | |
141 Profile* profile, | |
142 AppListControllerDelegate* list_controller) | |
143 : data_(std::move(data)), | |
144 profile_(profile), | |
145 list_controller_(list_controller), | |
146 weak_ptr_factory_(this) { | |
xiyuan
2017/06/23 19:57:38
weak_ptr_factory_ seems not used.
Jiaquan He
2017/06/23 21:10:31
Done. Removed.
| |
147 set_title(base::UTF8ToUTF16(label().value())); | |
148 set_id(kPlayAppPrefix + | |
149 crx_file::id_util::GenerateId(install_intent_uri().value())); | |
150 set_display_type(DISPLAY_TILE); | |
151 SetBadgeIcon(gfx::CreateVectorIcon( | |
152 is_instant_app() ? kIcBadgeInstantIcon : kIcBadgePlayIcon, | |
153 kAppBadgeIconSize, kBadgeColor)); | |
154 SetFormattedPrice(base::UTF8ToUTF16(formatted_price().value())); | |
155 SetRating(review_score()); | |
156 | |
157 icon_decode_request_ = base::MakeUnique<IconDecodeRequest>(this); | |
158 ImageDecoder::StartWithOptions(icon_decode_request_.get(), icon_png_data(), | |
159 ImageDecoder::DEFAULT_CODEC, true, | |
160 gfx::Size()); | |
161 } | |
162 | |
163 ArcPlayStoreSearchResult::~ArcPlayStoreSearchResult() = default; | |
164 | |
165 std::unique_ptr<SearchResult> ArcPlayStoreSearchResult::Duplicate() const { | |
166 std::unique_ptr<ArcPlayStoreSearchResult> result = | |
167 base::MakeUnique<ArcPlayStoreSearchResult>(data_.Clone(), profile_, | |
168 list_controller_); | |
169 result->SetIcon(icon()); | |
170 return result; | |
171 } | |
172 | |
173 void ArcPlayStoreSearchResult::Open(int event_flags) { | |
174 arc::mojom::AppInstance* app_instance = | |
175 arc::ArcServiceManager::Get() | |
176 ? ARC_GET_INSTANCE_FOR_METHOD( | |
177 arc::ArcServiceManager::Get()->arc_bridge_service()->app(), | |
178 LaunchIntent) | |
179 : nullptr; | |
180 if (app_instance == nullptr) | |
181 return; | |
182 | |
183 app_instance->LaunchIntent(install_intent_uri().value(), | |
184 base::Optional<gfx::Rect>()); | |
185 } | |
186 | |
187 ui::MenuModel* ArcPlayStoreSearchResult::GetContextMenuModel() { | |
188 context_menu_ = base::MakeUnique<ArcPlayStoreAppContextMenu>( | |
189 this, profile_, list_controller_); | |
190 return context_menu_->GetMenuModel(); | |
191 } | |
192 | |
193 void ArcPlayStoreSearchResult::ExecuteLaunchCommand(int event_flags) { | |
194 Open(event_flags); | |
195 } | |
196 | |
197 } // namespace app_list | |
OLD | NEW |