base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / wm / core / default_activation_client.cc
blob20c0d79b7dc7fd121c7d21c236ce38e34581bc1b
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 "ui/wm/core/default_activation_client.h"
7 #include "ui/aura/window.h"
8 #include "ui/wm/public/activation_change_observer.h"
9 #include "ui/wm/public/activation_delegate.h"
11 namespace wm {
13 // Takes care of observing root window destruction & destroying the client.
14 class DefaultActivationClient::Deleter : public aura::WindowObserver {
15 public:
16 Deleter(DefaultActivationClient* client, aura::Window* root_window)
17 : client_(client),
18 root_window_(root_window) {
19 root_window_->AddObserver(this);
22 private:
23 ~Deleter() override {}
25 // Overridden from WindowObserver:
26 void OnWindowDestroyed(aura::Window* window) override {
27 DCHECK_EQ(window, root_window_);
28 root_window_->RemoveObserver(this);
29 delete client_;
30 delete this;
33 DefaultActivationClient* client_;
34 aura::Window* root_window_;
36 DISALLOW_COPY_AND_ASSIGN(Deleter);
39 ////////////////////////////////////////////////////////////////////////////////
40 // DefaultActivationClient, public:
42 DefaultActivationClient::DefaultActivationClient(aura::Window* root_window)
43 : last_active_(NULL) {
44 aura::client::SetActivationClient(root_window, this);
45 new Deleter(this, root_window);
48 ////////////////////////////////////////////////////////////////////////////////
49 // DefaultActivationClient, client::ActivationClient implementation:
51 void DefaultActivationClient::AddObserver(
52 aura::client::ActivationChangeObserver* observer) {
53 observers_.AddObserver(observer);
56 void DefaultActivationClient::RemoveObserver(
57 aura::client::ActivationChangeObserver* observer) {
58 observers_.RemoveObserver(observer);
61 void DefaultActivationClient::ActivateWindow(aura::Window* window) {
62 aura::Window* last_active = GetActiveWindow();
63 if (last_active == window)
64 return;
66 last_active_ = last_active;
67 RemoveActiveWindow(window);
68 active_windows_.push_back(window);
69 window->parent()->StackChildAtTop(window);
70 window->AddObserver(this);
72 FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver,
73 observers_,
74 OnWindowActivated(window, last_active));
76 aura::client::ActivationChangeObserver* observer =
77 aura::client::GetActivationChangeObserver(last_active);
78 if (observer)
79 observer->OnWindowActivated(window, last_active);
80 observer = aura::client::GetActivationChangeObserver(window);
81 if (observer)
82 observer->OnWindowActivated(window, last_active);
85 void DefaultActivationClient::DeactivateWindow(aura::Window* window) {
86 aura::client::ActivationChangeObserver* observer =
87 aura::client::GetActivationChangeObserver(window);
88 if (observer)
89 observer->OnWindowActivated(NULL, window);
90 if (last_active_)
91 ActivateWindow(last_active_);
94 aura::Window* DefaultActivationClient::GetActiveWindow() {
95 if (active_windows_.empty())
96 return NULL;
97 return active_windows_.back();
100 aura::Window* DefaultActivationClient::GetActivatableWindow(
101 aura::Window* window) {
102 return NULL;
105 aura::Window* DefaultActivationClient::GetToplevelWindow(aura::Window* window) {
106 return NULL;
109 bool DefaultActivationClient::CanActivateWindow(aura::Window* window) const {
110 return true;
113 ////////////////////////////////////////////////////////////////////////////////
114 // DefaultActivationClient, aura::WindowObserver implementation:
116 void DefaultActivationClient::OnWindowDestroyed(aura::Window* window) {
117 if (window == last_active_)
118 last_active_ = NULL;
120 if (window == GetActiveWindow()) {
121 active_windows_.pop_back();
122 aura::Window* next_active = GetActiveWindow();
123 if (next_active && aura::client::GetActivationChangeObserver(next_active)) {
124 aura::client::GetActivationChangeObserver(next_active)->OnWindowActivated(
125 next_active, NULL);
127 return;
130 RemoveActiveWindow(window);
133 ////////////////////////////////////////////////////////////////////////////////
134 // DefaultActivationClient, private:
136 DefaultActivationClient::~DefaultActivationClient() {
137 for (unsigned int i = 0; i < active_windows_.size(); ++i) {
138 active_windows_[i]->RemoveObserver(this);
142 void DefaultActivationClient::RemoveActiveWindow(aura::Window* window) {
143 for (unsigned int i = 0; i < active_windows_.size(); ++i) {
144 if (active_windows_[i] == window) {
145 active_windows_.erase(active_windows_.begin() + i);
146 window->RemoveObserver(this);
147 return;
152 } // namespace wm