| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 15 #include "chrome/browser/image_decoder.h" | 15 #include "chrome/browser/image_decoder.h" |
| 16 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | 16 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 17 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" |
| 18 #include "chrome/grit/component_extension_resources.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 #include "extensions/grit/extensions_browser_resources.h" | 20 #include "extensions/grit/extensions_browser_resources.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
| 22 #include "ui/gfx/image/image_skia_operations.h" | 24 #include "ui/gfx/image/image_skia_operations.h" |
| 23 #include "ui/gfx/image/image_skia_source.h" | 25 #include "ui/gfx/image/image_skia_source.h" |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 bool disable_safe_decoding = false; | 29 bool disable_safe_decoding = false; |
| 28 | 30 |
| 29 } // namespace | 31 } // namespace |
| 30 | 32 |
| 31 //////////////////////////////////////////////////////////////////////////////// | 33 //////////////////////////////////////////////////////////////////////////////// |
| 32 // ArcAppIcon::ReadResult | 34 // ArcAppIcon::ReadResult |
| 33 | 35 |
| 34 struct ArcAppIcon::ReadResult { | 36 struct ArcAppIcon::ReadResult { |
| 35 enum class Status { | 37 ReadResult(bool error, |
| 36 OK, | 38 bool request_to_install, |
| 37 FAIL, | 39 ui::ScaleFactor scale_factor, |
| 38 REQUEST_TO_INSTALL, | 40 std::string unsafe_icon_data) |
| 39 }; | 41 : error(error), |
| 40 | 42 request_to_install(request_to_install), |
| 41 // Used for fail results or for the requests to install an icon. | |
| 42 ReadResult(Status status, ui::ScaleFactor scale_factor) | |
| 43 : status(status), scale_factor(scale_factor) { | |
| 44 DCHECK(status == Status::FAIL || status == Status::REQUEST_TO_INSTALL); | |
| 45 } | |
| 46 | |
| 47 // Used for successful results. | |
| 48 ReadResult(ui::ScaleFactor scale_factor, | |
| 49 const std::string& unsafe_icon_data) | |
| 50 : status(Status::OK), | |
| 51 scale_factor(scale_factor), | 43 scale_factor(scale_factor), |
| 52 unsafe_icon_data(unsafe_icon_data) { | 44 unsafe_icon_data(unsafe_icon_data) { |
| 53 } | 45 } |
| 54 | 46 |
| 55 Status status; | 47 bool error; |
| 48 bool request_to_install; |
| 56 ui::ScaleFactor scale_factor; | 49 ui::ScaleFactor scale_factor; |
| 57 std::string unsafe_icon_data; | 50 std::string unsafe_icon_data; |
| 58 }; | 51 }; |
| 59 | 52 |
| 60 //////////////////////////////////////////////////////////////////////////////// | 53 //////////////////////////////////////////////////////////////////////////////// |
| 61 // ArcAppIcon::Source | 54 // ArcAppIcon::Source |
| 62 | 55 |
| 63 class ArcAppIcon::Source : public gfx::ImageSkiaSource { | 56 class ArcAppIcon::Source : public gfx::ImageSkiaSource { |
| 64 public: | 57 public: |
| 65 Source(const base::WeakPtr<ArcAppIcon>& host, int resource_size_in_dip); | 58 Source(const base::WeakPtr<ArcAppIcon>& host, int resource_size_in_dip); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 85 } | 78 } |
| 86 | 79 |
| 87 ArcAppIcon::Source::~Source() { | 80 ArcAppIcon::Source::~Source() { |
| 88 } | 81 } |
| 89 | 82 |
| 90 gfx::ImageSkiaRep ArcAppIcon::Source::GetImageForScale(float scale) { | 83 gfx::ImageSkiaRep ArcAppIcon::Source::GetImageForScale(float scale) { |
| 91 if (host_) | 84 if (host_) |
| 92 host_->LoadForScaleFactor(ui::GetSupportedScaleFactor(scale)); | 85 host_->LoadForScaleFactor(ui::GetSupportedScaleFactor(scale)); |
| 93 | 86 |
| 94 // Host loads icon asynchronously, so use default icon so far. | 87 // Host loads icon asynchronously, so use default icon so far. |
| 88 const int resource_id = host_ && host_->app_id() == arc::kPlayStoreAppId ? |
| 89 IDR_ARC_SUPPORT_ICON : IDR_APP_DEFAULT_ICON; |
| 95 const gfx::ImageSkia* default_image = ResourceBundle::GetSharedInstance(). | 90 const gfx::ImageSkia* default_image = ResourceBundle::GetSharedInstance(). |
| 96 GetImageSkiaNamed(IDR_APP_DEFAULT_ICON); | 91 GetImageSkiaNamed(resource_id); |
| 97 CHECK(default_image); | 92 CHECK(default_image); |
| 98 return gfx::ImageSkiaOperations::CreateResizedImage( | 93 return gfx::ImageSkiaOperations::CreateResizedImage( |
| 99 *default_image, | 94 *default_image, |
| 100 skia::ImageOperations::RESIZE_BEST, | 95 skia::ImageOperations::RESIZE_BEST, |
| 101 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)). | 96 gfx::Size(resource_size_in_dip_, resource_size_in_dip_)). |
| 102 GetRepresentation(scale); | 97 GetRepresentation(scale); |
| 103 } | 98 } |
| 104 | 99 |
| 105 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest { | 100 class ArcAppIcon::DecodeRequest : public ImageDecoder::ImageRequest { |
| 106 public: | 101 public: |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 } | 190 } |
| 196 | 191 |
| 197 void ArcAppIcon::LoadForScaleFactor(ui::ScaleFactor scale_factor) { | 192 void ArcAppIcon::LoadForScaleFactor(ui::ScaleFactor scale_factor) { |
| 198 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_); | 193 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_); |
| 199 DCHECK(prefs); | 194 DCHECK(prefs); |
| 200 | 195 |
| 201 base::FilePath path = prefs->GetIconPath(app_id_, scale_factor); | 196 base::FilePath path = prefs->GetIconPath(app_id_, scale_factor); |
| 202 if (path.empty()) | 197 if (path.empty()) |
| 203 return; | 198 return; |
| 204 | 199 |
| 205 base::PostTaskAndReplyWithResult(content::BrowserThread::GetBlockingPool(), | 200 base::PostTaskAndReplyWithResult( |
| 206 FROM_HERE, | 201 content::BrowserThread::GetBlockingPool(), |
| 207 base::Bind(&ArcAppIcon::ReadOnFileThread, | 202 FROM_HERE, |
| 208 scale_factor, | 203 base::Bind(&ArcAppIcon::ReadOnFileThread, |
| 209 path), | 204 scale_factor, |
| 210 base::Bind(&ArcAppIcon::OnIconRead, | 205 path, |
| 211 weak_ptr_factory_.GetWeakPtr())); | 206 prefs->MaybeGetIconPathForDefaultApp(app_id_, scale_factor)), |
| 207 base::Bind(&ArcAppIcon::OnIconRead, weak_ptr_factory_.GetWeakPtr())); |
| 212 } | 208 } |
| 213 | 209 |
| 214 void ArcAppIcon::RequestIcon(ui::ScaleFactor scale_factor) { | 210 void ArcAppIcon::RequestIcon(ui::ScaleFactor scale_factor) { |
| 215 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 211 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 216 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_); | 212 ArcAppListPrefs* prefs = ArcAppListPrefs::Get(context_); |
| 217 DCHECK(prefs); | 213 DCHECK(prefs); |
| 218 | 214 |
| 219 // ArcAppListPrefs notifies ArcAppModelBuilder via Observer when icon is ready | 215 // ArcAppListPrefs notifies ArcAppModelBuilder via Observer when icon is ready |
| 220 // and ArcAppModelBuilder refreshes the icon of the corresponding item by | 216 // and ArcAppModelBuilder refreshes the icon of the corresponding item by |
| 221 // calling LoadScaleFactor. | 217 // calling LoadScaleFactor. |
| 222 prefs->RequestIcon(app_id_, scale_factor); | 218 prefs->RequestIcon(app_id_, scale_factor); |
| 223 } | 219 } |
| 224 | 220 |
| 225 // static | 221 // static |
| 226 std::unique_ptr<ArcAppIcon::ReadResult> ArcAppIcon::ReadOnFileThread( | 222 std::unique_ptr<ArcAppIcon::ReadResult> ArcAppIcon::ReadOnFileThread( |
| 227 ui::ScaleFactor scale_factor, | 223 ui::ScaleFactor scale_factor, |
| 228 const base::FilePath& path) { | 224 const base::FilePath& path, |
| 225 const base::FilePath& default_app_path) { |
| 229 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 226 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 230 DCHECK(!path.empty()); | 227 DCHECK(!path.empty()); |
| 231 | 228 |
| 232 if (!base::PathExists(path)) | 229 base::FilePath path_to_read; |
| 233 return base::WrapUnique(new ArcAppIcon::ReadResult( | 230 if (base::PathExists(path)) { |
| 234 ArcAppIcon::ReadResult::Status::REQUEST_TO_INSTALL, scale_factor)); | 231 path_to_read = path; |
| 232 } else { |
| 233 if (default_app_path.empty() || !base::PathExists(default_app_path)) { |
| 234 return base::WrapUnique(new ArcAppIcon::ReadResult( |
| 235 false, true, scale_factor, std::string())); |
| 236 } |
| 237 path_to_read = default_app_path; |
| 238 } |
| 235 | 239 |
| 236 // Read the file from disk. | 240 // Read the file from disk. |
| 237 std::string unsafe_icon_data; | 241 std::string unsafe_icon_data; |
| 238 if (!base::ReadFileToString(path, &unsafe_icon_data)) { | 242 if (!base::ReadFileToString(path_to_read, &unsafe_icon_data)) { |
| 239 VLOG(2) << "Failed to read an ARC icon from file " << path.MaybeAsASCII(); | 243 VLOG(2) << "Failed to read an ARC icon from file " << path.MaybeAsASCII(); |
| 240 return base::WrapUnique(new ArcAppIcon::ReadResult( | 244 return base::WrapUnique(new ArcAppIcon::ReadResult( |
| 241 ArcAppIcon::ReadResult::Status::FAIL, scale_factor)); | 245 true, path_to_read != path, scale_factor, std::string())); |
| 242 } | 246 } |
| 243 | 247 |
| 244 return base::WrapUnique( | 248 return base::WrapUnique(new ArcAppIcon::ReadResult(false, |
| 245 new ArcAppIcon::ReadResult(scale_factor, unsafe_icon_data)); | 249 path_to_read != path, |
| 250 scale_factor, |
| 251 unsafe_icon_data)); |
| 246 } | 252 } |
| 247 | 253 |
| 248 void ArcAppIcon::OnIconRead( | 254 void ArcAppIcon::OnIconRead( |
| 249 std::unique_ptr<ArcAppIcon::ReadResult> read_result) { | 255 std::unique_ptr<ArcAppIcon::ReadResult> read_result) { |
| 250 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 256 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 251 | 257 |
| 252 switch (read_result->status) { | 258 if (read_result->request_to_install) |
| 253 case ReadResult::Status::OK: | 259 RequestIcon(read_result->scale_factor); |
| 260 |
| 261 if (!read_result->unsafe_icon_data.empty()) { |
| 254 decode_requests_.push_back( | 262 decode_requests_.push_back( |
| 255 new DecodeRequest(weak_ptr_factory_.GetWeakPtr(), | 263 new DecodeRequest(weak_ptr_factory_.GetWeakPtr(), |
| 256 resource_size_in_dip_, | 264 resource_size_in_dip_, |
| 257 read_result->scale_factor)); | 265 read_result->scale_factor)); |
| 258 if (disable_safe_decoding) { | 266 if (disable_safe_decoding) { |
| 259 SkBitmap bitmap; | 267 SkBitmap bitmap; |
| 260 if (!read_result->unsafe_icon_data.empty() && | 268 if (!read_result->unsafe_icon_data.empty() && |
| 261 gfx::PNGCodec::Decode( | 269 gfx::PNGCodec::Decode( |
| 262 reinterpret_cast<const unsigned char*>( | 270 reinterpret_cast<const unsigned char*>( |
| 263 &read_result->unsafe_icon_data.front()), | 271 &read_result->unsafe_icon_data.front()), |
| 264 read_result->unsafe_icon_data.length(), | 272 read_result->unsafe_icon_data.length(), |
| 265 &bitmap)) { | 273 &bitmap)) { |
| 266 decode_requests_.back()->OnImageDecoded(bitmap); | 274 decode_requests_.back()->OnImageDecoded(bitmap); |
| 267 } else { | 275 } else { |
| 268 decode_requests_.back()->OnDecodeImageFailed(); | 276 decode_requests_.back()->OnDecodeImageFailed(); |
| 269 } | 277 } |
| 270 } else { | 278 } else { |
| 271 ImageDecoder::Start(decode_requests_.back(), | 279 ImageDecoder::Start(decode_requests_.back(), |
| 272 read_result->unsafe_icon_data); | 280 read_result->unsafe_icon_data); |
| 273 } | 281 } |
| 274 break; | |
| 275 case ReadResult::Status::FAIL: | |
| 276 break; | |
| 277 case ReadResult::Status::REQUEST_TO_INSTALL: | |
| 278 RequestIcon(read_result->scale_factor); | |
| 279 break; | |
| 280 default: | |
| 281 NOTREACHED(); | |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 | 284 |
| 285 void ArcAppIcon::Update(const gfx::ImageSkia* image) { | 285 void ArcAppIcon::Update(const gfx::ImageSkia* image) { |
| 286 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 286 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 287 CHECK(image && !image->isNull()); | 287 CHECK(image && !image->isNull()); |
| 288 | 288 |
| 289 std::vector<gfx::ImageSkiaRep> reps = image->image_reps(); | 289 std::vector<gfx::ImageSkiaRep> reps = image->image_reps(); |
| 290 for (const auto& image_rep : reps) { | 290 for (const auto& image_rep : reps) { |
| 291 if (ui::IsSupportedScale(image_rep.scale())) { | 291 if (ui::IsSupportedScale(image_rep.scale())) { |
| 292 image_skia_.RemoveRepresentation(image_rep.scale()); | 292 image_skia_.RemoveRepresentation(image_rep.scale()); |
| 293 image_skia_.AddRepresentation(image_rep); | 293 image_skia_.AddRepresentation(image_rep); |
| 294 } | 294 } |
| 295 } | 295 } |
| 296 | 296 |
| 297 image_ = gfx::Image(image_skia_); | 297 image_ = gfx::Image(image_skia_); |
| 298 | 298 |
| 299 observer_->OnIconUpdated(this); | 299 observer_->OnIconUpdated(this); |
| 300 } | 300 } |
| 301 | 301 |
| 302 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) { | 302 void ArcAppIcon::DiscardDecodeRequest(DecodeRequest* request) { |
| 303 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 303 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 304 | 304 |
| 305 ScopedVector<DecodeRequest>::iterator it = std::find(decode_requests_.begin(), | 305 ScopedVector<DecodeRequest>::iterator it = std::find(decode_requests_.begin(), |
| 306 decode_requests_.end(), | 306 decode_requests_.end(), |
| 307 request); | 307 request); |
| 308 CHECK(it != decode_requests_.end()); | 308 CHECK(it != decode_requests_.end()); |
| 309 decode_requests_.erase(it); | 309 decode_requests_.erase(it); |
| 310 } | 310 } |
| OLD | NEW |