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

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

Issue 2749973002: arc: Fix Default icon issue when cached icon file is corrupted. (Closed)
Patch Set: Nits fix. Created 3 years, 9 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/app_list/arc/arc_app_icon.h" 5 #include "chrome/browser/ui/app_list/arc/arc_app_icon.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest { 165 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest {
166 public: 166 public:
167 DecodeRequest(const base::WeakPtr<ArcAppIcon>& host, 167 DecodeRequest(const base::WeakPtr<ArcAppIcon>& host,
168 int dimension, 168 int dimension,
169 ui::ScaleFactor scale_factor); 169 ui::ScaleFactor scale_factor);
170 ~DecodeRequest() override; 170 ~DecodeRequest() override;
171 171
172 // ImageDecoder::ImageRequest 172 // ImageDecoder::ImageRequest
173 void OnImageDecoded(const SkBitmap& bitmap) override; 173 void OnImageDecoded(const SkBitmap& bitmap) override;
174 void OnDecodeImageFailed() override; 174 void OnDecodeImageFailed() override;
175
175 private: 176 private:
176 base::WeakPtr<ArcAppIcon> host_; 177 base::WeakPtr<ArcAppIcon> host_;
177 int dimension_; 178 int dimension_;
178 ui::ScaleFactor scale_factor_; 179 ui::ScaleFactor scale_factor_;
179 180
180 DISALLOW_COPY_AND_ASSIGN(DecodeRequest); 181 DISALLOW_COPY_AND_ASSIGN(DecodeRequest);
181 }; 182 };
182 183
183 //////////////////////////////////////////////////////////////////////////////// 184 ////////////////////////////////////////////////////////////////////////////////
184 // ArcAppIcon::DecodeRequest 185 // ArcAppIcon::DecodeRequest
(...skipping 14 matching lines...) Expand all
199 200
200 if (!host_) 201 if (!host_)
201 return; 202 return;
202 203
203 int expected_dim = static_cast<int>( 204 int expected_dim = static_cast<int>(
204 ui::GetScaleForScaleFactor(scale_factor_) * dimension_ + 0.5f); 205 ui::GetScaleForScaleFactor(scale_factor_) * dimension_ + 0.5f);
205 if (bitmap.width() != expected_dim || bitmap.height() != expected_dim) { 206 if (bitmap.width() != expected_dim || bitmap.height() != expected_dim) {
206 VLOG(2) << "Decoded ARC icon has unexpected dimension " 207 VLOG(2) << "Decoded ARC icon has unexpected dimension "
207 << bitmap.width() << "x" << bitmap.height() << ". Expected " 208 << bitmap.width() << "x" << bitmap.height() << ". Expected "
208 << expected_dim << "x" << "."; 209 << expected_dim << "x" << ".";
210
211 host_->MaybeRequestIcon(scale_factor_);
209 host_->DiscardDecodeRequest(this); 212 host_->DiscardDecodeRequest(this);
210 return; 213 return;
211 } 214 }
212 215
213 gfx::ImageSkia image_skia; 216 gfx::ImageSkia image_skia;
214 image_skia.AddRepresentation(gfx::ImageSkiaRep( 217 image_skia.AddRepresentation(gfx::ImageSkiaRep(
215 bitmap, 218 bitmap,
216 ui::GetScaleForScaleFactor(scale_factor_))); 219 ui::GetScaleForScaleFactor(scale_factor_)));
217
218 host_->Update(&image_skia); 220 host_->Update(&image_skia);
219 host_->DiscardDecodeRequest(this); 221 host_->DiscardDecodeRequest(this);
220 } 222 }
221 223
222 void ArcAppIcon::DecodeRequest::OnDecodeImageFailed() { 224 void ArcAppIcon::DecodeRequest::OnDecodeImageFailed() {
223 VLOG(2) << "Failed to decode ARC icon."; 225 VLOG(2) << "Failed to decode ARC icon.";
224 226
225 if (!host_) 227 if (!host_)
226 return; 228 return;
227 229
230 host_->MaybeRequestIcon(scale_factor_);
228 host_->DiscardDecodeRequest(this); 231 host_->DiscardDecodeRequest(this);
229 } 232 }
230 233
231 //////////////////////////////////////////////////////////////////////////////// 234 ////////////////////////////////////////////////////////////////////////////////
232 // ArcAppIcon 235 // ArcAppIcon
233 236
234 // static 237 // static
235 void ArcAppIcon::DisableSafeDecodingForTesting() { 238 void ArcAppIcon::DisableSafeDecodingForTesting() {
236 disable_safe_decoding = true; 239 disable_safe_decoding = true;
237 } 240 }
(...skipping 25 matching lines...) Expand all
263 return; 266 return;
264 267
265 base::PostTaskWithTraitsAndReplyWithResult( 268 base::PostTaskWithTraitsAndReplyWithResult(
266 FROM_HERE, base::TaskTraits().MayBlock().WithPriority( 269 FROM_HERE, base::TaskTraits().MayBlock().WithPriority(
267 base::TaskPriority::BACKGROUND), 270 base::TaskPriority::BACKGROUND),
268 base::Bind(&ArcAppIcon::ReadOnFileThread, scale_factor, path, 271 base::Bind(&ArcAppIcon::ReadOnFileThread, scale_factor, path,
269 prefs->MaybeGetIconPathForDefaultApp(app_id_, scale_factor)), 272 prefs->MaybeGetIconPathForDefaultApp(app_id_, scale_factor)),
270 base::Bind(&ArcAppIcon::OnIconRead, weak_ptr_factory_.GetWeakPtr())); 273 base::Bind(&ArcAppIcon::OnIconRead, weak_ptr_factory_.GetWeakPtr()));
271 } 274 }
272 275
273 void ArcAppIcon::RequestIcon(ui::ScaleFactor scale_factor) { 276 void ArcAppIcon::MaybeRequestIcon(ui::ScaleFactor scale_factor) {
274 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 277 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
275 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_); 278 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_);
276 DCHECK(prefs); 279 DCHECK(prefs);
277 280
278 // ArcAppListPrefs notifies ArcAppModelBuilder via Observer when icon is ready 281 // ArcAppListPrefs notifies ArcAppModelBuilder via Observer when icon is ready
279 // and ArcAppModelBuilder refreshes the icon of the corresponding item by 282 // and ArcAppModelBuilder refreshes the icon of the corresponding item by
280 // calling LoadScaleFactor. 283 // calling LoadScaleFactor.
281 prefs->RequestIcon(app_id_, scale_factor); 284 prefs->MaybeRequestIcon(app_id_, scale_factor);
282 } 285 }
283 286
284 // static 287 // static
285 std::unique_ptr<ArcAppIcon::ReadResult> ArcAppIcon::ReadOnFileThread( 288 std::unique_ptr<ArcAppIcon::ReadResult> ArcAppIcon::ReadOnFileThread(
286 ui::ScaleFactor scale_factor, 289 ui::ScaleFactor scale_factor,
287 const base::FilePath& path, 290 const base::FilePath& path,
288 const base::FilePath& default_app_path) { 291 const base::FilePath& default_app_path) {
289 DCHECK(!path.empty()); 292 DCHECK(!path.empty());
290 293
291 base::FilePath path_to_read; 294 base::FilePath path_to_read;
292 if (base::PathExists(path)) { 295 if (base::PathExists(path)) {
293 path_to_read = path; 296 path_to_read = path;
294 } else { 297 } else {
295 if (default_app_path.empty() || !base::PathExists(default_app_path)) { 298 if (default_app_path.empty() || !base::PathExists(default_app_path)) {
296 return base::WrapUnique(new ArcAppIcon::ReadResult( 299 return base::MakeUnique<ArcAppIcon::ReadResult>(false, true, scale_factor,
297 false, true, scale_factor, std::string())); 300 std::string());
298 } 301 }
299 path_to_read = default_app_path; 302 path_to_read = default_app_path;
300 } 303 }
301 304
305 bool request_to_install = path_to_read != path;
306
302 // Read the file from disk. 307 // Read the file from disk.
303 std::string unsafe_icon_data; 308 std::string unsafe_icon_data;
304 if (!base::ReadFileToString(path_to_read, &unsafe_icon_data)) { 309 if (!base::ReadFileToString(path_to_read, &unsafe_icon_data) ||
310 unsafe_icon_data.empty()) {
305 VLOG(2) << "Failed to read an ARC icon from file " << path.MaybeAsASCII(); 311 VLOG(2) << "Failed to read an ARC icon from file " << path.MaybeAsASCII();
306 return base::WrapUnique(new ArcAppIcon::ReadResult( 312
307 true, path_to_read != path, scale_factor, std::string())); 313 // If |unsafe_icon_data| is empty typically means we have a file corruption
314 // on cached icon file. Send request to re install the icon.
315 request_to_install |= unsafe_icon_data.empty();
316 return base::MakeUnique<ArcAppIcon::ReadResult>(
317 true, request_to_install, scale_factor, std::string());
308 } 318 }
309 319
310 return base::WrapUnique(new ArcAppIcon::ReadResult(false, 320 return base::MakeUnique<ArcAppIcon::ReadResult>(
311 path_to_read != path, 321 false, request_to_install, scale_factor, unsafe_icon_data);
312 scale_factor,
313 unsafe_icon_data));
314 } 322 }
315 323
316 void ArcAppIcon::OnIconRead( 324 void ArcAppIcon::OnIconRead(
317 std::unique_ptr<ArcAppIcon::ReadResult> read_result) { 325 std::unique_ptr<ArcAppIcon::ReadResult> read_result) {
318 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 326 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
319 327
320 if (read_result->request_to_install) 328 if (read_result->request_to_install)
321 RequestIcon(read_result->scale_factor); 329 MaybeRequestIcon(read_result->scale_factor);
322 330
323 if (!read_result->unsafe_icon_data.empty()) { 331 if (!read_result->unsafe_icon_data.empty()) {
324 decode_requests_.push_back(base::MakeUnique<DecodeRequest>( 332 decode_requests_.push_back(base::MakeUnique<DecodeRequest>(
325 weak_ptr_factory_.GetWeakPtr(), resource_size_in_dip_, 333 weak_ptr_factory_.GetWeakPtr(), resource_size_in_dip_,
326 read_result->scale_factor)); 334 read_result->scale_factor));
327 if (disable_safe_decoding) { 335 if (disable_safe_decoding) {
328 SkBitmap bitmap; 336 SkBitmap bitmap;
329 if (!read_result->unsafe_icon_data.empty() && 337 if (!read_result->unsafe_icon_data.empty() &&
330 gfx::PNGCodec::Decode( 338 gfx::PNGCodec::Decode(
331 reinterpret_cast<const unsigned char*>( 339 reinterpret_cast<const unsigned char*>(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) { 371 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) {
364 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 372 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
365 373
366 auto it = std::find_if(decode_requests_.begin(), decode_requests_.end(), 374 auto it = std::find_if(decode_requests_.begin(), decode_requests_.end(),
367 [request](const std::unique_ptr<DecodeRequest>& ptr) { 375 [request](const std::unique_ptr<DecodeRequest>& ptr) {
368 return ptr.get() == request; 376 return ptr.get() == request;
369 }); 377 });
370 CHECK(it != decode_requests_.end()); 378 CHECK(it != decode_requests_.end());
371 decode_requests_.erase(it); 379 decode_requests_.erase(it);
372 } 380 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/app_list/arc/arc_app_icon.h ('k') | chrome/browser/ui/app_list/arc/arc_app_list_prefs.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698