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_service.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "ash/shell.h" | |
12 #include "ash/wallpaper/wallpaper_controller.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/task_runner_util.h" | |
16 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | |
17 #include "components/arc/arc_bridge_service.h" | |
18 #include "components/signin/core/account_id/account_id.h" | |
19 #include "components/user_manager/user_manager.h" | |
20 #include "components/wallpaper/wallpaper_files_id.h" | |
21 #include "components/wallpaper/wallpaper_layout.h" | |
22 #include "content/public/browser/browser_thread.h" | |
23 #include "ui/gfx/codec/png_codec.h" | |
24 #include "ui/gfx/image/image.h" | |
25 #include "ui/gfx/image/image_skia.h" | |
26 #include "ui/gfx/image/image_util.h" | |
27 | |
28 using user_manager::UserManager; | |
29 | |
30 namespace arc { | |
31 | |
32 namespace { | |
33 | |
34 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | |
35 | |
36 // Sets a decoded bitmap as the wallpaper. | |
37 void SetBitmapAsWallpaper(const SkBitmap& bitmap) { | |
38 // Make the SkBitmap immutable as we won't modify it. This is important | |
39 // because otherwise it gets duplicated during painting, wasting memory. | |
40 SkBitmap immutable_bitmap(bitmap); | |
41 immutable_bitmap.setImmutable(); | |
42 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(immutable_bitmap); | |
43 image.MakeThreadSafe(); | |
44 | |
45 chromeos::WallpaperManager* wallpaper_manager = | |
46 chromeos::WallpaperManager::Get(); | |
47 | |
48 const AccountId& account_id = | |
49 UserManager::Get()->GetPrimaryUser()->GetAccountId(); | |
50 wallpaper::WallpaperFilesId wallpaper_files_id = | |
51 wallpaper_manager->GetFilesId(account_id); | |
52 const bool update_wallpaper = | |
53 account_id == UserManager::Get()->GetActiveUser()->GetAccountId(); | |
54 // TODO(crbug.com/618922): Allow specifying layout. | |
55 wallpaper_manager->SetCustomWallpaper( | |
56 account_id, wallpaper_files_id, kAndroidWallpaperFilename, | |
57 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, | |
58 image, update_wallpaper); | |
59 | |
60 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper | |
61 // picker. Currently the new wallpaper does not appear there. The best way to | |
62 // make this happen seems to do the same things as wallpaper_api.cc and | |
63 // wallpaper_private_api.cc. | |
64 } | |
65 | |
66 std::vector<uint8_t> EncodeImagePng(const gfx::ImageSkia image) { | |
67 std::vector<uint8_t> result; | |
68 gfx::PNGCodec::FastEncodeBGRASkBitmap(*image.bitmap(), true, &result); | |
69 return result; | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) | |
75 : ArcService(bridge_service), binding_(this) { | |
76 arc_bridge_service()->wallpaper()->AddObserver(this); | |
77 DCHECK(!ArcWallpaperService::instance()); | |
78 ArcWallpaperService::set_instance(this); | |
79 } | |
80 | |
81 ArcWallpaperService::~ArcWallpaperService() { | |
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
83 // Make sure the callback is never called after destruction. It is safe to | |
84 // call Cancel() even when there is no in-flight request. | |
85 ImageDecoder::Cancel(this); | |
86 arc_bridge_service()->wallpaper()->RemoveObserver(this); | |
87 DCHECK(ArcWallpaperService::instance() == this); | |
88 ArcWallpaperService::set_instance(nullptr); | |
89 } | |
90 | |
91 void ArcWallpaperService::OnInstanceReady() { | |
92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
93 mojom::WallpaperInstance* wallpaper_instance = | |
94 arc_bridge_service()->wallpaper()->instance(); | |
95 if (!wallpaper_instance) { | |
96 LOG(DFATAL) << "OnWallpaperInstanceReady called, " | |
97 << "but no wallpaper instance found"; | |
98 return; | |
99 } | |
100 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
101 } | |
102 | |
103 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | |
104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
105 ImageDecoder::Cancel(this); | |
106 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), | |
107 ImageDecoder::ROBUST_PNG_CODEC, true); | |
108 } | |
109 | |
110 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { | |
111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
112 ash::WallpaperController* wc = | |
113 ash::Shell::GetInstance()->wallpaper_controller(); | |
114 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | |
115 base::PostTaskAndReplyWithResult( | |
116 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
117 base::Bind(&EncodeImagePng, wallpaper), callback); | |
118 } | |
119 | |
120 void ArcWallpaperService::SetWallpaperJpeg( | |
121 const std::vector<uint8_t>& jpeg_data) { | |
122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
123 // If there is an in-flight request, cancel it. It is safe to call Cancel() | |
124 // even when there is no in-flight request. | |
125 ImageDecoder::Cancel(this); | |
126 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), | |
127 ImageDecoder::ROBUST_JPEG_CODEC, true); | |
128 } | |
129 | |
130 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { | |
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
132 SetBitmapAsWallpaper(bitmap); | |
133 } | |
134 | |
135 void ArcWallpaperService::OnDecodeImageFailed() { | |
136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
137 LOG(ERROR) << "Failed to decode wallpaper image."; | |
138 } | |
139 | |
140 } // namespace arc | |
OLD | NEW |