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

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

Powered by Google App Engine
This is Rietveld 408576698