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
3 // found in the LICENSE file.
5 #ifndef ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_
6 #define ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_
8 #include "ash/ash_export.h"
9 #include "ash/display/display_controller.h"
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/timer/timer.h"
17 #include "ui/aura/window.h"
18 #include "ui/compositor/layer.h"
19 #include "ui/gfx/image/image_skia.h"
21 typedef unsigned int SkColor
;
31 enum WallpaperLayout
{
32 // Center the wallpaper on the desktop without scaling it. The wallpaper
34 WALLPAPER_LAYOUT_CENTER
,
35 // Scale the wallpaper (while preserving its aspect ratio) to cover the
36 // desktop; the wallpaper may be cropped.
37 WALLPAPER_LAYOUT_CENTER_CROPPED
,
38 // Scale the wallpaper (without preserving its aspect ratio) to match the
40 WALLPAPER_LAYOUT_STRETCH
,
41 // Tile the wallpaper over the background without scaling it.
42 WALLPAPER_LAYOUT_TILE
,
45 enum WallpaperResolution
{
46 WALLPAPER_RESOLUTION_LARGE
,
47 WALLPAPER_RESOLUTION_SMALL
50 const SkColor kLoginWallpaperColor
= 0xFEFEFE;
52 // The width and height of small/large resolution wallpaper. When screen size is
53 // smaller than |kSmallWallpaperMaxWidth| and |kSmallWallpaperMaxHeight|, the
54 // small resolution wallpaper should be used. Otherwise, uses the large
55 // resolution wallpaper.
56 ASH_EXPORT
extern const int kSmallWallpaperMaxWidth
;
57 ASH_EXPORT
extern const int kSmallWallpaperMaxHeight
;
58 ASH_EXPORT
extern const int kLargeWallpaperMaxWidth
;
59 ASH_EXPORT
extern const int kLargeWallpaperMaxHeight
;
61 // The width and heigh of wallpaper thumbnails.
62 ASH_EXPORT
extern const int kWallpaperThumbnailWidth
;
63 ASH_EXPORT
extern const int kWallpaperThumbnailHeight
;
65 class DesktopBackgroundControllerObserver
;
66 class WallpaperResizer
;
68 // Loads selected desktop wallpaper from file system asynchronously and updates
69 // background layer if loaded successfully.
70 class ASH_EXPORT DesktopBackgroundController
71 : public DisplayController::Observer
{
78 DesktopBackgroundController();
79 virtual ~DesktopBackgroundController();
81 BackgroundMode
desktop_background_mode() const {
82 return desktop_background_mode_
;
85 void set_command_line_for_testing(CommandLine
* command_line
) {
86 command_line_for_testing_
= command_line
;
89 // Add/Remove observers.
90 void AddObserver(DesktopBackgroundControllerObserver
* observer
);
91 void RemoveObserver(DesktopBackgroundControllerObserver
* observer
);
93 // Provides current image on the background, or empty gfx::ImageSkia if there
94 // is no image, e.g. background is none.
95 gfx::ImageSkia
GetWallpaper() const;
97 WallpaperLayout
GetWallpaperLayout() const;
99 // Initialize root window's background.
100 void OnRootWindowAdded(aura::RootWindow
* root_window
);
102 // Loads builtin wallpaper asynchronously and sets to current wallpaper
103 // after loaded. Returns true if the controller started loading the
104 // wallpaper and false otherwise (i.e. the appropriate wallpaper was
105 // already loading or loaded).
106 bool SetDefaultWallpaper(bool is_guest
);
108 // Sets the user selected custom wallpaper. Called when user selected a file
109 // from file system or changed the layout of wallpaper.
110 void SetCustomWallpaper(const gfx::ImageSkia
& image
, WallpaperLayout layout
);
112 // Cancels the current wallpaper loading operation.
113 void CancelPendingWallpaperOperation();
115 // Creates an empty wallpaper. Some tests require a wallpaper widget is ready
116 // when running. However, the wallpaper widgets are now created asynchronously
117 // . If loading a real wallpaper, there are cases that these tests crash
118 // because the required widget is not ready. This function synchronously
119 // creates an empty widget for those tests to prevent crashes. An example test
120 // is SystemGestureEventFilterTest.ThreeFingerSwipe.
121 void CreateEmptyWallpaper();
123 // Returns the appropriate wallpaper resolution for all root windows.
124 WallpaperResolution
GetAppropriateResolution();
126 // Move all desktop widgets to locked container.
127 // Returns true if the desktop moved.
128 bool MoveDesktopToLockedContainer();
130 // Move all desktop widgets to unlocked container.
131 // Returns true if the desktop moved.
132 bool MoveDesktopToUnlockedContainer();
134 // Overrides DisplayController::Observer:
135 virtual void OnDisplayConfigurationChanged() OVERRIDE
;
138 friend class DesktopBackgroundControllerTest
;
139 FRIEND_TEST_ALL_PREFIXES(DesktopBackgroundControllerTest
, GetMaxDisplaySize
);
141 // An operation to asynchronously loads wallpaper.
142 class WallpaperLoader
;
144 // Returns true if the specified default wallpaper is already being
145 // loaded by |wallpaper_loader_| or stored in |current_wallpaper_|.
146 bool DefaultWallpaperIsAlreadyLoadingOrLoaded(
147 const base::FilePath
& image_file
, int image_resource_id
) const;
149 // Returns true if the specified custom wallpaper is already stored
150 // in |current_wallpaper_|.
151 bool CustomWallpaperIsAlreadyLoaded(const gfx::ImageSkia
& image
) const;
153 // Creates view for all root windows, or notifies them to repaint if they
155 void SetDesktopBackgroundImageMode();
157 // Creates a new background widget and sets the background mode to image mode.
158 // Called after a default wallpaper has been loaded successfully.
159 void OnDefaultWallpaperLoadCompleted(scoped_refptr
<WallpaperLoader
> loader
);
161 // Creates and adds component for current mode (either Widget or Layer) to
163 void InstallDesktopController(aura::RootWindow
* root_window
);
165 // Creates and adds component for current mode (either Widget or Layer) to
167 void InstallDesktopControllerForAllWindows();
169 // Moves all desktop components from one container to other across all root
170 // windows. Returns true if a desktop moved.
171 bool ReparentBackgroundWidgets(int src_container
, int dst_container
);
173 // Returns id for background container for unlocked and locked states.
174 int GetBackgroundContainerId(bool locked
);
176 // Send notification that background animation finished.
177 void NotifyAnimationFinished();
179 // Reload the wallpaper.
180 void UpdateWallpaper();
182 void set_wallpaper_reload_delay_for_test(bool value
) {
183 wallpaper_reload_delay_
= value
;
186 // Returns the maximum size of all displays combined in native
187 // resolutions. Note that this isn't the bounds of the display who
188 // has maximum resolutions. Instead, this returns the size of the
189 // maximum width of all displays, and the maximum height of all displays.
190 static gfx::Size
GetMaxDisplaySizeInNative();
192 // If non-NULL, used in place of the real command line.
193 CommandLine
* command_line_for_testing_
;
195 // Can change at runtime.
198 BackgroundMode desktop_background_mode_
;
200 SkColor background_color_
;
202 ObserverList
<DesktopBackgroundControllerObserver
> observers_
;
204 // The current wallpaper.
205 scoped_ptr
<WallpaperResizer
> current_wallpaper_
;
207 // If a default wallpaper is stored in |current_wallpaper_|, the path and
208 // resource ID that were passed to WallpaperLoader when loading it.
209 // Otherwise, empty and -1, respectively.
210 base::FilePath current_default_wallpaper_path_
;
211 int current_default_wallpaper_resource_id_
;
213 gfx::Size current_max_display_size_
;
215 scoped_refptr
<WallpaperLoader
> wallpaper_loader_
;
217 base::WeakPtrFactory
<DesktopBackgroundController
> weak_ptr_factory_
;
219 base::OneShotTimer
<DesktopBackgroundController
> timer_
;
221 int wallpaper_reload_delay_
;
223 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundController
);
228 #endif // ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_