| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 "views/focus/view_storage.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/stl_util.h" | |
| 11 | |
| 12 namespace views { | |
| 13 | |
| 14 // static | |
| 15 ViewStorage* ViewStorage::GetInstance() { | |
| 16 return Singleton<ViewStorage>::get(); | |
| 17 } | |
| 18 | |
| 19 ViewStorage::ViewStorage() : view_storage_next_id_(0) { | |
| 20 } | |
| 21 | |
| 22 ViewStorage::~ViewStorage() { | |
| 23 STLDeleteContainerPairSecondPointers(view_to_ids_.begin(), | |
| 24 view_to_ids_.end()); | |
| 25 } | |
| 26 | |
| 27 int ViewStorage::CreateStorageID() { | |
| 28 return view_storage_next_id_++; | |
| 29 } | |
| 30 | |
| 31 void ViewStorage::StoreView(int storage_id, View* view) { | |
| 32 DCHECK(view); | |
| 33 std::map<int, View*>::iterator iter = id_to_view_.find(storage_id); | |
| 34 | |
| 35 if (iter != id_to_view_.end()) { | |
| 36 NOTREACHED(); | |
| 37 RemoveView(storage_id); | |
| 38 } | |
| 39 | |
| 40 id_to_view_[storage_id] = view; | |
| 41 | |
| 42 std::vector<int>* ids = NULL; | |
| 43 std::map<View*, std::vector<int>*>::iterator id_iter = | |
| 44 view_to_ids_.find(view); | |
| 45 if (id_iter == view_to_ids_.end()) { | |
| 46 ids = new std::vector<int>(); | |
| 47 view_to_ids_[view] = ids; | |
| 48 } else { | |
| 49 ids = id_iter->second; | |
| 50 } | |
| 51 ids->push_back(storage_id); | |
| 52 } | |
| 53 | |
| 54 View* ViewStorage::RetrieveView(int storage_id) { | |
| 55 std::map<int, View*>::iterator iter = id_to_view_.find(storage_id); | |
| 56 if (iter == id_to_view_.end()) | |
| 57 return NULL; | |
| 58 return iter->second; | |
| 59 } | |
| 60 | |
| 61 void ViewStorage::RemoveView(int storage_id) { | |
| 62 EraseView(storage_id, false); | |
| 63 } | |
| 64 | |
| 65 void ViewStorage::ViewRemoved(View* removed) { | |
| 66 // Let's first retrieve the ids for that view. | |
| 67 std::map<View*, std::vector<int>*>::iterator ids_iter = | |
| 68 view_to_ids_.find(removed); | |
| 69 | |
| 70 if (ids_iter == view_to_ids_.end()) { | |
| 71 // That view is not in the view storage. | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 std::vector<int>* ids = ids_iter->second; | |
| 76 DCHECK(!ids->empty()); | |
| 77 EraseView((*ids)[0], true); | |
| 78 } | |
| 79 | |
| 80 void ViewStorage::EraseView(int storage_id, bool remove_all_ids) { | |
| 81 // Remove the view from id_to_view_location_. | |
| 82 std::map<int, View*>::iterator view_iter = id_to_view_.find(storage_id); | |
| 83 if (view_iter == id_to_view_.end()) | |
| 84 return; | |
| 85 | |
| 86 View* view = view_iter->second; | |
| 87 id_to_view_.erase(view_iter); | |
| 88 | |
| 89 // Also update view_to_ids_. | |
| 90 std::map<View*, std::vector<int>*>::iterator ids_iter = | |
| 91 view_to_ids_.find(view); | |
| 92 DCHECK(ids_iter != view_to_ids_.end()); | |
| 93 std::vector<int>* ids = ids_iter->second; | |
| 94 | |
| 95 if (remove_all_ids) { | |
| 96 for (size_t i = 0; i < ids->size(); ++i) { | |
| 97 view_iter = id_to_view_.find((*ids)[i]); | |
| 98 if (view_iter != id_to_view_.end()) | |
| 99 id_to_view_.erase(view_iter); | |
| 100 } | |
| 101 ids->clear(); | |
| 102 } else { | |
| 103 std::vector<int>::iterator id_iter = | |
| 104 std::find(ids->begin(), ids->end(), storage_id); | |
| 105 DCHECK(id_iter != ids->end()); | |
| 106 ids->erase(id_iter); | |
| 107 } | |
| 108 | |
| 109 if (ids->empty()) { | |
| 110 delete ids; | |
| 111 view_to_ids_.erase(ids_iter); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 } // namespace views | |
| OLD | NEW |