Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: ash/desktop_background/desktop_background_controller.cc

Issue 2200893002: mash: Partially migrate ash/desktop_background to ash common types. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Working on it.. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ash/desktop_background/desktop_background_controller.h" 5 #include "ash/desktop_background/desktop_background_controller.h"
6 6
7 #include "ash/aura/wm_window_aura.h" 7 #include "ash/aura/wm_window_aura.h"
8 #include "ash/common/ash_switches.h"
9 #include "ash/common/display/display_info.h" 8 #include "ash/common/display/display_info.h"
10 #include "ash/common/shell_window_ids.h" 9 #include "ash/common/shell_window_ids.h"
11 #include "ash/common/wm/root_window_layout_manager.h"
12 #include "ash/common/wm_shell.h" 10 #include "ash/common/wm_shell.h"
13 #include "ash/desktop_background/desktop_background_controller_observer.h" 11 #include "ash/desktop_background/desktop_background_controller_observer.h"
14 #include "ash/desktop_background/desktop_background_view.h" 12 #include "ash/desktop_background/desktop_background_view.h"
15 #include "ash/desktop_background/desktop_background_widget_controller.h" 13 #include "ash/desktop_background/desktop_background_widget_controller.h"
16 #include "ash/desktop_background/user_wallpaper_delegate.h" 14 #include "ash/desktop_background/user_wallpaper_delegate.h"
17 #include "ash/display/display_manager.h"
18 #include "ash/root_window_controller.h" 15 #include "ash/root_window_controller.h"
19 #include "ash/shell.h" 16 #include "ash/shell.h"
20 #include "ash/shell_factory.h"
21 #include "base/bind.h" 17 #include "base/bind.h"
22 #include "base/command_line.h"
23 #include "base/files/file_util.h"
24 #include "base/logging.h" 18 #include "base/logging.h"
25 #include "base/synchronization/cancellation_flag.h"
26 #include "base/threading/sequenced_worker_pool.h" 19 #include "base/threading/sequenced_worker_pool.h"
27 #include "components/wallpaper/wallpaper_resizer.h" 20 #include "components/wallpaper/wallpaper_resizer.h"
28 #include "ui/aura/window.h" 21 #include "ui/aura/window.h"
29 #include "ui/aura/window_event_dispatcher.h"
30 #include "ui/compositor/layer.h"
31 #include "ui/display/screen.h" 22 #include "ui/display/screen.h"
32 #include "ui/gfx/codec/jpeg_codec.h"
33 #include "ui/gfx/geometry/rect.h"
34 #include "ui/gfx/image/image_skia.h"
35 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
36 24
37 using wallpaper::WallpaperResizer;
James Cook 2016/08/02 16:51:50 totally optional: using declarations don't bother
msw 2016/08/02 18:28:57 I only like them when they cleanup a large part of
38 using wallpaper::WallpaperLayout;
39 using wallpaper::WALLPAPER_LAYOUT_CENTER;
40 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
41 using wallpaper::WALLPAPER_LAYOUT_STRETCH;
42 using wallpaper::WALLPAPER_LAYOUT_TILE;
43
44 namespace ash { 25 namespace ash {
45 namespace { 26 namespace {
46 27
47 // How long to wait reloading the wallpaper after the max display has 28 // How long to wait reloading the wallpaper after the max display has changed.
James Cook 2016/08/02 16:51:50 nit: What the heck does "after the max display has
msw 2016/08/02 18:28:57 Done. Simplified the comment.
48 // changed?
49 const int kWallpaperReloadDelayMs = 100; 29 const int kWallpaperReloadDelayMs = 100;
50 30
51 } // namespace 31 } // namespace
52 32
53 DesktopBackgroundController::DesktopBackgroundController( 33 DesktopBackgroundController::DesktopBackgroundController(
54 base::SequencedWorkerPool* blocking_pool) 34 base::SequencedWorkerPool* blocking_pool)
55 : locked_(false), 35 : locked_(false),
56 desktop_background_mode_(BACKGROUND_NONE), 36 desktop_background_mode_(BACKGROUND_NONE),
57 wallpaper_reload_delay_(kWallpaperReloadDelayMs), 37 wallpaper_reload_delay_(kWallpaperReloadDelayMs),
58 blocking_pool_(blocking_pool) { 38 blocking_pool_(blocking_pool) {
59 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this); 39 WmShell::Get()->AddDisplayObserver(this);
60 WmShell::Get()->AddShellObserver(this); 40 WmShell::Get()->AddShellObserver(this);
61 } 41 }
62 42
63 DesktopBackgroundController::~DesktopBackgroundController() { 43 DesktopBackgroundController::~DesktopBackgroundController() {
64 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); 44 WmShell::Get()->RemoveDisplayObserver(this);
65 WmShell::Get()->RemoveShellObserver(this); 45 WmShell::Get()->RemoveShellObserver(this);
66 } 46 }
67 47
68 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { 48 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
69 if (current_wallpaper_) 49 if (current_wallpaper_)
70 return current_wallpaper_->image(); 50 return current_wallpaper_->image();
71 return gfx::ImageSkia(); 51 return gfx::ImageSkia();
72 } 52 }
73 53
74 void DesktopBackgroundController::AddObserver( 54 void DesktopBackgroundController::AddObserver(
75 DesktopBackgroundControllerObserver* observer) { 55 DesktopBackgroundControllerObserver* observer) {
76 observers_.AddObserver(observer); 56 observers_.AddObserver(observer);
77 } 57 }
78 58
79 void DesktopBackgroundController::RemoveObserver( 59 void DesktopBackgroundController::RemoveObserver(
80 DesktopBackgroundControllerObserver* observer) { 60 DesktopBackgroundControllerObserver* observer) {
81 observers_.RemoveObserver(observer); 61 observers_.RemoveObserver(observer);
82 } 62 }
83 63
84 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { 64 wallpaper::WallpaperLayout DesktopBackgroundController::GetWallpaperLayout()
65 const {
85 if (current_wallpaper_) 66 if (current_wallpaper_)
86 return current_wallpaper_->layout(); 67 return current_wallpaper_->layout();
87 return WALLPAPER_LAYOUT_CENTER_CROPPED; 68 return wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
88 } 69 }
89 70
90 bool DesktopBackgroundController::SetWallpaperImage(const gfx::ImageSkia& image, 71 bool DesktopBackgroundController::SetWallpaperImage(
91 WallpaperLayout layout) { 72 const gfx::ImageSkia& image,
92 VLOG(1) << "SetWallpaper: image_id=" << WallpaperResizer::GetImageId(image) 73 wallpaper::WallpaperLayout layout) {
74 VLOG(1) << "SetWallpaper: image_id="
75 << wallpaper::WallpaperResizer::GetImageId(image)
93 << " layout=" << layout; 76 << " layout=" << layout;
94 77
95 if (WallpaperIsAlreadyLoaded(image, true /* compare_layouts */, layout)) { 78 if (WallpaperIsAlreadyLoaded(image, true /* compare_layouts */, layout)) {
96 VLOG(1) << "Wallpaper is already loaded"; 79 VLOG(1) << "Wallpaper is already loaded";
97 return false; 80 return false;
98 } 81 }
99 82
100 current_wallpaper_.reset(new WallpaperResizer( 83 current_wallpaper_.reset(new wallpaper::WallpaperResizer(
101 image, GetMaxDisplaySizeInNative(), layout, blocking_pool_)); 84 image, GetMaxDisplaySizeInNative(), layout, blocking_pool_));
102 current_wallpaper_->StartResize(); 85 current_wallpaper_->StartResize();
103 86
104 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, 87 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_,
105 OnWallpaperDataChanged()); 88 OnWallpaperDataChanged());
106 SetDesktopBackgroundImageMode(); 89 desktop_background_mode_ = BACKGROUND_IMAGE;
90 InstallDesktopControllerForAllWindows();
107 return true; 91 return true;
108 } 92 }
109 93
110 void DesktopBackgroundController::CreateEmptyWallpaper() { 94 void DesktopBackgroundController::CreateEmptyWallpaper() {
111 current_wallpaper_.reset(NULL); 95 current_wallpaper_.reset(nullptr);
James Cook 2016/08/02 16:51:50 optional nit: or just reset()
msw 2016/08/02 18:28:56 Done.
112 SetDesktopBackgroundImageMode(); 96 desktop_background_mode_ = BACKGROUND_IMAGE;
97 InstallDesktopControllerForAllWindows();
113 } 98 }
114 99
115 bool DesktopBackgroundController::MoveDesktopToLockedContainer() { 100 bool DesktopBackgroundController::MoveDesktopToLockedContainer() {
116 if (locked_) 101 if (locked_)
117 return false; 102 return false;
118 locked_ = true; 103 locked_ = true;
119 return ReparentBackgroundWidgets(GetBackgroundContainerId(false), 104 return ReparentBackgroundWidgets(GetBackgroundContainerId(false),
120 GetBackgroundContainerId(true)); 105 GetBackgroundContainerId(true));
121 } 106 }
122 107
123 bool DesktopBackgroundController::MoveDesktopToUnlockedContainer() { 108 bool DesktopBackgroundController::MoveDesktopToUnlockedContainer() {
124 if (!locked_) 109 if (!locked_)
125 return false; 110 return false;
126 locked_ = false; 111 locked_ = false;
127 return ReparentBackgroundWidgets(GetBackgroundContainerId(true), 112 return ReparentBackgroundWidgets(GetBackgroundContainerId(true),
128 GetBackgroundContainerId(false)); 113 GetBackgroundContainerId(false));
129 } 114 }
130 115
131 void DesktopBackgroundController::OnDisplayConfigurationChanged() { 116 void DesktopBackgroundController::OnDisplayConfigurationChanged() {
132 gfx::Size max_display_size = GetMaxDisplaySizeInNative(); 117 gfx::Size max_display_size = GetMaxDisplaySizeInNative();
133 if (current_max_display_size_ != max_display_size) { 118 if (current_max_display_size_ != max_display_size) {
134 current_max_display_size_ = max_display_size; 119 current_max_display_size_ = max_display_size;
135 if (desktop_background_mode_ == BACKGROUND_IMAGE && 120 if (desktop_background_mode_ == BACKGROUND_IMAGE && current_wallpaper_) {
136 current_wallpaper_.get()) {
137 timer_.Stop(); 121 timer_.Stop();
138 timer_.Start(FROM_HERE, 122 timer_.Start(FROM_HERE,
139 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_), 123 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_),
140 base::Bind(&DesktopBackgroundController::UpdateWallpaper, 124 base::Bind(&DesktopBackgroundController::UpdateWallpaper,
141 base::Unretained(this), false /* clear cache */)); 125 base::Unretained(this), false /* clear cache */));
142 } 126 }
143 } 127 }
144 } 128 }
145 129
146 void DesktopBackgroundController::OnRootWindowAdded(WmWindow* root_window) { 130 void DesktopBackgroundController::OnRootWindowAdded(WmWindow* root_window) {
147 // The background hasn't been set yet. 131 // The background hasn't been set yet.
148 if (desktop_background_mode_ == BACKGROUND_NONE) 132 if (desktop_background_mode_ == BACKGROUND_NONE)
149 return; 133 return;
150 134
151 // Handle resolution change for "built-in" images. 135 // Handle resolution change for "built-in" images.
152 gfx::Size max_display_size = GetMaxDisplaySizeInNative(); 136 gfx::Size max_display_size = GetMaxDisplaySizeInNative();
153 if (current_max_display_size_ != max_display_size) { 137 if (current_max_display_size_ != max_display_size) {
154 current_max_display_size_ = max_display_size; 138 current_max_display_size_ = max_display_size;
155 if (desktop_background_mode_ == BACKGROUND_IMAGE && 139 if (desktop_background_mode_ == BACKGROUND_IMAGE && current_wallpaper_)
156 current_wallpaper_.get())
157 UpdateWallpaper(true /* clear cache */); 140 UpdateWallpaper(true /* clear cache */);
158 } 141 }
159 142
160 InstallDesktopController(WmWindowAura::GetAuraWindow(root_window)); 143 InstallDesktopController(root_window);
161 } 144 }
162 145
163 // static 146 // static
164 gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() { 147 gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() {
165 // Return an empty size for test environments where the screen is null. 148 // Return an empty size for test environments where the screen is null.
166 if (!display::Screen::GetScreen()) 149 if (!display::Screen::GetScreen())
167 return gfx::Size(); 150 return gfx::Size();
168 151
169 int width = 0; 152 gfx::Size max;
170 int height = 0;
171 DisplayManager* display_manager =
172 Shell::HasInstance() ? Shell::GetInstance()->display_manager() : nullptr;
173 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { 153 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
174 // Don't use size_in_pixel because we want to use the native pixel size. 154 // Use the native size, not DisplayInfo::size_in_pixel or Display::size.
175 // TODO(msw): Fix this for Mash/Mus; see http://crbug.com/613657. 155 gfx::Size size =
176 gfx::Size size_in_pixel = 156 WmShell::Get()->GetDisplayInfo(display.id()).bounds_in_native().size();
177 display_manager 157 // TODO(msw): Avoid using Display::size here; see http://crbug.com/613657.
178 ? display_manager->GetDisplayInfo(display.id()) 158 size.SetToMax(display.size());
179 .bounds_in_native()
180 .size()
181 : display.size();
182 if (display.rotation() == display::Display::ROTATE_90 || 159 if (display.rotation() == display::Display::ROTATE_90 ||
183 display.rotation() == display::Display::ROTATE_270) { 160 display.rotation() == display::Display::ROTATE_270) {
184 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); 161 size = gfx::Size(size.height(), size.width());
185 } 162 }
186 width = std::max(size_in_pixel.width(), width); 163 max.SetToMax(size);
187 height = std::max(size_in_pixel.height(), height);
188 } 164 }
189 return gfx::Size(width, height); 165
166 return max;
190 } 167 }
191 168
192 bool DesktopBackgroundController::WallpaperIsAlreadyLoaded( 169 bool DesktopBackgroundController::WallpaperIsAlreadyLoaded(
193 const gfx::ImageSkia& image, 170 const gfx::ImageSkia& image,
194 bool compare_layouts, 171 bool compare_layouts,
195 WallpaperLayout layout) const { 172 wallpaper::WallpaperLayout layout) const {
196 if (!current_wallpaper_.get()) 173 if (!current_wallpaper_)
197 return false; 174 return false;
198 175
199 // Compare layouts only if necessary. 176 // Compare layouts only if necessary.
200 if (compare_layouts && layout != current_wallpaper_->layout()) 177 if (compare_layouts && layout != current_wallpaper_->layout())
201 return false; 178 return false;
202 179
203 return WallpaperResizer::GetImageId(image) == 180 return wallpaper::WallpaperResizer::GetImageId(image) ==
204 current_wallpaper_->original_image_id(); 181 current_wallpaper_->original_image_id();
205 } 182 }
206 183
207 void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
208 desktop_background_mode_ = BACKGROUND_IMAGE;
209 InstallDesktopControllerForAllWindows();
210 }
211
212 void DesktopBackgroundController::InstallDesktopController( 184 void DesktopBackgroundController::InstallDesktopController(
213 aura::Window* root_window) { 185 WmWindow* root_window) {
214 DesktopBackgroundWidgetController* component = NULL; 186 DesktopBackgroundWidgetController* component = nullptr;
215 int container_id = GetBackgroundContainerId(locked_); 187 int container_id = GetBackgroundContainerId(locked_);
216 188
217 switch (desktop_background_mode_) { 189 switch (desktop_background_mode_) {
218 case BACKGROUND_IMAGE: { 190 case BACKGROUND_IMAGE: {
219 views::Widget* widget = 191 component = new DesktopBackgroundWidgetController(
220 CreateDesktopBackground(root_window, container_id); 192 CreateDesktopBackground(root_window, container_id));
221 component = new DesktopBackgroundWidgetController(widget);
222 break; 193 break;
223 } 194 }
224 case BACKGROUND_NONE: 195 case BACKGROUND_NONE:
225 NOTREACHED(); 196 NOTREACHED();
226 return; 197 return;
227 } 198 }
228 GetRootWindowController(root_window)
229 ->SetAnimatingWallpaperController(
230 new AnimatingDesktopController(component));
231 199
232 component->StartAnimating(GetRootWindowController(root_window)); 200 aura::Window* aura_root_window = WmWindowAura::GetAuraWindow(root_window);
201 RootWindowController* controller = GetRootWindowController(aura_root_window);
202 controller->SetAnimatingWallpaperController(
203 new AnimatingDesktopController(component));
204 component->StartAnimating(controller);
233 } 205 }
234 206
235 void DesktopBackgroundController::InstallDesktopControllerForAllWindows() { 207 void DesktopBackgroundController::InstallDesktopControllerForAllWindows() {
236 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); 208 for (WmWindow* root : WmShell::Get()->GetAllRootWindows())
237 for (aura::Window::Windows::iterator iter = root_windows.begin(); 209 InstallDesktopController(root);
238 iter != root_windows.end(); ++iter) {
239 InstallDesktopController(*iter);
240 }
241 current_max_display_size_ = GetMaxDisplaySizeInNative(); 210 current_max_display_size_ = GetMaxDisplaySizeInNative();
242 } 211 }
243 212
244 bool DesktopBackgroundController::ReparentBackgroundWidgets(int src_container, 213 bool DesktopBackgroundController::ReparentBackgroundWidgets(int src_container,
245 int dst_container) { 214 int dst_container) {
246 bool moved = false; 215 bool moved = false;
247 Shell::RootWindowControllerList controllers = 216 for (auto root_window_controller : Shell::GetAllRootWindowControllers()) {
James Cook 2016/08/02 16:51:50 nit: auto* or spell out the type
msw 2016/08/02 18:28:56 Done.
248 Shell::GetAllRootWindowControllers();
249 for (Shell::RootWindowControllerList::iterator iter = controllers.begin();
250 iter != controllers.end(); ++iter) {
251 RootWindowController* root_window_controller = *iter;
252 // In the steady state (no animation playing) the background widget 217 // In the steady state (no animation playing) the background widget
253 // controller exists in the RootWindowController. 218 // controller exists in the RootWindowController.
254 DesktopBackgroundWidgetController* desktop_controller = 219 DesktopBackgroundWidgetController* desktop_controller =
255 root_window_controller->wallpaper_controller(); 220 root_window_controller->wallpaper_controller();
256 if (desktop_controller) { 221 if (desktop_controller) {
257 moved |= 222 moved |=
258 desktop_controller->Reparent(root_window_controller->GetRootWindow(), 223 desktop_controller->Reparent(root_window_controller->GetRootWindow(),
259 src_container, dst_container); 224 src_container, dst_container);
260 } 225 }
261 // During desktop show animations the controller lives in 226 // During desktop show animations the controller lives in
262 // AnimatingDesktopController owned by RootWindowController. 227 // AnimatingDesktopController owned by RootWindowController.
263 // NOTE: If a wallpaper load happens during a desktop show animation there 228 // NOTE: If a wallpaper load happens during a desktop show animation there
264 // can temporarily be two desktop background widgets. We must reparent 229 // can temporarily be two desktop background widgets. We must reparent
265 // both of them - one above and one here. 230 // both of them - one above and one here.
266 DesktopBackgroundWidgetController* animating_controller = 231 DesktopBackgroundWidgetController* animating_controller =
267 root_window_controller->animating_wallpaper_controller() 232 root_window_controller->animating_wallpaper_controller()
268 ? root_window_controller->animating_wallpaper_controller() 233 ? root_window_controller->animating_wallpaper_controller()
269 ->GetController(false) 234 ->GetController(false)
270 : NULL; 235 : nullptr;
271 if (animating_controller) { 236 if (animating_controller) {
272 moved |= animating_controller->Reparent( 237 moved |= animating_controller->Reparent(
273 root_window_controller->GetRootWindow(), src_container, 238 root_window_controller->GetRootWindow(), src_container,
274 dst_container); 239 dst_container);
275 } 240 }
276 } 241 }
277 return moved; 242 return moved;
278 } 243 }
279 244
280 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) { 245 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
281 return locked ? kShellWindowId_LockScreenBackgroundContainer 246 return locked ? kShellWindowId_LockScreenBackgroundContainer
282 : kShellWindowId_DesktopBackgroundContainer; 247 : kShellWindowId_DesktopBackgroundContainer;
283 } 248 }
284 249
285 void DesktopBackgroundController::UpdateWallpaper(bool clear_cache) { 250 void DesktopBackgroundController::UpdateWallpaper(bool clear_cache) {
286 current_wallpaper_.reset(NULL); 251 current_wallpaper_.reset(nullptr);
James Cook 2016/08/02 16:51:50 optional nit: reset()
msw 2016/08/02 18:28:57 Done.
287 ash::Shell::GetInstance()->user_wallpaper_delegate()->UpdateWallpaper( 252 Shell::GetInstance()->user_wallpaper_delegate()->UpdateWallpaper(clear_cache);
288 clear_cache);
289 } 253 }
290 254
291 } // namespace ash 255 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698