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.
5 #include "ui/views/accessibility/ax_aura_obj_cache.h"
7 #include "base/memory/singleton.h"
8 #include "base/stl_util.h"
9 #include "ui/aura/window.h"
10 #include "ui/views/accessibility/ax_aura_obj_wrapper.h"
11 #include "ui/views/accessibility/ax_view_obj_wrapper.h"
12 #include "ui/views/accessibility/ax_widget_obj_wrapper.h"
13 #include "ui/views/accessibility/ax_window_obj_wrapper.h"
14 #include "ui/views/view.h"
15 #include "ui/views/widget/widget.h"
20 AXAuraObjCache
* AXAuraObjCache::GetInstance() {
21 return base::Singleton
<AXAuraObjCache
>::get();
24 AXAuraObjWrapper
* AXAuraObjCache::GetOrCreate(View
* view
) {
25 return CreateInternal
<AXViewObjWrapper
>(view
, view_to_id_map_
);
28 AXAuraObjWrapper
* AXAuraObjCache::GetOrCreate(Widget
* widget
) {
29 return CreateInternal
<AXWidgetObjWrapper
>(widget
, widget_to_id_map_
);
32 AXAuraObjWrapper
* AXAuraObjCache::GetOrCreate(aura::Window
* window
) {
33 return CreateInternal
<AXWindowObjWrapper
>(window
, window_to_id_map_
);
36 int32
AXAuraObjCache::GetID(View
* view
) {
37 return GetIDInternal(view
, view_to_id_map_
);
40 int32
AXAuraObjCache::GetID(Widget
* widget
) {
41 return GetIDInternal(widget
, widget_to_id_map_
);
44 int32
AXAuraObjCache::GetID(aura::Window
* window
) {
45 return GetIDInternal(window
, window_to_id_map_
);
48 void AXAuraObjCache::Remove(View
* view
) {
49 RemoveInternal(view
, view_to_id_map_
);
52 void AXAuraObjCache::RemoveViewSubtree(View
* view
) {
54 for (int i
= 0; i
< view
->child_count(); ++i
)
55 RemoveViewSubtree(view
->child_at(i
));
58 void AXAuraObjCache::Remove(Widget
* widget
) {
59 RemoveInternal(widget
, widget_to_id_map_
);
61 // When an entire widget is deleted, it doesn't always send a notification
62 // on each of its views, so we need to explore them recursively.
63 if (widget
->GetRootView())
64 RemoveViewSubtree(widget
->GetRootView());
67 void AXAuraObjCache::Remove(aura::Window
* window
) {
68 RemoveInternal(window
, window_to_id_map_
);
71 AXAuraObjWrapper
* AXAuraObjCache::Get(int32 id
) {
72 std::map
<int32
, AXAuraObjWrapper
*>::iterator it
= cache_
.find(id
);
74 if (it
== cache_
.end())
80 void AXAuraObjCache::Remove(int32 id
) {
81 AXAuraObjWrapper
* obj
= Get(id
);
90 void AXAuraObjCache::GetTopLevelWindows(
91 std::vector
<AXAuraObjWrapper
*>* children
) {
92 for (std::map
<aura::Window
*, int32
>::iterator it
= window_to_id_map_
.begin();
93 it
!= window_to_id_map_
.end(); ++it
) {
94 if (!it
->first
->parent())
95 children
->push_back(GetOrCreate(it
->first
));
99 AXAuraObjCache::AXAuraObjCache() : current_id_(1), is_destroying_(false) {
102 AXAuraObjCache::~AXAuraObjCache() {
103 is_destroying_
= true;
104 STLDeleteContainerPairSecondPointers(cache_
.begin(), cache_
.end());
108 template <typename AuraViewWrapper
, typename AuraView
>
109 AXAuraObjWrapper
* AXAuraObjCache::CreateInternal(
110 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
114 typename
std::map
<AuraView
*, int32
>::iterator it
=
115 aura_view_to_id_map
.find(aura_view
);
117 if (it
!= aura_view_to_id_map
.end())
118 return Get(it
->second
);
120 AXAuraObjWrapper
* wrapper
= new AuraViewWrapper(aura_view
);
121 aura_view_to_id_map
[aura_view
] = current_id_
;
122 cache_
[current_id_
] = wrapper
;
127 template<typename AuraView
> int32
AXAuraObjCache::GetIDInternal(
128 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
132 typename
std::map
<AuraView
*, int32
>::iterator it
=
133 aura_view_to_id_map
.find(aura_view
);
135 if (it
!= aura_view_to_id_map
.end())
141 template<typename AuraView
>
142 void AXAuraObjCache::RemoveInternal(
143 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
144 int32 id
= GetID(aura_view
);
147 aura_view_to_id_map
.erase(aura_view
);