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 #include "base/strings/utf_string_conversions.h" | |
7 #include "chrome/grit/component_extension_resources.h" | |
8 #include "components/crx_file/id_util.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "ui/app_list/app_list_constants.h" | |
11 #include "ui/gfx/codec/png_codec.h" | |
12 #include "ui/gfx/geometry/size.h" | |
13 #include "ui/gfx/image/image_skia_operations.h" | |
14 #include "ui/gfx/image/image_skia_rep.h" | |
15 #include "ui/gfx/image/image_skia_source.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 #if defined(OS_CHROMEOS) | |
20 | |
21 namespace app_list { | |
22 | |
23 //////////////////////////////////////////////////////////////////////////////// | |
24 // IconSource | |
25 | |
26 class PlaystoreSearchResult::IconSource : public gfx::ImageSkiaSource { | |
27 public: | |
28 IconSource(const base::WeakPtr<PlaystoreSearchResult>& host, | |
29 int resource_size_in_dip); | |
30 ~IconSource() override; | |
Luis Héctor Chávez
2017/06/16 22:24:34
nit: ~IconSource() override = default;
Jiaquan He
2017/06/22 04:30:03
Done.
| |
31 | |
32 private: | |
33 gfx::ImageSkiaRep GetImageForScale(float scale) override; | |
34 | |
35 const int resource_size_in_dip_; | |
36 const base::WeakPtr<PlaystoreSearchResult> host_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(IconSource); | |
39 }; | |
40 | |
41 PlaystoreSearchResult::IconSource::IconSource( | |
42 const base::WeakPtr<PlaystoreSearchResult>& host, | |
43 int resource_size_in_dip) | |
44 : resource_size_in_dip_(resource_size_in_dip), host_(host) {} | |
45 | |
46 PlaystoreSearchResult::IconSource::~IconSource() {} | |
47 | |
48 gfx::ImageSkiaRep PlaystoreSearchResult::IconSource::GetImageForScale( | |
49 float scale) { | |
50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
51 const gfx::ImageSkia* icon_to_scale; | |
52 if (host_) { | |
53 icon_to_scale = &host_->decoded_icon_; | |
54 } else { | |
55 int resource_id = | |
56 scale >= 1.5f ? IDR_ARC_SUPPORT_ICON_96 : IDR_ARC_SUPPORT_ICON_48; | |
57 icon_to_scale = | |
58 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); | |
59 } | |
60 CHECK(icon_to_scale); | |
Luis Héctor Chávez
2017/06/16 22:24:34
Do not use CHECK: https://chromium.googlesource.co
Jiaquan He
2017/06/22 04:30:03
Done.
| |
61 | |
62 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( | |
63 *icon_to_scale, skia::ImageOperations::RESIZE_BEST, | |
64 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)); | |
65 return resized_image.GetRepresentation(scale); | |
66 } | |
67 | |
68 //////////////////////////////////////////////////////////////////////////////// | |
69 // PlaystoreSearchResult | |
70 | |
71 PlaystoreSearchResult::PlaystoreSearchResult( | |
72 const std::string& launch_intent_uri, | |
73 const std::string& install_intent_uri, | |
74 const std::string& label, | |
75 bool is_instant_app, | |
76 bool is_recent, | |
77 const std::string& publisher_name, | |
78 const std::vector<uint8_t>& icon_png_data) | |
79 : launch_intent_uri_(launch_intent_uri), | |
80 install_intent_uri_(install_intent_uri), | |
81 label_(label), | |
82 is_instant_app_(is_instant_app), | |
83 is_recent_(is_recent), | |
84 publisher_name_(publisher_name), | |
85 icon_png_data_(icon_png_data), | |
86 weak_ptr_factory_(this) { | |
87 set_title(base::ASCIIToUTF16(label)); | |
Luis Héctor Chávez
2017/06/16 22:24:33
I don't think there's a guarantee that this is ASC
Jiaquan He
2017/06/22 04:30:03
Done.
| |
88 set_id(kPlayAppPrefix + crx_file::id_util::GenerateId(install_intent_uri_)); | |
89 set_display_type(DISPLAY_TILE); | |
90 | |
91 // Decode the icon | |
92 SkBitmap decoded_bitmap; | |
93 if (gfx::PNGCodec::Decode( | |
Luis Héctor Chávez
2017/06/16 22:24:34
I don't think it's safe to call this from the brow
Jiaquan He
2017/06/22 04:30:03
Done.
| |
94 reinterpret_cast<const unsigned char*>(&(icon_png_data_[0])), | |
Luis Héctor Chávez
2017/06/16 22:24:34
nit: s/&(icon_png_data_[0])/icon_png_data_.data()/
Jiaquan He
2017/06/22 04:30:03
Done.
| |
95 icon_png_data_.size(), &decoded_bitmap)) { | |
96 // Now decoded_bitmap holds an icon of its original size. | |
97 // We store it into decoded_icon_, from which we'll get scaled icons | |
98 // in the future. | |
99 decoded_icon_.AddRepresentation(gfx::ImageSkiaRep( | |
100 decoded_bitmap, ui::GetScaleForScaleFactor(ui::SCALE_FACTOR_100P))); | |
101 | |
102 int resource_size_in_dip = app_list::kGridIconDimension; | |
103 source_ = | |
Luis Héctor Chávez
2017/06/16 22:24:34
You cannot hold to the created IconSource since gf
Jiaquan He
2017/06/22 04:30:03
Done.
| |
104 new IconSource(weak_ptr_factory_.GetWeakPtr(), resource_size_in_dip); | |
105 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip); | |
106 gfx::ImageSkia icon = gfx::ImageSkia(source_, resource_size); | |
107 icon.EnsureRepsForSupportedScales(); | |
108 | |
109 SetIcon(icon); | |
110 } | |
111 } | |
112 | |
113 PlaystoreSearchResult::~PlaystoreSearchResult() {} | |
114 | |
115 std::unique_ptr<SearchResult> PlaystoreSearchResult::Duplicate() const { | |
116 std::unique_ptr<SearchResult> result = | |
117 base::MakeUnique<PlaystoreSearchResult>( | |
118 launch_intent_uri_, install_intent_uri_, label_, is_instant_app_, | |
119 is_recent_, publisher_name_, icon_png_data_); | |
120 result->SetIcon(icon()); | |
121 return result; | |
122 } | |
123 | |
124 void PlaystoreSearchResult::Open(int event_flags) { | |
125 arc::mojom::AppInstance* app_instance = GET_APP_INSTANCE(LaunchIntent); | |
126 if (app_instance == nullptr) | |
127 return; | |
128 | |
129 app_instance->LaunchIntent(install_intent_uri_, base::Optional<gfx::Rect>()); | |
130 } | |
131 | |
132 } // namespace app_list | |
133 | |
134 #endif | |
OLD | NEW |