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/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 "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "ui/aura/window_observer.h"
19 #include "ui/wm/core/transient_window_observer.h"
22 class MultiUserNotificationBlockerChromeOS
;
23 class MultiUserNotificationBlockerChromeOSTest
;
35 class MultiUserWindowManagerChromeOSTest
;
42 class UserSwitchAnimatorChromeOS
;
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 // The speed which should be used to perform animations.
66 ANIMATION_SPEED_NORMAL
, // The normal animation speed.
67 ANIMATION_SPEED_FAST
, // Unit test speed which test animations.
68 ANIMATION_SPEED_DISABLED
// Unit tests which do not require animations.
71 // Create the manager and use |active_user_id| as the active user.
72 explicit MultiUserWindowManagerChromeOS(const std::string
& active_user_id
);
73 ~MultiUserWindowManagerChromeOS() override
;
75 // Initializes the manager after its creation. Should only be called once.
78 // MultiUserWindowManager overrides:
80 aura::Window
* window
, const std::string
& user_id
) override
;
81 const std::string
& GetWindowOwner(
82 aura::Window
* window
) const override
;
83 void ShowWindowForUser(
84 aura::Window
* window
, const std::string
& user_id
) override
;
85 bool AreWindowsSharedAmongUsers() const override
;
86 void GetOwnersOfVisibleWindows(
87 std::set
<std::string
>* user_ids
) const override
;
88 bool IsWindowOnDesktopOfUser(
90 const std::string
& user_id
) const override
;
91 const std::string
& GetUserPresentingWindow(
92 aura::Window
* window
) const override
;
93 void AddUser(content::BrowserContext
* context
) override
;
94 void AddObserver(Observer
* observer
) override
;
95 void RemoveObserver(Observer
* observer
) override
;
97 // SessionStateObserver overrides:
98 void ActiveUserChanged(const std::string
& user_id
) override
;
100 // WindowObserver overrides:
101 void OnWindowDestroyed(aura::Window
* window
) override
;
102 void OnWindowVisibilityChanging(aura::Window
* window
, bool visible
) override
;
103 void OnWindowVisibilityChanged(aura::Window
* window
, bool visible
) override
;
105 // TransientWindowObserver overrides:
106 void OnTransientChildAdded(aura::Window
* window
,
107 aura::Window
* transient
) override
;
108 void OnTransientChildRemoved(aura::Window
* window
,
109 aura::Window
* transient
) override
;
111 // content::NotificationObserver overrides:
112 void Observe(int type
,
113 const content::NotificationSource
& source
,
114 const content::NotificationDetails
& details
) override
;
116 // Disable any animations for unit tests.
117 void SetAnimationSpeedForTest(AnimationSpeed speed
);
119 // Returns true when a user switch animation is running. For unit tests.
120 bool IsAnimationRunningForTest();
122 // Returns the current user for unit tests.
123 const std::string
& GetCurrentUserForTest() const;
126 friend class UserSwitchAnimatorChromeOS
;
130 explicit WindowEntry(const std::string
& user_id
)
132 show_for_user_(user_id
),
134 virtual ~WindowEntry() {}
136 // Returns the owner of this window. This cannot be changed.
137 const std::string
& owner() const { return owner_
; }
139 // Returns the user for which this should be shown.
140 const std::string
& show_for_user() const { return show_for_user_
; }
142 // Returns if the window should be shown for the "show user" or not.
143 bool show() const { return show_
; }
145 // Set the user which will display the window on the owned desktop. If
146 // an empty user id gets passed the owner will be used.
147 void set_show_for_user(const std::string
& user_id
) {
148 show_for_user_
= user_id
.empty() ? owner_
: user_id
;
151 // Sets if the window gets shown for the active user or not.
152 void set_show(bool show
) { show_
= show
; }
155 // The user id of the owner of this window.
156 const std::string owner_
;
158 // The user id of the user on which desktop the window gets shown.
159 std::string show_for_user_
;
161 // True if the window should be visible for the user which shows the window.
164 DISALLOW_COPY_AND_ASSIGN(WindowEntry
);
167 typedef std::map
<aura::Window
*, WindowEntry
*> WindowToEntryMap
;
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 // Show / hide the given window. Note: By not doing this within the functions,
175 // this allows to either switching to different ways to show/hide and / or to
176 // distinguish state changes performed by this class vs. state changes
177 // performed by the others. Note furthermore that system modal dialogs will
178 // not get hidden. We will switch instead to the owners desktop.
179 // The |animation_time_in_ms| is the time the animation should take. Set to 0
180 // if it should get set instantly.
181 void SetWindowVisibility(aura::Window
* window
,
183 int animation_time_in_ms
);
185 const WindowToEntryMap
& window_to_entry() { return window_to_entry_
; }
186 MultiUserNotificationBlockerChromeOS
* notification_blocker() {
187 return notification_blocker_
.get();
191 friend class ::MultiUserNotificationBlockerChromeOSTest
;
192 friend class ash::test::MultiUserWindowManagerChromeOSTest
;
194 typedef std::map
<std::string
, AppObserver
*> UserIDToAppWindowObserver
;
195 typedef std::map
<aura::Window
*, bool> TransientWindowToVisibility
;
197 // Add a browser window to the system so that the owner can be remembered.
198 void AddBrowserWindow(Browser
* browser
);
200 // Show the window and its transient children. However - if a transient child
201 // was turned invisible by some other operation, it will stay invisible.
202 // Use the given |animation_time_in_ms| for transitioning.
203 void ShowWithTransientChildrenRecursive(aura::Window
* window
,
204 int animation_time_in_ms
);
206 // Find the first owned window in the chain.
207 // Returns NULL when the window itself is owned.
208 aura::Window
* GetOwningWindowInTransientChain(aura::Window
* window
) const;
210 // A |window| and its children were attached as transient children to an
211 // |owning_parent| and need to be registered. Note that the |owning_parent|
212 // itself will not be registered, but its children will.
213 void AddTransientOwnerRecursive(aura::Window
* window
,
214 aura::Window
* owning_parent
);
216 // A window and its children were removed from its parent and can be
218 void RemoveTransientOwnerRecursive(aura::Window
* window
);
220 // Animate a |window| to be |visible| in |animation_time_in_ms|.
221 void SetWindowVisible(aura::Window
* window
,
223 int aimation_time_in_ms
);
225 // Get the animation time in milliseconds dependent on the |AnimationSpeed|
226 // from the passed |default_time_in_ms|.
227 int GetAdjustedAnimationTimeInMS(int default_time_in_ms
) const;
229 // A lookup to see to which user the given window belongs to, where and if it
231 WindowToEntryMap window_to_entry_
;
233 // A list of all known users and their app window observers.
234 UserIDToAppWindowObserver user_id_to_app_observer_
;
236 // An observer list to be notified upon window owner changes.
237 ObserverList
<Observer
> observers_
;
239 // A map which remembers for owned transient windows their own visibility.
240 TransientWindowToVisibility transient_window_to_visibility_
;
242 // The currently selected active user. It is used to find the proper
243 // visibility state in various cases. The state is stored here instead of
244 // being read from the user manager to be in sync while a switch occurs.
245 std::string current_user_id_
;
247 // The blocker which controls the desktop notification visibility based on the
248 // current multi-user status.
249 scoped_ptr
<MultiUserNotificationBlockerChromeOS
> notification_blocker_
;
251 // The notification registrar to track the creation of browser windows.
252 content::NotificationRegistrar registrar_
;
254 // Suppress changes to the visibility flag while we are changing it ourselves.
255 bool suppress_visibility_changes_
;
257 // Caching the current multi profile mode since the detection which mode is
258 // used is quite expensive.
259 static MultiProfileMode multi_user_mode_
;
261 // The speed which is used to perform any animations.
262 AnimationSpeed animation_speed_
;
264 // The animation between users.
265 scoped_ptr
<UserSwitchAnimatorChromeOS
> animation_
;
267 DISALLOW_COPY_AND_ASSIGN(MultiUserWindowManagerChromeOS
);
270 } // namespace chrome
272 #endif // CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_