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 AXAuraObjCache::AXAuraObjCache() : current_id_(1), is_destroying_(false) {
82 AXAuraObjCache::~AXAuraObjCache() {
83 is_destroying_
= true;
84 STLDeleteContainerPairSecondPointers(cache_
.begin(), cache_
.end());
88 template <typename AuraViewWrapper
, typename AuraView
>
89 AXAuraObjWrapper
* AXAuraObjCache::CreateInternal(
90 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
94 typename
std::map
<AuraView
*, int32
>::iterator it
=
95 aura_view_to_id_map
.find(aura_view
);
97 if (it
!= aura_view_to_id_map
.end())
98 return Get(it
->second
);
100 AXAuraObjWrapper
* wrapper
= new AuraViewWrapper(aura_view
);
101 aura_view_to_id_map
[aura_view
] = current_id_
;
102 cache_
[current_id_
] = wrapper
;
107 template<typename AuraView
> int32
AXAuraObjCache::GetIDInternal(
108 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
112 typename
std::map
<AuraView
*, int32
>::iterator it
=
113 aura_view_to_id_map
.find(aura_view
);
115 if (it
!= aura_view_to_id_map
.end())
121 template<typename AuraView
>
122 void AXAuraObjCache::RemoveInternal(
123 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
124 int32 id
= GetID(aura_view
);
127 aura_view_to_id_map
.erase(aura_view
);