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

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

Issue 11415015: Remove use of index in wallpaper picker code and some refactor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Please review this patch Created 8 years, 1 month 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_controller_observer.h" 7 #include "ash/desktop_background/desktop_background_controller_observer.h"
8 #include "ash/desktop_background/desktop_background_view.h" 8 #include "ash/desktop_background/desktop_background_view.h"
9 #include "ash/desktop_background/desktop_background_widget_controller.h" 9 #include "ash/desktop_background/desktop_background_widget_controller.h"
10 #include "ash/desktop_background/user_wallpaper_delegate.h" 10 #include "ash/desktop_background/user_wallpaper_delegate.h"
11 #include "ash/root_window_controller.h" 11 #include "ash/root_window_controller.h"
12 #include "ash/shell.h" 12 #include "ash/shell.h"
13 #include "ash/shell_factory.h" 13 #include "ash/shell_factory.h"
14 #include "ash/shell_window_ids.h" 14 #include "ash/shell_window_ids.h"
15 #include "ash/wm/root_window_layout_manager.h" 15 #include "ash/wm/root_window_layout_manager.h"
16 #include "base/bind.h" 16 #include "base/bind.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/synchronization/cancellation_flag.h" 18 #include "base/synchronization/cancellation_flag.h"
19 #include "base/threading/worker_pool.h" 19 #include "base/threading/worker_pool.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 #include "grit/ash_wallpaper_resources.h"
21 #include "ui/aura/root_window.h" 22 #include "ui/aura/root_window.h"
22 #include "ui/aura/window.h" 23 #include "ui/aura/window.h"
23 #include "ui/base/resource/resource_bundle.h" 24 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/compositor/layer.h" 25 #include "ui/compositor/layer.h"
25 #include "ui/gfx/rect.h" 26 #include "ui/gfx/rect.h"
26 #include "ui/gfx/image/image.h" 27 #include "ui/gfx/image/image.h"
27 #include "ui/views/widget/widget.h" 28 #include "ui/views/widget/widget.h"
28 29
29 using ash::internal::DesktopBackgroundWidgetController; 30 using ash::internal::DesktopBackgroundWidgetController;
30 using ash::internal::kAnimatingDesktopController; 31 using ash::internal::kAnimatingDesktopController;
31 using ash::internal::kDesktopController; 32 using ash::internal::kDesktopController;
32 using content::BrowserThread; 33 using content::BrowserThread;
33 34
34 namespace ash { 35 namespace ash {
35 namespace { 36 namespace {
36 37
37 const SkColor kTransparentColor = SkColorSetARGB(0x00, 0x00, 0x00, 0x00); 38 const SkColor kTransparentColor = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
38 39
39 internal::RootWindowLayoutManager* GetRootWindowLayoutManager( 40 internal::RootWindowLayoutManager* GetRootWindowLayoutManager(
40 aura::RootWindow* root_window) { 41 aura::RootWindow* root_window) {
41 return static_cast<internal::RootWindowLayoutManager*>( 42 return static_cast<internal::RootWindowLayoutManager*>(
42 root_window->layout_manager()); 43 root_window->layout_manager());
43 } 44 }
44 } // namespace 45 } // namespace
45 46
47 #if defined(GOOGLE_CHROME_BUILD)
48 const WallpaperInfo kDefaultLargeWallpaper =
49 { IDR_AURA_WALLPAPERS_2_LANDSCAPE8_LARGE, CENTER_CROPPED };
50 const WallpaperInfo kDefaultSmallWallpaper =
51 { IDR_AURA_WALLPAPERS_2_LANDSCAPE8_SMALL, CENTER };
52 const WallpaperInfo kGuestLargeWallpaper =
53 { IDR_AURA_WALLPAPERS_2_LANDSCAPE7_LARGE, CENTER_CROPPED };
54 const WallpaperInfo kGuestSmallWallpaper =
55 { IDR_AURA_WALLPAPERS_2_LANDSCAPE7_SMALL, CENTER };
56 #else
57 const WallpaperInfo kDefaultLargeWallpaper =
58 { IDR_AURA_WALLPAPERS_5_GRADIENT5_LARGE, TILE };
59 const WallpaperInfo kDefaultSmallWallpaper =
60 { IDR_AURA_WALLPAPERS_5_GRADIENT5_SMALL, TILE };
61 const WallpaperInfo kGuestLargeWallpaper = kDefaultLargeWallpaper;
62 const WallpaperInfo kGuestSmallWallpaper = kDefaultSmallWallpaper;
63 #endif
64
46 const int kSmallWallpaperMaxWidth = 1366; 65 const int kSmallWallpaperMaxWidth = 1366;
47 const int kSmallWallpaperMaxHeight = 800; 66 const int kSmallWallpaperMaxHeight = 800;
48 const int kLargeWallpaperMaxWidth = 2560; 67 const int kLargeWallpaperMaxWidth = 2560;
49 const int kLargeWallpaperMaxHeight = 1700; 68 const int kLargeWallpaperMaxHeight = 1700;
50 69
51 // Stores the current wallpaper data. 70 // Stores the current wallpaper data.
52 struct DesktopBackgroundController::WallpaperData { 71 struct DesktopBackgroundController::WallpaperData {
53 WallpaperData(int index, WallpaperResolution resolution) 72 explicit WallpaperData(const WallpaperInfo& info)
54 : is_default_wallpaper(true), 73 : wallpaper_idr(info.idr),
55 wallpaper_index(index), 74 wallpaper_layout(info.layout),
56 wallpaper_layout(GetWallpaperViewInfo(index, resolution).layout),
57 wallpaper_image(*(ui::ResourceBundle::GetSharedInstance().GetImageNamed( 75 wallpaper_image(*(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
58 GetWallpaperViewInfo(index, resolution).id).ToImageSkia())) { 76 info.idr).ToImageSkia())) {
59 } 77 }
60 WallpaperData(WallpaperLayout layout, const gfx::ImageSkia& image) 78 WallpaperData(WallpaperLayout layout, const gfx::ImageSkia& image)
61 : is_default_wallpaper(false), 79 : wallpaper_idr(-1),
62 wallpaper_index(-1),
63 wallpaper_layout(layout), 80 wallpaper_layout(layout),
64 wallpaper_image(image) { 81 wallpaper_image(image) {
65 } 82 }
66 const bool is_default_wallpaper; 83 int wallpaper_idr;
sky 2012/11/19 22:21:19 Can this use WallpaperInfo?
bshe 2012/11/20 00:33:59 Done.
67 const int wallpaper_index;
68 const WallpaperLayout wallpaper_layout; 84 const WallpaperLayout wallpaper_layout;
69 const gfx::ImageSkia wallpaper_image; 85 const gfx::ImageSkia wallpaper_image;
70 }; 86 };
71 87
72 // DesktopBackgroundController::WallpaperOperation wraps background wallpaper 88 // DesktopBackgroundController::WallpaperOperation wraps background wallpaper
73 // loading. 89 // loading.
74 class DesktopBackgroundController::WallpaperOperation 90 class DesktopBackgroundController::WallpaperOperation
sky 2012/11/19 22:21:19 If this loads, shouldn't the name be WallpaperLoad
bshe 2012/11/20 00:33:59 Done.
75 : public base::RefCountedThreadSafe< 91 : public base::RefCountedThreadSafe<
76 DesktopBackgroundController::WallpaperOperation> { 92 DesktopBackgroundController::WallpaperOperation> {
77 public: 93 public:
78 WallpaperOperation(int index, WallpaperResolution resolution) 94 explicit WallpaperOperation(const WallpaperInfo& info)
79 : index_(index), 95 : info_(info) {
80 resolution_(resolution) {
81 } 96 }
82 97
83 static void Run(scoped_refptr<WallpaperOperation> wo) { 98 static void Run(scoped_refptr<WallpaperOperation> wo) {
84 wo->LoadingWallpaper(); 99 wo->LoadingWallpaper();
sky 2012/11/19 22:21:19 Add DCHECK not on UI thread. Similarly naming this
bshe 2012/11/20 00:33:59 Done.
85 } 100 }
86 101
87 void LoadingWallpaper() { 102 void LoadingWallpaper() {
sky 2012/11/19 22:21:19 Does this need to be public?
bshe 2012/11/20 00:33:59 Done.
88 if (cancel_flag_.IsSet()) 103 if (cancel_flag_.IsSet())
89 return; 104 return;
90 wallpaper_data_.reset(new WallpaperData(index_, resolution_)); 105 wallpaper_data_.reset(new WallpaperData(info_));
91 } 106 }
92 107
93 void Cancel() { 108 void Cancel() {
94 cancel_flag_.Set(); 109 cancel_flag_.Set();
95 } 110 }
96 111
97 int index() const { 112 int idr() {
sky 2012/11/19 22:21:19 how come you removed the const?
bshe 2012/11/20 00:33:59 Done.
98 return index_; 113 return info_.idr;
99 } 114 }
100 115
101 WallpaperData* ReleaseWallpaperData() { 116 WallpaperData* ReleaseWallpaperData() {
102 return wallpaper_data_.release(); 117 return wallpaper_data_.release();
103 } 118 }
104 119
105 private: 120 private:
106 friend class base::RefCountedThreadSafe< 121 friend class base::RefCountedThreadSafe<
107 DesktopBackgroundController::WallpaperOperation>; 122 DesktopBackgroundController::WallpaperOperation>;
108 123
109 ~WallpaperOperation() {} 124 ~WallpaperOperation() {}
110 125
111 base::CancellationFlag cancel_flag_; 126 base::CancellationFlag cancel_flag_;
112 127
113 scoped_ptr<WallpaperData> wallpaper_data_; 128 scoped_ptr<WallpaperData> wallpaper_data_;
114 129
115 const int index_; 130 const WallpaperInfo info_;
116
117 const WallpaperResolution resolution_;
118 131
119 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); 132 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
120 }; 133 };
121 134
122 DesktopBackgroundController::DesktopBackgroundController() 135 DesktopBackgroundController::DesktopBackgroundController()
123 : locked_(false), 136 : locked_(false),
124 desktop_background_mode_(BACKGROUND_SOLID_COLOR), 137 desktop_background_mode_(BACKGROUND_SOLID_COLOR),
125 background_color_(kTransparentColor), 138 background_color_(kTransparentColor),
126 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 139 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
127 } 140 }
(...skipping 23 matching lines...) Expand all
151 return current_wallpaper_->wallpaper_layout; 164 return current_wallpaper_->wallpaper_layout;
152 return CENTER_CROPPED; 165 return CENTER_CROPPED;
153 } 166 }
154 167
155 gfx::ImageSkia DesktopBackgroundController::GetCurrentWallpaperImage() { 168 gfx::ImageSkia DesktopBackgroundController::GetCurrentWallpaperImage() {
156 if (desktop_background_mode_ != BACKGROUND_IMAGE) 169 if (desktop_background_mode_ != BACKGROUND_IMAGE)
157 return gfx::ImageSkia(); 170 return gfx::ImageSkia();
158 return GetWallpaper(); 171 return GetWallpaper();
159 } 172 }
160 173
174 int DesktopBackgroundController::GetWallpaperIDR() const {
175 if (wallpaper_op_.get())
176 return wallpaper_op_->idr();
177 else if (current_wallpaper_.get())
178 return current_wallpaper_->wallpaper_idr;
179 else
180 return -1;
181 }
182
161 void DesktopBackgroundController::OnRootWindowAdded( 183 void DesktopBackgroundController::OnRootWindowAdded(
162 aura::RootWindow* root_window) { 184 aura::RootWindow* root_window) {
163 // Handle resolution change for "built-in" images." 185 // Handle resolution change for "built-in" images."
164 if (BACKGROUND_IMAGE == desktop_background_mode_ && 186 if (BACKGROUND_IMAGE == desktop_background_mode_ &&
165 current_wallpaper_.get()) { 187 current_wallpaper_.get()) {
166 gfx::Size root_window_size = root_window->GetHostSize(); 188 gfx::Size root_window_size = root_window->GetHostSize();
167 // Loads a higher resolution wallpaper if new root window is larger than 189 // Loads a higher resolution wallpaper if new root window is larger than
168 // small screen. 190 // small screen.
169 if (kSmallWallpaperMaxWidth < root_window_size.width() || 191 if (kSmallWallpaperMaxWidth < root_window_size.width() ||
170 kSmallWallpaperMaxHeight < root_window_size.height()) { 192 kSmallWallpaperMaxHeight < root_window_size.height()) {
171 if (current_wallpaper_->is_default_wallpaper) { 193 current_wallpaper_.reset(NULL);
172 ReloadDefaultWallpaper(); 194 ash::Shell::GetInstance()->user_wallpaper_delegate()->
173 } else { 195 UpdateWallpaper();
174 ash::Shell::GetInstance()->user_wallpaper_delegate()->
175 UpdateWallpaper();
176 }
177 } 196 }
178 } 197 }
179 198
180 InstallDesktopController(root_window); 199 InstallDesktopController(root_window);
181 } 200 }
182 201
183 void DesktopBackgroundController::CacheDefaultWallpaper(int index) { 202 void DesktopBackgroundController::SetDefaultWallpaper(WallpaperInfo info) {
184 DCHECK(index >= 0); 203 DCHECK(GetWallpaperIDR() != info.idr);
sky 2012/11/19 22:21:19 DCHECK_NE How come this is a DCHECK? Isn't it poss
bshe 2012/11/20 00:33:59 I have moved the logic to prevent reloading same w
185
186 WallpaperResolution resolution = GetAppropriateResolution();
187 scoped_refptr<WallpaperOperation> wallpaper_op =
188 new WallpaperOperation(index, resolution);
189 base::WorkerPool::PostTask(
190 FROM_HERE,
191 base::Bind(&WallpaperOperation::Run, wallpaper_op),
192 true);
193 }
194
195 void DesktopBackgroundController::SetDefaultWallpaper(int index) {
196 // We should not change background when index is invalid. For instance, at
197 // login screen or stub_user login.
198 if (index == GetInvalidWallpaperIndex()) {
199 CreateEmptyWallpaper();
200 return;
201 } else if (index == GetSolidColorIndex()) {
202 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor);
203 return;
204 }
205
206 // Prevents loading of the same wallpaper as the currently loading/loaded
207 // one.
208 if ((wallpaper_op_.get() && wallpaper_op_->index() == index) ||
209 (current_wallpaper_.get() &&
210 current_wallpaper_->wallpaper_index == index)) {
211 return;
212 }
213 204
214 CancelPendingWallpaperOperation(); 205 CancelPendingWallpaperOperation();
215 206 wallpaper_op_ = new WallpaperOperation(info);
216 WallpaperResolution resolution = GetAppropriateResolution();
217
218 wallpaper_op_ = new WallpaperOperation(index, resolution);
219 base::WorkerPool::PostTaskAndReply( 207 base::WorkerPool::PostTaskAndReply(
220 FROM_HERE, 208 FROM_HERE,
221 base::Bind(&WallpaperOperation::Run, wallpaper_op_), 209 base::Bind(&WallpaperOperation::Run, wallpaper_op_),
222 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, 210 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
223 weak_ptr_factory_.GetWeakPtr(), 211 weak_ptr_factory_.GetWeakPtr(),
224 wallpaper_op_), 212 wallpaper_op_),
225 true /* task_is_slow */); 213 true /* task_is_slow */);
226 } 214 }
227 215
228 void DesktopBackgroundController::ReloadDefaultWallpaper() {
229 int index = current_wallpaper_->wallpaper_index;
230 current_wallpaper_.reset(NULL);
231 SetDefaultWallpaper(index);
232 }
233
234 void DesktopBackgroundController::SetCustomWallpaper( 216 void DesktopBackgroundController::SetCustomWallpaper(
235 const gfx::ImageSkia& wallpaper, 217 const gfx::ImageSkia& wallpaper,
236 WallpaperLayout layout) { 218 WallpaperLayout layout) {
237 CancelPendingWallpaperOperation(); 219 CancelPendingWallpaperOperation();
238 if (current_wallpaper_.get() && 220 if (current_wallpaper_.get() &&
239 current_wallpaper_->wallpaper_image.BackedBySameObjectAs(wallpaper)) { 221 current_wallpaper_->wallpaper_image.BackedBySameObjectAs(wallpaper)) {
240 return; 222 return;
241 } 223 }
242 224
243 current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); 225 current_wallpaper_.reset(new WallpaperData(layout, wallpaper));
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 } 406 }
425 return moved; 407 return moved;
426 } 408 }
427 409
428 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) { 410 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
429 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer : 411 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer :
430 internal::kShellWindowId_DesktopBackgroundContainer; 412 internal::kShellWindowId_DesktopBackgroundContainer;
431 } 413 }
432 414
433 } // namespace ash 415 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698