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 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::Remove(Widget
* widget
) {
53 RemoveInternal(widget
, widget_to_id_map_
);
56 void AXAuraObjCache::Remove(aura::Window
* window
) {
57 RemoveInternal(window
, window_to_id_map_
);
60 AXAuraObjWrapper
* AXAuraObjCache::Get(int32 id
) {
61 std::map
<int32
, AXAuraObjWrapper
*>::iterator it
= cache_
.find(id
);
63 if (it
== cache_
.end())
69 void AXAuraObjCache::Remove(int32 id
) {
70 AXAuraObjWrapper
* obj
= Get(id
);
79 void AXAuraObjCache::GetTopLevelWindows(
80 std::vector
<AXAuraObjWrapper
*>* children
) {
81 for (std::map
<aura::Window
*, int32
>::iterator it
= window_to_id_map_
.begin();
82 it
!= window_to_id_map_
.end(); ++it
) {
83 if (!it
->first
->parent())
84 children
->push_back(GetOrCreate(it
->first
));
88 AXAuraObjCache::AXAuraObjCache() : current_id_(1), is_destroying_(false) {
91 AXAuraObjCache::~AXAuraObjCache() {
92 is_destroying_
= true;
93 STLDeleteContainerPairSecondPointers(cache_
.begin(), cache_
.end());
97 template <typename AuraViewWrapper
, typename AuraView
>
98 AXAuraObjWrapper
* AXAuraObjCache::CreateInternal(
99 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
103 typename
std::map
<AuraView
*, int32
>::iterator it
=
104 aura_view_to_id_map
.find(aura_view
);
106 if (it
!= aura_view_to_id_map
.end())
107 return Get(it
->second
);
109 AXAuraObjWrapper
* wrapper
= new AuraViewWrapper(aura_view
);
110 aura_view_to_id_map
[aura_view
] = current_id_
;
111 cache_
[current_id_
] = wrapper
;
116 template<typename AuraView
> int32
AXAuraObjCache::GetIDInternal(
117 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
121 typename
std::map
<AuraView
*, int32
>::iterator it
=
122 aura_view_to_id_map
.find(aura_view
);
124 if (it
!= aura_view_to_id_map
.end())
130 template<typename AuraView
>
131 void AXAuraObjCache::RemoveInternal(
132 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
133 int32 id
= GetID(aura_view
);
136 aura_view_to_id_map
.erase(aura_view
);