OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/arc/arc_wallpaper_service.h" | 5 #include "chrome/browser/chromeos/arc/arc_wallpaper_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
Yusuke Sato
2016/09/16 21:23:50
same (line 7-8)
Muyuan
2016/09/16 21:38:48
Done.
| |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "ash/common/wallpaper/wallpaper_controller.h" | 11 #include "ash/common/wallpaper/wallpaper_controller.h" |
12 #include "ash/common/wm_shell.h" | 12 #include "ash/common/wm_shell.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
16 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 16 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
17 #include "components/arc/arc_bridge_service.h" | 17 #include "components/arc/arc_bridge_service.h" |
18 #include "components/signin/core/account_id/account_id.h" | 18 #include "components/signin/core/account_id/account_id.h" |
19 #include "components/user_manager/user_manager.h" | 19 #include "components/user_manager/user_manager.h" |
20 #include "components/wallpaper/wallpaper_files_id.h" | 20 #include "components/wallpaper/wallpaper_files_id.h" |
21 #include "components/wallpaper/wallpaper_layout.h" | 21 #include "components/wallpaper/wallpaper_layout.h" |
22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
23 #include "ui/gfx/codec/png_codec.h" | 23 #include "ui/gfx/codec/png_codec.h" |
24 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
25 #include "ui/gfx/image/image_skia.h" | 25 #include "ui/gfx/image/image_skia.h" |
26 #include "ui/gfx/image/image_util.h" | 26 #include "ui/gfx/image/image_util.h" |
27 | 27 |
28 using user_manager::UserManager; | 28 using user_manager::UserManager; |
29 | 29 |
30 namespace arc { | 30 namespace arc { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 constexpr uint32_t kMinOnWallpaperChangedVersion = 1; | |
34 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | 35 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; |
35 | 36 |
36 // Sets a decoded bitmap as the wallpaper. | 37 // Sets a decoded bitmap as the wallpaper. |
37 void SetBitmapAsWallpaper(const SkBitmap& bitmap) { | 38 void SetBitmapAsWallpaper(const SkBitmap& bitmap) { |
38 // Make the SkBitmap immutable as we won't modify it. This is important | 39 // Make the SkBitmap immutable as we won't modify it. This is important |
39 // because otherwise it gets duplicated during painting, wasting memory. | 40 // because otherwise it gets duplicated during painting, wasting memory. |
40 SkBitmap immutable_bitmap(bitmap); | 41 SkBitmap immutable_bitmap(bitmap); |
41 immutable_bitmap.setImmutable(); | 42 immutable_bitmap.setImmutable(); |
42 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(immutable_bitmap); | 43 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(immutable_bitmap); |
43 image.MakeThreadSafe(); | 44 image.MakeThreadSafe(); |
(...skipping 30 matching lines...) Expand all Loading... | |
74 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) | 75 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) |
75 : ArcService(bridge_service), binding_(this) { | 76 : ArcService(bridge_service), binding_(this) { |
76 arc_bridge_service()->wallpaper()->AddObserver(this); | 77 arc_bridge_service()->wallpaper()->AddObserver(this); |
77 } | 78 } |
78 | 79 |
79 ArcWallpaperService::~ArcWallpaperService() { | 80 ArcWallpaperService::~ArcWallpaperService() { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 // Make sure the callback is never called after destruction. It is safe to | 82 // Make sure the callback is never called after destruction. It is safe to |
82 // call Cancel() even when there is no in-flight request. | 83 // call Cancel() even when there is no in-flight request. |
83 ImageDecoder::Cancel(this); | 84 ImageDecoder::Cancel(this); |
85 ash::WallpaperController* wc = GetWallpaperController(); | |
86 if (wc) { | |
87 wc->RemoveObserver(this); | |
88 } | |
84 arc_bridge_service()->wallpaper()->RemoveObserver(this); | 89 arc_bridge_service()->wallpaper()->RemoveObserver(this); |
85 } | 90 } |
86 | 91 |
87 void ArcWallpaperService::OnInstanceReady() { | 92 void ArcWallpaperService::OnInstanceReady() { |
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
89 mojom::WallpaperInstance* wallpaper_instance = | 94 mojom::WallpaperInstance* wallpaper_instance = |
90 arc_bridge_service()->wallpaper()->instance(); | 95 arc_bridge_service()->wallpaper()->instance(); |
91 if (!wallpaper_instance) { | 96 if (!wallpaper_instance) { |
92 LOG(DFATAL) << "OnWallpaperInstanceReady called, " | 97 LOG(DFATAL) << "OnWallpaperInstanceReady called, " |
93 << "but no wallpaper instance found"; | 98 << "but no wallpaper instance found"; |
94 return; | 99 return; |
95 } | 100 } |
96 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); | 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 } | |
97 } | 111 } |
98 | 112 |
99 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | 113 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
101 ImageDecoder::Cancel(this); | 115 ImageDecoder::Cancel(this); |
102 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), | 116 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), |
103 ImageDecoder::ROBUST_PNG_CODEC, true); | 117 ImageDecoder::ROBUST_PNG_CODEC, true); |
104 } | 118 } |
105 | 119 |
106 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { | 120 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { |
107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
108 ash::WallpaperController* wc = ash::WmShell::Get()->wallpaper_controller(); | 122 ash::WallpaperController* wc = ash::WmShell::Get()->wallpaper_controller(); |
109 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | 123 gfx::ImageSkia wallpaper = wc->GetWallpaper(); |
110 base::PostTaskAndReplyWithResult( | 124 base::PostTaskAndReplyWithResult( |
111 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 125 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
112 base::Bind(&EncodeImagePng, wallpaper), callback); | 126 base::Bind(&EncodeImagePng, wallpaper), callback); |
113 } | 127 } |
114 | 128 |
115 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { | 129 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { |
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 130 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
117 SetBitmapAsWallpaper(bitmap); | 131 SetBitmapAsWallpaper(bitmap); |
118 } | 132 } |
119 | 133 |
120 void ArcWallpaperService::OnDecodeImageFailed() { | 134 void ArcWallpaperService::OnDecodeImageFailed() { |
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 135 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
122 LOG(ERROR) << "Failed to decode wallpaper image."; | 136 LOG(ERROR) << "Failed to decode wallpaper image."; |
123 } | 137 } |
124 | 138 |
139 void ArcWallpaperService::OnWallpaperDataChanged() { | |
140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
141 mojom::WallpaperInstance* instance = | |
142 GetWallpaperInstance(kMinOnWallpaperChangedVersion); | |
143 if (!instance) { | |
Luis Héctor Chávez
2016/09/16 21:05:56
nit: you can elide the braces. Same in all the oth
Muyuan
2016/09/16 21:21:09
Done.
| |
144 return; | |
145 } | |
146 instance->OnWallpaperChanged(); | |
147 } | |
148 | |
149 ash::WallpaperController* ArcWallpaperService::GetWallpaperController() { | |
Luis Héctor Chávez
2016/09/16 21:05:56
This doesn't need to be a method. A standalone fun
Muyuan
2016/09/16 21:21:09
Done.
| |
150 ash::WmShell* wm_shell = ash::WmShell::Get(); | |
151 if (wm_shell && wm_shell->wallpaper_controller()) { | |
152 return wm_shell->wallpaper_controller(); | |
153 } | |
154 return nullptr; | |
155 } | |
156 | |
157 mojom::WallpaperInstance* ArcWallpaperService::GetWallpaperInstance( | |
158 uint32_t min_version) { | |
159 uint32_t version = arc_bridge_service()->wallpaper()->version(); | |
160 if (version < min_version) { | |
161 VLOG(1) << "ARC wallpaper instance is too old. required: " << min_version | |
162 << ", actual: " << version; | |
163 return nullptr; | |
164 } | |
165 | |
166 mojom::WallpaperInstance* instance = | |
167 arc_bridge_service()->wallpaper()->instance(); | |
168 if (!instance) { | |
169 VLOG(2) << "ARC wallpaper instance is not ready."; | |
170 } | |
171 | |
172 return instance; | |
173 } | |
174 | |
125 } // namespace arc | 175 } // namespace arc |
OLD | NEW |