1 // Copyright 2013 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 CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_
6 #define CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_
11 #include "ash/session_state_observer.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "ui/aura/window_observer.h"
20 #include "ui/wm/core/transient_window_observer.h"
23 class MultiUserNotificationBlockerChromeOS;
24 class MultiUserNotificationBlockerChromeOSTest;
36 class MultiUserWindowManagerChromeOSTest;
44 // This ChromeOS implementation of the MultiUserWindowManager interface is
45 // detecting app and browser creations, tagging their windows automatically and
46 // using (currently) show and hide to make the owned windows visible - or not.
47 // If it becomes necessary, the function |SetWindowVisibility| can be
48 // overwritten to match new ways of doing this.
50 // - aura::Window::Hide() is currently hiding the window and all owned transient
51 // children. However aura::Window::Show() is only showing the window itself.
52 // To address that, all transient children (and their children) are remembered
53 // in |transient_window_to_visibility_| and monitored to keep track of the
54 // visibility changes from the owning user. This way the visibility can be
55 // changed back to its requested state upon showing by us - or when the window
56 // gets detached from its current owning parent.
57 class MultiUserWindowManagerChromeOS
58 : public MultiUserWindowManager,
59 public ash::SessionStateObserver,
60 public aura::WindowObserver,
61 public content::NotificationObserver,
62 public wm::TransientWindowObserver {
64 // Create the manager and use |active_user_id| as the active user.
65 explicit MultiUserWindowManagerChromeOS(const std::string& active_user_id);
66 virtual ~MultiUserWindowManagerChromeOS();
68 // MultiUserWindowManager overrides:
69 virtual void SetWindowOwner(
70 aura::Window* window, const std::string& user_id) OVERRIDE;
71 virtual const std::string& GetWindowOwner(aura::Window* window) OVERRIDE;
72 virtual void ShowWindowForUser(
73 aura::Window* window, const std::string& user_id) OVERRIDE;
74 virtual bool AreWindowsSharedAmongUsers() OVERRIDE;
75 virtual void GetOwnersOfVisibleWindows(
76 std::set<std::string>* user_ids) OVERRIDE;
77 virtual bool IsWindowOnDesktopOfUser(aura::Window* window,
78 const std::string& user_id) OVERRIDE;
79 virtual const std::string& GetUserPresentingWindow(
80 aura::Window* window) OVERRIDE;
81 virtual void AddUser(content::BrowserContext* context) OVERRIDE;
82 virtual void AddObserver(Observer* observer) OVERRIDE;
83 virtual void RemoveObserver(Observer* observer) OVERRIDE;
85 // SessionStateObserver overrides:
86 virtual void ActiveUserChanged(const std::string& user_id) OVERRIDE;
88 // WindowObserver overrides:
89 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
90 virtual void OnWindowVisibilityChanging(aura::Window* window,
91 bool visible) OVERRIDE;
92 virtual void OnWindowVisibilityChanged(aura::Window* window,
93 bool visible) OVERRIDE;
95 // TransientWindowObserver overrides:
96 virtual void OnTransientChildAdded(aura::Window* window,
97 aura::Window* transient) OVERRIDE;
98 virtual void OnTransientChildRemoved(aura::Window* window,
99 aura::Window* transient) OVERRIDE;
101 // content::NotificationObserver overrides:
102 virtual void Observe(int type,
103 const content::NotificationSource& source,
104 const content::NotificationDetails& details) OVERRIDE;
106 // Disable any animations for unit tests.
107 void SetAnimationsForTest(bool disable);
109 // Returns true when a user switch animation is running. For unit tests.
110 bool IsAnimationRunningForTest();
112 // Returns the current user for unit tests.
113 const std::string& GetCurrentUserForTest();
116 friend class ::MultiUserNotificationBlockerChromeOSTest;
117 friend class ash::test::MultiUserWindowManagerChromeOSTest;
121 explicit WindowEntry(const std::string& user_id)
123 show_for_user_(user_id),
125 virtual ~WindowEntry() {}
127 // Returns the owner of this window. This cannot be changed.
128 const std::string& owner() const { return owner_; }
130 // Returns the user for which this should be shown.
131 const std::string& show_for_user() const { return show_for_user_; }
133 // Returns if the window should be shown for the "show user" or not.
134 bool show() const { return show_; }
136 // Set the user which will display the window on the owned desktop. If
137 // an empty user id gets passed the owner will be used.
138 void set_show_for_user(const std::string& user_id) {
139 show_for_user_ = user_id.empty() ? owner_ : user_id;
142 // Sets if the window gets shown for the active user or not.
143 void set_show(bool show) { show_ = show; }
146 // The user id of the owner of this window.
147 const std::string owner_;
149 // The user id of the user on which desktop the window gets shown.
150 std::string show_for_user_;
152 // True if the window should be visible for the user which shows the window.
155 DISALLOW_COPY_AND_ASSIGN(WindowEntry);
158 typedef std::map<aura::Window*, WindowEntry*> WindowToEntryMap;
159 typedef std::map<std::string, AppObserver*> UserIDToAppWindowObserver;
160 typedef std::map<aura::Window*, bool> TransientWindowToVisibility;
162 // The animation step for the user change animation. First the old user gets
163 // hidden and then the new one gets presented.
169 // Show a window for a user without switching the user.
170 // Returns true when the window moved to a new desktop.
171 bool ShowWindowForUserIntern(aura::Window* window,
172 const std::string& user_id);
174 // Start the user change animation required for |animation_step|.
175 // Note that a call with SHOW_NEW_USER will finalize the animation and kill
176 // the timer (if there is one).
177 void TransitionUser(AnimationStep animtion_step);
179 // Start the user wallpaper animations.
180 void TransitionWallpaper(AnimationStep animtion_step);
182 // Start the user shelf animations.
183 void TransitionUserShelf(AnimationStep animtion_step);
185 // Add a browser window to the system so that the owner can be remembered.
186 void AddBrowserWindow(Browser* browser);
188 // Show / hide the given window. Note: By not doing this within the functions,
189 // this allows to either switching to different ways to show/hide and / or to
190 // distinguish state changes performed by this class vs. state changes
191 // performed by the others. Note furthermore that system modal dialogs will
192 // not get hidden. We will switch instead to the owners desktop.
193 // The |animation_time_in_ms| is the time the animation should take. Set to 0
194 // if it should get set instantly.
195 void SetWindowVisibility(aura::Window* window,
197 int animation_time_in_ms);
199 // Show the window and its transient children. However - if a transient child
200 // was turned invisible by some other operation, it will stay invisible.
201 // Use the given |animation_time_in_ms| for transitioning.
202 void ShowWithTransientChildrenRecursive(aura::Window* window,
203 int animation_time_in_ms);
205 // Find the first owned window in the chain.
206 // Returns NULL when the window itself is owned.
207 aura::Window* GetOwningWindowInTransientChain(aura::Window* window);
209 // A |window| and its children were attached as transient children to an
210 // |owning_parent| and need to be registered. Note that the |owning_parent|
211 // itself will not be registered, but its children will.
212 void AddTransientOwnerRecursive(aura::Window* window,
213 aura::Window* owning_parent);
215 // A window and its children were removed from its parent and can be
217 void RemoveTransientOwnerRecursive(aura::Window* window);
219 // Animate a |window| to be |visible| in |animation_time_in_ms|.
220 void SetWindowVisible(aura::Window* window,
222 int aimation_time_in_ms);
224 // A lookup to see to which user the given window belongs to, where and if it
226 WindowToEntryMap window_to_entry_;
228 // A list of all known users and their app window observers.
229 UserIDToAppWindowObserver user_id_to_app_observer_;
231 // An observer list to be notified upon window owner changes.
232 ObserverList<Observer> observers_;
234 // A map which remembers for owned transient windows their own visibility.
235 TransientWindowToVisibility transient_window_to_visibility_;
237 // The currently selected active user. It is used to find the proper
238 // visibility state in various cases. The state is stored here instead of
239 // being read from the user manager to be in sync while a switch occurs.
240 std::string current_user_id_;
242 // The blocker which controls the desktop notification visibility based on the
243 // current multi-user status.
244 scoped_ptr<MultiUserNotificationBlockerChromeOS> notification_blocker_;
246 // The notification registrar to track the creation of browser windows.
247 content::NotificationRegistrar registrar_;
249 // Suppress changes to the visibility flag while we are changing it ourselves.
250 bool suppress_visibility_changes_;
252 // Caching the current multi profile mode since the detection which mode is
253 // used is quite expensive.
254 static MultiProfileMode multi_user_mode_;
256 // A timer which watches to executes the second part of a "user changed"
257 // animation. Note that this timer exists only during such an animation.
258 scoped_ptr<base::Timer> user_changed_animation_timer_;
260 // If true, all animations will be suppressed.
261 bool animations_disabled_;
263 DISALLOW_COPY_AND_ASSIGN(MultiUserWindowManagerChromeOS);
266 } // namespace chrome
268 #endif // CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_