Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chromeos/arc/arc_wallpaper_handler.h" | 5 #include "chrome/browser/chromeos/arc/arc_wallpaper_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 12 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
| 13 #include "chrome/browser/image_decoder.h" | |
| 14 #include "components/signin/core/account_id/account_id.h" | 13 #include "components/signin/core/account_id/account_id.h" |
| 15 #include "components/user_manager/user_manager.h" | 14 #include "components/user_manager/user_manager.h" |
| 16 #include "components/wallpaper/wallpaper_files_id.h" | 15 #include "components/wallpaper/wallpaper_files_id.h" |
| 17 #include "components/wallpaper/wallpaper_layout.h" | 16 #include "components/wallpaper/wallpaper_layout.h" |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 #include "ui/gfx/image/image_skia.h" | 18 #include "ui/gfx/image/image_skia.h" |
| 20 | 19 |
| 21 using user_manager::UserManager; | 20 using user_manager::UserManager; |
| 22 | 21 |
| 23 namespace arc { | 22 namespace arc { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 51 image, update_wallpaper); | 50 image, update_wallpaper); |
| 52 | 51 |
| 53 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper | 52 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper |
| 54 // picker. Currently the new wallpaper does not appear there. The best way to | 53 // picker. Currently the new wallpaper does not appear there. The best way to |
| 55 // make this happen seems to do the same things as wallpaper_api.cc and | 54 // make this happen seems to do the same things as wallpaper_api.cc and |
| 56 // wallpaper_private_api.cc. | 55 // wallpaper_private_api.cc. |
| 57 } | 56 } |
| 58 | 57 |
| 59 } // namespace | 58 } // namespace |
| 60 | 59 |
| 61 // An implementation of ImageDecoder::ImageRequest that just calls back | |
| 62 // ArcWallpaperHandler. | |
| 63 class ArcWallpaperHandler::ImageRequestImpl | |
| 64 : public ImageDecoder::ImageRequest { | |
| 65 public: | |
| 66 // |handler| outlives this instance. | |
| 67 explicit ImageRequestImpl(ArcWallpaperHandler* handler) : handler_(handler) {} | |
| 68 | |
| 69 // ImageDecoder::ImageRequest implementation. | |
| 70 void OnImageDecoded(const SkBitmap& bitmap) override { | |
| 71 handler_->OnImageDecoded(this, bitmap); | |
| 72 } | |
| 73 void OnDecodeImageFailed() override { handler_->OnDecodeImageFailed(this); } | |
| 74 | |
| 75 private: | |
| 76 ArcWallpaperHandler* const handler_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(ImageRequestImpl); | |
| 79 }; | |
| 80 | |
| 81 ArcWallpaperHandler::ArcWallpaperHandler() = default; | 60 ArcWallpaperHandler::ArcWallpaperHandler() = default; |
| 82 | 61 |
| 83 ArcWallpaperHandler::~ArcWallpaperHandler() { | 62 ArcWallpaperHandler::~ArcWallpaperHandler() { |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 85 // Cancel in-flight requests. | 64 // Make sure the callback is never called after destruction. It is safe to |
| 86 for (auto& request : inflight_requests_) | 65 // call Cancel() even when there is no in-flight request. |
| 87 ImageDecoder::Cancel(request.get()); | 66 ImageDecoder::Cancel(this); |
| 88 inflight_requests_.clear(); | |
| 89 } | 67 } |
| 90 | 68 |
| 91 void ArcWallpaperHandler::SetWallpaper(const std::vector<uint8_t>& jpeg_data) { | 69 void ArcWallpaperHandler::SetWallpaper(const std::vector<uint8_t>& jpeg_data) { |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 93 std::unique_ptr<ImageRequestImpl> request = | |
| 94 base::MakeUnique<ImageRequestImpl>(this); | |
| 95 // TODO(nya): Improve ImageDecoder to minimize copy. | 71 // TODO(nya): Improve ImageDecoder to minimize copy. |
|
hidehiko
2016/07/26 06:19:10
As we chatted offline, to make the behavior slight
Shuhei Takahashi
2016/07/26 06:22:07
Done.
| |
| 96 std::string jpeg_data_as_string( | 72 std::string jpeg_data_as_string( |
| 97 reinterpret_cast<const char*>(jpeg_data.data()), jpeg_data.size()); | 73 reinterpret_cast<const char*>(jpeg_data.data()), jpeg_data.size()); |
| 98 ImageDecoder::StartWithOptions(request.get(), jpeg_data_as_string, | 74 ImageDecoder::StartWithOptions(this, jpeg_data_as_string, |
| 99 ImageDecoder::ROBUST_JPEG_CODEC, true); | 75 ImageDecoder::ROBUST_JPEG_CODEC, true); |
| 100 inflight_requests_.insert(std::move(request)); | |
| 101 } | 76 } |
| 102 | 77 |
| 103 void ArcWallpaperHandler::OnImageDecoded(ImageRequestImpl* request, | 78 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { |
| 104 const SkBitmap& bitmap) { | |
| 105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 106 inflight_requests_.erase(base::WrapUnique(request)); | |
| 107 SetBitmapAsWallpaper(bitmap); | 80 SetBitmapAsWallpaper(bitmap); |
| 108 } | 81 } |
| 109 | 82 |
| 110 void ArcWallpaperHandler::OnDecodeImageFailed(ImageRequestImpl* request) { | 83 void ArcWallpaperHandler::OnDecodeImageFailed() { |
| 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 112 inflight_requests_.erase(base::WrapUnique(request)); | |
| 113 LOG(ERROR) << "Failed to decode wallpaper image."; | 85 LOG(ERROR) << "Failed to decode wallpaper image."; |
| 114 } | 86 } |
| 115 | 87 |
| 116 } // namespace arc | 88 } // namespace arc |
| OLD | NEW |