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

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

Issue 208273005: If customization includes default wallpaper, download and apply it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Deleted DBC::SetDefaultWallpaper. Created 6 years, 9 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/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "ash/desktop_background/desktop_background_controller_observer.h" 8 #include "ash/desktop_background/desktop_background_controller_observer.h"
9 #include "ash/desktop_background/desktop_background_view.h" 9 #include "ash/desktop_background/desktop_background_view.h"
10 #include "ash/desktop_background/desktop_background_widget_controller.h" 10 #include "ash/desktop_background/desktop_background_widget_controller.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 // changed? 43 // changed?
44 const int kWallpaperReloadDelayMs = 2000; 44 const int kWallpaperReloadDelayMs = 2000;
45 45
46 } // namespace 46 } // namespace
47 47
48 const int kSmallWallpaperMaxWidth = 1366; 48 const int kSmallWallpaperMaxWidth = 1366;
49 const int kSmallWallpaperMaxHeight = 800; 49 const int kSmallWallpaperMaxHeight = 800;
50 const int kLargeWallpaperMaxWidth = 2560; 50 const int kLargeWallpaperMaxWidth = 2560;
51 const int kLargeWallpaperMaxHeight = 1700; 51 const int kLargeWallpaperMaxHeight = 1700;
52 const int kWallpaperThumbnailWidth = 108; 52 const int kWallpaperThumbnailWidth = 108;
53 const int kWallpaperThumbnailHeight = 68; 53 const int kWallpaperThumbnailHeight = 68;
Daniel Erat 2014/03/27 01:45:50 delete these if you don't use them
Alexander Alekseev 2014/03/31 14:15:40 They are used in extensions/wallpaper_api.cc
54 54
55 // DesktopBackgroundController::WallpaperLoader wraps background wallpaper 55 // DesktopBackgroundController::WallpaperLoader wraps background wallpaper
56 // loading. 56 // loading.
57 class DesktopBackgroundController::WallpaperLoader 57 class DesktopBackgroundController::WallpaperLoader
Daniel Erat 2014/03/27 01:45:50 delete WallpaperLoader if you don't use it anymore
Alexander Alekseev 2014/03/31 14:15:40 Done.
58 : public base::RefCountedThreadSafe< 58 : public base::RefCountedThreadSafe<
59 DesktopBackgroundController::WallpaperLoader> { 59 DesktopBackgroundController::WallpaperLoader> {
60 public: 60 public:
61 // If set, |file_path| must be a trusted (i.e. read-only, 61 // If set, |file_path| must be a trusted (i.e. read-only,
62 // non-user-controlled) file containing a JPEG image. 62 // non-user-controlled) file containing a JPEG image.
63 WallpaperLoader(const base::FilePath& file_path, 63 WallpaperLoader(const base::FilePath& file_path,
64 WallpaperLayout file_layout, 64 WallpaperLayout file_layout,
65 int resource_id, 65 int resource_id,
66 WallpaperLayout resource_layout) 66 WallpaperLayout resource_layout)
67 : file_path_(file_path), 67 : file_path_(file_path),
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 // ID of an image resource to use if |file_path_| is empty or unloadable. 148 // ID of an image resource to use if |file_path_| is empty or unloadable.
149 int resource_id_; 149 int resource_id_;
150 150
151 // Layout to be used when displaying |resource_id_|. 151 // Layout to be used when displaying |resource_id_|.
152 WallpaperLayout resource_layout_; 152 WallpaperLayout resource_layout_;
153 153
154 DISALLOW_COPY_AND_ASSIGN(WallpaperLoader); 154 DISALLOW_COPY_AND_ASSIGN(WallpaperLoader);
155 }; 155 };
156 156
157 DesktopBackgroundController::DesktopBackgroundController() 157 DesktopBackgroundController::DesktopBackgroundController()
158 : command_line_for_testing_(NULL), 158 : locked_(false),
159 locked_(false),
160 desktop_background_mode_(BACKGROUND_NONE), 159 desktop_background_mode_(BACKGROUND_NONE),
161 current_default_wallpaper_resource_id_(-1),
162 weak_ptr_factory_(this),
163 wallpaper_reload_delay_(kWallpaperReloadDelayMs) { 160 wallpaper_reload_delay_(kWallpaperReloadDelayMs) {
164 Shell::GetInstance()->display_controller()->AddObserver(this); 161 Shell::GetInstance()->display_controller()->AddObserver(this);
165 } 162 }
166 163
167 DesktopBackgroundController::~DesktopBackgroundController() { 164 DesktopBackgroundController::~DesktopBackgroundController() {
168 CancelDefaultWallpaperLoader();
169 Shell::GetInstance()->display_controller()->RemoveObserver(this); 165 Shell::GetInstance()->display_controller()->RemoveObserver(this);
170 } 166 }
171 167
172 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { 168 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
173 if (current_wallpaper_) 169 if (current_wallpaper_)
174 return current_wallpaper_->image(); 170 return current_wallpaper_->image();
175 return gfx::ImageSkia(); 171 return gfx::ImageSkia();
176 } 172 }
177 173
178 void DesktopBackgroundController::AddObserver( 174 void DesktopBackgroundController::AddObserver(
(...skipping 22 matching lines...) Expand all
201 if (current_max_display_size_ != max_display_size) { 197 if (current_max_display_size_ != max_display_size) {
202 current_max_display_size_ = max_display_size; 198 current_max_display_size_ = max_display_size;
203 if (desktop_background_mode_ == BACKGROUND_IMAGE && 199 if (desktop_background_mode_ == BACKGROUND_IMAGE &&
204 current_wallpaper_.get()) 200 current_wallpaper_.get())
205 UpdateWallpaper(); 201 UpdateWallpaper();
206 } 202 }
207 203
208 InstallDesktopController(root_window); 204 InstallDesktopController(root_window);
209 } 205 }
210 206
211 bool DesktopBackgroundController::SetDefaultWallpaper(bool is_guest) {
212 VLOG(1) << "SetDefaultWallpaper: is_guest=" << is_guest;
213 const bool use_large =
214 GetAppropriateResolution() == WALLPAPER_RESOLUTION_LARGE;
215
216 base::FilePath file_path;
217 WallpaperLayout file_layout = use_large ? WALLPAPER_LAYOUT_CENTER_CROPPED :
218 WALLPAPER_LAYOUT_CENTER;
219 int resource_id = use_large ? IDR_AURA_WALLPAPER_DEFAULT_LARGE :
220 IDR_AURA_WALLPAPER_DEFAULT_SMALL;
221 WallpaperLayout resource_layout = WALLPAPER_LAYOUT_TILE;
222
223 CommandLine* command_line = command_line_for_testing_ ?
224 command_line_for_testing_ : CommandLine::ForCurrentProcess();
225 const char* switch_name = NULL;
226 if (is_guest) {
227 switch_name = use_large ? switches::kAshGuestWallpaperLarge :
228 switches::kAshGuestWallpaperSmall;
229 } else {
230 switch_name = use_large ? switches::kAshDefaultWallpaperLarge :
231 switches::kAshDefaultWallpaperSmall;
232 }
233 file_path = command_line->GetSwitchValuePath(switch_name);
234
235 if (DefaultWallpaperIsAlreadyLoadingOrLoaded(file_path, resource_id)) {
236 VLOG(1) << "Default wallpaper is already loading or loaded";
237 return false;
238 }
239
240 CancelDefaultWallpaperLoader();
241 default_wallpaper_loader_ = new WallpaperLoader(
242 file_path, file_layout, resource_id, resource_layout);
243 base::WorkerPool::PostTaskAndReply(
244 FROM_HERE,
245 base::Bind(&WallpaperLoader::LoadOnWorkerPoolThread,
246 default_wallpaper_loader_),
247 base::Bind(&DesktopBackgroundController::OnDefaultWallpaperLoadCompleted,
248 weak_ptr_factory_.GetWeakPtr(),
249 default_wallpaper_loader_),
250 true /* task_is_slow */);
251 return true;
252 }
253
254 void DesktopBackgroundController::SetCustomWallpaper( 207 void DesktopBackgroundController::SetCustomWallpaper(
255 const gfx::ImageSkia& image, 208 const gfx::ImageSkia& image,
256 WallpaperLayout layout) { 209 WallpaperLayout layout) {
257 VLOG(1) << "SetCustomWallpaper: image_id=" 210 VLOG(1) << "SetCustomWallpaper: image_id="
258 << WallpaperResizer::GetImageId(image) << " layout=" << layout; 211 << WallpaperResizer::GetImageId(image) << " layout=" << layout;
259 CancelDefaultWallpaperLoader();
260 212
261 if (CustomWallpaperIsAlreadyLoaded(image)) { 213 if (CustomWallpaperIsAlreadyLoaded(image)) {
262 VLOG(1) << "Custom wallpaper is already loaded"; 214 VLOG(1) << "Custom wallpaper is already loaded";
263 return; 215 return;
264 } 216 }
265 217
266 current_wallpaper_.reset(new WallpaperResizer( 218 current_wallpaper_.reset(new WallpaperResizer(
267 image, GetMaxDisplaySizeInNative(), layout)); 219 image, GetMaxDisplaySizeInNative(), layout));
268 current_wallpaper_->StartResize(); 220 current_wallpaper_->StartResize();
269 221
270 current_default_wallpaper_path_ = base::FilePath();
271 current_default_wallpaper_resource_id_ = -1;
272
273 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, 222 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_,
274 OnWallpaperDataChanged()); 223 OnWallpaperDataChanged());
275 SetDesktopBackgroundImageMode(); 224 SetDesktopBackgroundImageMode();
276 } 225 }
277 226
278 void DesktopBackgroundController::CancelDefaultWallpaperLoader() {
279 // Set canceled flag of previous request to skip unneeded loading.
280 if (default_wallpaper_loader_.get())
281 default_wallpaper_loader_->Cancel();
282
283 // Cancel reply callback for previous request.
284 weak_ptr_factory_.InvalidateWeakPtrs();
285 }
286
287 void DesktopBackgroundController::CreateEmptyWallpaper() { 227 void DesktopBackgroundController::CreateEmptyWallpaper() {
Daniel Erat 2014/03/27 01:45:50 delete this if it's not called anymore
Alexander Alekseev 2014/03/31 14:15:40 It's used by Wallpapermanager, API, DefaultDelegat
288 current_wallpaper_.reset(NULL); 228 current_wallpaper_.reset(NULL);
289 SetDesktopBackgroundImageMode(); 229 SetDesktopBackgroundImageMode();
290 } 230 }
291 231
292 WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() { 232 WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() {
Daniel Erat 2014/03/27 01:45:50 delete this if you don't use it
Alexander Alekseev 2014/03/31 14:15:40 It's used by Wallpapermanager (to select cached wa
293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
294 gfx::Size size = GetMaxDisplaySizeInNative(); 234 gfx::Size size = GetMaxDisplaySizeInNative();
295 return (size.width() > kSmallWallpaperMaxWidth || 235 return (size.width() > kSmallWallpaperMaxWidth ||
296 size.height() > kSmallWallpaperMaxHeight) ? 236 size.height() > kSmallWallpaperMaxHeight) ?
297 WALLPAPER_RESOLUTION_LARGE : WALLPAPER_RESOLUTION_SMALL; 237 WALLPAPER_RESOLUTION_LARGE : WALLPAPER_RESOLUTION_SMALL;
298 } 238 }
299 239
300 bool DesktopBackgroundController::MoveDesktopToLockedContainer() { 240 bool DesktopBackgroundController::MoveDesktopToLockedContainer() {
301 if (locked_) 241 if (locked_)
302 return false; 242 return false;
(...skipping 18 matching lines...) Expand all
321 current_wallpaper_.get()) { 261 current_wallpaper_.get()) {
322 timer_.Stop(); 262 timer_.Stop();
323 timer_.Start(FROM_HERE, 263 timer_.Start(FROM_HERE,
324 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_), 264 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_),
325 this, 265 this,
326 &DesktopBackgroundController::UpdateWallpaper); 266 &DesktopBackgroundController::UpdateWallpaper);
327 } 267 }
328 } 268 }
329 } 269 }
330 270
331 bool DesktopBackgroundController::DefaultWallpaperIsAlreadyLoadingOrLoaded(
332 const base::FilePath& image_file,
333 int image_resource_id) const {
334 return (default_wallpaper_loader_.get() &&
335 !default_wallpaper_loader_->IsCanceled() &&
336 default_wallpaper_loader_->file_path() == image_file &&
337 default_wallpaper_loader_->resource_id() == image_resource_id) ||
338 (current_wallpaper_.get() &&
339 current_default_wallpaper_path_ == image_file &&
340 current_default_wallpaper_resource_id_ == image_resource_id);
341 }
342
343 bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded( 271 bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded(
344 const gfx::ImageSkia& image) const { 272 const gfx::ImageSkia& image) const {
345 return current_wallpaper_.get() && 273 return current_wallpaper_.get() &&
346 (WallpaperResizer::GetImageId(image) == 274 (WallpaperResizer::GetImageId(image) ==
347 current_wallpaper_->original_image_id()); 275 current_wallpaper_->original_image_id());
348 } 276 }
349 277
350 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { 278 void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
351 desktop_background_mode_ = BACKGROUND_IMAGE; 279 desktop_background_mode_ = BACKGROUND_IMAGE;
352 InstallDesktopControllerForAllWindows(); 280 InstallDesktopControllerForAllWindows();
353 } 281 }
354 282
355 void DesktopBackgroundController::OnDefaultWallpaperLoadCompleted(
356 scoped_refptr<WallpaperLoader> loader) {
357 VLOG(1) << "OnDefaultWallpaperLoadCompleted";
358 current_wallpaper_.reset(loader->ReleaseWallpaperResizer());
359 current_wallpaper_->StartResize();
360 current_default_wallpaper_path_ = loader->file_path();
361 current_default_wallpaper_resource_id_ = loader->resource_id();
362 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_,
363 OnWallpaperDataChanged());
364
365 SetDesktopBackgroundImageMode();
366
367 DCHECK(loader.get() == default_wallpaper_loader_.get());
368 default_wallpaper_loader_ = NULL;
369 }
370
371 void DesktopBackgroundController::InstallDesktopController( 283 void DesktopBackgroundController::InstallDesktopController(
372 aura::Window* root_window) { 284 aura::Window* root_window) {
373 internal::DesktopBackgroundWidgetController* component = NULL; 285 internal::DesktopBackgroundWidgetController* component = NULL;
374 int container_id = GetBackgroundContainerId(locked_); 286 int container_id = GetBackgroundContainerId(locked_);
375 287
376 switch (desktop_background_mode_) { 288 switch (desktop_background_mode_) {
377 case BACKGROUND_IMAGE: { 289 case BACKGROUND_IMAGE: {
378 views::Widget* widget = internal::CreateDesktopBackground(root_window, 290 views::Widget* widget = internal::CreateDesktopBackground(root_window,
379 container_id); 291 container_id);
380 component = new internal::DesktopBackgroundWidgetController(widget); 292 component = new internal::DesktopBackgroundWidgetController(widget);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 return moved; 350 return moved;
439 } 351 }
440 352
441 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) { 353 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
442 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer : 354 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer :
443 internal::kShellWindowId_DesktopBackgroundContainer; 355 internal::kShellWindowId_DesktopBackgroundContainer;
444 } 356 }
445 357
446 void DesktopBackgroundController::UpdateWallpaper() { 358 void DesktopBackgroundController::UpdateWallpaper() {
447 current_wallpaper_.reset(NULL); 359 current_wallpaper_.reset(NULL);
448 current_default_wallpaper_path_ = base::FilePath();
449 current_default_wallpaper_resource_id_ = -1;
450 ash::Shell::GetInstance()->user_wallpaper_delegate()-> 360 ash::Shell::GetInstance()->user_wallpaper_delegate()->
451 UpdateWallpaper(true /* clear cache */); 361 UpdateWallpaper(true /* clear cache */);
452 } 362 }
453 363
454 // static 364 // static
455 gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() { 365 gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() {
456 int width = 0; 366 int width = 0;
457 int height = 0; 367 int height = 0;
458 std::vector<gfx::Display> displays = Shell::GetScreen()->GetAllDisplays(); 368 std::vector<gfx::Display> displays = Shell::GetScreen()->GetAllDisplays();
459 internal::DisplayManager* display_manager = 369 internal::DisplayManager* display_manager =
460 Shell::GetInstance()->display_manager(); 370 Shell::GetInstance()->display_manager();
461 371
462 for (std::vector<gfx::Display>::iterator iter = displays.begin(); 372 for (std::vector<gfx::Display>::iterator iter = displays.begin();
463 iter != displays.end(); ++iter) { 373 iter != displays.end(); ++iter) {
464 // Don't use size_in_pixel because we want to use the native pixel size. 374 // Don't use size_in_pixel because we want to use the native pixel size.
465 gfx::Size size_in_pixel = 375 gfx::Size size_in_pixel =
466 display_manager->GetDisplayInfo(iter->id()).bounds_in_native().size(); 376 display_manager->GetDisplayInfo(iter->id()).bounds_in_native().size();
467 if (iter->rotation() == gfx::Display::ROTATE_90 || 377 if (iter->rotation() == gfx::Display::ROTATE_90 ||
468 iter->rotation() == gfx::Display::ROTATE_270) { 378 iter->rotation() == gfx::Display::ROTATE_270) {
469 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); 379 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width());
470 } 380 }
471 width = std::max(size_in_pixel.width(), width); 381 width = std::max(size_in_pixel.width(), width);
472 height = std::max(size_in_pixel.height(), height); 382 height = std::max(size_in_pixel.height(), height);
473 } 383 }
474 return gfx::Size(width, height); 384 return gfx::Size(width, height);
475 } 385 }
476 386
477 } // namespace ash 387 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698