Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/focus/view_storage.h" | 5 #include "ui/views/focus/view_storage.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/stl_util.h" | |
| 12 | 11 |
| 13 namespace views { | 12 namespace views { |
| 14 | 13 |
| 15 // static | 14 // static |
| 16 ViewStorage* ViewStorage::GetInstance() { | 15 ViewStorage* ViewStorage::GetInstance() { |
| 17 return base::Singleton<ViewStorage>::get(); | 16 return base::Singleton<ViewStorage>::get(); |
| 18 } | 17 } |
| 19 | 18 |
| 20 ViewStorage::ViewStorage() : view_storage_next_id_(0) { | 19 ViewStorage::ViewStorage() : view_storage_next_id_(0) { |
| 21 } | 20 } |
| 22 | 21 |
| 23 ViewStorage::~ViewStorage() { | 22 ViewStorage::~ViewStorage() {} |
| 24 base::STLDeleteContainerPairSecondPointers(view_to_ids_.begin(), | |
| 25 view_to_ids_.end()); | |
| 26 } | |
| 27 | 23 |
| 28 int ViewStorage::CreateStorageID() { | 24 int ViewStorage::CreateStorageID() { |
| 29 return view_storage_next_id_++; | 25 return view_storage_next_id_++; |
| 30 } | 26 } |
| 31 | 27 |
| 32 void ViewStorage::StoreView(int storage_id, View* view) { | 28 void ViewStorage::StoreView(int storage_id, View* view) { |
| 33 DCHECK(view); | 29 DCHECK(view); |
| 34 std::map<int, View*>::iterator iter = id_to_view_.find(storage_id); | |
| 35 | 30 |
| 36 if (iter != id_to_view_.end()) { | 31 if (id_to_view_.find(storage_id) != id_to_view_.end()) { |
| 37 NOTREACHED(); | 32 NOTREACHED(); |
| 38 RemoveView(storage_id); | 33 RemoveView(storage_id); |
| 39 } | 34 } |
| 40 | 35 |
| 41 id_to_view_[storage_id] = view; | 36 id_to_view_[storage_id] = view; |
| 42 | 37 view_to_ids_[view].push_back(storage_id); |
| 43 std::vector<int>* ids = NULL; | |
| 44 std::map<View*, std::vector<int>*>::iterator id_iter = | |
| 45 view_to_ids_.find(view); | |
| 46 if (id_iter == view_to_ids_.end()) { | |
| 47 ids = new std::vector<int>(); | |
| 48 view_to_ids_[view] = ids; | |
| 49 } else { | |
| 50 ids = id_iter->second; | |
| 51 } | |
| 52 ids->push_back(storage_id); | |
| 53 } | 38 } |
| 54 | 39 |
| 55 View* ViewStorage::RetrieveView(int storage_id) { | 40 View* ViewStorage::RetrieveView(int storage_id) { |
| 56 std::map<int, View*>::iterator iter = id_to_view_.find(storage_id); | 41 auto iter = id_to_view_.find(storage_id); |
| 57 if (iter == id_to_view_.end()) | 42 if (iter == id_to_view_.end()) |
| 58 return NULL; | 43 return nullptr; |
| 59 return iter->second; | 44 return iter->second; |
| 60 } | 45 } |
| 61 | 46 |
| 62 void ViewStorage::RemoveView(int storage_id) { | 47 void ViewStorage::RemoveView(int storage_id) { |
| 63 EraseView(storage_id, false); | 48 EraseView(storage_id, false); |
| 64 } | 49 } |
| 65 | 50 |
| 66 void ViewStorage::ViewRemoved(View* removed) { | 51 void ViewStorage::ViewRemoved(View* removed) { |
| 67 // Let's first retrieve the ids for that view. | 52 // Let's first retrieve the ids for that view. |
| 68 std::map<View*, std::vector<int>*>::iterator ids_iter = | 53 auto ids_iter = view_to_ids_.find(removed); |
| 69 view_to_ids_.find(removed); | |
| 70 | 54 |
| 71 if (ids_iter == view_to_ids_.end()) { | 55 if (ids_iter == view_to_ids_.end()) { |
| 72 // That view is not in the view storage. | 56 // That view is not in the view storage. |
| 73 return; | 57 return; |
| 74 } | 58 } |
| 75 | 59 |
| 76 std::vector<int>* ids = ids_iter->second; | 60 const std::vector<int>& ids = ids_iter->second; |
| 77 DCHECK(!ids->empty()); | 61 DCHECK(!ids.empty()); |
| 78 EraseView((*ids)[0], true); | 62 EraseView(ids[0], true); |
| 79 } | 63 } |
| 80 | 64 |
| 81 void ViewStorage::EraseView(int storage_id, bool remove_all_ids) { | 65 void ViewStorage::EraseView(int storage_id, bool remove_all_ids) { |
| 82 // Remove the view from id_to_view_location_. | 66 // Remove the view from id_to_view_location_. |
| 83 std::map<int, View*>::iterator view_iter = id_to_view_.find(storage_id); | 67 auto view_iter = id_to_view_.find(storage_id); |
| 84 if (view_iter == id_to_view_.end()) | 68 if (view_iter == id_to_view_.end()) |
| 85 return; | 69 return; |
| 86 | 70 |
| 87 View* view = view_iter->second; | 71 View* view = view_iter->second; |
| 88 id_to_view_.erase(view_iter); | 72 id_to_view_.erase(view_iter); |
| 89 | 73 |
| 90 // Also update view_to_ids_. | 74 // Also update view_to_ids_. |
| 91 std::map<View*, std::vector<int>*>::iterator ids_iter = | 75 auto ids_iter = view_to_ids_.find(view); |
| 92 view_to_ids_.find(view); | |
| 93 DCHECK(ids_iter != view_to_ids_.end()); | 76 DCHECK(ids_iter != view_to_ids_.end()); |
| 94 std::vector<int>* ids = ids_iter->second; | 77 std::vector<int>& ids = ids_iter->second; |
| 95 | 78 |
| 96 if (remove_all_ids) { | 79 if (remove_all_ids) { |
| 97 for (size_t i = 0; i < ids->size(); ++i) { | 80 for (int id : ids) |
| 98 view_iter = id_to_view_.find((*ids)[i]); | 81 id_to_view_.erase(id); |
| 99 if (view_iter != id_to_view_.end()) | 82 view_to_ids_.erase(ids_iter); |
| 100 id_to_view_.erase(view_iter); | 83 } else if (ids.size() == 1) { |
|
Nico
2016/09/19 17:32:30
pulling this out took me longer to read than the r
| |
| 101 } | 84 view_to_ids_.erase(ids_iter); |
| 102 ids->clear(); | |
| 103 } else { | 85 } else { |
| 104 std::vector<int>::iterator id_iter = | 86 auto id_iter = std::find(ids.begin(), ids.end(), storage_id); |
| 105 std::find(ids->begin(), ids->end(), storage_id); | 87 DCHECK(id_iter != ids.end()); |
| 106 DCHECK(id_iter != ids->end()); | 88 ids.erase(id_iter); |
| 107 ids->erase(id_iter); | |
| 108 } | |
| 109 | |
| 110 if (ids->empty()) { | |
| 111 delete ids; | |
| 112 view_to_ids_.erase(ids_iter); | |
| 113 } | 89 } |
| 114 } | 90 } |
| 115 | 91 |
| 116 } // namespace views | 92 } // namespace views |
| OLD | NEW |