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 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::WmShell* wm_shell = ash::WmShell::Get(); | |
86 if (wm_shell && wm_shell->wallpaper_controller()) { | |
87 wm_shell->wallpaper_controller()->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); | |
97 } | 103 } |
98 | 104 |
99 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | 105 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
101 ImageDecoder::Cancel(this); | 107 ImageDecoder::Cancel(this); |
102 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), | 108 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), |
103 ImageDecoder::ROBUST_PNG_CODEC, true); | 109 ImageDecoder::ROBUST_PNG_CODEC, true); |
104 } | 110 } |
105 | 111 |
106 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { | 112 void ArcWallpaperService::GetWallpaper(const GetWallpaperCallback& callback) { |
107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
108 ash::WallpaperController* wc = ash::WmShell::Get()->wallpaper_controller(); | 114 ash::WallpaperController* wc = ash::WmShell::Get()->wallpaper_controller(); |
109 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | 115 gfx::ImageSkia wallpaper = wc->GetWallpaper(); |
110 base::PostTaskAndReplyWithResult( | 116 base::PostTaskAndReplyWithResult( |
111 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 117 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
112 base::Bind(&EncodeImagePng, wallpaper), callback); | 118 base::Bind(&EncodeImagePng, wallpaper), callback); |
113 } | 119 } |
114 | 120 |
115 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { | 121 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { |
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
117 SetBitmapAsWallpaper(bitmap); | 123 SetBitmapAsWallpaper(bitmap); |
118 } | 124 } |
119 | 125 |
120 void ArcWallpaperService::OnDecodeImageFailed() { | 126 void ArcWallpaperService::OnDecodeImageFailed() { |
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
122 LOG(ERROR) << "Failed to decode wallpaper image."; | 128 LOG(ERROR) << "Failed to decode wallpaper image."; |
123 } | 129 } |
124 | 130 |
131 void ArcWallpaperService::OnWallpaperDataChanged() { | |
132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
133 mojom::WallpaperInstance* instance = | |
134 GetWallpaperInstance(kMinOnWallpaperChangedVersion); | |
135 if (!instance) { | |
136 ash::WmShell::Get()->wallpaper_controller()->RemoveObserver(this); | |
Luis Héctor Chávez
2016/09/16 19:58:11
It's better to do this in the OnInstanceClosed() r
Muyuan
2016/09/16 20:19:08
I see. So here is a question:
Is there a guarantee
Luis Héctor Chávez
2016/09/16 20:27:37
There are cases where Android outlives Chrome, but
Muyuan
2016/09/16 20:48:56
Done.
| |
137 return; | |
138 } | |
139 instance->OnWallpaperChanged(); | |
140 } | |
141 | |
142 mojom::WallpaperInstance* ArcWallpaperService::GetWallpaperInstance( | |
143 uint32_t min_version) { | |
144 uint32_t version = arc_bridge_service()->wallpaper()->version(); | |
145 if (version < min_version) { | |
146 VLOG(1) << "ARC wallpaper instance is too old. required: " << min_version | |
147 << ", actual: " << version; | |
148 return nullptr; | |
149 } | |
150 | |
151 mojom::WallpaperInstance* instance = | |
152 arc_bridge_service()->wallpaper()->instance(); | |
153 if (!instance) { | |
154 VLOG(2) << "ARC wallpaper instance is not ready."; | |
155 } | |
156 | |
157 return instance; | |
158 } | |
159 | |
125 } // namespace arc | 160 } // namespace arc |
OLD | NEW |