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 "ash/shell_observer.h"
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/timer/timer.h"
18 #include "ui/compositor/layer.h"
19 #include "ui/gfx/image/image_skia.h"
21 typedef unsigned int SkColor
;
29 enum WallpaperLayout
{
30 // Center the wallpaper on the desktop without scaling it. The wallpaper
32 WALLPAPER_LAYOUT_CENTER
,
33 // Scale the wallpaper (while preserving its aspect ratio) to cover the
34 // desktop; the wallpaper may be cropped.
35 WALLPAPER_LAYOUT_CENTER_CROPPED
,
36 // Scale the wallpaper (without preserving its aspect ratio) to match the
38 WALLPAPER_LAYOUT_STRETCH
,
39 // Tile the wallpaper over the background without scaling it.
43 const SkColor kLoginWallpaperColor
= 0xFEFEFE;
45 class DesktopBackgroundControllerObserver
;
46 class WallpaperResizer
;
48 // Updates background layer if necessary.
49 class ASH_EXPORT DesktopBackgroundController
50 : public DisplayController::Observer
,
51 public ShellObserver
{
60 DesktopBackgroundController();
61 virtual ~DesktopBackgroundController();
63 BackgroundMode
desktop_background_mode() const {
64 return desktop_background_mode_
;
67 // Add/Remove observers.
68 void AddObserver(DesktopBackgroundControllerObserver
* observer
);
69 void RemoveObserver(DesktopBackgroundControllerObserver
* observer
);
71 // Provides current image on the background, or empty gfx::ImageSkia if there
72 // is no image, e.g. background is none.
73 gfx::ImageSkia
GetWallpaper() const;
75 WallpaperLayout
GetWallpaperLayout() const;
77 // Sets wallpaper. This is mostly called by WallpaperManager to set
78 // the default or user selected custom wallpaper.
79 // Returns true if new image was actually set. And false when duplicate set
81 bool SetWallpaperImage(const gfx::ImageSkia
& image
, WallpaperLayout layout
);
83 // Creates an empty wallpaper. Some tests require a wallpaper widget is ready
84 // when running. However, the wallpaper widgets are now created
85 // asynchronously. If loading a real wallpaper, there are cases that these
86 // tests crash because the required widget is not ready. This function
87 // synchronously creates an empty widget for those tests to prevent
88 // crashes. An example test is SystemGestureEventFilterTest.ThreeFingerSwipe.
89 void CreateEmptyWallpaper();
91 // Move all desktop widgets to locked container.
92 // Returns true if the desktop moved.
93 bool MoveDesktopToLockedContainer();
95 // Move all desktop widgets to unlocked container.
96 // Returns true if the desktop moved.
97 bool MoveDesktopToUnlockedContainer();
99 // DisplayController::Observer:
100 virtual void OnDisplayConfigurationChanged() override
;
103 virtual void OnRootWindowAdded(aura::Window
* root_window
) override
;
105 // Returns the maximum size of all displays combined in native
106 // resolutions. Note that this isn't the bounds of the display who
107 // has maximum resolutions. Instead, this returns the size of the
108 // maximum width of all displays, and the maximum height of all displays.
109 static gfx::Size
GetMaxDisplaySizeInNative();
111 // Returns true if the specified wallpaper is already stored
112 // in |current_wallpaper_|.
113 // If |compare_layouts| is false, layout is ignored.
114 bool WallpaperIsAlreadyLoaded(const gfx::ImageSkia
& image
,
115 bool compare_layouts
,
116 WallpaperLayout layout
) const;
119 friend class DesktopBackgroundControllerTest
;
120 // friend class chromeos::WallpaperManagerBrowserTestDefaultWallpaper;
121 FRIEND_TEST_ALL_PREFIXES(DesktopBackgroundControllerTest
, GetMaxDisplaySize
);
123 // Creates view for all root windows, or notifies them to repaint if they
125 void SetDesktopBackgroundImageMode();
127 // Creates and adds component for current mode (either Widget or Layer) to
129 void InstallDesktopController(aura::Window
* root_window
);
131 // Creates and adds component for current mode (either Widget or Layer) to
133 void InstallDesktopControllerForAllWindows();
135 // Moves all desktop components from one container to other across all root
136 // windows. Returns true if a desktop moved.
137 bool ReparentBackgroundWidgets(int src_container
, int dst_container
);
139 // Returns id for background container for unlocked and locked states.
140 int GetBackgroundContainerId(bool locked
);
142 // Send notification that background animation finished.
143 void NotifyAnimationFinished();
145 // Reload the wallpaper.
146 void UpdateWallpaper();
148 void set_wallpaper_reload_delay_for_test(bool value
) {
149 wallpaper_reload_delay_
= value
;
152 // Can change at runtime.
155 BackgroundMode desktop_background_mode_
;
157 SkColor background_color_
;
159 ObserverList
<DesktopBackgroundControllerObserver
> observers_
;
161 // The current wallpaper.
162 scoped_ptr
<WallpaperResizer
> current_wallpaper_
;
164 gfx::Size current_max_display_size_
;
166 base::OneShotTimer
<DesktopBackgroundController
> timer_
;
168 int wallpaper_reload_delay_
;
170 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundController
);
175 #endif // ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_