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