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 "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" 16 17 namespace views { 18 19 // static 20 AXAuraObjCache* AXAuraObjCache::GetInstance() { 21 return Singleton<AXAuraObjCache>::get(); 22 } 23 24 AXAuraObjWrapper* AXAuraObjCache::GetOrCreate(View* view) { 25 return CreateInternal<AXViewObjWrapper>(view, view_to_id_map_); 26 } 27 28 AXAuraObjWrapper* AXAuraObjCache::GetOrCreate(Widget* widget) { 29 return CreateInternal<AXWidgetObjWrapper>(widget, widget_to_id_map_); 30 } 31 32 AXAuraObjWrapper* AXAuraObjCache::GetOrCreate(aura::Window* window) { 33 return CreateInternal<AXWindowObjWrapper>(window, window_to_id_map_); 34 } 35 36 int32 AXAuraObjCache::GetID(View* view) { 37 return GetIDInternal(view, view_to_id_map_); 38 } 39 40 int32 AXAuraObjCache::GetID(Widget* widget) { 41 return GetIDInternal(widget, widget_to_id_map_); 42 } 43 44 int32 AXAuraObjCache::GetID(aura::Window* window) { 45 return GetIDInternal(window, window_to_id_map_); 46 } 47 48 void AXAuraObjCache::Remove(View* view) { 49 RemoveInternal(view, view_to_id_map_); 50 } 51 52 void AXAuraObjCache::Remove(Widget* widget) { 53 RemoveInternal(widget, widget_to_id_map_); 54 } 55 56 void AXAuraObjCache::Remove(aura::Window* window) { 57 RemoveInternal(window, window_to_id_map_); 58 } 59 60 AXAuraObjWrapper* AXAuraObjCache::Get(int32 id) { 61 std::map<int32, AXAuraObjWrapper*>::iterator it = cache_.find(id); 62 63 if (it == cache_.end()) 64 return NULL; 65 66 return it->second; 67 } 68 69 void AXAuraObjCache::Remove(int32 id) { 70 AXAuraObjWrapper* obj = Get(id); 71 72 if (id == -1 || !obj) 73 return; 74 75 cache_.erase(id); 76 delete obj; 77 } 78 79 AXAuraObjCache::AXAuraObjCache() : current_id_(1) { 80 } 81 82 AXAuraObjCache::~AXAuraObjCache() { 83 STLDeleteContainerPairSecondPointers(cache_.begin(), cache_.end()); 84 cache_.clear(); 85 } 86 87 template <typename AuraViewWrapper, typename AuraView> 88 AXAuraObjWrapper* AXAuraObjCache::CreateInternal( 89 AuraView* aura_view, std::map<AuraView*, int32>& aura_view_to_id_map) { 90 if (!aura_view) 91 return NULL; 92 93 typename std::map<AuraView*, int32>::iterator it = 94 aura_view_to_id_map.find(aura_view); 95 96 if (it != aura_view_to_id_map.end()) 97 return Get(it->second); 98 99 AXAuraObjWrapper* wrapper = new AuraViewWrapper(aura_view); 100 aura_view_to_id_map[aura_view] = current_id_; 101 cache_[current_id_] = wrapper; 102 current_id_++; 103 return wrapper; 104 } 105 106 template<typename AuraView> int32 AXAuraObjCache::GetIDInternal( 107 AuraView* aura_view, std::map<AuraView*, int32>& aura_view_to_id_map) { 108 if (!aura_view) 109 return -1; 110 111 typename std::map<AuraView*, int32>::iterator it = 112 aura_view_to_id_map.find(aura_view); 113 114 if (it != aura_view_to_id_map.end()) 115 return it->second; 116 117 return -1; 118 } 119 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); 124 if (id == -1) 125 return; 126 aura_view_to_id_map.erase(aura_view); 127 Remove(id); 128 } 129 130 } // namespace views 131