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) {
82 AXAuraObjCache::~AXAuraObjCache() {
83 STLDeleteContainerPairSecondPointers(cache_
.begin(), cache_
.end());
87 template <typename AuraViewWrapper
, typename AuraView
>
88 AXAuraObjWrapper
* AXAuraObjCache::CreateInternal(
89 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
93 typename
std::map
<AuraView
*, int32
>::iterator it
=
94 aura_view_to_id_map
.find(aura_view
);
96 if (it
!= aura_view_to_id_map
.end())
97 return Get(it
->second
);
99 AXAuraObjWrapper
* wrapper
= new AuraViewWrapper(aura_view
);
100 aura_view_to_id_map
[aura_view
] = current_id_
;
101 cache_
[current_id_
] = wrapper
;
106 template<typename AuraView
> int32
AXAuraObjCache::GetIDInternal(
107 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
111 typename
std::map
<AuraView
*, int32
>::iterator it
=
112 aura_view_to_id_map
.find(aura_view
);
114 if (it
!= aura_view_to_id_map
.end())
120 template<typename AuraView
>
121 void AXAuraObjCache::RemoveInternal(
122 AuraView
* aura_view
, std::map
<AuraView
*, int32
>& aura_view_to_id_map
) {
123 int32 id
= GetID(aura_view
);
126 aura_view_to_id_map
.erase(aura_view
);