Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/chromeos/arc/arc_wallpaper_handler.cc

Issue 2061183002: arc: IPC method to set custom wallpaper. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address yusukes' comments. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/bind.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
14 #include "chrome/browser/image_decoder.h"
15 #include "components/signin/core/account_id/account_id.h"
16 #include "components/user_manager/user_manager.h"
17 #include "components/wallpaper/wallpaper_files_id.h"
18 #include "components/wallpaper/wallpaper_layout.h"
19 #include "ui/gfx/image/image_skia.h"
20
21 namespace arc {
22
23 namespace {
24
25 constexpr char kAndroidWallpaperFilename[] = "android.jpg";
26
27 // A thin wrapper of ImageDecoder to decode untrusted JPEG images.
28 class UnsafeJpegImageDecoder : public ImageDecoder::ImageRequest {
hidehiko 2016/06/16 05:49:24 This looks just an adapter of from Callback to Ima
Shuhei Takahashi 2016/06/16 14:56:10 With ImageDecoder::Cancel(), I can do this, thanks
29 public:
30 // Type of the callback function called on image decode completion.
31 // ImageSkia instance can be null value if decoding failed.
rickyz (no longer on Chrome) 2016/06/16 01:38:48 Heh, didn't realize this was referring to ImageSki
Shuhei Takahashi 2016/06/16 14:56:10 That's why I wrote null "value" but it might not b
32 using Callback = base::Callback<void(const gfx::ImageSkia&)>;
33
34 // Starts decoding |jpeg_data| and invokes |callback| on completion.
35 static void Start(const std::vector<uint8_t>& jpeg_data,
36 const Callback& callback);
37
38 // ImageDecoder::ImageRequest implementation.
39 void OnImageDecoded(const SkBitmap& image) override;
40 void OnDecodeImageFailed() override;
41
42 private:
43 UnsafeJpegImageDecoder(const std::vector<uint8_t>& jpeg_data,
44 const Callback& callback);
45 ~UnsafeJpegImageDecoder() override = default;
46
47 Callback callback_;
48
49 DISALLOW_COPY_AND_ASSIGN(UnsafeJpegImageDecoder);
50 };
51
52 // static
53 void UnsafeJpegImageDecoder::Start(const std::vector<uint8_t>& jpeg_data,
54 const Callback& callback) {
55 // Unfortunately ImageDecoder accepts a raw pointer to ImageRequest only,
56 // so callers must not delete ImageRequest instance until decoding finishes.
57 // In our case, let OnImageDecoded/OnDecodeImageFailed delete this
58 // instance on completion.
59 new UnsafeJpegImageDecoder(jpeg_data, callback);
hidehiko 2016/06/16 05:49:24 We should cancel the operation, on the destruction
Shuhei Takahashi 2016/06/16 14:56:10 Ah I did not notice there is ImageDecoder::Cancel(
60 }
61
62 void UnsafeJpegImageDecoder::OnImageDecoded(const SkBitmap& decoded_image) {
63 // Make the SkBitmap immutable as we won't modify it. This is important
64 // because otherwise it gets duplicated during painting, wasting memory.
65 SkBitmap immutable(decoded_image);
66 immutable.setImmutable();
67 gfx::ImageSkia final_image = gfx::ImageSkia::CreateFrom1xBitmap(immutable);
68 final_image.MakeThreadSafe();
69 callback_.Run(final_image);
70 delete this;
71 }
72
73 void UnsafeJpegImageDecoder::OnDecodeImageFailed() {
74 callback_.Run(gfx::ImageSkia());
75 delete this;
76 }
77
78 UnsafeJpegImageDecoder::UnsafeJpegImageDecoder(
79 const std::vector<uint8_t>& jpeg_data,
80 const Callback& callback)
81 : callback_(callback) {
82 std::string jpeg_data_as_string(reinterpret_cast<const char*>(jpeg_data[0]),
rickyz (no longer on Chrome) 2016/06/16 01:38:48 Blech, I'm reading through ImageDecoder::StartWith
hidehiko 2016/06/16 05:49:24 Ah, +1 for :-(. Maybe, we want to fix it separatel
Shuhei Takahashi 2016/06/16 14:56:10 Agreed. I'll leave a TODO here.
83 jpeg_data.size());
84 ImageDecoder::StartWithOptions(this, jpeg_data_as_string,
85 ImageDecoder::ROBUST_JPEG_CODEC, true);
86 }
87
88 // Sets a decoded image as the wallpaper.
89 void SetImageAsWallpaper(const gfx::ImageSkia& image) {
90 if (image.isNull()) {
91 LOG(ERROR) << "Failed to decode wallpaper image.";
92 return;
93 }
94
95 chromeos::WallpaperManager* wallpaper_manager =
96 chromeos::WallpaperManager::Get();
97
98 const AccountId& account_id =
99 user_manager::UserManager::Get()->GetPrimaryUser()->GetAccountId();
100 wallpaper::WallpaperFilesId wallpaper_files_id =
101 wallpaper_manager->GetFilesId(account_id);
102 bool update_wallpaper =
103 account_id ==
104 user_manager::UserManager::Get()->GetActiveUser()->GetAccountId();
105 // TODO(crbug.com/618922): Allow specifying layout.
106 wallpaper_manager->SetCustomWallpaper(
107 account_id, wallpaper_files_id, kAndroidWallpaperFilename,
108 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED,
109 image, update_wallpaper);
110
111 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper
112 // picker. Currently the new wallpaper does not appear there. The best way to
113 // make this happen seems to do the same things as wallpaper_api.cc and
114 // wallpaper_private_api.cc.
115 }
116
117 } // namespace
118
119 void ArcWallpaperHandler::SetWallpaper(const std::vector<uint8_t>& jpeg_data) {
120 UnsafeJpegImageDecoder::Start(jpeg_data, base::Bind(&SetImageAsWallpaper));
121 }
122
123 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698