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. 4 5 #ifndef ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_ 6 #define ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_ 7 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" 20 21 typedef unsigned int SkColor; 22 23 namespace aura { 24 class Window; 25 } 26 27 namespace ash { 28 29 enum WallpaperLayout { 30 // Center the wallpaper on the desktop without scaling it. The wallpaper 31 // may be cropped. 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 37 // desktop's size. 38 WALLPAPER_LAYOUT_STRETCH, 39 // Tile the wallpaper over the background without scaling it. 40 WALLPAPER_LAYOUT_TILE 41 }; 42 43 const SkColor kLoginWallpaperColor = 0xFEFEFE; 44 45 class DesktopBackgroundControllerObserver; 46 class WallpaperResizer; 47 48 // Updates background layer if necessary. 49 class ASH_EXPORT DesktopBackgroundController 50 : public DisplayController::Observer, 51 public ShellObserver { 52 public: 53 class TestAPI; 54 55 enum BackgroundMode { 56 BACKGROUND_NONE, 57 BACKGROUND_IMAGE, 58 }; 59 60 DesktopBackgroundController(); 61 virtual ~DesktopBackgroundController(); 62 63 BackgroundMode desktop_background_mode() const { 64 return desktop_background_mode_; 65 } 66 67 // Add/Remove observers. 68 void AddObserver(DesktopBackgroundControllerObserver* observer); 69 void RemoveObserver(DesktopBackgroundControllerObserver* observer); 70 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; 74 75 WallpaperLayout GetWallpaperLayout() const; 76 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 80 // request detected. 81 bool SetWallpaperImage(const gfx::ImageSkia& image, WallpaperLayout layout); 82 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(); 90 91 // Move all desktop widgets to locked container. 92 // Returns true if the desktop moved. 93 bool MoveDesktopToLockedContainer(); 94 95 // Move all desktop widgets to unlocked container. 96 // Returns true if the desktop moved. 97 bool MoveDesktopToUnlockedContainer(); 98 99 // DisplayController::Observer: 100 virtual void OnDisplayConfigurationChanged() OVERRIDE; 101 102 // ShellObserver: 103 virtual void OnRootWindowAdded(aura::Window* root_window) OVERRIDE; 104 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(); 110 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; 117 118 private: 119 friend class DesktopBackgroundControllerTest; 120 // friend class chromeos::WallpaperManagerBrowserTestDefaultWallpaper; 121 FRIEND_TEST_ALL_PREFIXES(DesktopBackgroundControllerTest, GetMaxDisplaySize); 122 123 // Creates view for all root windows, or notifies them to repaint if they 124 // already exist. 125 void SetDesktopBackgroundImageMode(); 126 127 // Creates and adds component for current mode (either Widget or Layer) to 128 // |root_window|. 129 void InstallDesktopController(aura::Window* root_window); 130 131 // Creates and adds component for current mode (either Widget or Layer) to 132 // all root windows. 133 void InstallDesktopControllerForAllWindows(); 134 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); 138 139 // Returns id for background container for unlocked and locked states. 140 int GetBackgroundContainerId(bool locked); 141 142 // Send notification that background animation finished. 143 void NotifyAnimationFinished(); 144 145 // Reload the wallpaper. 146 void UpdateWallpaper(); 147 148 void set_wallpaper_reload_delay_for_test(bool value) { 149 wallpaper_reload_delay_ = value; 150 } 151 152 // Can change at runtime. 153 bool locked_; 154 155 BackgroundMode desktop_background_mode_; 156 157 SkColor background_color_; 158 159 ObserverList<DesktopBackgroundControllerObserver> observers_; 160 161 // The current wallpaper. 162 scoped_ptr<WallpaperResizer> current_wallpaper_; 163 164 gfx::Size current_max_display_size_; 165 166 base::OneShotTimer<DesktopBackgroundController> timer_; 167 168 int wallpaper_reload_delay_; 169 170 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundController); 171 }; 172 173 } // namespace ash 174 175 #endif // ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_ 176