Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: ash/common/window_user_data.h

Issue 2735983006: Renames WmWindowUserData and converts to using aura (Closed)
Patch Set: ownership comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef ASH_COMMON_WM_WINDOW_USER_DATA_H_ 5 #ifndef ASH_COMMON_WINDOW_USER_DATA_H_
6 #define ASH_COMMON_WM_WINDOW_USER_DATA_H_ 6 #define ASH_COMMON_WINDOW_USER_DATA_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "ash/common/wm_window.h"
13 #include "base/macros.h" 12 #include "base/macros.h"
14 #include "ui/aura/window.h" 13 #include "ui/aura/window.h"
15 #include "ui/aura/window_observer.h" 14 #include "ui/aura/window_observer.h"
16 15
17 namespace ash { 16 namespace ash {
18 17
19 // WmWindowUserData provides a way to associate arbitrary objects with a 18 // WindowUserData provides a way to associate an object with a Window and have
20 // WmWindow. WmWindowUserData owns the data, deleting it either when 19 // that object destroyed when the window is destroyed, or when WindowUserData
21 // WmWindowUserData is deleted, or when the window the data is associated with
22 // is destroyed (from aura::WindowObserver::OnWindowDestroying()). 20 // is destroyed (from aura::WindowObserver::OnWindowDestroying()).
21 //
22 // NOTE: WindowUserData does not make use of the Set/GetProperty API offered
23 // on aura::Window. This is done to avoid collisions in the case of multiple
24 // WindowUserDatas operating on the same Window.
23 template <typename UserData> 25 template <typename UserData>
24 class WmWindowUserData : public aura::WindowObserver { 26 class WindowUserData : public aura::WindowObserver {
25 public: 27 public:
26 WmWindowUserData() {} 28 WindowUserData() {}
27 29
28 ~WmWindowUserData() override { clear(); } 30 ~WindowUserData() override { clear(); }
29 31
30 void clear() { 32 void clear() {
31 for (auto& pair : window_to_data_) 33 for (auto& pair : window_to_data_)
32 pair.first->aura_window()->RemoveObserver(this); 34 pair.first->RemoveObserver(this);
33 window_to_data_.clear(); 35 window_to_data_.clear();
34 } 36 }
35 37
36 // Sets the data associated with window. This destroys any existing data. 38 // Sets the data associated with window. This destroys any existing data.
37 // |data| may be null. 39 // |data| may be null.
38 void Set(WmWindow* window, std::unique_ptr<UserData> data) { 40 void Set(aura::Window* window, std::unique_ptr<UserData> data) {
39 if (!data) { 41 if (!data) {
40 if (window_to_data_.erase(window)) 42 if (window_to_data_.erase(window))
41 window->aura_window()->RemoveObserver(this); 43 window->RemoveObserver(this);
42 return; 44 return;
43 } 45 }
44 if (window_to_data_.count(window) == 0u) 46 if (window_to_data_.count(window) == 0u)
45 window->aura_window()->AddObserver(this); 47 window->AddObserver(this);
46 window_to_data_[window] = std::move(data); 48 window_to_data_[window] = std::move(data);
47 } 49 }
48 50
49 // Returns the data associated with the window, or null if none set. The 51 // Returns the data associated with the window, or null if none set. The
50 // returned object is owned by WmWindowUserData. 52 // returned object is owned by WindowUserData.
51 UserData* Get(WmWindow* window) { 53 UserData* Get(aura::Window* window) {
52 auto it = window_to_data_.find(window); 54 auto it = window_to_data_.find(window);
53 return it == window_to_data_.end() ? nullptr : it->second.get(); 55 return it == window_to_data_.end() ? nullptr : it->second.get();
54 } 56 }
55 57
56 // Returns the set of windows with data associated with them. 58 // Returns the set of windows with data associated with them.
57 std::set<WmWindow*> GetWindows() { 59 std::set<aura::Window*> GetWindows() {
58 std::set<WmWindow*> windows; 60 std::set<aura::Window*> windows;
59 for (auto& pair : window_to_data_) 61 for (auto& pair : window_to_data_)
60 windows.insert(pair.first); 62 windows.insert(pair.first);
61 return windows; 63 return windows;
62 } 64 }
63 65
64 private: 66 private:
65 // aura::WindowObserver: 67 // aura::WindowObserver:
66 void OnWindowDestroying(aura::Window* window) override { 68 void OnWindowDestroying(aura::Window* window) override {
67 window->RemoveObserver(this); 69 window->RemoveObserver(this);
68 window_to_data_.erase(WmWindow::Get(window)); 70 window_to_data_.erase(window);
69 } 71 }
70 72
71 std::map<WmWindow*, std::unique_ptr<UserData>> window_to_data_; 73 std::map<aura::Window*, std::unique_ptr<UserData>> window_to_data_;
72 74
73 DISALLOW_COPY_AND_ASSIGN(WmWindowUserData); 75 DISALLOW_COPY_AND_ASSIGN(WindowUserData);
74 }; 76 };
75 77
76 } // namespace ash 78 } // namespace ash
77 79
78 #endif // ASH_COMMON_WM_WINDOW_USER_DATA_H_ 80 #endif // ASH_COMMON_WINDOW_USER_DATA_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698