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 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.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" | |
| 15 #include "components/user_manager/user_manager.h" | |
| 16 #include "components/wallpaper/wallpaper_files_id.h" | |
| 17 #include "components/wallpaper/wallpaper_layout.h" | |
| 18 #include "ui/gfx/image/image_skia.h" | |
| 19 | |
| 20 namespace arc { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | |
| 25 | |
| 26 // A thin wrapper of ImageDecoder to decode untrusted JPEG images. | |
| 27 class UnsafeJpegImageDecoder : public ImageDecoder::ImageRequest { | |
| 28 public: | |
| 29 // Type of the callback function called on image decode completion. | |
| 30 // ImageSkia instance can be null value if decoding failed. | |
| 31 using Callback = base::Callback<void(const gfx::ImageSkia&)>; | |
| 32 | |
| 33 // Starts decoding |jpeg_data| and invokes |callback| on completion. | |
| 34 static void Start(const std::string& jpeg_data, const Callback& callback); | |
| 35 | |
| 36 // ImageDecoder::ImageRequest implementation. | |
| 37 void OnImageDecoded(const SkBitmap& image) override; | |
| 38 void OnDecodeImageFailed() override; | |
| 39 | |
| 40 private: | |
| 41 UnsafeJpegImageDecoder(const std::string& jpeg_data, | |
| 42 const Callback& callback); | |
| 43 ~UnsafeJpegImageDecoder() {} | |
|
Yusuke Sato
2016/06/15 19:44:32
override = default;
Shuhei Takahashi
2016/06/15 22:58:38
Done.
| |
| 44 | |
| 45 Callback callback_; | |
| 46 | |
| 47 DISALLOW_COPY_AND_ASSIGN(UnsafeJpegImageDecoder); | |
| 48 }; | |
| 49 | |
| 50 void UnsafeJpegImageDecoder::Start(const std::string& jpeg_data, | |
|
Yusuke Sato
2016/06/15 19:44:32
nit: What about adding '// static' here? I was a l
Shuhei Takahashi
2016/06/15 22:58:38
Done.
| |
| 51 const Callback& callback) { | |
| 52 // Unfortunately ImageDecoder accepts a raw pointer to ImageRequest only, | |
| 53 // so callers must not delete ImageRequest instance until decoding finishes. | |
| 54 // In our case, let OnImageDecoded/OnDecodeImageFailed delete this | |
| 55 // instance on completion. | |
| 56 new UnsafeJpegImageDecoder(jpeg_data, callback); | |
| 57 } | |
| 58 | |
| 59 void UnsafeJpegImageDecoder::OnImageDecoded(const SkBitmap& decoded_image) { | |
| 60 SkBitmap immutable(decoded_image); | |
|
Yusuke Sato
2016/06/15 19:44:32
Can you add a short comment on why setImmutable()
Shuhei Takahashi
2016/06/15 22:58:38
Hmm, actually I just followed the way chrome/brows
| |
| 61 immutable.setImmutable(); | |
| 62 gfx::ImageSkia final_image = gfx::ImageSkia::CreateFrom1xBitmap(immutable); | |
| 63 final_image.MakeThreadSafe(); | |
| 64 callback_.Run(final_image); | |
| 65 delete this; | |
| 66 } | |
| 67 | |
| 68 void UnsafeJpegImageDecoder::OnDecodeImageFailed() { | |
| 69 callback_.Run(gfx::ImageSkia()); | |
| 70 delete this; | |
| 71 } | |
| 72 | |
| 73 UnsafeJpegImageDecoder::UnsafeJpegImageDecoder(const std::string& jpeg_data, | |
| 74 const Callback& callback) | |
| 75 : callback_(callback) { | |
| 76 ImageDecoder::StartWithOptions(this, jpeg_data, | |
| 77 ImageDecoder::ROBUST_JPEG_CODEC, true); | |
| 78 } | |
| 79 | |
| 80 // Sets a decoded image as the wallpaper. | |
| 81 void SetImageAsWallpaper(const gfx::ImageSkia& image) { | |
| 82 if (image.isNull()) { | |
|
Yusuke Sato
2016/06/15 19:44:32
Does this catch the callback_.Run(gfx::ImageSkia()
Shuhei Takahashi
2016/06/15 22:58:38
Yes.
https://cs.chromium.org/chromium/src/ui/gfx/
| |
| 83 LOG(ERROR) << "Failed to decode wallpaper image."; | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 chromeos::WallpaperManager* wallpaper_manager = | |
| 88 chromeos::WallpaperManager::Get(); | |
| 89 | |
| 90 const AccountId& account_id = | |
| 91 user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId(); | |
| 92 wallpaper::WallpaperFilesId wallpaper_files_id = | |
| 93 wallpaper_manager->GetFilesId(account_id); | |
| 94 bool update_wallpaper = | |
| 95 account_id == | |
| 96 user_manager::UserManager::Get()->GetActiveUser()->GetAccountId(); | |
| 97 // TODO(crbug.com/618922): Allow specifying layout. | |
| 98 wallpaper_manager->SetCustomWallpaper( | |
| 99 account_id, wallpaper_files_id, kAndroidWallpaperFilename, | |
| 100 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, | |
| 101 image, update_wallpaper); | |
| 102 | |
| 103 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper | |
| 104 // picker. Currently the new wallpaper does not appear there. The best way to | |
| 105 // make this happen seems to do the same things as wallpaper_api.cc and | |
| 106 // wallpaper_private_api.cc. | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 void ArcWallpaperHandler::SetWallpaper(const std::string& jpeg_data) { | |
| 112 UnsafeJpegImageDecoder::Start(jpeg_data, base::Bind(&SetImageAsWallpaper)); | |
| 113 } | |
| 114 | |
| 115 } // namespace arc | |
| OLD | NEW |