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