Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / multi_user / multi_user_window_manager_chromeos.h
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.
4
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_
7
8 #include <map>
9 #include <string>
10
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"
21
22 class Browser;
23 class MultiUserNotificationBlockerChromeOS;
24 class MultiUserNotificationBlockerChromeOSTest;
25
26 namespace content {
27 class BrowserContext;
28 }
29
30 namespace aura {
31 class Window;
32 }  // namespace aura
33
34 namespace ash {
35 namespace test {
36 class MultiUserWindowManagerChromeOSTest;
37 }  // namespace test
38 }  // namespace ash
39
40 namespace chrome {
41
42 class AppObserver;
43
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   // Create the manager and use |active_user_id| as the active user.
65   explicit MultiUserWindowManagerChromeOS(const std::string& active_user_id);
66   virtual ~MultiUserWindowManagerChromeOS();
67
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;
84
85   // SessionStateObserver overrides:
86   virtual void ActiveUserChanged(const std::string& user_id) OVERRIDE;
87
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;
94
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;
100
101   // content::NotificationObserver overrides:
102   virtual void Observe(int type,
103                const content::NotificationSource& source,
104                const content::NotificationDetails& details) OVERRIDE;
105
106   // Disable any animations for unit tests.
107   void SetAnimationsForTest(bool disable);
108
109   // Returns true when a user switch animation is running. For unit tests.
110   bool IsAnimationRunningForTest();
111
112   // Returns the current user for unit tests.
113   const std::string& GetCurrentUserForTest();
114
115  private:
116   friend class ::MultiUserNotificationBlockerChromeOSTest;
117   friend class ash::test::MultiUserWindowManagerChromeOSTest;
118
119   class WindowEntry {
120    public:
121     explicit WindowEntry(const std::string& user_id)
122         : owner_(user_id),
123           show_for_user_(user_id),
124           show_(true) {}
125     virtual ~WindowEntry() {}
126
127     // Returns the owner of this window. This cannot be changed.
128     const std::string& owner() const { return owner_; }
129
130     // Returns the user for which this should be shown.
131     const std::string& show_for_user() const { return show_for_user_; }
132
133     // Returns if the window should be shown for the "show user" or not.
134     bool show() const { return show_; }
135
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;
140     }
141
142     // Sets if the window gets shown for the active user or not.
143     void set_show(bool show) { show_ = show; }
144
145    private:
146     // The user id of the owner of this window.
147     const std::string owner_;
148
149     // The user id of the user on which desktop the window gets shown.
150     std::string show_for_user_;
151
152     // True if the window should be visible for the user which shows the window.
153     bool show_;
154
155     DISALLOW_COPY_AND_ASSIGN(WindowEntry);
156   };
157
158   typedef std::map<aura::Window*, WindowEntry*> WindowToEntryMap;
159   typedef std::map<std::string, AppObserver*> UserIDToAppWindowObserver;
160   typedef std::map<aura::Window*, bool> TransientWindowToVisibility;
161
162   // The animation step for the user change animation. First the old user gets
163   // hidden and then the new one gets presented.
164   enum AnimationStep {
165     HIDE_OLD_USER,
166     SHOW_NEW_USER
167   };
168
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);
173
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);
178
179   // Start the user wallpaper animations.
180   void TransitionWallpaper(AnimationStep animtion_step);
181
182   // Start the user shelf animations.
183   void TransitionUserShelf(AnimationStep animtion_step);
184
185   // Add a browser window to the system so that the owner can be remembered.
186   void AddBrowserWindow(Browser* browser);
187
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,
196                            bool visible,
197                            int animation_time_in_ms);
198
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);
204
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);
208
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);
214
215   // A window and its children were removed from its parent and can be
216   // unregistered.
217   void RemoveTransientOwnerRecursive(aura::Window* window);
218
219   // Animate a |window| to be |visible| in |animation_time_in_ms|.
220   void SetWindowVisible(aura::Window* window,
221                         bool visible,
222                         int aimation_time_in_ms);
223
224   // A lookup to see to which user the given window belongs to, where and if it
225   // should get shown.
226   WindowToEntryMap window_to_entry_;
227
228   // A list of all known users and their app window observers.
229   UserIDToAppWindowObserver user_id_to_app_observer_;
230
231   // An observer list to be notified upon window owner changes.
232   ObserverList<Observer> observers_;
233
234   // A map which remembers for owned transient windows their own visibility.
235   TransientWindowToVisibility transient_window_to_visibility_;
236
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_;
241
242   // The blocker which controls the desktop notification visibility based on the
243   // current multi-user status.
244   scoped_ptr<MultiUserNotificationBlockerChromeOS> notification_blocker_;
245
246   // The notification registrar to track the creation of browser windows.
247   content::NotificationRegistrar registrar_;
248
249   // Suppress changes to the visibility flag while we are changing it ourselves.
250   bool suppress_visibility_changes_;
251
252   // Caching the current multi profile mode since the detection which mode is
253   // used is quite expensive.
254   static MultiProfileMode multi_user_mode_;
255
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_;
259
260   // If true, all animations will be suppressed.
261   bool animations_disabled_;
262
263   DISALLOW_COPY_AND_ASSIGN(MultiUserWindowManagerChromeOS);
264 };
265
266 }  // namespace chrome
267
268 #endif  // CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_