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

Side by Side Diff: chrome/browser/ui/app_list/arc_app_icon.cc

Issue 1413153007: arc-app-launcher: Minimal support for ARC app launcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: switch to new IPC Created 5 years, 1 month 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 2015 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/arc_app_icon.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "chrome/browser/image_decoder.h"
13 #include "chrome/browser/ui/app_list/arc_app_prefs.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "extensions/grit/extensions_browser_resources.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/geometry/size.h"
18 #include "ui/gfx/image/image_skia_source.h"
19
20 ////////////////////////////////////////////////////////////////////////////////
21 // ArcAppIcon::Source
22
23 class ArcAppIcon::Source : public gfx::ImageSkiaSource {
24 public:
25 Source(ArcAppIcon* host, const gfx::Size& size_in_dip);
xiyuan 2015/11/11 22:15:49 |size_in_dip| seems not used?
khmel1 2015/11/12 08:05:29 It is planned to use in future for new UI items bu
26 ~Source() override;
27
28 void ResetHost();
29
30 private:
31 // gfx::ImageSkiaSource overrides:
32 gfx::ImageSkiaRep GetImageForScale(float scale) override;
33
34 // Used to load images asynchronously. NULLed out when the ArcAppIcon is
35 // destroyed.
36 ArcAppIcon* host_;
xiyuan 2015/11/11 22:15:49 Do we want to use a WeakPtr<ArcAppIcon> here? This
khmel1 2015/11/12 08:05:29 Thanks for good advice! It is more elegant (I used
37
38 DISALLOW_COPY_AND_ASSIGN(Source);
39 };
40
41 ArcAppIcon::Source::Source(ArcAppIcon* host, const gfx::Size& size_in_dip)
42 : host_(host) {
43 }
44
45 ArcAppIcon::Source::~Source() {
46 }
47
48 void ArcAppIcon::Source::ResetHost() {
49 host_ = NULL;
xiyuan 2015/11/11 22:15:49 nit: NULL -> nullptr
khmel1 2015/11/12 08:05:29 Removed once WeakPtr is used.
50 }
51
52 gfx::ImageSkiaRep ArcAppIcon::Source::GetImageForScale(float scale) {
53 if (host_) {
54 host_->LoadForScaleFactor(ui::GetSupportedScaleFactor(scale));
55 }
56
57 return gfx::ImageSkiaRep();
58 }
59
60 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest {
61 public:
62 DecodeRequest(ArcAppIcon* host, int dimension, ui::ScaleFactor scale_factor);
63 ~DecodeRequest() override;
64
65 void ResetHost();
66
67 // ImageDecoder::ImageRequest
68 void OnImageDecoded(const SkBitmap& bitmap) override;
69 void OnDecodeImageFailed() override;
70 private:
71 ArcAppIcon* host_;
72 int dimension_;
73 ui::ScaleFactor scale_factor_;
74
75 DISALLOW_COPY_AND_ASSIGN(DecodeRequest);
76 };
77
78 ////////////////////////////////////////////////////////////////////////////////
79 // ArcAppIcon::DecodeRequest
80
81 ArcAppIcon::DecodeRequest::DecodeRequest(ArcAppIcon* host,
82 int dimension,
83 ui::ScaleFactor scale_factor)
84 : host_(host),
85 dimension_(dimension),
86 scale_factor_(scale_factor) {
87 }
88
89 ArcAppIcon::DecodeRequest::~DecodeRequest() {
90 }
91
92 void ArcAppIcon::DecodeRequest::ResetHost() {
93 host_ = NULL;
xiyuan 2015/11/11 22:15:49 nit: NULL -> nullptr
khmel1 2015/11/12 08:05:29 Done.
94 }
95
96 void ArcAppIcon::DecodeRequest::OnImageDecoded(const SkBitmap& bitmap) {
97 DCHECK(!bitmap.isNull() && !bitmap.empty());
98
99 if (!host_) {
100 return;
101 }
102
103 int expected_dim = static_cast<int>(
104 ui::GetScaleForScaleFactor(scale_factor_) * dimension_ + 0.5f);
105 if (bitmap.width() != expected_dim || bitmap.height() != expected_dim) {
106 LOG(ERROR) << "Decoded ARC icon has unexpected dimension "
107 << bitmap.width() << "x" << bitmap.height() << ". Expected "
108 << expected_dim << "x" << ".";
109 return;
110 }
111
112 gfx::ImageSkia image_skia;
113 image_skia.AddRepresentation(gfx::ImageSkiaRep(
114 bitmap,
115 ui::GetScaleForScaleFactor(scale_factor_)));
116
117 host_->Update(&image_skia);
118 host_->DiscardDecodeRequest(this);
119
120 host_->observer_->OnIconUpdated();
xiyuan 2015/11/11 22:15:49 Would this be a use-after-free? |this| should be d
khmel1 2015/11/12 08:05:29 Done.
121 }
122
123 void ArcAppIcon::DecodeRequest::OnDecodeImageFailed() {
124 LOG(ERROR) << "Failed to decode ARC icon.";
125
126 if (!host_) {
127 return;
128 }
129
130 host_->DiscardDecodeRequest(this);
131 }
132
133 ////////////////////////////////////////////////////////////////////////////////
134 // ArcAppIcon
135
136 ArcAppIcon::ArcAppIcon(content::BrowserContext* context,
137 const std::string& app_id,
138 int resource_size_in_dip,
139 Observer* observer)
140 : context_(context),
141 app_id_(app_id),
142 resource_size_in_dip_(resource_size_in_dip),
143 observer_(observer),
144 source_(NULL),
xiyuan 2015/11/11 22:15:49 nit: think in-class member initialization is prefe
khmel1 2015/11/12 08:05:29 Done.
145 weak_ptr_factory_(this) {
146 CHECK(observer_ != NULL);
147 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip);
148 source_ = new Source(this, resource_size);
149 image_skia_ = gfx::ImageSkia(source_, resource_size);
150 Update(ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
xiyuan 2015/11/11 22:15:49 This Update() call has some side effects. ImageSk
khmel1 2015/11/12 08:05:29 Yes, I also noticed this issue when was implementi
151 IDR_APP_DEFAULT_ICON));
152 }
153
154 ArcAppIcon::~ArcAppIcon() {
155 source_->ResetHost();
156 for (size_t i = 0; i < decode_requests_.size(); ++i) {
157 decode_requests_[i]->ResetHost();
158 ImageDecoder::Cancel(decode_requests_[i]);
xiyuan 2015/11/11 22:15:49 nit: Cancel() is not necessary here since ImageDec
khmel1 2015/11/12 08:05:29 Done.
159 }
160 }
161
162 void ArcAppIcon::LoadForScaleFactor(ui::ScaleFactor scale_factor) {
163 ArcAppPrefs* prefs = ArcAppPrefs::Get(context_);
164 if (!prefs) {
165 LOG(ERROR) << "ARC preferences service is not available.";
166 return;
167 }
168
169 base::FilePath path = prefs->GetIconPath(app_id_, scale_factor);
170 if (path.empty()) {
171 return;
172 }
173
174 base::Closure task = base::Bind(&ArcAppIcon::ReadOnFileThread,
175 base::Unretained(this),
xiyuan 2015/11/11 22:15:49 Should this be weak_ptr_factory_.GetWeakPtr() inst
khmel1 2015/11/12 08:05:29 Yes, it should
176 scale_factor,
177 path);
178 content::BrowserThread::PostTask(content::BrowserThread::FILE,
xiyuan 2015/11/11 22:15:49 File thread is deprecated (because it is only one
khmel1 2015/11/12 08:05:29 Good to know! Done
179 FROM_HERE,
180 task);
181 }
182
183 void ArcAppIcon::RequestIcon(ui::ScaleFactor scale_factor) {
184 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
185 ArcAppPrefs* prefs = ArcAppPrefs::Get(context_);
186 if (!prefs) {
187 LOG(ERROR) << "ARC preferences service is not available.";
188 return;
189 }
190 prefs->RequestIcon(app_id_, scale_factor);
xiyuan 2015/11/11 22:15:49 How would we find out after ArcAppPrefs gets the i
khmel1 2015/11/12 08:05:29 I added comment.
191 }
192
193 void ArcAppIcon::ReadOnFileThread(ui::ScaleFactor scale_factor,
194 const base::FilePath& path) {
195 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
196 DCHECK(!path.empty());
197
198 if (!base::PathExists(path)) {
199 base::Closure task = base::Bind(&ArcAppIcon::RequestIcon,
200 base::Unretained(this),
xiyuan 2015/11/11 22:15:49 weak_ptr_factory_.GetWeakPtr()?
khmel1 2015/11/12 08:05:29 Done.
201 scale_factor);
202 content::BrowserThread::PostTask(content::BrowserThread::UI,
203 FROM_HERE,
204 task);
205 return;
206 }
207
208 std::string unsafe_icon_data;
209 // Read the file from disk.
210 if (!base::ReadFileToString(path, &unsafe_icon_data)) {
211 LOG(ERROR) << "Failed to read an ARC icon from file "
212 << path.MaybeAsASCII();
213 return;
214 }
215
216 base::Closure task = base::Bind(&ArcAppIcon::OnIconRead,
217 base::Unretained(this),
xiyuan 2015/11/11 22:15:49 weak_ptr_factory_.GetWeakPtr()?
khmel1 2015/11/12 08:05:29 Done.
218 scale_factor,
219 unsafe_icon_data);
220 content::BrowserThread::PostTask(content::BrowserThread::UI,
221 FROM_HERE,
222 task);
223 }
224
225 void ArcAppIcon::OnIconRead(ui::ScaleFactor scale_factor,
226 const std::string& unsafe_image_data) {
227 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
228
229 decode_requests_.push_back(new DecodeRequest(this,
230 resource_size_in_dip_,
231 scale_factor));
xiyuan 2015/11/11 22:15:49 nit: align with previous line.
khmel1 2015/11/12 08:05:29 Done.
232 ImageDecoder::Start(decode_requests_.back(), unsafe_image_data);
233 }
234
235 void ArcAppIcon::Update(const gfx::ImageSkia* image) {
236 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
237 CHECK(image && !image->isNull());
238
239 std::vector<gfx::ImageSkiaRep> reps = image->image_reps();
240 for (const auto& image_rep : reps) {
241 if (ui::IsSupportedScale(image_rep.scale())) {
242 image_skia_.RemoveRepresentation(image_rep.scale());
243 image_skia_.AddRepresentation(image_rep);
244 }
245 }
246
247 image_ = gfx::Image(image_skia_);
248 }
249
250 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) {
251 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
252
253 ScopedVector<DecodeRequest>::iterator it = std::find(decode_requests_.begin(),
254 decode_requests_.end(),
255 request);
256 CHECK(it != decode_requests_.end());
257 decode_requests_.erase(it);
258 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698