multi_user_window_manager_impl.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #include "ash/multi_user/multi_user_window_manager_impl.h"
  5. #include <set>
  6. #include <vector>
  7. #include "ash/media/media_controller_impl.h"
  8. #include "ash/multi_user/user_switch_animator.h"
  9. #include "ash/public/cpp/multi_user_window_manager_delegate.h"
  10. #include "ash/public/cpp/shell_window_ids.h"
  11. #include "ash/session/session_controller_impl.h"
  12. #include "ash/shell.h"
  13. #include "ash/wm/desks/desks_util.h"
  14. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  15. #include "base/auto_reset.h"
  16. #include "ui/aura/client/aura_constants.h"
  17. #include "ui/aura/window.h"
  18. #include "ui/base/ui_base_types.h"
  19. #include "ui/events/event.h"
  20. #include "ui/wm/core/transient_window_manager.h"
  21. #include "ui/wm/core/window_animations.h"
  22. #include "ui/wm/core/window_util.h"
  23. namespace ash {
  24. namespace {
  25. // The animation time for a single window that is fading in / out.
  26. constexpr base::TimeDelta kAnimationTime = base::Milliseconds(100);
  27. // The animation time for the fade in and / or out when switching users.
  28. constexpr base::TimeDelta kUserFadeTime = base::Milliseconds(110);
  29. // The animation time in ms for a window which get teleported to another screen.
  30. constexpr base::TimeDelta kTeleportAnimationTime = base::Milliseconds(300);
  31. MultiUserWindowManagerImpl* g_instance = nullptr;
  32. bool HasSystemModalTransientChildWindow(aura::Window* window) {
  33. if (window == nullptr)
  34. return false;
  35. aura::Window* system_modal_container = window->GetRootWindow()->GetChildById(
  36. ash::kShellWindowId_SystemModalContainer);
  37. if (window->parent() == system_modal_container)
  38. return true;
  39. for (aura::Window* transient_child : ::wm::GetTransientChildren(window)) {
  40. if (HasSystemModalTransientChildWindow(transient_child))
  41. return true;
  42. }
  43. return false;
  44. }
  45. } // namespace
  46. // A class to temporarily change the animation properties for a window.
  47. class AnimationSetter {
  48. public:
  49. AnimationSetter(aura::Window* window, base::TimeDelta animation_time)
  50. : window_(window),
  51. previous_animation_type_(
  52. ::wm::GetWindowVisibilityAnimationType(window_)),
  53. previous_animation_time_(
  54. ::wm::GetWindowVisibilityAnimationDuration(*window_)) {
  55. ::wm::SetWindowVisibilityAnimationType(
  56. window_, ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
  57. ::wm::SetWindowVisibilityAnimationDuration(window_, animation_time);
  58. }
  59. AnimationSetter(const AnimationSetter&) = delete;
  60. AnimationSetter& operator=(const AnimationSetter&) = delete;
  61. ~AnimationSetter() {
  62. ::wm::SetWindowVisibilityAnimationType(window_, previous_animation_type_);
  63. ::wm::SetWindowVisibilityAnimationDuration(window_,
  64. previous_animation_time_);
  65. }
  66. private:
  67. // The window which gets used.
  68. aura::Window* window_;
  69. // Previous animation type.
  70. const int previous_animation_type_;
  71. // Previous animation time.
  72. const base::TimeDelta previous_animation_time_;
  73. };
  74. MultiUserWindowManagerImpl::WindowEntry::WindowEntry(
  75. const AccountId& account_id)
  76. : owner_(account_id), show_for_user_(account_id) {}
  77. MultiUserWindowManagerImpl::WindowEntry::~WindowEntry() = default;
  78. MultiUserWindowManagerImpl::MultiUserWindowManagerImpl(
  79. MultiUserWindowManagerDelegate* delegate,
  80. const AccountId& account_id)
  81. : delegate_(delegate), current_account_id_(account_id) {
  82. DCHECK(delegate_);
  83. g_instance = this;
  84. Shell::Get()->tablet_mode_controller()->AddObserver(this);
  85. Shell::Get()->session_controller()->AddObserver(this);
  86. }
  87. MultiUserWindowManagerImpl::~MultiUserWindowManagerImpl() {
  88. // When the MultiUserWindowManager gets destroyed, ash::Shell is mostly gone.
  89. // As such we should not try to finalize any outstanding user animations.
  90. // Note that the destruction of the object can be done later.
  91. if (animation_.get())
  92. animation_->CancelAnimation();
  93. // Remove all window observers.
  94. while (!window_to_entry_.empty()) {
  95. // Explicitly remove this from window observer list since OnWindowDestroyed
  96. // no longer does that.
  97. aura::Window* window = window_to_entry_.begin()->first;
  98. window->RemoveObserver(this);
  99. OnWindowDestroyed(window);
  100. }
  101. Shell::Get()->session_controller()->RemoveObserver(this);
  102. Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
  103. g_instance = nullptr;
  104. }
  105. // static
  106. MultiUserWindowManagerImpl* MultiUserWindowManagerImpl::Get() {
  107. return g_instance;
  108. }
  109. void MultiUserWindowManagerImpl::SetWindowOwner(aura::Window* window,
  110. const AccountId& account_id) {
  111. // Make sure the window is valid and there was no owner yet.
  112. DCHECK(window);
  113. DCHECK(account_id.is_valid());
  114. if (GetWindowOwner(window) == account_id)
  115. return;
  116. // Transient window ownership is tracked by the parent window's ownership.
  117. if (GetOwningWindowInTransientChain(window))
  118. return;
  119. DCHECK(GetWindowOwner(window).empty());
  120. std::unique_ptr<WindowEntry> window_entry_ptr =
  121. std::make_unique<WindowEntry>(account_id);
  122. WindowEntry* window_entry = window_entry_ptr.get();
  123. window_to_entry_[window] = std::move(window_entry_ptr);
  124. // Remember the initial visibility of the window.
  125. window_entry->set_show(window->TargetVisibility());
  126. // Add observers to track state changes.
  127. window->AddObserver(this);
  128. ::wm::TransientWindowManager::GetOrCreate(window)->AddObserver(this);
  129. // Check if this window was created due to a user interaction. If it was,
  130. // transfer it to the current user.
  131. const bool show_for_current_user =
  132. window->GetProperty(aura::client::kCreatedByUserGesture);
  133. if (show_for_current_user)
  134. window_entry->set_show_for_user(current_account_id_);
  135. // Add all transient children to our set of windows. Note that the function
  136. // will add the children but not the owner to the transient children map.
  137. AddTransientOwnerRecursive(window, window);
  138. if (!IsWindowOnDesktopOfUser(window, current_account_id_))
  139. SetWindowVisibility(window, false);
  140. }
  141. void MultiUserWindowManagerImpl::ShowWindowForUser(
  142. aura::Window* window,
  143. const AccountId& account_id) {
  144. DCHECK(window);
  145. const AccountId previous_owner(GetUserPresentingWindow(window));
  146. if (!ShowWindowForUserIntern(window, account_id))
  147. return;
  148. // The window switched to a new desktop and we have to switch to that desktop,
  149. // but only when it was on the visible desktop and the the target is not the
  150. // visible desktop.
  151. if (account_id == current_account_id_ ||
  152. previous_owner != current_account_id_)
  153. return;
  154. Shell::Get()->session_controller()->SwitchActiveUser(account_id);
  155. }
  156. const AccountId& MultiUserWindowManagerImpl::GetWindowOwner(
  157. const aura::Window* window) const {
  158. WindowToEntryMap::const_iterator it =
  159. window_to_entry_.find(const_cast<aura::Window*>(window));
  160. return it != window_to_entry_.end() ? it->second->owner() : EmptyAccountId();
  161. }
  162. bool MultiUserWindowManagerImpl::AreWindowsSharedAmongUsers() const {
  163. for (auto& window_pair : window_to_entry_) {
  164. if (window_pair.second->owner() != window_pair.second->show_for_user())
  165. return true;
  166. }
  167. return false;
  168. }
  169. std::set<AccountId> MultiUserWindowManagerImpl::GetOwnersOfVisibleWindows()
  170. const {
  171. std::set<AccountId> result;
  172. for (auto& window_pair : window_to_entry_) {
  173. if (window_pair.first->IsVisible())
  174. result.insert(window_pair.second->owner());
  175. }
  176. return result;
  177. }
  178. const AccountId& MultiUserWindowManagerImpl::GetUserPresentingWindow(
  179. const aura::Window* window) const {
  180. auto iter = window_to_entry_.find(const_cast<aura::Window*>(window));
  181. // If the window is not owned by anyone it is shown on all desktops and we
  182. // return the empty string.
  183. return (iter == window_to_entry_.end()) ? EmptyAccountId()
  184. : iter->second->show_for_user();
  185. }
  186. const AccountId& MultiUserWindowManagerImpl::CurrentAccountId() const {
  187. return current_account_id_;
  188. }
  189. bool MultiUserWindowManagerImpl::IsWindowOnDesktopOfUser(
  190. aura::Window* window,
  191. const AccountId& account_id) const {
  192. const AccountId& presenting_user = GetUserPresentingWindow(window);
  193. return (!presenting_user.is_valid()) || presenting_user == account_id;
  194. }
  195. const AccountId& MultiUserWindowManagerImpl::GetUserPresentingWindow(
  196. aura::Window* window) const {
  197. WindowToEntryMap::const_iterator it = window_to_entry_.find(window);
  198. // If the window is not owned by anyone it is shown on all desktops and we
  199. // return the empty string.
  200. if (it == window_to_entry_.end())
  201. return EmptyAccountId();
  202. // Otherwise we ask the object for its desktop.
  203. return it->second->show_for_user();
  204. }
  205. void MultiUserWindowManagerImpl::OnActiveUserSessionChanged(
  206. const AccountId& account_id) {
  207. // MultiUserWindowManagerImpl is created with an account before the change has
  208. // potentially made it to SessionController. This means
  209. // MultiUserWindowManagerImpl may be notified of a switch to the current user.
  210. // Ignore this. Ignoring this is especially important in tests, which may be
  211. // impacted by running the animation (when the animation closes, observers are
  212. // notified, which may have side effects in downstream code).
  213. if (account_id == current_account_id_)
  214. return;
  215. // This needs to be set before the animation starts.
  216. current_account_id_ = account_id;
  217. // Here to avoid a very nasty race condition, we must destruct any previously
  218. // created animation before creating a new one. Otherwise, the newly
  219. // constructed will hide all windows of the old user in the first step of the
  220. // animation only to be reshown again by the destructor of the old animation.
  221. animation_.reset();
  222. animation_ = std::make_unique<UserSwitchAnimator>(
  223. this, current_account_id_, GetAdjustedAnimationTime(kUserFadeTime));
  224. // Call RequestCaptureState here instead of having MediaClient observe
  225. // ActiveUserChanged because it must happen after
  226. // MultiUserWindowManagerImpl is notified.
  227. Shell::Get()->media_controller()->RequestCaptureState();
  228. }
  229. void MultiUserWindowManagerImpl::OnWindowDestroyed(aura::Window* window) {
  230. if (GetWindowOwner(window).empty()) {
  231. // This must be a window in the transient chain - remove it and its
  232. // children from the owner.
  233. RemoveTransientOwnerRecursive(window);
  234. return;
  235. }
  236. ::wm::TransientWindowManager::GetOrCreate(window)->RemoveObserver(this);
  237. window_to_entry_.erase(window);
  238. }
  239. void MultiUserWindowManagerImpl::OnWindowVisibilityChanging(
  240. aura::Window* window,
  241. bool visible) {
  242. // This command gets called first and immediately when show or hide gets
  243. // called. We remember here the desired state for restoration IF we were
  244. // not ourselves issuing the call.
  245. // Note also that using the OnWindowVisibilityChanged callback cannot be
  246. // used for this.
  247. if (suppress_visibility_changes_)
  248. return;
  249. WindowToEntryMap::iterator it = window_to_entry_.find(window);
  250. // If the window is not owned by anyone it is shown on all desktops.
  251. if (it != window_to_entry_.end()) {
  252. // Remember what was asked for so that we can restore this when the user's
  253. // desktop gets restored.
  254. it->second->set_show(visible);
  255. } else {
  256. TransientWindowToVisibility::iterator it =
  257. transient_window_to_visibility_.find(window);
  258. if (it != transient_window_to_visibility_.end())
  259. it->second = visible;
  260. }
  261. }
  262. void MultiUserWindowManagerImpl::OnWindowVisibilityChanged(aura::Window* window,
  263. bool visible) {
  264. if (suppress_visibility_changes_)
  265. return;
  266. // Don't allow to make the window visible if it shouldn't be.
  267. if (visible && !IsWindowOnDesktopOfUser(window, current_account_id_)) {
  268. SetWindowVisibility(window, false);
  269. return;
  270. }
  271. aura::Window* owned_parent = GetOwningWindowInTransientChain(window);
  272. if (owned_parent && owned_parent != window && visible &&
  273. !IsWindowOnDesktopOfUser(owned_parent, current_account_id_))
  274. SetWindowVisibility(window, false);
  275. }
  276. void MultiUserWindowManagerImpl::OnTransientChildAdded(
  277. aura::Window* window,
  278. aura::Window* transient_window) {
  279. if (!GetWindowOwner(window).empty()) {
  280. AddTransientOwnerRecursive(transient_window, window);
  281. return;
  282. }
  283. aura::Window* owned_parent =
  284. GetOwningWindowInTransientChain(transient_window);
  285. if (!owned_parent)
  286. return;
  287. AddTransientOwnerRecursive(transient_window, owned_parent);
  288. }
  289. void MultiUserWindowManagerImpl::OnTransientChildRemoved(
  290. aura::Window* window,
  291. aura::Window* transient_window) {
  292. // Remove the transient child if the window itself is owned, or one of the
  293. // windows in its transient parents chain.
  294. if (!GetWindowOwner(window).empty() ||
  295. GetOwningWindowInTransientChain(window)) {
  296. RemoveTransientOwnerRecursive(transient_window);
  297. }
  298. }
  299. void MultiUserWindowManagerImpl::OnTabletModeStarted() {
  300. for (auto& entry : window_to_entry_)
  301. Shell::Get()->tablet_mode_controller()->AddWindow(entry.first);
  302. }
  303. void MultiUserWindowManagerImpl::SetAnimationSpeedForTest(
  304. MultiUserWindowManagerImpl::AnimationSpeed speed) {
  305. animation_speed_ = speed;
  306. }
  307. bool MultiUserWindowManagerImpl::IsAnimationRunningForTest() {
  308. return animation_ && !animation_->IsAnimationFinished();
  309. }
  310. const AccountId& MultiUserWindowManagerImpl::GetCurrentUserForTest() const {
  311. return current_account_id_;
  312. }
  313. bool MultiUserWindowManagerImpl::ShowWindowForUserIntern(
  314. aura::Window* window,
  315. const AccountId& account_id) {
  316. // If there is either no owner, or the owner is the current user, no action
  317. // is required.
  318. const AccountId& owner = GetWindowOwner(window);
  319. if ((!owner.is_valid()) ||
  320. (owner == account_id && IsWindowOnDesktopOfUser(window, account_id)))
  321. return false;
  322. bool minimized = wm::WindowStateIs(window, ui::SHOW_STATE_MINIMIZED);
  323. // Check that we are not trying to transfer ownership of a minimized window.
  324. if (account_id != owner && minimized)
  325. return false;
  326. WindowEntry* window_entry = window_to_entry_[window].get();
  327. window_entry->set_show_for_user(account_id);
  328. const bool teleported = !IsWindowOnDesktopOfUser(window, owner);
  329. // Show the window if the added user is the current one.
  330. if (account_id == current_account_id_) {
  331. // Only show the window if it should be shown according to its state.
  332. if (window_entry->show())
  333. SetWindowVisibility(window, true, kTeleportAnimationTime);
  334. } else {
  335. SetWindowVisibility(window, false, kTeleportAnimationTime);
  336. }
  337. delegate_->OnWindowOwnerEntryChanged(window, account_id, minimized,
  338. teleported);
  339. return true;
  340. }
  341. void MultiUserWindowManagerImpl::SetWindowVisibility(
  342. aura::Window* window,
  343. bool visible,
  344. base::TimeDelta animation_time) {
  345. if (desks_util::BelongsToActiveDesk(window) && window->IsVisible() == visible)
  346. return;
  347. // Hiding a system modal dialog should not be allowed. Instead we switch to
  348. // the user which is showing the system modal window.
  349. // Note that in some cases (e.g. unit test) windows might not have a root
  350. // window.
  351. if (!visible && window->GetRootWindow()) {
  352. if (HasSystemModalTransientChildWindow(window)) {
  353. // The window is system modal and we need to find the parent which owns
  354. // it so that we can switch to the desktop accordingly.
  355. AccountId account_id = GetUserPresentingWindow(window);
  356. if (!account_id.is_valid()) {
  357. aura::Window* owning_window = GetOwningWindowInTransientChain(window);
  358. DCHECK(owning_window);
  359. account_id = GetUserPresentingWindow(owning_window);
  360. DCHECK(account_id.is_valid());
  361. }
  362. Shell::Get()->session_controller()->SwitchActiveUser(account_id);
  363. return;
  364. }
  365. }
  366. // To avoid that these commands are recorded as any other commands, we are
  367. // suppressing any window entry changes while this is going on.
  368. base::AutoReset<bool> suppressor(&suppress_visibility_changes_, true);
  369. if (visible)
  370. ShowWithTransientChildrenRecursive(window, animation_time);
  371. else
  372. SetWindowVisible(window, false, animation_time);
  373. }
  374. void MultiUserWindowManagerImpl::ShowWithTransientChildrenRecursive(
  375. aura::Window* window,
  376. base::TimeDelta animation_time) {
  377. for (aura::Window* transient_child : ::wm::GetTransientChildren(window))
  378. ShowWithTransientChildrenRecursive(transient_child, animation_time);
  379. // We show all children which were not explicitly hidden.
  380. TransientWindowToVisibility::iterator it =
  381. transient_window_to_visibility_.find(window);
  382. if (it == transient_window_to_visibility_.end() || it->second)
  383. SetWindowVisible(window, true, animation_time);
  384. }
  385. aura::Window* MultiUserWindowManagerImpl::GetOwningWindowInTransientChain(
  386. aura::Window* window) const {
  387. if (!GetWindowOwner(window).empty())
  388. return nullptr;
  389. aura::Window* parent = ::wm::GetTransientParent(window);
  390. while (parent) {
  391. if (!GetWindowOwner(parent).empty())
  392. return parent;
  393. parent = ::wm::GetTransientParent(parent);
  394. }
  395. return nullptr;
  396. }
  397. void MultiUserWindowManagerImpl::AddTransientOwnerRecursive(
  398. aura::Window* window,
  399. aura::Window* owned_parent) {
  400. // First add all child windows.
  401. for (aura::Window* transient_child : ::wm::GetTransientChildren(window))
  402. AddTransientOwnerRecursive(transient_child, owned_parent);
  403. // If this window is the owned window, we do not have to handle it again.
  404. if (window == owned_parent)
  405. return;
  406. // Remember the current visibility.
  407. DCHECK(transient_window_to_visibility_.find(window) ==
  408. transient_window_to_visibility_.end());
  409. transient_window_to_visibility_[window] = window->IsVisible();
  410. // Add observers to track state changes.
  411. window->AddObserver(this);
  412. ::wm::TransientWindowManager::GetOrCreate(window)->AddObserver(this);
  413. // Hide the window if it should not be shown. Note that this hide operation
  414. // will hide recursively this and all children - but we have already collected
  415. // their initial view state.
  416. if (!IsWindowOnDesktopOfUser(owned_parent, current_account_id_))
  417. SetWindowVisibility(window, false, kAnimationTime);
  418. }
  419. void MultiUserWindowManagerImpl::RemoveTransientOwnerRecursive(
  420. aura::Window* window) {
  421. // First remove all child windows.
  422. for (aura::Window* transient_child : ::wm::GetTransientChildren(window))
  423. RemoveTransientOwnerRecursive(transient_child);
  424. // Find from transient window storage the visibility for the given window,
  425. // set the visibility accordingly and delete the window from the map.
  426. TransientWindowToVisibility::iterator visibility_item =
  427. transient_window_to_visibility_.find(window);
  428. DCHECK(visibility_item != transient_window_to_visibility_.end());
  429. window->RemoveObserver(this);
  430. ::wm::TransientWindowManager::GetOrCreate(window)->RemoveObserver(this);
  431. bool unowned_view_state = visibility_item->second;
  432. transient_window_to_visibility_.erase(visibility_item);
  433. if (unowned_view_state && !window->IsVisible() &&
  434. desks_util::BelongsToActiveDesk(window)) {
  435. // To prevent these commands from being recorded as any other commands, we
  436. // are suppressing any window entry changes while this is going on.
  437. // Instead of calling SetWindowVisible, only show gets called here since all
  438. // dependents have been shown previously already.
  439. base::AutoReset<bool> suppressor(&suppress_visibility_changes_, true);
  440. window->Show();
  441. }
  442. }
  443. void MultiUserWindowManagerImpl::SetWindowVisible(
  444. aura::Window* window,
  445. bool visible,
  446. base::TimeDelta animation_time) {
  447. // The TabletModeWindowManager will not handle invisible windows since they
  448. // are not user activatable. Since invisible windows are not being tracked,
  449. // we tell it to maximize / track this window now before it gets shown, to
  450. // reduce animation jank from multiple resizes.
  451. if (visible)
  452. Shell::Get()->tablet_mode_controller()->AddWindow(window);
  453. AnimationSetter animation_setter(window,
  454. GetAdjustedAnimationTime(animation_time));
  455. if (visible)
  456. window->Show();
  457. else
  458. window->Hide();
  459. }
  460. base::TimeDelta MultiUserWindowManagerImpl::GetAdjustedAnimationTime(
  461. base::TimeDelta default_time) const {
  462. return animation_speed_ == ANIMATION_SPEED_NORMAL
  463. ? default_time
  464. : (animation_speed_ == ANIMATION_SPEED_FAST
  465. ? base::Milliseconds(10)
  466. : base::TimeDelta());
  467. }
  468. // static
  469. std::unique_ptr<MultiUserWindowManager> MultiUserWindowManager::Create(
  470. MultiUserWindowManagerDelegate* delegate,
  471. const AccountId& account_id) {
  472. return std::make_unique<MultiUserWindowManagerImpl>(delegate, account_id);
  473. }
  474. } // namespace ash