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

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

Issue 10829173: Revert 149869 because it broke ash_unittests on win_aura and linux_chromeos. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 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 | Annotate | Revision Log
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/desktop_background/desktop_background_view.h" 7 #include "ash/desktop_background/desktop_background_view.h"
8 #include "ash/desktop_background/desktop_background_widget_controller.h"
9 #include "ash/shell.h" 8 #include "ash/shell.h"
10 #include "ash/shell_factory.h" 9 #include "ash/shell_factory.h"
11 #include "ash/shell_window_ids.h" 10 #include "ash/shell_window_ids.h"
12 #include "ash/wm/root_window_layout_manager.h" 11 #include "ash/wm/root_window_layout_manager.h"
13 #include "base/bind.h" 12 #include "base/bind.h"
14 #include "base/logging.h" 13 #include "base/logging.h"
15 #include "base/synchronization/cancellation_flag.h" 14 #include "base/synchronization/cancellation_flag.h"
16 #include "base/threading/worker_pool.h" 15 #include "base/threading/worker_pool.h"
17 #include "grit/ui_resources.h" 16 #include "grit/ui_resources.h"
18 #include "ui/aura/root_window.h" 17 #include "ui/aura/root_window.h"
19 #include "ui/aura/window.h" 18 #include "ui/aura/window.h"
20 #include "ui/base/resource/resource_bundle.h" 19 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/compositor/layer.h" 20 #include "ui/compositor/layer.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/image/image.h" 21 #include "ui/gfx/image/image.h"
24 #include "ui/views/widget/widget.h" 22 #include "ui/views/widget/widget.h"
25 23
26 namespace ash { 24 namespace ash {
27 namespace { 25 namespace {
28 internal::RootWindowLayoutManager* GetRootWindowLayoutManager( 26 internal::RootWindowLayoutManager* GetRootWindowLayoutManager(
29 aura::RootWindow* root_window) { 27 aura::RootWindow* root_window) {
30 return static_cast<internal::RootWindowLayoutManager*>( 28 return static_cast<internal::RootWindowLayoutManager*>(
31 root_window->layout_manager()); 29 root_window->layout_manager());
32 } 30 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 base::CancellationFlag cancel_flag_; 84 base::CancellationFlag cancel_flag_;
87 85
88 scoped_ptr<WallpaperData> wallpaper_data_; 86 scoped_ptr<WallpaperData> wallpaper_data_;
89 87
90 int index_; 88 int index_;
91 89
92 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); 90 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
93 }; 91 };
94 92
95 DesktopBackgroundController::DesktopBackgroundController() 93 DesktopBackgroundController::DesktopBackgroundController()
96 : locked_(false), 94 : desktop_background_mode_(BACKGROUND_IMAGE),
97 desktop_background_mode_(BACKGROUND_SOLID_COLOR),
98 background_color_(SK_ColorGRAY), 95 background_color_(SK_ColorGRAY),
99 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 96 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
100 InstallComponentForAllWindows();
101 } 97 }
102 98
103 DesktopBackgroundController::~DesktopBackgroundController() { 99 DesktopBackgroundController::~DesktopBackgroundController() {
104 CancelPendingWallpaperOperation(); 100 CancelPendingWallpaperOperation();
105 } 101 }
106 102
107 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { 103 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
108 if (current_wallpaper_.get()) 104 if (current_wallpaper_.get())
109 return current_wallpaper_->wallpaper_image; 105 return current_wallpaper_->wallpaper_image;
110 return gfx::ImageSkia(); 106 return gfx::ImageSkia();
111 } 107 }
112 108
113 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { 109 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const {
114 if (current_wallpaper_.get()) 110 if (current_wallpaper_.get())
115 return current_wallpaper_->wallpaper_layout; 111 return current_wallpaper_->wallpaper_layout;
116 return CENTER_CROPPED; 112 return CENTER_CROPPED;
117 } 113 }
118 114
119 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() { 115 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() {
120 if (desktop_background_mode_ != BACKGROUND_IMAGE) 116 if (desktop_background_mode_ != BACKGROUND_IMAGE)
121 return SkBitmap(); 117 return SkBitmap();
122 return GetWallpaper(); 118 return GetWallpaper();
123 } 119 }
124 120
125 void DesktopBackgroundController::OnRootWindowAdded( 121 void DesktopBackgroundController::OnRootWindowAdded(
126 aura::RootWindow* root_window) { 122 aura::RootWindow* root_window) {
127 InstallComponent(root_window); 123 switch (desktop_background_mode_) {
124 case BACKGROUND_IMAGE:
125 if (current_wallpaper_.get()) {
126 SetDesktopBackgroundImage(root_window);
127 } else {
128 internal::CreateDesktopBackground(root_window);
129 }
130 break;
131 case BACKGROUND_SOLID_COLOR:
132 SetDesktopBackgroundSolidColorMode(background_color_);
133 break;
134 }
128 } 135 }
129 136
130 void DesktopBackgroundController::SetDefaultWallpaper(int index) { 137 void DesktopBackgroundController::SetDefaultWallpaper(int index) {
131 // We should not change background when index is invalid. For instance, at 138 // We should not change background when index is invalid. For instance, at
132 // login screen or stub_user login. 139 // login screen or stub_user login.
133 if (index == GetInvalidWallpaperIndex()) { 140 if (index == ash::GetInvalidWallpaperIndex()) {
134 CreateEmptyWallpaper(); 141 CreateEmptyWallpaper();
135 return; 142 return;
136 } else if (index == GetSolidColorIndex()) { 143 } else if (index == ash::GetSolidColorIndex()) {
137 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); 144 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor);
138 return; 145 return;
139 } 146 }
140 147
141 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index) 148 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index)
142 return; 149 return;
143 150
144 CancelPendingWallpaperOperation(); 151 CancelPendingWallpaperOperation();
145 152
146 wallpaper_op_ = new WallpaperOperation(index); 153 wallpaper_op_ = new WallpaperOperation(index);
147 base::WorkerPool::PostTaskAndReply( 154 base::WorkerPool::PostTaskAndReply(
148 FROM_HERE, 155 FROM_HERE,
149 base::Bind(&WallpaperOperation::Run, wallpaper_op_), 156 base::Bind(&WallpaperOperation::Run, wallpaper_op_),
150 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, 157 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
151 weak_ptr_factory_.GetWeakPtr(), 158 weak_ptr_factory_.GetWeakPtr(),
152 wallpaper_op_), 159 wallpaper_op_),
153 true /* task_is_slow */); 160 true /* task_is_slow */);
154 } 161 }
155 162
156 void DesktopBackgroundController::SetCustomWallpaper( 163 void DesktopBackgroundController::SetCustomWallpaper(
157 const gfx::ImageSkia& wallpaper, 164 const gfx::ImageSkia& wallpaper,
158 WallpaperLayout layout) { 165 WallpaperLayout layout) {
159 CancelPendingWallpaperOperation(); 166 CancelPendingWallpaperOperation();
160 current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); 167 current_wallpaper_.reset(new WallpaperData(layout, wallpaper));
161 SetDesktopBackgroundImageMode(); 168 desktop_background_mode_ = BACKGROUND_IMAGE;
169 UpdateDesktopBackgroundImageMode();
162 } 170 }
163 171
164 void DesktopBackgroundController::CancelPendingWallpaperOperation() { 172 void DesktopBackgroundController::CancelPendingWallpaperOperation() {
165 // Set canceled flag of previous request to skip unneeded loading. 173 // Set canceled flag of previous request to skip unneeded loading.
166 if (wallpaper_op_.get()) 174 if (wallpaper_op_.get())
167 wallpaper_op_->Cancel(); 175 wallpaper_op_->Cancel();
168 176
169 // Cancel reply callback for previous request. 177 // Cancel reply callback for previous request.
170 weak_ptr_factory_.InvalidateWeakPtrs(); 178 weak_ptr_factory_.InvalidateWeakPtrs();
171 } 179 }
172 180
173 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode( 181 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode(
174 SkColor color) { 182 SkColor color) {
183 // Set a solid black background.
184 // TODO(derat): Remove this in favor of having the compositor only clear the
185 // viewport when there are regions not covered by a layer:
186 // http://crbug.com/113445
187 current_wallpaper_.reset(NULL);
175 background_color_ = color; 188 background_color_ = color;
176 if (desktop_background_mode_ != BACKGROUND_SOLID_COLOR) { 189 desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
177 desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
178 InstallComponentForAllWindows();
179 return;
180 }
181
182 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 190 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
183 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 191 for (Shell::RootWindowList::iterator iter = root_windows.begin();
184 iter != root_windows.end(); ++iter) { 192 iter != root_windows.end(); ++iter) {
193 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
194 background_layer->SetColor(color);
185 aura::RootWindow* root_window = *iter; 195 aura::RootWindow* root_window = *iter;
186 internal::DesktopBackgroundWidgetController* component = root_window-> 196 Shell::GetContainer(
187 GetProperty(internal::kWindowDesktopComponent); 197 root_window,
188 DCHECK(component); 198 internal::kShellWindowId_DesktopBackgroundContainer)->
189 DCHECK(component->layer()); 199 layer()->Add(background_layer);
190 component->layer()->SetColor(background_color_ ); 200 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer(
201 background_layer);
202 GetRootWindowLayoutManager(root_window)->SetBackgroundWidget(NULL);
191 } 203 }
192 } 204 }
193 205
194 void DesktopBackgroundController::CreateEmptyWallpaper() { 206 void DesktopBackgroundController::SetDesktopBackgroundImage(
195 current_wallpaper_.reset(NULL); 207 aura::RootWindow* root_window) {
196 SetDesktopBackgroundImageMode(); 208 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer(NULL);
209 if (current_wallpaper_.get() &&
210 !current_wallpaper_->wallpaper_image.empty())
211 internal::CreateDesktopBackground(root_window);
197 } 212 }
198 213
199 void DesktopBackgroundController::MoveDesktopToLockedContainer() { 214 void DesktopBackgroundController::UpdateDesktopBackgroundImageMode() {
200 if (locked_) 215 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
201 return;
202 locked_ = true;
203 ReparentBackgroundWidgets(GetBackgroundContainerId(false),
204 GetBackgroundContainerId(true));
205 }
206 216
207 void DesktopBackgroundController::MoveDesktopToUnlockedContainer() { 217 for (Shell::RootWindowList::iterator iter = root_windows.begin();
208 if (!locked_) 218 iter != root_windows.end(); ++iter) {
209 return; 219 SetDesktopBackgroundImage(*iter);
210 locked_ = false;
211 ReparentBackgroundWidgets(GetBackgroundContainerId(true),
212 GetBackgroundContainerId(false));
213 }
214
215 void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
216 if (desktop_background_mode_ != BACKGROUND_IMAGE) {
217 desktop_background_mode_ = BACKGROUND_IMAGE;
218 InstallComponentForAllWindows();
219 return;
220 } 220 }
221 221 desktop_background_mode_ = BACKGROUND_IMAGE;
222 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
223 for (Shell::RootWindowList::iterator iter = root_windows.begin();
224 iter != root_windows.end(); ++iter) {
225 aura::RootWindow* root_window = *iter;
226 internal::DesktopBackgroundWidgetController* component = root_window->
227 GetProperty(internal::kWindowDesktopComponent);
228 DCHECK(component);
229 DCHECK(component->widget());
230 aura::Window* window = component->widget()->GetNativeView();
231 gfx::Rect bounds = window->bounds();
232 window->SchedulePaintInRect(gfx::Rect(0, 0,
233 bounds.width(), bounds.height()));
234 }
235 } 222 }
236 223
237 void DesktopBackgroundController::OnWallpaperLoadCompleted( 224 void DesktopBackgroundController::OnWallpaperLoadCompleted(
238 scoped_refptr<WallpaperOperation> wo) { 225 scoped_refptr<WallpaperOperation> wo) {
239 current_wallpaper_.reset(wo->ReleaseWallpaperData()); 226 current_wallpaper_.reset(wo->ReleaseWallpaperData());
240 227
241 SetDesktopBackgroundImageMode(); 228 UpdateDesktopBackgroundImageMode();
242 229
243 DCHECK(wo.get() == wallpaper_op_.get()); 230 DCHECK(wo.get() == wallpaper_op_.get());
244 wallpaper_op_ = NULL; 231 wallpaper_op_ = NULL;
245 } 232 }
246 233
247 ui::Layer* DesktopBackgroundController::SetColorLayerForContainer( 234 void DesktopBackgroundController::CreateEmptyWallpaper() {
248 SkColor color, 235 current_wallpaper_.reset(NULL);
249 aura::RootWindow* root_window, 236 desktop_background_mode_ = BACKGROUND_IMAGE;
250 int container_id) {
251 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
252 background_layer->SetColor(color);
253
254 Shell::GetContainer(root_window,container_id)->
255 layer()->Add(background_layer);
256 return background_layer;
257 }
258
259 void DesktopBackgroundController::InstallComponent(
260 aura::RootWindow* root_window) {
261 internal::DesktopBackgroundWidgetController* component = NULL;
262 int container_id = GetBackgroundContainerId(locked_);
263
264 switch (desktop_background_mode_) {
265 case BACKGROUND_IMAGE: {
266 views::Widget* widget = internal::CreateDesktopBackground(root_window,
267 container_id);
268 component = new internal::DesktopBackgroundWidgetController(widget);
269 break;
270 }
271 case BACKGROUND_SOLID_COLOR: {
272 ui::Layer* layer = SetColorLayerForContainer(background_color_,
273 root_window,
274 container_id);
275 component = new internal::DesktopBackgroundWidgetController(layer);
276 break;
277 }
278 default: {
279 NOTREACHED();
280 }
281 }
282 root_window->SetProperty(internal::kWindowDesktopComponent, component);
283 }
284
285 void DesktopBackgroundController::InstallComponentForAllWindows() {
286 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 237 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
287 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 238 for (Shell::RootWindowList::iterator iter = root_windows.begin();
288 iter != root_windows.end(); ++iter) { 239 iter != root_windows.end(); ++iter) {
289 InstallComponent(*iter); 240 internal::CreateDesktopBackground(*iter);
290 } 241 }
291 } 242 }
292 243
293 void DesktopBackgroundController::ReparentBackgroundWidgets(int src_container,
294 int dst_container) {
295 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
296 for (Shell::RootWindowList::iterator iter = root_windows.begin();
297 iter != root_windows.end(); ++iter) {
298 aura::RootWindow* root_window = *iter;
299 internal::DesktopBackgroundWidgetController* component = root_window->
300 GetProperty(internal::kWindowDesktopComponent);
301 DCHECK(component);
302 component->Reparent(root_window,
303 src_container,
304 dst_container);
305 }
306 }
307
308 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
309 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer :
310 internal::kShellWindowId_DesktopBackgroundContainer;
311 }
312
313 } // namespace ash 244 } // namespace ash
OLDNEW
« no previous file with comments | « ash/desktop_background/desktop_background_controller.h ('k') | ash/desktop_background/desktop_background_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698