Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / ash / multi_user / multi_user_window_manager_chromeos.h
blob23ac68696542a7aab6882404e4e32e2288c3c123
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_state_observer.h"
12 #include "ash/wm/window_state_observer.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.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/views/corewm/transient_window_observer.h"
21 class Browser;
22 class MultiUserNotificationBlockerChromeOS;
23 class MultiUserNotificationBlockerChromeOSTest;
24 class Profile;
26 namespace aura {
27 class Window;
28 class WindowObserver;
31 namespace chrome {
33 class AppObserver;
35 // This ChromeOS implementation of the MultiUserWindowManager interface is
36 // detecting app and browser creations, tagging their windows automatically and
37 // using (currently) show and hide to make the owned windows visible - or not.
38 // If it becomes necessary, the function |SetWindowVisibility| can be
39 // overwritten to match new ways of doing this.
40 // Note:
41 // - aura::Window::Hide() is currently hiding the window and all owned transient
42 // children. However aura::Window::Show() is only showing the window itself.
43 // To address that, all transient children (and their children) are remembered
44 // in |transient_window_to_visibility_| and monitored to keep track of the
45 // visibility changes from the owning user. This way the visibility can be
46 // changed back to its requested state upon showing by us - or when the window
47 // gets detached from its current owning parent.
48 class MultiUserWindowManagerChromeOS
49 : public MultiUserWindowManager,
50 public ash::SessionStateObserver,
51 public ash::wm::WindowStateObserver,
52 public aura::WindowObserver,
53 public content::NotificationObserver,
54 public views::corewm::TransientWindowObserver {
55 public:
56 // Create the manager and use |active_user_id| as the active user.
57 explicit MultiUserWindowManagerChromeOS(const std::string& active_user_id);
58 virtual ~MultiUserWindowManagerChromeOS();
60 // MultiUserWindowManager overrides:
61 virtual void SetWindowOwner(
62 aura::Window* window, const std::string& user_id) OVERRIDE;
63 virtual const std::string& GetWindowOwner(aura::Window* window) OVERRIDE;
64 virtual void ShowWindowForUser(
65 aura::Window* window, const std::string& user_id) OVERRIDE;
66 virtual bool AreWindowsSharedAmongUsers() OVERRIDE;
67 virtual void GetOwnersOfVisibleWindows(
68 std::set<std::string>* user_ids) OVERRIDE;
69 virtual bool IsWindowOnDesktopOfUser(aura::Window* window,
70 const std::string& user_id) OVERRIDE;
71 virtual const std::string& GetUserPresentingWindow(
72 aura::Window* window) OVERRIDE;
73 virtual void AddUser(Profile* profile) OVERRIDE;
75 // SessionStateObserver overrides:
76 virtual void ActiveUserChanged(const std::string& user_id) OVERRIDE;
78 // WindowObserver overrides:
79 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
80 virtual void OnWindowVisibilityChanging(aura::Window* window,
81 bool visible) OVERRIDE;
82 virtual void OnWindowVisibilityChanged(aura::Window* window,
83 bool visible) OVERRIDE;
85 // TransientWindowObserver overrides:
86 virtual void OnTransientChildAdded(aura::Window* window,
87 aura::Window* transient) OVERRIDE;
88 virtual void OnTransientChildRemoved(aura::Window* window,
89 aura::Window* transient) OVERRIDE;
91 // Window .. overrides:
92 virtual void OnWindowShowTypeChanged(
93 ash::wm::WindowState* state,
94 ash::wm::WindowShowType old_type) OVERRIDE;
96 // content::NotificationObserver overrides:
97 virtual void Observe(int type,
98 const content::NotificationSource& source,
99 const content::NotificationDetails& details) OVERRIDE;
101 private:
102 friend class ::MultiUserNotificationBlockerChromeOSTest;
104 class WindowEntry {
105 public:
106 explicit WindowEntry(const std::string& user_id)
107 : owner_(user_id),
108 show_for_user_(user_id),
109 show_(true) {}
110 virtual ~WindowEntry() {}
112 // Returns the owner of this window. This cannot be changed.
113 const std::string& owner() const { return owner_; }
115 // Returns the user for which this should be shown.
116 const std::string& show_for_user() const { return show_for_user_; }
118 // Returns if the window should be shown for the "show user" or not.
119 bool show() const { return show_; }
121 // Set the user which will display the window on the owned desktop. If
122 // an empty user id gets passed the owner will be used.
123 void set_show_for_user(const std::string& user_id) {
124 show_for_user_ = user_id.empty() ? owner_ : user_id;
127 // Sets if the window gets shown for the active user or not.
128 void set_show(bool show) { show_ = show; }
130 private:
131 // The user id of the owner of this window.
132 const std::string owner_;
134 // The user id of the user on which desktop the window gets shown.
135 std::string show_for_user_;
137 // True if the window should be visible for the user which shows the window.
138 bool show_;
140 DISALLOW_COPY_AND_ASSIGN(WindowEntry);
143 typedef std::map<aura::Window*, WindowEntry*> WindowToEntryMap;
144 typedef std::map<std::string, AppObserver*> UserIDToShellWindowObserver;
145 typedef std::map<aura::Window*, bool> TransientWindowToVisibility;
147 // Add a browser window to the system so that the owner can be remembered.
148 void AddBrowserWindow(Browser* browser);
150 // Show / hide the given window. Note: By not doing this within the functions,
151 // this allows to either switching to different ways to show/hide and / or to
152 // distinguish state changes performed by this class vs. state changes
153 // performed by the others. Note furthermore that system modal dialogs will
154 // not get hidden. We will switch instead to the owners desktop.
155 void SetWindowVisibility(aura::Window* window, bool visible);
157 // Show the window and its transient children. However - if a transient child
158 // was turned invisible by some other operation, it will stay invisible.
159 void ShowWithTransientChildrenRecursive(aura::Window* window);
161 // Find the first owned window in the chain.
162 // Returns NULL when the window itself is owned.
163 aura::Window* GetOwningWindowInTransientChain(aura::Window* window);
165 // A |window| and its children were attached as transient children to an
166 // |owning_parent| and need to be registered. Note that the |owning_parent|
167 // itself will not be registered, but its children will.
168 void AddTransientOwnerRecursive(aura::Window* window,
169 aura::Window* owning_parent);
171 // A window and its children were removed from its parent and can be
172 // unregistered.
173 void RemoveTransientOwnerRecursive(aura::Window* window);
175 // A lookup to see to which user the given window belongs to, where and if it
176 // should get shown.
177 WindowToEntryMap window_to_entry_;
179 // A list of all known users and their shell window observers.
180 UserIDToShellWindowObserver user_id_to_app_observer_;
182 // A map which remembers for owned transient windows their own visibility.
183 TransientWindowToVisibility transient_window_to_visibility_;
185 // The currently selected active user. It is used to find the proper
186 // visibility state in various cases. The state is stored here instead of
187 // being read from the user manager to be in sync while a switch occurs.
188 std::string current_user_id_;
190 // The blocker which controls the desktop notification visibility based on the
191 // current multi-user status.
192 scoped_ptr<MultiUserNotificationBlockerChromeOS> notification_blocker_;
194 // The notification registrar to track the creation of browser windows.
195 content::NotificationRegistrar registrar_;
197 // Suppress changes to the visibility flag while we are changing it ourselves.
198 bool suppress_visibility_changes_;
200 // Caching the current multi profile mode since the detection which mode is
201 // used is quite expensive.
202 static MultiProfileMode multi_user_mode_;
204 DISALLOW_COPY_AND_ASSIGN(MultiUserWindowManagerChromeOS);
207 } // namespace chrome
209 #endif // CHROME_BROWSER_UI_ASH_MULTI_USER_MULTI_USER_WINDOW_MANAGER_CHROMEOS_H_