blob: 0eca4834bc24b3330e8708a22ddf84dc8a7cc50c [file] [log] [blame]
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <set>
#include "ash/shell_observer.h"
#include "base/macros.h"
#include "base/scoped_observer.h"
#include "ui/aura/window_observer.h"
#include "ui/wm/public/activation_change_observer.h"
namespace ash {
class ShelfModel;
// ShelfWindowWatcher manages ShelfItems for dialogs in the default container
// with valid ShelfItemType and ShelfID window properties (ie. task manager).
// ShelfWindowWatcher also tracks the active shelf item via window activation.
// Some windows are experimentally given default properties in MultiProcessMash.
// TODO(|887156): Resolve, KSV etc. tracking approach for Mash.
class ShelfWindowWatcher : public ::wm::ActivationChangeObserver,
public ShellObserver {
explicit ShelfWindowWatcher(ShelfModel* model);
~ShelfWindowWatcher() override;
static const char kDefaultShelfIdPrefix[];
// Observes for windows being added to a root window's default container.
class ContainerWindowObserver : public aura::WindowObserver {
explicit ContainerWindowObserver(ShelfWindowWatcher* window_watcher);
~ContainerWindowObserver() override;
// aura::WindowObserver:
void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override;
void OnWindowDestroying(aura::Window* window) override;
ShelfWindowWatcher* window_watcher_;
// Observes individual user windows to detect when they are closed or when
// their shelf item properties have changed.
class UserWindowObserver : public aura::WindowObserver {
explicit UserWindowObserver(ShelfWindowWatcher* window_watcher);
~UserWindowObserver() override;
// aura::WindowObserver:
void OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) override;
void OnWindowDestroying(aura::Window* window) override;
void OnWindowVisibilityChanged(aura::Window* window, bool visible) override;
void OnWindowTitleChanged(aura::Window* window) override;
ShelfWindowWatcher* window_watcher_;
// Creates a ShelfItem for |window|.
void AddShelfItem(aura::Window* window);
// Removes a ShelfItem for |window|.
void RemoveShelfItem(aura::Window* window);
// Cleans up observers on |container|.
void OnContainerWindowDestroying(aura::Window* container);
// Adds a shelf item for new windows added to the default container that have
// valid ShelfItemType and ShelfID property values.
void OnUserWindowAdded(aura::Window* window);
// Adds, updates or removes the shelf item based on a property change.
void OnUserWindowPropertyChanged(aura::Window* window);
// Removes the shelf item when a window closes.
void OnUserWindowDestroying(aura::Window* window);
// wm::ActivationChangeObserver:
void OnWindowActivated(ActivationReason reason,
aura::Window* gained_active,
aura::Window* lost_active) override;
// ShellObserver:
void OnRootWindowAdded(aura::Window* root_window) override;
ShelfModel* model_;
ContainerWindowObserver container_window_observer_;
UserWindowObserver user_window_observer_;
ScopedObserver<aura::Window, ContainerWindowObserver>
ScopedObserver<aura::Window, UserWindowObserver> observed_user_windows_;
// The set of windows with shelf items managed by this ShelfWindowWatcher.
std::set<aura::Window*> user_windows_with_items_;
} // namespace ash