#include "ash/multi_user/multi_user_window_manager_impl.h"
#include "base/memory/raw_ptr.h"
#include <set>
#include <vector>
#include "ash/media/media_controller_impl.h"
#include "ash/multi_user/user_switch_animator.h"
#include "ash/public/cpp/multi_user_window_manager_delegate.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/auto_reset.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/base/ui_base_types.h"
#include "ui/events/event.h"
#include "ui/wm/core/transient_window_manager.h"
#include "ui/wm/core/window_animations.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
constexpr base::TimeDelta kAnimationTime = base::Milliseconds(100);
constexpr base::TimeDelta kUserFadeTime = base::Milliseconds(110);
constexpr base::TimeDelta kTeleportAnimationTime = base::Milliseconds(300);
MultiUserWindowManagerImpl* g_instance = nullptr;
bool HasSystemModalTransientChildWindow(aura::Window* window) {
if (window == nullptr)
return false;
aura::Window* system_modal_container = window->GetRootWindow()->GetChildById(
ash::kShellWindowId_SystemModalContainer);
if (window->parent() == system_modal_container)
return true;
for (aura::Window* transient_child : ::wm::GetTransientChildren(window)) {
if (HasSystemModalTransientChildWindow(transient_child))
return true;
}
return false;
}
}
class AnimationSetter {
public:
AnimationSetter(aura::Window* window, base::TimeDelta animation_time)
: window_(window),
previous_animation_type_(
::wm::GetWindowVisibilityAnimationType(window_)),
previous_animation_time_(
::wm::GetWindowVisibilityAnimationDuration(*window_)) {
::wm::SetWindowVisibilityAnimationType(
window_, ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
::wm::SetWindowVisibilityAnimationDuration(window_, animation_time);
}
AnimationSetter(const AnimationSetter&) = delete;
AnimationSetter& operator=(const AnimationSetter&) = delete;
~AnimationSetter() {
::wm::SetWindowVisibilityAnimationType(window_, previous_animation_type_);
::wm::SetWindowVisibilityAnimationDuration(window_,
previous_animation_time_);
}
private:
raw_ptr<aura::Window, ExperimentalAsh> window_;
const int previous_animation_type_;
const base::TimeDelta previous_animation_time_;
};
MultiUserWindowManagerImpl::WindowEntry::WindowEntry(
const AccountId& account_id)
: owner_(account_id), show_for_user_(account_id) {}
MultiUserWindowManagerImpl::WindowEntry::~WindowEntry() = default;
MultiUserWindowManagerImpl::MultiUserWindowManagerImpl(
MultiUserWindowManagerDelegate* delegate,
const AccountId& account_id)
: delegate_(delegate), current_account_id_(account_id) {
DCHECK(delegate_);
g_instance = this;
Shell::Get()->tablet_mode_controller()->AddObserver(this);
Shell::Get()->session_controller()->AddObserver(this);
}
MultiUserWindowManagerImpl::~MultiUserWindowManagerImpl() {
if (animation_.get())
animation_->CancelAnimation();
while (!window_to_entry_.empty()) {
aura::Window* window = window_to_entry_.begin()->first;
window->RemoveObserver(this);
OnWindowDestroyed(window);
}
Shell::Get()->session_controller()->RemoveObserver(this);
Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
g_instance = nullptr;
}
MultiUserWindowManagerImpl* MultiUserWindowManagerImpl::Get() {
return g_instance;
}
void MultiUserWindowManagerImpl::SetWindowOwner(aura::Window* window,
const AccountId& account_id) {
DCHECK(window);
DCHECK(account_id.is_valid());
if (GetWindowOwner(window) == account_id)
return;
if (GetOwningWindowInTransientChain(window))
return;
DCHECK(GetWindowOwner(window).empty());
std::unique_ptr<WindowEntry> window_entry_ptr =
std::make_unique<WindowEntry>(account_id);
WindowEntry* window_entry = window_entry_ptr.get();
window_to_entry_[window] = std::move(window_entry_ptr);
window_entry->set_show(window->TargetVisibility());
window->AddObserver(this);
::wm::TransientWindowManager::GetOrCreate(window)->AddObserver(this);
const bool show_for_current_user =
window->GetProperty(aura::client::kCreatedByUserGesture);
if (show_for_current_user)
window_entry->set_show_for_user(current_account_id_);
AddTransientOwnerRecursive(window, window);
if (!IsWindowOnDesktopOfUser(window, current_account_id_))
SetWindowVisibility(window, false);
}
void MultiUserWindowManagerImpl::ShowWindowForUser(
aura::Window* window,
const AccountId& account_id) {
DCHECK(window);
const AccountId previous_owner(GetUserPresentingWindow(window));
if (!ShowWindowForUserIntern(window, account_id))
return;
if (account_id == current_account_id_ ||
previous_owner != current_account_id_)
return;
Shell::Get()->session_controller()->SwitchActiveUser(account_id);
}
const AccountId& MultiUserWindowManagerImpl::GetWindowOwner(
const aura::Window* window) const {
WindowToEntryMap::const_iterator it =
window_to_entry_.find(const_cast<aura::Window*>(window));
return it != window_to_entry_.end() ? it->second->owner() : EmptyAccountId();
}
bool MultiUserWindowManagerImpl::AreWindowsSharedAmongUsers() const {
for (auto& window_pair : window_to_entry_) {
if (window_pair.second->owner() != window_pair.second->show_for_user())
return true;
}
return false;
}
std::set<AccountId> MultiUserWindowManagerImpl::GetOwnersOfVisibleWindows()
const {
std::set<AccountId> result;
for (auto& window_pair : window_to_entry_) {
if (window_pair.first->IsVisible())
result.insert(window_pair.second->owner());
}
return result;
}
const AccountId& MultiUserWindowManagerImpl::GetUserPresentingWindow(
const aura::Window* window) const {
auto iter = window_to_entry_.find(const_cast<aura::Window*>(window));
return (iter == window_to_entry_.end()) ? EmptyAccountId()
: iter->second->show_for_user();
}
const AccountId& MultiUserWindowManagerImpl::CurrentAccountId() const {
return current_account_id_;
}
bool MultiUserWindowManagerImpl::IsWindowOnDesktopOfUser(
aura::Window* window,
const AccountId& account_id) const {
const AccountId& presenting_user = GetUserPresentingWindow(window);
return (!presenting_user.is_valid()) || presenting_user == account_id;
}
const AccountId& MultiUserWindowManagerImpl::GetUserPresentingWindow(
aura::Window* window) const {
WindowToEntryMap::const_iterator it = window_to_entry_.find(window);
if (it == window_to_entry_.end())
return EmptyAccountId();
return it->second->show_for_user();
}
void MultiUserWindowManagerImpl::OnActiveUserSessionChanged(
const AccountId& account_id) {
if (account_id == current_account_id_)
return;
current_account_id_ = account_id;
animation_.reset();
animation_ = std::make_unique<UserSwitchAnimator>(
this, current_account_id_, GetAdjustedAnimationTime(kUserFadeTime));
Shell::Get()->media_controller()->RequestCaptureState();
}
void MultiUserWindowManagerImpl::OnWindowDestroyed(aura::Window* window) {
if (GetWindowOwner(window).empty()) {
RemoveTransientOwnerRecursive(window);
return;
}
::wm::TransientWindowManager::GetOrCreate(window)->RemoveObserver(this);
window_to_entry_.erase(window);
}
void MultiUserWindowManagerImpl::OnWindowVisibilityChanging(
aura::Window* window,
bool visible) {
if (suppress_visibility_changes_)
return;
if (WindowToEntryMap::iterator it = window_to_entry_.find(window);
it != window_to_entry_.end()) {
it->second->set_show(visible);
return;
}
if (TransientWindowToVisibility::iterator it =
transient_window_to_visibility_.find(window);
it != transient_window_to_visibility_.end()) {
it->second = visible;
}
}
void MultiUserWindowManagerImpl::OnWindowVisibilityChanged(aura::Window* window,
bool visible) {
if (suppress_visibility_changes_)
return;
if (visible && !IsWindowOnDesktopOfUser(window, current_account_id_)) {
SetWindowVisibility(window, false);
return;
}
aura::Window* owned_parent = GetOwningWindowInTransientChain(window);
if (owned_parent && owned_parent != window && visible &&
!IsWindowOnDesktopOfUser(owned_parent, current_account_id_))
SetWindowVisibility(window, false);
}
void MultiUserWindowManagerImpl::OnTransientChildAdded(
aura::Window* window,
aura::Window* transient_window) {
if (!GetWindowOwner(window).empty()) {
AddTransientOwnerRecursive(transient_window, window);
return;
}
aura::Window* owned_parent =
GetOwningWindowInTransientChain(transient_window);
if (!owned_parent)
return;
AddTransientOwnerRecursive(transient_window, owned_parent);
}
void MultiUserWindowManagerImpl::OnTransientChildRemoved(
aura::Window* window,
aura::Window* transient_window) {
if (!GetWindowOwner(window).empty() ||
GetOwningWindowInTransientChain(window)) {
RemoveTransientOwnerRecursive(transient_window);
}
}
void MultiUserWindowManagerImpl::OnTabletModeStarted() {
for (auto& entry : window_to_entry_)
Shell::Get()->tablet_mode_controller()->AddWindow(entry.first);
}
void MultiUserWindowManagerImpl::SetAnimationSpeedForTest(
MultiUserWindowManagerImpl::AnimationSpeed speed) {
animation_speed_ = speed;
}
bool MultiUserWindowManagerImpl::IsAnimationRunningForTest() {
return animation_ && !animation_->IsAnimationFinished();
}
const AccountId& MultiUserWindowManagerImpl::GetCurrentUserForTest() const {
return current_account_id_;
}
bool MultiUserWindowManagerImpl::ShowWindowForUserIntern(
aura::Window* window,
const AccountId& account_id) {
const AccountId& owner = GetWindowOwner(window);
if ((!owner.is_valid()) ||
(owner == account_id && IsWindowOnDesktopOfUser(window, account_id)))
return false;
bool minimized = wm::WindowStateIs(window, ui::SHOW_STATE_MINIMIZED);
if (account_id != owner && minimized)
return false;
WindowEntry* window_entry = window_to_entry_[window].get();
window_entry->set_show_for_user(account_id);
const bool teleported = !IsWindowOnDesktopOfUser(window, owner);
if (account_id == current_account_id_) {
if (window_entry->show())
SetWindowVisibility(window, true, kTeleportAnimationTime);
} else {
SetWindowVisibility(window, false, kTeleportAnimationTime);
}
delegate_->OnWindowOwnerEntryChanged(window, account_id, minimized,
teleported);
return true;
}
void MultiUserWindowManagerImpl::SetWindowVisibility(
aura::Window* window,
bool visible,
base::TimeDelta animation_time) {
if (desks_util::BelongsToActiveDesk(window) && window->IsVisible() == visible)
return;
if (!visible && window->GetRootWindow()) {
if (HasSystemModalTransientChildWindow(window)) {
AccountId account_id = GetUserPresentingWindow(window);
if (!account_id.is_valid()) {
aura::Window* owning_window = GetOwningWindowInTransientChain(window);
DCHECK(owning_window);
account_id = GetUserPresentingWindow(owning_window);
DCHECK(account_id.is_valid());
}
Shell::Get()->session_controller()->SwitchActiveUser(account_id);
return;
}
}
base::AutoReset<bool> suppressor(&suppress_visibility_changes_, true);
if (visible)
ShowWithTransientChildrenRecursive(window, animation_time);
else
SetWindowVisible(window, false, animation_time);
}
void MultiUserWindowManagerImpl::ShowWithTransientChildrenRecursive(
aura::Window* window,
base::TimeDelta animation_time) {
for (aura::Window* transient_child : ::wm::GetTransientChildren(window))
ShowWithTransientChildrenRecursive(transient_child, animation_time);
TransientWindowToVisibility::iterator it =
transient_window_to_visibility_.find(window);
if (it == transient_window_to_visibility_.end() || it->second)
SetWindowVisible(window, true, animation_time);
}
aura::Window* MultiUserWindowManagerImpl::GetOwningWindowInTransientChain(
aura::Window* window) const {
if (!GetWindowOwner(window).empty())
return nullptr;
aura::Window* parent = ::wm::GetTransientParent(window);
while (parent) {
if (!GetWindowOwner(parent).empty())
return parent;
parent = ::wm::GetTransientParent(parent);
}
return nullptr;
}
void MultiUserWindowManagerImpl::AddTransientOwnerRecursive(
aura::Window* window,
aura::Window* owned_parent) {
for (aura::Window* transient_child : ::wm::GetTransientChildren(window))
AddTransientOwnerRecursive(transient_child, owned_parent);
if (window == owned_parent)
return;
DCHECK(transient_window_to_visibility_.find(window) ==
transient_window_to_visibility_.end());
transient_window_to_visibility_[window] = window->IsVisible();
window->AddObserver(this);
::wm::TransientWindowManager::GetOrCreate(window)->AddObserver(this);
if (!IsWindowOnDesktopOfUser(owned_parent, current_account_id_))
SetWindowVisibility(window, false, kAnimationTime);
}
void MultiUserWindowManagerImpl::RemoveTransientOwnerRecursive(
aura::Window* window) {
for (aura::Window* transient_child : ::wm::GetTransientChildren(window))
RemoveTransientOwnerRecursive(transient_child);
TransientWindowToVisibility::iterator visibility_item =
transient_window_to_visibility_.find(window);
DCHECK(visibility_item != transient_window_to_visibility_.end());
window->RemoveObserver(this);
::wm::TransientWindowManager::GetOrCreate(window)->RemoveObserver(this);
bool unowned_view_state = visibility_item->second;
transient_window_to_visibility_.erase(visibility_item);
if (unowned_view_state && !window->IsVisible() &&
desks_util::BelongsToActiveDesk(window)) {
base::AutoReset<bool> suppressor(&suppress_visibility_changes_, true);
window->Show();
}
}
void MultiUserWindowManagerImpl::SetWindowVisible(
aura::Window* window,
bool visible,
base::TimeDelta animation_time) {
if (visible)
Shell::Get()->tablet_mode_controller()->AddWindow(window);
AnimationSetter animation_setter(window,
GetAdjustedAnimationTime(animation_time));
if (visible)
window->Show();
else
window->Hide();
}
base::TimeDelta MultiUserWindowManagerImpl::GetAdjustedAnimationTime(
base::TimeDelta default_time) const {
return animation_speed_ == ANIMATION_SPEED_NORMAL
? default_time
: (animation_speed_ == ANIMATION_SPEED_FAST
? base::Milliseconds(10)
: base::TimeDelta());
}
std::unique_ptr<MultiUserWindowManager> MultiUserWindowManager::Create(
MultiUserWindowManagerDelegate* delegate,
const AccountId& account_id) {
return std::make_unique<MultiUserWindowManagerImpl>(delegate, account_id);
}
}