window_user_data.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2016 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.
  4. #ifndef ASH_WINDOW_USER_DATA_H_
  5. #define ASH_WINDOW_USER_DATA_H_
  6. #include <map>
  7. #include <memory>
  8. #include <utility>
  9. #include "ui/aura/window.h"
  10. #include "ui/aura/window_observer.h"
  11. namespace ash {
  12. // WindowUserData provides a way to associate an object with a Window and have
  13. // that object destroyed when the window is destroyed, or when WindowUserData
  14. // is destroyed (from aura::WindowObserver::OnWindowDestroying()).
  15. //
  16. // NOTE: WindowUserData does not make use of the Set/GetProperty API offered
  17. // on aura::Window. This is done to avoid collisions in the case of multiple
  18. // WindowUserDatas operating on the same Window.
  19. template <typename UserData>
  20. class WindowUserData : public aura::WindowObserver {
  21. public:
  22. WindowUserData() {}
  23. WindowUserData(const WindowUserData&) = delete;
  24. WindowUserData& operator=(const WindowUserData&) = delete;
  25. ~WindowUserData() override { clear(); }
  26. void clear() {
  27. // Take care to destroy the data after removing from the map.
  28. while (!window_to_data_.empty()) {
  29. auto iter = window_to_data_.begin();
  30. iter->first->RemoveObserver(this);
  31. std::unique_ptr<UserData> user_data = std::move(iter->second);
  32. window_to_data_.erase(iter);
  33. }
  34. }
  35. // Sets the data associated with window. This destroys any existing data.
  36. // |data| may be null.
  37. void Set(aura::Window* window, std::unique_ptr<UserData> data) {
  38. if (!data) {
  39. if (window_to_data_.erase(window))
  40. window->RemoveObserver(this);
  41. return;
  42. }
  43. if (window_to_data_.count(window) == 0u)
  44. window->AddObserver(this);
  45. window_to_data_[window] = std::move(data);
  46. }
  47. // Returns the data associated with the window, or null if none set. The
  48. // returned object is owned by WindowUserData.
  49. UserData* Get(aura::Window* window) {
  50. auto it = window_to_data_.find(window);
  51. return it == window_to_data_.end() ? nullptr : it->second.get();
  52. }
  53. // Returns the set of windows with data associated with them.
  54. std::set<aura::Window*> GetWindows() {
  55. std::set<aura::Window*> windows;
  56. for (auto& pair : window_to_data_)
  57. windows.insert(pair.first);
  58. return windows;
  59. }
  60. private:
  61. // aura::WindowObserver:
  62. void OnWindowDestroying(aura::Window* window) override {
  63. window->RemoveObserver(this);
  64. window_to_data_.erase(window);
  65. }
  66. std::map<aura::Window*, std::unique_ptr<UserData>> window_to_data_;
  67. };
  68. } // namespace ash
  69. #endif // ASH_WINDOW_USER_DATA_H_