Commit 448a2490 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

chromeos: use unique_ptr in MultiUserWindowManager

BUG=756085,875111
TEST=covered by test

Change-Id: Idc65b7ca5b340401855694f1f123402612b73003
Reviewed-on: https://chromium-review.googlesource.com/c/1341021
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarAhmed Fakhry <afakhry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609372}
parent 76b18224
...@@ -164,13 +164,12 @@ MultiUserWindowManager::~MultiUserWindowManager() { ...@@ -164,13 +164,12 @@ MultiUserWindowManager::~MultiUserWindowManager() {
animation_->CancelAnimation(); animation_->CancelAnimation();
// Remove all window observers. // Remove all window observers.
WindowToEntryMap::iterator window = window_to_entry_.begin(); while (!window_to_entry_.empty()) {
while (window != window_to_entry_.end()) {
// Explicitly remove this from window observer list since OnWindowDestroyed // Explicitly remove this from window observer list since OnWindowDestroyed
// no longer does that. // no longer does that.
window->first->RemoveObserver(this); aura::Window* window = window_to_entry_.begin()->first;
OnWindowDestroyed(window->first); window->RemoveObserver(this);
window = window_to_entry_.begin(); OnWindowDestroyed(window);
} }
Shell::Get()->session_controller()->RemoveObserver(this); Shell::Get()->session_controller()->RemoveObserver(this);
...@@ -193,10 +192,13 @@ void MultiUserWindowManager::SetWindowOwner(aura::Window* window, ...@@ -193,10 +192,13 @@ void MultiUserWindowManager::SetWindowOwner(aura::Window* window,
if (GetWindowOwner(window) == account_id) if (GetWindowOwner(window) == account_id)
return; return;
DCHECK(GetWindowOwner(window).empty()); DCHECK(GetWindowOwner(window).empty());
window_to_entry_[window] = new WindowEntry(account_id); 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);
// Remember the initial visibility of the window. // Remember the initial visibility of the window.
window_to_entry_[window]->set_show(window->IsVisible()); window_entry->set_show(window->IsVisible());
// Add observers to track state changes. // Add observers to track state changes.
window->AddObserver(this); window->AddObserver(this);
...@@ -205,7 +207,7 @@ void MultiUserWindowManager::SetWindowOwner(aura::Window* window, ...@@ -205,7 +207,7 @@ void MultiUserWindowManager::SetWindowOwner(aura::Window* window,
// Check if this window was created due to a user interaction. If it was, // Check if this window was created due to a user interaction. If it was,
// transfer it to the current user. // transfer it to the current user.
if (show_for_current_user) if (show_for_current_user)
window_to_entry_[window]->set_show_for_user(current_account_id_); window_entry->set_show_for_user(current_account_id_);
// Add all transient children to our set of windows. Note that the function // Add all transient children to our set of windows. Note that the function
// will add the children but not the owner to the transient children map. // will add the children but not the owner to the transient children map.
...@@ -237,9 +239,8 @@ void MultiUserWindowManager::ShowWindowForUser(aura::Window* window, ...@@ -237,9 +239,8 @@ void MultiUserWindowManager::ShowWindowForUser(aura::Window* window,
} }
bool MultiUserWindowManager::AreWindowsSharedAmongUsers() const { bool MultiUserWindowManager::AreWindowsSharedAmongUsers() const {
WindowToEntryMap::const_iterator it = window_to_entry_.begin(); for (auto& window_pair : window_to_entry_) {
for (; it != window_to_entry_.end(); ++it) { if (window_pair.second->owner() != window_pair.second->show_for_user())
if (it->second->owner() != it->second->show_for_user())
return true; return true;
} }
return false; return false;
...@@ -300,8 +301,6 @@ void MultiUserWindowManager::OnWindowDestroyed(aura::Window* window) { ...@@ -300,8 +301,6 @@ void MultiUserWindowManager::OnWindowDestroyed(aura::Window* window) {
return; return;
} }
::wm::TransientWindowManager::GetOrCreate(window)->RemoveObserver(this); ::wm::TransientWindowManager::GetOrCreate(window)->RemoveObserver(this);
// Remove the window from the owners list.
delete window_to_entry_[window];
window_to_entry_.erase(window); window_to_entry_.erase(window);
} }
...@@ -371,7 +370,7 @@ void MultiUserWindowManager::OnTransientChildRemoved( ...@@ -371,7 +370,7 @@ void MultiUserWindowManager::OnTransientChildRemoved(
} }
void MultiUserWindowManager::OnTabletModeStarted() { void MultiUserWindowManager::OnTabletModeStarted() {
for (auto entry : window_to_entry_) for (auto& entry : window_to_entry_)
Shell::Get()->tablet_mode_controller()->AddWindow(entry.first); Shell::Get()->tablet_mode_controller()->AddWindow(entry.first);
} }
...@@ -403,15 +402,15 @@ bool MultiUserWindowManager::ShowWindowForUserIntern( ...@@ -403,15 +402,15 @@ bool MultiUserWindowManager::ShowWindowForUserIntern(
if (account_id != owner && minimized) if (account_id != owner && minimized)
return false; return false;
WindowToEntryMap::iterator it = window_to_entry_.find(window); WindowEntry* window_entry = window_to_entry_[window].get();
it->second->set_show_for_user(account_id); window_entry->set_show_for_user(account_id);
const bool teleported = !IsWindowOnDesktopOfUser(window, owner); const bool teleported = !IsWindowOnDesktopOfUser(window, owner);
// Show the window if the added user is the current one. // Show the window if the added user is the current one.
if (account_id == current_account_id_) { if (account_id == current_account_id_) {
// Only show the window if it should be shown according to its state. // Only show the window if it should be shown according to its state.
if (it->second->show()) if (window_entry->show())
SetWindowVisibility(window, true, kTeleportAnimationTime); SetWindowVisibility(window, true, kTeleportAnimationTime);
} else { } else {
SetWindowVisibility(window, false, kTeleportAnimationTime); SetWindowVisibility(window, false, kTeleportAnimationTime);
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <set>
#include <string>
#include "ash/ash_export.h" #include "ash/ash_export.h"
#include "ash/session/session_observer.h" #include "ash/session/session_observer.h"
...@@ -140,8 +138,8 @@ class ASH_EXPORT MultiUserWindowManager : public SessionObserver, ...@@ -140,8 +138,8 @@ class ASH_EXPORT MultiUserWindowManager : public SessionObserver,
DISALLOW_COPY_AND_ASSIGN(WindowEntry); DISALLOW_COPY_AND_ASSIGN(WindowEntry);
}; };
// TODO: make map to std::unique_ptr<WindowEntry>. using WindowToEntryMap =
using WindowToEntryMap = std::map<aura::Window*, WindowEntry*>; std::map<aura::Window*, std::unique_ptr<WindowEntry>>;
const AccountId& GetWindowOwner(aura::Window* window) const; const AccountId& GetWindowOwner(aura::Window* window) const;
......
...@@ -150,13 +150,12 @@ MultiUserWindowManagerChromeOS::~MultiUserWindowManagerChromeOS() { ...@@ -150,13 +150,12 @@ MultiUserWindowManagerChromeOS::~MultiUserWindowManagerChromeOS() {
ash_multi_user_window_manager_.reset(); ash_multi_user_window_manager_.reset();
// Remove all window observers. // Remove all window observers.
WindowToEntryMap::iterator window = window_to_entry_.begin(); while (!window_to_entry_.empty()) {
while (window != window_to_entry_.end()) {
// Explicitly remove this from window observer list since OnWindowDestroyed // Explicitly remove this from window observer list since OnWindowDestroyed
// no longer does that. // no longer does that.
window->first->RemoveObserver(this); aura::Window* window = window_to_entry_.begin()->first;
OnWindowDestroyed(window->first); window->RemoveObserver(this);
window = window_to_entry_.begin(); OnWindowDestroyed(window);
} }
// Remove all app observers. // Remove all app observers.
...@@ -207,7 +206,10 @@ void MultiUserWindowManagerChromeOS::SetWindowOwner( ...@@ -207,7 +206,10 @@ void MultiUserWindowManagerChromeOS::SetWindowOwner(
return; return;
DCHECK(GetWindowOwner(window).empty()); DCHECK(GetWindowOwner(window).empty());
window_to_entry_[window] = new WindowEntry(account_id); 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);
// Check if this window was created due to a user interaction. If it was, // Check if this window was created due to a user interaction. If it was,
// transfer it to the current user. // transfer it to the current user.
...@@ -228,7 +230,7 @@ void MultiUserWindowManagerChromeOS::SetWindowOwner( ...@@ -228,7 +230,7 @@ void MultiUserWindowManagerChromeOS::SetWindowOwner(
window->AddObserver(this); window->AddObserver(this);
if (show_for_current_user) if (show_for_current_user)
window_to_entry_[window]->set_show_for_user(current_account_id_); window_entry->set_show_for_user(current_account_id_);
// Notify entry adding. // Notify entry adding.
for (Observer& observer : observers_) for (Observer& observer : observers_)
...@@ -259,9 +261,8 @@ void MultiUserWindowManagerChromeOS::ShowWindowForUser( ...@@ -259,9 +261,8 @@ void MultiUserWindowManagerChromeOS::ShowWindowForUser(
} }
bool MultiUserWindowManagerChromeOS::AreWindowsSharedAmongUsers() const { bool MultiUserWindowManagerChromeOS::AreWindowsSharedAmongUsers() const {
WindowToEntryMap::const_iterator it = window_to_entry_.begin(); for (auto& window_pair : window_to_entry_) {
for (; it != window_to_entry_.end(); ++it) { if (window_pair.second->owner() != window_pair.second->show_for_user())
if (it->second->owner() != it->second->show_for_user())
return true; return true;
} }
return false; return false;
...@@ -269,10 +270,9 @@ bool MultiUserWindowManagerChromeOS::AreWindowsSharedAmongUsers() const { ...@@ -269,10 +270,9 @@ bool MultiUserWindowManagerChromeOS::AreWindowsSharedAmongUsers() const {
void MultiUserWindowManagerChromeOS::GetOwnersOfVisibleWindows( void MultiUserWindowManagerChromeOS::GetOwnersOfVisibleWindows(
std::set<AccountId>* account_ids) const { std::set<AccountId>* account_ids) const {
for (WindowToEntryMap::const_iterator it = window_to_entry_.begin(); for (auto& window_pair : window_to_entry_) {
it != window_to_entry_.end(); ++it) { if (window_pair.first->IsVisible())
if (it->first->IsVisible()) account_ids->insert(window_pair.second->owner());
account_ids->insert(it->second->owner());
} }
} }
...@@ -333,8 +333,6 @@ void MultiUserWindowManagerChromeOS::RemoveObserver(Observer* observer) { ...@@ -333,8 +333,6 @@ void MultiUserWindowManagerChromeOS::RemoveObserver(Observer* observer) {
} }
void MultiUserWindowManagerChromeOS::OnWindowDestroyed(aura::Window* window) { void MultiUserWindowManagerChromeOS::OnWindowDestroyed(aura::Window* window) {
// Remove the window from the owners list.
delete window_to_entry_[window];
window_to_entry_.erase(window); window_to_entry_.erase(window);
} }
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <string>
#include "ash/multi_user/multi_user_window_manager_delegate.h" #include "ash/multi_user/multi_user_window_manager_delegate.h"
#include "ash/public/interfaces/multi_user_window_manager.mojom.h" #include "ash/public/interfaces/multi_user_window_manager.mojom.h"
...@@ -92,8 +91,8 @@ class MultiUserWindowManagerChromeOS ...@@ -92,8 +91,8 @@ class MultiUserWindowManagerChromeOS
// Returns the current user for unit tests. // Returns the current user for unit tests.
const AccountId& GetCurrentUserForTest() const; const AccountId& GetCurrentUserForTest() const;
protected: private:
friend class UserSwitchAnimatorChromeOS; friend class ash::MultiUserWindowManagerChromeOSTest;
class WindowEntry { class WindowEntry {
public: public:
...@@ -123,15 +122,10 @@ class MultiUserWindowManagerChromeOS ...@@ -123,15 +122,10 @@ class MultiUserWindowManagerChromeOS
DISALLOW_COPY_AND_ASSIGN(WindowEntry); DISALLOW_COPY_AND_ASSIGN(WindowEntry);
}; };
// TODO(sky): make this map to unique_ptr<WindowEntry>. using AccountIdToAppWindowObserver = std::map<AccountId, AppObserver*>;
using WindowToEntryMap = std::map<aura::Window*, WindowEntry*>;
const WindowToEntryMap& window_to_entry() { return window_to_entry_; }
private:
friend class ash::MultiUserWindowManagerChromeOSTest;
typedef std::map<AccountId, AppObserver*> AccountIdToAppWindowObserver; using WindowToEntryMap =
std::map<aura::Window*, std::unique_ptr<WindowEntry>>;
// Add a browser window to the system so that the owner can be remembered. // Add a browser window to the system so that the owner can be remembered.
void AddBrowserWindow(Browser* browser); void AddBrowserWindow(Browser* browser);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment