Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / wm / mru_window_tracker.cc
blob00e1773313884235def30939470aa317b40409b8
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 #include "ash/wm/mru_window_tracker.h"
7 #include <algorithm>
9 #include "ash/session/session_state_delegate.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/switchable_windows.h"
13 #include "ash/wm/ash_focus_rules.h"
14 #include "ash/wm/window_state.h"
15 #include "ash/wm/window_util.h"
16 #include "ash/wm/workspace_controller.h"
17 #include "base/bind.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_handler.h"
21 #include "ui/wm/public/activation_client.h"
23 namespace ash {
25 namespace {
27 typedef base::Callback<bool(aura::Window*)> CanActivateWindowPredicate;
29 // Adds the windows that can be cycled through for the specified window id to
30 // |windows|.
31 void AddTrackedWindows(aura::Window* root,
32 int container_id,
33 MruWindowTracker::WindowList* windows) {
34 aura::Window* container = Shell::GetContainer(root, container_id);
35 const MruWindowTracker::WindowList& children(container->children());
36 windows->insert(windows->end(), children.begin(), children.end());
39 // Returns whether |w1| should be considered less recently used than |w2|. This
40 // is used for a stable sort to move minimized windows to the LRU end of the
41 // list.
42 bool CompareWindowState(aura::Window* w1, aura::Window* w2) {
43 return ash::wm::IsWindowMinimized(w1) && !ash::wm::IsWindowMinimized(w2);
46 // Returns a list of windows ordered by their stacking order.
47 // If |mru_windows| is passed, these windows are moved to the front of the list.
48 // It uses the given |should_include_window_predicate| to determine whether to
49 // include a window in the returned list or not.
50 MruWindowTracker::WindowList BuildWindowListInternal(
51 const std::list<aura::Window*>* mru_windows,
52 const CanActivateWindowPredicate& should_include_window_predicate) {
53 MruWindowTracker::WindowList windows;
54 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
56 aura::Window* active_root = Shell::GetTargetRootWindow();
57 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
58 iter != root_windows.end(); ++iter) {
59 if (*iter == active_root)
60 continue;
61 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i)
62 AddTrackedWindows(*iter, kSwitchableWindowContainerIds[i], &windows);
65 // Add windows in the active root windows last so that the topmost window
66 // in the active root window becomes the front of the list.
67 for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i)
68 AddTrackedWindows(active_root, kSwitchableWindowContainerIds[i], &windows);
70 // Removes unfocusable windows.
71 std::vector<aura::Window*>::iterator itr = windows.begin();
72 while (itr != windows.end()) {
73 if (!should_include_window_predicate.Run(*itr))
74 itr = windows.erase(itr);
75 else
76 ++itr;
79 // Put the windows in the mru_windows list at the head, if it's available.
80 if (mru_windows) {
81 // Iterate through the list backwards, so that we can move each window to
82 // the front of the windows list as we find them.
83 for (std::list<aura::Window*>::const_reverse_iterator ix =
84 mru_windows->rbegin();
85 ix != mru_windows->rend(); ++ix) {
86 // Exclude windows in non-switchable containers and those which cannot
87 // be activated.
88 if (!IsSwitchableContainer((*ix)->parent()) ||
89 !should_include_window_predicate.Run(*ix)) {
90 continue;
93 MruWindowTracker::WindowList::iterator window =
94 std::find(windows.begin(), windows.end(), *ix);
95 if (window != windows.end()) {
96 windows.erase(window);
97 windows.push_back(*ix);
102 // Move minimized windows to the beginning (LRU end) of the list.
103 std::stable_sort(windows.begin(), windows.end(), CompareWindowState);
105 // Window cycling expects the topmost window at the front of the list.
106 std::reverse(windows.begin(), windows.end());
108 return windows;
111 } // namespace
113 //////////////////////////////////////////////////////////////////////////////
114 // MruWindowTracker, public:
116 MruWindowTracker::MruWindowTracker(
117 aura::client::ActivationClient* activation_client,
118 ash::wm::AshFocusRules* focus_rules)
119 : activation_client_(activation_client),
120 focus_rules_(focus_rules),
121 ignore_window_activations_(false) {
122 activation_client_->AddObserver(this);
125 MruWindowTracker::~MruWindowTracker() {
126 for (std::list<aura::Window*>::iterator iter = mru_windows_.begin();
127 iter != mru_windows_.end(); ++iter) {
128 (*iter)->RemoveObserver(this);
131 activation_client_->RemoveObserver(this);
134 MruWindowTracker::WindowList MruWindowTracker::BuildMruWindowList() const {
135 return BuildWindowListInternal(&mru_windows_,
136 base::Bind(&ash::wm::CanActivateWindow));
139 MruWindowTracker::WindowList
140 MruWindowTracker::BuildWindowListIgnoreModal() const {
141 return BuildWindowListInternal(
142 NULL,
143 base::Bind(&MruWindowTracker::IsWindowConsideredActivateable,
144 base::Unretained(this)));
147 void MruWindowTracker::SetIgnoreActivations(bool ignore) {
148 ignore_window_activations_ = ignore;
150 // If no longer ignoring window activations, move currently active window
151 // to front.
152 if (!ignore)
153 SetActiveWindow(wm::GetActiveWindow());
156 //////////////////////////////////////////////////////////////////////////////
157 // MruWindowTracker, private:
159 void MruWindowTracker::SetActiveWindow(aura::Window* active_window) {
160 if (!active_window)
161 return;
163 std::list<aura::Window*>::iterator iter =
164 std::find(mru_windows_.begin(), mru_windows_.end(), active_window);
165 // Observe all newly tracked windows.
166 if (iter == mru_windows_.end())
167 active_window->AddObserver(this);
168 else
169 mru_windows_.erase(iter);
170 // TODO(flackr): Remove this check if this doesn't fire for a while. This
171 // should verify that all tracked windows start with a layer, see
172 // http://crbug.com/291354.
173 CHECK(active_window->layer());
174 mru_windows_.push_front(active_window);
177 void MruWindowTracker::OnWindowActivated(
178 aura::client::ActivationChangeObserver::ActivationReason reason,
179 aura::Window* gained_active,
180 aura::Window* lost_active) {
181 if (!ignore_window_activations_)
182 SetActiveWindow(gained_active);
185 void MruWindowTracker::OnWindowDestroyed(aura::Window* window) {
186 // It's possible for OnWindowActivated() to be called after
187 // OnWindowDestroying(). This means we need to override OnWindowDestroyed()
188 // else we may end up with a deleted window in |mru_windows_|.
189 mru_windows_.remove(window);
190 window->RemoveObserver(this);
193 bool MruWindowTracker::IsWindowConsideredActivateable(
194 aura::Window* window) const {
195 return focus_rules_->IsWindowConsideredActivatable(window);
198 } // namespace ash