summaryrefslogtreecommitdiff
path: root/chromium/ui/base/window_tracker_template.h
blob: e91bdac8077b29cd53cc9aca13dbf753b25e3201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2016 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.

#ifndef UI_BASE_WINDOW_TRACKER_TEMPLATE_H_
#define UI_BASE_WINDOW_TRACKER_TEMPLATE_H_

#include <vector>

#include "base/macros.h"
#include "base/stl_util.h"

namespace ui {

// This class is used to track an ordered list of objects that support an
// observer interface with the function OnWindowDestroying(). When the object
// is destroyed it is removed from the ordered list of objects.
// Examples of T include aura::Window and its corresponding
// aura::WindowObserver interface.
template <class T, class TObserver>
class WindowTrackerTemplate : public TObserver {
 public:
  // A vector<> is used for tracking the windows (instead of a set<>) because
  // the user may want to know about the order of the windows that have been
  // added.
  using WindowList = std::vector<T*>;

  explicit WindowTrackerTemplate(const WindowList& windows) {
    for (T* window : windows)
      Add(window);
  }
  WindowTrackerTemplate() {}
  ~WindowTrackerTemplate() override { RemoveAll(); }

  // Returns the set of windows being observed.
  const WindowList& windows() const { return windows_; }

  // Adds |window| to the set of Windows being tracked.
  void Add(T* window) {
    if (base::ContainsValue(windows_, window))
      return;

    window->AddObserver(this);
    windows_.push_back(window);
  }

  void RemoveAll() {
    for (T* window : windows_)
      window->RemoveObserver(this);
    windows_.clear();
  }

  // Removes |window| from the set of windows being tracked.
  void Remove(T* window) {
    auto iter = std::find(windows_.begin(), windows_.end(), window);
    if (iter != windows_.end()) {
      window->RemoveObserver(this);
      windows_.erase(iter);
    }
  }

  T* Pop() {
    DCHECK(!windows_.empty());
    T* result = windows_[0];
    Remove(result);
    return result;
  }

  // Returns true if |window| was previously added and has not been removed or
  // deleted.
  bool Contains(T* window) const {
    return base::ContainsValue(windows_, window);
  }

  // Observer overrides:
  void OnWindowDestroying(T* window) override {
    DCHECK(Contains(window));
    Remove(window);
  }

 private:
  WindowList windows_;

  DISALLOW_COPY_AND_ASSIGN(WindowTrackerTemplate);
};

}  // namespace ui

#endif  // UI_BASE_WINDOW_TRACKER_TEMPLATE_H_