Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/arc/arc_wallpaper_handler.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | |
| 13 #include "components/signin/core/account_id/account_id.h" | |
| 14 #include "components/user_manager/user_manager.h" | |
| 15 #include "components/wallpaper/wallpaper_files_id.h" | |
| 16 #include "components/wallpaper/wallpaper_layout.h" | |
| 17 #include "ui/gfx/image/image_skia.h" | |
| 18 | |
| 19 namespace arc { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | |
| 24 | |
| 25 // Sets a decoded bitmap as the wallpaper. | |
| 26 void SetBitmapAsWallpaper(const SkBitmap& bitmap) { | |
| 27 // Make the SkBitmap immutable as we won't modify it. This is important | |
| 28 // because otherwise it gets duplicated during painting, wasting memory. | |
| 29 SkBitmap immutable_bitmap(bitmap); | |
| 30 immutable_bitmap.setImmutable(); | |
| 31 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(immutable_bitmap); | |
| 32 image.MakeThreadSafe(); | |
| 33 | |
| 34 chromeos::WallpaperManager* wallpaper_manager = | |
| 35 chromeos::WallpaperManager::Get(); | |
| 36 | |
| 37 const AccountId& account_id = | |
| 38 user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId(); | |
| 39 wallpaper::WallpaperFilesId wallpaper_files_id = | |
| 40 wallpaper_manager->GetFilesId(account_id); | |
| 41 bool update_wallpaper = | |
| 42 account_id == | |
| 43 user_manager::UserManager::Get()->GetActiveUser()->GetAccountId(); | |
| 44 // TODO(crbug.com/618922): Allow specifying layout. | |
| 45 wallpaper_manager->SetCustomWallpaper( | |
| 46 account_id, wallpaper_files_id, kAndroidWallpaperFilename, | |
| 47 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, | |
| 48 image, update_wallpaper); | |
| 49 | |
| 50 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper | |
| 51 // picker. Currently the new wallpaper does not appear there. The best way to | |
| 52 // make this happen seems to do the same things as wallpaper_api.cc and | |
| 53 // wallpaper_private_api.cc. | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 // An implementation of ImageDecoder::ImageRequest that just calls back | |
| 59 // ArcWallpaperHandler. | |
| 60 class ArcWallpaperHandler::ImageRequestImpl | |
| 61 : public ImageDecoder::ImageRequest { | |
| 62 public: | |
| 63 // |handler| outlives this instance. | |
| 64 explicit ImageRequestImpl(ArcWallpaperHandler* handler) : handler_(handler) {} | |
| 65 | |
| 66 // ImageDecoder::ImageRequest implementation. | |
| 67 void OnImageDecoded(const SkBitmap& bitmap) override { | |
| 68 handler_->OnImageDecoded(this, bitmap); | |
| 69 } | |
| 70 void OnDecodeImageFailed() override { handler_->OnDecodeImageFailed(this); } | |
| 71 | |
| 72 private: | |
| 73 ArcWallpaperHandler* handler_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(ImageRequestImpl); | |
| 76 }; | |
| 77 | |
| 78 ArcWallpaperHandler::ArcWallpaperHandler() = default; | |
| 79 | |
| 80 ArcWallpaperHandler::~ArcWallpaperHandler() { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 // Cancel in-flight requests. | |
| 83 for (ImageRequestImpl* request : inflight_requests_) { | |
| 84 ImageDecoder::Cancel(request); | |
| 85 delete request; | |
| 86 } | |
| 87 inflight_requests_.clear(); | |
| 88 } | |
| 89 | |
| 90 void ArcWallpaperHandler::SetWallpaper(const std::vector<uint8_t>& jpeg_data) { | |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 92 std::unique_ptr<ImageRequestImpl> request = | |
| 93 base::MakeUnique<ImageRequestImpl>(this); | |
| 94 // TODO(nya): Improve ImageDecoder to minimize copy. | |
| 95 std::string jpeg_data_as_string(reinterpret_cast<const char*>(jpeg_data[0]), | |
| 96 jpeg_data.size()); | |
| 97 ImageDecoder::StartWithOptions(request.get(), jpeg_data_as_string, | |
| 98 ImageDecoder::ROBUST_JPEG_CODEC, true); | |
| 99 inflight_requests_.insert(request.release()); | |
| 100 } | |
| 101 | |
| 102 void ArcWallpaperHandler::OnImageDecoded(ImageRequestImpl* request, | |
| 103 const SkBitmap& bitmap) { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 inflight_requests_.erase(request); | |
| 106 delete request; | |
|
dcheng
2016/06/17 13:23:27
Just curious, but do we really need to handle mult
Shuhei Takahashi
2016/06/20 12:10:30
You're right. That sounds better. Let me address i
| |
| 107 SetBitmapAsWallpaper(bitmap); | |
| 108 } | |
| 109 | |
| 110 void ArcWallpaperHandler::OnDecodeImageFailed(ImageRequestImpl* request) { | |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 112 inflight_requests_.erase(request); | |
| 113 delete request; | |
| 114 LOG(ERROR) << "Failed to decode wallpaper image."; | |
| 115 } | |
| 116 | |
| 117 } // namespace arc | |
| OLD | NEW |