OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 | |
5 #include "ui/views/accessibility/ax_aura_obj_cache.h" | |
6 | |
7 #include "base/memory/singleton.h" | |
8 #include "ui/aura/window.h" | |
9 #include "ui/views/accessibility/ax_aura_obj_wrapper.h" | |
10 #include "ui/views/accessibility/ax_view_obj_wrapper.h" | |
11 #include "ui/views/accessibility/ax_widget_obj_wrapper.h" | |
12 #include "ui/views/accessibility/ax_window_obj_wrapper.h" | |
13 #include "ui/views/view.h" | |
14 #include "ui/views/widget/widget.h" | |
15 | |
16 namespace views { | |
17 | |
18 // static | |
19 AXAuraObjCache* AXAuraObjCache::GetInstance() { | |
20 return Singleton<AXAuraObjCache>::get(); | |
21 } | |
22 | |
23 AXAuraObjWrapper* AXAuraObjCache::GetOrCreate(View* view) { | |
24 if (!view) | |
25 return NULL; | |
26 | |
27 std::map<View*, int32>::iterator it = | |
aboxhall
2014/04/28 19:50:38
Nit: combine with below into one line.
David Tseng
2014/04/29 01:35:03
Done.
| |
28 view_to_id_map_.find(view); | |
29 | |
30 if (it != view_to_id_map_.end()) | |
31 return Get(it->second); | |
32 | |
33 AXAuraObjWrapper* wrapper = new AXViewObjWrapper(view); | |
34 view_to_id_map_[view] = current_id_; | |
35 cache_[current_id_] = wrapper; | |
36 current_id_++; | |
37 return wrapper; | |
38 } | |
39 | |
40 AXAuraObjWrapper* AXAuraObjCache::GetOrCreate(Widget* widget) { | |
41 if (!widget) | |
42 return NULL; | |
43 | |
44 std::map<Widget*, int32>::iterator it = | |
45 widget_to_id_map_.find(widget); | |
46 | |
47 if (it != widget_to_id_map_.end()) | |
48 return Get(it->second); | |
49 | |
50 AXAuraObjWrapper* wrapper = new AXWidgetObjWrapper(widget); | |
51 widget_to_id_map_[widget] = current_id_; | |
aboxhall
2014/04/28 19:50:38
Perhaps this pattern (AuraObjWrapper* wrapper = ne
David Tseng
2014/04/29 01:35:03
Made these into templates.
| |
52 cache_[current_id_] = wrapper; | |
53 current_id_++; | |
54 return wrapper; | |
55 } | |
56 | |
57 AXAuraObjWrapper* AXAuraObjCache::GetOrCreate(aura::Window* window) { | |
58 if (!window) | |
59 return NULL; | |
60 | |
61 std::map<aura::Window*, int32>::iterator it = | |
62 window_to_id_map_.find(window); | |
63 | |
64 if (it != window_to_id_map_.end()) | |
65 return Get(it->second); | |
66 | |
67 AXAuraObjWrapper* wrapper = new AXWindowObjWrapper(window); | |
68 window_to_id_map_[window] = current_id_; | |
69 cache_[current_id_] = wrapper; | |
70 current_id_++; | |
71 return wrapper; | |
72 } | |
73 | |
74 int32 AXAuraObjCache::GetID(View* view) { | |
75 if (!view) | |
76 return -1; | |
77 | |
78 std::map<View*, int32>::iterator it = | |
79 view_to_id_map_.find(view); | |
80 | |
81 if (it != view_to_id_map_.end()) | |
82 return it->second; | |
83 | |
84 return -1; | |
85 } | |
86 | |
87 int32 AXAuraObjCache::GetID(Widget* widget) { | |
88 if (!widget) | |
89 return -1; | |
90 | |
91 std::map<Widget*, int32>::iterator it = | |
92 widget_to_id_map_.find(widget); | |
93 | |
94 if (it != widget_to_id_map_.end()) | |
95 return it->second; | |
96 | |
97 return -1; | |
98 } | |
99 | |
100 int32 AXAuraObjCache::GetID(aura::Window* window) { | |
101 if (!window) | |
102 return -1; | |
103 | |
104 std::map<aura::Window*, int32>::iterator it = | |
105 window_to_id_map_.find(window); | |
106 | |
107 if (it != window_to_id_map_.end()) | |
108 return it->second; | |
109 | |
110 return -1; | |
111 } | |
112 | |
113 void AXAuraObjCache::Remove(View* view) { | |
114 int32 id = GetID(view); | |
115 | |
116 if (id == -1) | |
117 return; | |
118 | |
119 view_to_id_map_.erase(view); | |
120 Remove(id); | |
121 } | |
122 | |
123 void AXAuraObjCache::Remove(Widget* widget) { | |
124 int32 id = GetID(widget); | |
125 | |
126 if (id == -1) | |
127 return; | |
128 | |
129 widget_to_id_map_.erase(widget); | |
130 Remove(id); | |
131 } | |
132 | |
133 void AXAuraObjCache::Remove(aura::Window* window) { | |
134 int32 id = GetID(window); | |
135 | |
136 if (id == -1) | |
137 return; | |
138 | |
139 window_to_id_map_.erase(window); | |
140 Remove(id); | |
141 } | |
142 | |
143 AXAuraObjWrapper* AXAuraObjCache::Get(int32 id) { | |
144 std::map<int32, AXAuraObjWrapper*>::iterator it = cache_.find(id); | |
145 | |
146 if (it == cache_.end()) | |
147 return NULL; | |
148 | |
149 return it->second; | |
150 } | |
151 | |
152 void AXAuraObjCache::Remove(int32 id) { | |
153 AXAuraObjWrapper* obj = Get(id); | |
154 | |
155 if (id == -1 || !obj) | |
156 return; | |
157 | |
158 cache_.erase(id); | |
159 delete obj; | |
160 } | |
161 | |
162 AXAuraObjCache::AXAuraObjCache() : current_id_(1) { | |
163 } | |
164 | |
165 AXAuraObjCache::~AXAuraObjCache() {} | |
166 | |
167 } // namespace views | |
OLD | NEW |