Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / ash / multi_user / multi_user_window_manager_chromeos.h
blob3507d5ee710d5d6d8be74ccaaf2d5feb3240a8cf
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_
8 #include <map>
9 #include <string>
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"
21 class Browser;
22 class MultiUserNotificationBlockerChromeOS;
23 class MultiUserNotificationBlockerChromeOSTest;
25 namespace content {
26 class BrowserContext;
29 namespace aura {
30 class Window;
31 } // namespace aura
33 namespace ash {
34 namespace test {
35 class MultiUserWindowManagerChromeOSTest;
36 } // namespace test
37 } // namespace ash
39 namespace chrome {
41 class AppObserver;
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.
49 // Note:
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 {
63 public:
64 // The speed which should be used to perform animations.
65 enum AnimationSpeed {
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.
76 void Init();
78 // MultiUserWindowManager overrides:
79 void SetWindowOwner(
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(
89 aura::Window* window,
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;
125 protected:
126 friend class UserSwitchAnimatorChromeOS;
128 class WindowEntry {
129 public:
130 explicit WindowEntry(const std::string& user_id)
131 : owner_(user_id),
132 show_for_user_(user_id),
133 show_(true) {}
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; }
154 private:
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.
162 bool show_;
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,
182 bool visible,
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();
190 private:
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
217 // unregistered.
218 void RemoveTransientOwnerRecursive(aura::Window* window);
220 // Animate a |window| to be |visible| in |animation_time_in_ms|.
221 void SetWindowVisible(aura::Window* window,
222 bool visible,
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
230 // should get shown.
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 base::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 // The speed which is used to perform any animations.
258 AnimationSpeed animation_speed_;
260 // The animation between users.
261 scoped_ptr<UserSwitchAnimatorChromeOS> animation_;
263 DISALLOW_COPY_AND_ASSIGN(MultiUserWindowManagerChromeOS);
266 } // namespace chrome
268 #endif // CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_