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> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... | |
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 18 matching lines...) Expand all Loading... | |
62 // make this happen seems to do the same things as wallpaper_api.cc and | 63 // make this happen seems to do the same things as wallpaper_api.cc and |
63 // wallpaper_private_api.cc. | 64 // wallpaper_private_api.cc. |
64 } | 65 } |
65 | 66 |
66 std::vector<uint8_t> EncodeImagePng(const gfx::ImageSkia image) { | 67 std::vector<uint8_t> EncodeImagePng(const gfx::ImageSkia image) { |
67 std::vector<uint8_t> result; | 68 std::vector<uint8_t> result; |
68 gfx::PNGCodec::FastEncodeBGRASkBitmap(*image.bitmap(), true, &result); | 69 gfx::PNGCodec::FastEncodeBGRASkBitmap(*image.bitmap(), true, &result); |
69 return result; | 70 return result; |
70 } | 71 } |
71 | 72 |
73 ash::WallpaperController* GetWallpaperController() { | |
74 ash::WmShell* wm_shell = ash::WmShell::Get(); | |
75 if (wm_shell && wm_shell->wallpaper_controller()) { | |
Luis Héctor Chávez
2016/09/16 21:23:16
Forgot this one too:
In general you want to do
i
Yusuke Sato
2016/09/16 21:23:50
no {}
Muyuan
2016/09/16 21:38:48
Done.
| |
76 return wm_shell->wallpaper_controller(); | |
77 } | |
78 return nullptr; | |
79 } | |
80 | |
72 } // namespace | 81 } // namespace |
73 | 82 |
74 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) | 83 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) |
75 : ArcService(bridge_service), binding_(this) { | 84 : ArcService(bridge_service), binding_(this) { |
76 arc_bridge_service()->wallpaper()->AddObserver(this); | 85 arc_bridge_service()->wallpaper()->AddObserver(this); |
77 } | 86 } |
78 | 87 |
79 ArcWallpaperService::~ArcWallpaperService() { | 88 ArcWallpaperService::~ArcWallpaperService() { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 // Make sure the callback is never called after destruction. It is safe to | 90 // Make sure the callback is never called after destruction. It is safe to |
82 // call Cancel() even when there is no in-flight request. | 91 // call Cancel() even when there is no in-flight request. |
83 ImageDecoder::Cancel(this); | 92 ImageDecoder::Cancel(this); |
93 ash::WallpaperController* wc = GetWallpaperController(); | |
94 if (wc) | |
95 wc->RemoveObserver(this); | |
84 arc_bridge_service()->wallpaper()->RemoveObserver(this); | 96 arc_bridge_service()->wallpaper()->RemoveObserver(this); |
85 } | 97 } |
86 | 98 |
87 void ArcWallpaperService::OnInstanceReady() { | 99 void ArcWallpaperService::OnInstanceReady() { |
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
89 mojom::WallpaperInstance* wallpaper_instance = | 101 mojom::WallpaperInstance* wallpaper_instance = |
90 arc_bridge_service()->wallpaper()->instance(); | 102 arc_bridge_service()->wallpaper()->instance(); |
91 if (!wallpaper_instance) { | 103 if (!wallpaper_instance) { |
92 LOG(DFATAL) << "OnWallpaperInstanceReady called, " | 104 LOG(DFATAL) << "OnWallpaperInstanceReady called, " |
93 << "but no wallpaper instance found"; | 105 << "but no wallpaper instance found"; |
94 return; | 106 return; |
95 } | 107 } |
96 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); | 108 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); |
109 ash::WmShell::Get()->wallpaper_controller()->AddObserver(this); | |
110 } | |
111 | |
112 void ArcWallpaperService::OnInstanceClosed() { | |
113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
114 ash::WallpaperController* wc = GetWallpaperController(); | |
115 if (wc) | |
116 wc->RemoveObserver(this); | |
97 } | 117 } |
98 | 118 |
99 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | 119 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
101 ImageDecoder::Cancel(this); | 121 ImageDecoder::Cancel(this); |
102 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), | 122 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), |
103 ImageDecoder::ROBUST_PNG_CODEC, true); | 123 ImageDecoder::ROBUST_PNG_CODEC, true); |
104 } | 124 } |
105 | 125 |
106 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { | 126 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { |
107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
108 ash::WallpaperController* wc = ash::WmShell::Get()->wallpaper_controller(); | 128 ash::WallpaperController* wc = ash::WmShell::Get()->wallpaper_controller(); |
109 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | 129 gfx::ImageSkia wallpaper = wc->GetWallpaper(); |
110 base::PostTaskAndReplyWithResult( | 130 base::PostTaskAndReplyWithResult( |
111 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 131 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
112 base::Bind(&EncodeImagePng, wallpaper), callback); | 132 base::Bind(&EncodeImagePng, wallpaper), callback); |
113 } | 133 } |
114 | 134 |
115 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { | 135 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { |
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
117 SetBitmapAsWallpaper(bitmap); | 137 SetBitmapAsWallpaper(bitmap); |
118 } | 138 } |
119 | 139 |
120 void ArcWallpaperService::OnDecodeImageFailed() { | 140 void ArcWallpaperService::OnDecodeImageFailed() { |
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
122 LOG(ERROR) << "Failed to decode wallpaper image."; | 142 LOG(ERROR) << "Failed to decode wallpaper image."; |
123 } | 143 } |
124 | 144 |
145 void ArcWallpaperService::OnWallpaperDataChanged() { | |
146 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
147 mojom::WallpaperInstance* instance = | |
148 GetWallpaperInstance(kMinOnWallpaperChangedVersion); | |
149 if (!instance) { | |
Yusuke Sato
2016/09/16 21:23:50
same
| |
150 return; | |
151 } | |
152 instance->OnWallpaperChanged(); | |
153 } | |
154 | |
155 mojom::WallpaperInstance* ArcWallpaperService::GetWallpaperInstance( | |
156 uint32_t min_version) { | |
157 uint32_t version = arc_bridge_service()->wallpaper()->version(); | |
158 if (version < min_version) { | |
159 VLOG(1) << "ARC wallpaper instance is too old. required: " << min_version | |
160 << ", actual: " << version; | |
161 return nullptr; | |
162 } | |
163 | |
164 mojom::WallpaperInstance* instance = | |
165 arc_bridge_service()->wallpaper()->instance(); | |
166 if (!instance) { | |
Yusuke Sato
2016/09/16 21:23:50
same
Muyuan
2016/09/16 21:38:49
Done.
| |
167 VLOG(2) << "ARC wallpaper instance is not ready."; | |
168 } | |
169 | |
170 return instance; | |
171 } | |
172 | |
125 } // namespace arc | 173 } // namespace arc |
OLD | NEW |