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

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

Issue 10207030: Asynchronously load wallpapers when user pod is selected. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nit Created 8 years, 8 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/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/gfx/compositor/layer.h" 18 #include "ui/gfx/compositor/layer.h"
16 #include "ui/gfx/image/image.h" 19 #include "ui/gfx/image/image.h"
17 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.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 wallpaper_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
45 GetWallpaperInfo(index_).id).ToSkBitmap();
46 if (cancel_flag_.IsSet())
47 return;
48 layout_ = GetWallpaperInfo(index_).layout;
49 }
50
51 void Cancel() {
52 cancel_flag_.Set();
53 }
54
55 const SkBitmap* wallpaper() {
56 return wallpaper_;
57 }
58
59 ImageLayout image_layout() {
60 return layout_;
61 }
62
63 int index() {
64 return index_;
65 }
66
67 private:
68 friend class base::RefCountedThreadSafe<
69 DesktopBackgroundController::WallpaperOperation>;
70
71 base::CancellationFlag cancel_flag_;
72
73 const SkBitmap* wallpaper_;
74 ImageLayout layout_;
75 int index_;
76
77 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
78 };
79
80 DesktopBackgroundController::DesktopBackgroundController()
81 : desktop_background_mode_(BACKGROUND_IMAGE),
82 previous_index_(-1),
83 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
24 } 84 }
25 85
26 DesktopBackgroundController::~DesktopBackgroundController() { 86 DesktopBackgroundController::~DesktopBackgroundController() {
27 } 87 }
28 88
29 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { 89 void DesktopBackgroundController::SetDefaultWallpaper(int index) {
30 internal::RootWindowLayoutManager* root_window_layout = 90 if (previous_index_ == index)
31 Shell::GetInstance()->root_window_layout(); 91 return;
92
93 CancelPendingWallpaperOperation();
94
95 wallpaper_op_ = new WallpaperOperation(index);
96 base::WorkerPool::PostTaskAndReply(
97 FROM_HERE,
98 base::Bind(&WallpaperOperation::Run, wallpaper_op_),
99 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
100 weak_ptr_factory_.GetWeakPtr(),
101 wallpaper_op_),
102 true /* task_is_slow */);
103 }
104
105 void DesktopBackgroundController::CancelPendingWallpaperOperation() {
106 // Set canceled flag of previous request to skip unneeded loading.
107 if (wallpaper_op_.get())
108 wallpaper_op_->Cancel();
109
110 // Cancel reply callback for previous request.
111 weak_ptr_factory_.InvalidateWeakPtrs();
112 }
113
114 void DesktopBackgroundController::SetLoggedInUserWallpaper() {
32 int index = Shell::GetInstance()->user_wallpaper_delegate()-> 115 int index = Shell::GetInstance()->user_wallpaper_delegate()->
33 GetUserWallpaperIndex(); 116 GetUserWallpaperIndex();
34 root_window_layout->SetBackgroundLayer(NULL); 117 SetDefaultWallpaper(index);
35 internal::CreateDesktopBackground(GetWallpaper(index),
36 GetWallpaperInfo(index).layout);
37 desktop_background_mode_ = BACKGROUND_IMAGE;
38 } 118 }
39 119
40 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode() { 120 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode() {
41 // Set a solid black background. 121 // Set a solid black background.
42 // TODO(derat): Remove this in favor of having the compositor only clear the 122 // TODO(derat): Remove this in favor of having the compositor only clear the
43 // viewport when there are regions not covered by a layer: 123 // viewport when there are regions not covered by a layer:
44 // http://crbug.com/113445 124 // http://crbug.com/113445
45 Shell* shell = Shell::GetInstance(); 125 Shell* shell = Shell::GetInstance();
46 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); 126 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
47 background_layer->SetColor(SK_ColorBLACK); 127 background_layer->SetColor(SK_ColorBLACK);
48 shell->GetContainer(internal::kShellWindowId_DesktopBackgroundContainer)-> 128 shell->GetContainer(internal::kShellWindowId_DesktopBackgroundContainer)->
49 layer()->Add(background_layer); 129 layer()->Add(background_layer);
50 shell->root_window_layout()->SetBackgroundLayer(background_layer); 130 shell->root_window_layout()->SetBackgroundLayer(background_layer);
51 shell->root_window_layout()->SetBackgroundWidget(NULL); 131 shell->root_window_layout()->SetBackgroundWidget(NULL);
52 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; 132 desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
53 } 133 }
54 134
135 void DesktopBackgroundController::SetDesktopBackgroundImageMode(
136 scoped_refptr<WallpaperOperation> wo) {
137 internal::RootWindowLayoutManager* root_window_layout =
138 Shell::GetInstance()->root_window_layout();
139 root_window_layout->SetBackgroundLayer(NULL);
140 if(wo->wallpaper()) {
141 internal::CreateDesktopBackground(*wo->wallpaper(), wo->image_layout());
142 desktop_background_mode_ = BACKGROUND_IMAGE;
143 }
144 }
145
146 void DesktopBackgroundController::OnWallpaperLoadCompleted(
147 scoped_refptr<WallpaperOperation> wo) {
148 SetDesktopBackgroundImageMode(wo);
149 previous_index_ = wo->index();
150
151 DCHECK(wo.get() == wallpaper_op_.get());
152 wallpaper_op_ = NULL;
153 }
154
55 } // namespace ash 155 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698