window_list.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_BROWSER_WINDOW_LIST_H_
  5. #define ATOM_BROWSER_WINDOW_LIST_H_
  6. #include <vector>
  7. #include "base/lazy_instance.h"
  8. #include "base/macros.h"
  9. #include "base/observer_list.h"
  10. namespace atom {
  11. class NativeWindow;
  12. class WindowListObserver;
  13. class WindowList {
  14. public:
  15. typedef std::vector<NativeWindow*> WindowVector;
  16. typedef WindowVector::iterator iterator;
  17. typedef WindowVector::const_iterator const_iterator;
  18. // Windows are added to the list before they have constructed windows,
  19. // so the |window()| member function may return NULL.
  20. const_iterator begin() const { return windows_.begin(); }
  21. const_iterator end() const { return windows_.end(); }
  22. iterator begin() { return windows_.begin(); }
  23. iterator end() { return windows_.end(); }
  24. bool empty() const { return windows_.empty(); }
  25. size_t size() const { return windows_.size(); }
  26. NativeWindow* get(size_t index) const { return windows_[index]; }
  27. static WindowList* GetInstance();
  28. // Adds or removes |window| from the list it is associated with.
  29. static void AddWindow(NativeWindow* window);
  30. static void RemoveWindow(NativeWindow* window);
  31. // Called by window when a close is cancelled by beforeunload handler.
  32. static void WindowCloseCancelled(NativeWindow* window);
  33. // Adds and removes |observer| from the observer list.
  34. static void AddObserver(WindowListObserver* observer);
  35. static void RemoveObserver(WindowListObserver* observer);
  36. // Closes all windows.
  37. static void CloseAllWindows();
  38. private:
  39. WindowList();
  40. ~WindowList();
  41. // A vector of the windows in this list, in the order they were added.
  42. WindowVector windows_;
  43. // A list of observers which will be notified of every window addition and
  44. // removal across all WindowLists.
  45. static base::LazyInstance<base::ObserverList<WindowListObserver>>::Leaky
  46. observers_;
  47. static WindowList* instance_;
  48. DISALLOW_COPY_AND_ASSIGN(WindowList);
  49. };
  50. } // namespace atom
  51. #endif // ATOM_BROWSER_WINDOW_LIST_H_