| OLD | NEW |
| 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/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "ash/shell_factory.h" | 9 #include "ash/shell_factory.h" |
| 10 #include "ash/shell_window_ids.h" | 10 #include "ash/shell_window_ids.h" |
| 11 #include "ash/wm/root_window_layout_manager.h" | 11 #include "ash/wm/root_window_layout_manager.h" |
| 12 #include "base/bind.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/synchronization/cancellation_flag.h" |
| 15 #include "base/threading/worker_pool.h" |
| 13 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
| 14 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/compositor/layer.h" | 19 #include "ui/compositor/layer.h" |
| 17 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
| 18 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 19 | 22 |
| 20 namespace ash { | 23 namespace ash { |
| 21 | 24 |
| 22 DesktopBackgroundController::DesktopBackgroundController() : | 25 // DesktopBackgroundController::WallpaperOperation wraps background wallpaper |
| 23 desktop_background_mode_(BACKGROUND_IMAGE) { | 26 // loading. |
| 27 class DesktopBackgroundController::WallpaperOperation |
| 28 : public base::RefCountedThreadSafe< |
| 29 DesktopBackgroundController::WallpaperOperation> { |
| 30 public: |
| 31 WallpaperOperation(int index) |
| 32 : wallpaper_(NULL), |
| 33 layout_(CENTER_CROPPED), |
| 34 index_(index) { |
| 35 } |
| 36 |
| 37 static void Run(scoped_refptr<WallpaperOperation> wo) { |
| 38 wo->LoadingWallpaper(); |
| 39 } |
| 40 |
| 41 void LoadingWallpaper() { |
| 42 if (cancel_flag_.IsSet()) |
| 43 return; |
| 44 |
| 45 wallpaper_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 46 GetWallpaperInfo(index_).id).ToSkBitmap(); |
| 47 |
| 48 if (cancel_flag_.IsSet()) |
| 49 return; |
| 50 layout_ = GetWallpaperInfo(index_).layout; |
| 51 } |
| 52 |
| 53 void Cancel() { |
| 54 cancel_flag_.Set(); |
| 55 } |
| 56 |
| 57 const SkBitmap* wallpaper() { |
| 58 return wallpaper_; |
| 59 } |
| 60 |
| 61 ImageLayout image_layout() { |
| 62 return layout_; |
| 63 } |
| 64 |
| 65 int index() { |
| 66 return index_; |
| 67 } |
| 68 |
| 69 private: |
| 70 friend class base::RefCountedThreadSafe< |
| 71 DesktopBackgroundController::WallpaperOperation>; |
| 72 ~WallpaperOperation(){}; |
| 73 |
| 74 base::CancellationFlag cancel_flag_; |
| 75 |
| 76 const SkBitmap* wallpaper_; |
| 77 ImageLayout layout_; |
| 78 int index_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); |
| 81 }; |
| 82 |
| 83 DesktopBackgroundController::DesktopBackgroundController() |
| 84 : desktop_background_mode_(BACKGROUND_IMAGE), |
| 85 previous_index_(-1), |
| 86 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 24 } | 87 } |
| 25 | 88 |
| 26 DesktopBackgroundController::~DesktopBackgroundController() { | 89 DesktopBackgroundController::~DesktopBackgroundController() { |
| 90 CancelPendingWallpaperOperation(); |
| 27 } | 91 } |
| 28 | 92 |
| 29 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { | 93 void DesktopBackgroundController::SetDefaultWallpaper(int index) { |
| 30 internal::RootWindowLayoutManager* root_window_layout = | 94 if (previous_index_ == index) |
| 31 Shell::GetInstance()->root_window_layout(); | 95 return; |
| 96 CancelPendingWallpaperOperation(); |
| 97 |
| 98 wallpaper_op_ = new WallpaperOperation(index); |
| 99 base::WorkerPool::PostTaskAndReply( |
| 100 FROM_HERE, |
| 101 base::Bind(&WallpaperOperation::Run, wallpaper_op_), |
| 102 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, |
| 103 weak_ptr_factory_.GetWeakPtr(), |
| 104 wallpaper_op_), |
| 105 true /* task_is_slow */); |
| 106 } |
| 107 |
| 108 void DesktopBackgroundController::CancelPendingWallpaperOperation() { |
| 109 // Set canceled flag of previous request to skip unneeded loading. |
| 110 if (wallpaper_op_.get()) |
| 111 wallpaper_op_->Cancel(); |
| 112 |
| 113 // Cancel reply callback for previous request. |
| 114 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 115 } |
| 116 |
| 117 void DesktopBackgroundController::SetLoggedInUserWallpaper() { |
| 32 int index = Shell::GetInstance()->user_wallpaper_delegate()-> | 118 int index = Shell::GetInstance()->user_wallpaper_delegate()-> |
| 33 GetUserWallpaperIndex(); | 119 GetUserWallpaperIndex(); |
| 34 // We should not change background when index is invalid. For instance, at | 120 // We should not change background when index is invalid. For instance, at |
| 35 // login screen. | 121 // login screen or stub_user login. |
| 36 if (index == ash::GetInvalidWallpaperIndex()) | 122 if (index == ash::GetInvalidWallpaperIndex()) { |
| 123 CreateEmptyWallpaper(); |
| 37 return; | 124 return; |
| 38 root_window_layout->SetBackgroundLayer(NULL); | 125 } |
| 39 internal::CreateDesktopBackground(GetWallpaper(index), | 126 |
| 40 GetWallpaperInfo(index).layout); | 127 SetDefaultWallpaper(index); |
| 41 desktop_background_mode_ = BACKGROUND_IMAGE; | |
| 42 } | 128 } |
| 43 | 129 |
| 44 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode() { | 130 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode() { |
| 45 // Set a solid black background. | 131 // Set a solid black background. |
| 46 // TODO(derat): Remove this in favor of having the compositor only clear the | 132 // TODO(derat): Remove this in favor of having the compositor only clear the |
| 47 // viewport when there are regions not covered by a layer: | 133 // viewport when there are regions not covered by a layer: |
| 48 // http://crbug.com/113445 | 134 // http://crbug.com/113445 |
| 49 Shell* shell = Shell::GetInstance(); | 135 Shell* shell = Shell::GetInstance(); |
| 50 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); | 136 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); |
| 51 background_layer->SetColor(SK_ColorBLACK); | 137 background_layer->SetColor(SK_ColorBLACK); |
| 52 shell->GetContainer(internal::kShellWindowId_DesktopBackgroundContainer)-> | 138 shell->GetContainer(internal::kShellWindowId_DesktopBackgroundContainer)-> |
| 53 layer()->Add(background_layer); | 139 layer()->Add(background_layer); |
| 54 shell->root_window_layout()->SetBackgroundLayer(background_layer); | 140 shell->root_window_layout()->SetBackgroundLayer(background_layer); |
| 55 shell->root_window_layout()->SetBackgroundWidget(NULL); | 141 shell->root_window_layout()->SetBackgroundWidget(NULL); |
| 56 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; | 142 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; |
| 57 } | 143 } |
| 58 | 144 |
| 145 void DesktopBackgroundController::SetDesktopBackgroundImageMode( |
| 146 scoped_refptr<WallpaperOperation> wo) { |
| 147 internal::RootWindowLayoutManager* root_window_layout = |
| 148 Shell::GetInstance()->root_window_layout(); |
| 149 root_window_layout->SetBackgroundLayer(NULL); |
| 150 if(wo->wallpaper()) { |
| 151 internal::CreateDesktopBackground(*wo->wallpaper(), wo->image_layout()); |
| 152 desktop_background_mode_ = BACKGROUND_IMAGE; |
| 153 } |
| 154 } |
| 155 |
| 156 void DesktopBackgroundController::OnWallpaperLoadCompleted( |
| 157 scoped_refptr<WallpaperOperation> wo) { |
| 158 SetDesktopBackgroundImageMode(wo); |
| 159 previous_index_ = wo->index(); |
| 160 |
| 161 DCHECK(wo.get() == wallpaper_op_.get()); |
| 162 wallpaper_op_ = NULL; |
| 163 } |
| 164 |
| 165 void DesktopBackgroundController::CreateEmptyWallpaper() { |
| 166 SkBitmap dummy; |
| 167 internal::CreateDesktopBackground(dummy, CENTER); |
| 168 desktop_background_mode_ = BACKGROUND_IMAGE; |
| 169 } |
| 170 |
| 59 } // namespace ash | 171 } // namespace ash |
| OLD | NEW |