Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: ui/app_list/app_list_item_list.cc

Issue 148403007: Protect AppListItemList Add/Remove and fix sync bugs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/app_list/app_list_item_list.h" 5 #include "ui/app_list/app_list_item_list.h"
6 6
7 #include "ui/app_list/app_list_item.h" 7 #include "ui/app_list/app_list_item.h"
8 8
9 namespace app_list { 9 namespace app_list {
10 10
(...skipping 24 matching lines...) Expand all
35 for (size_t i = 0; i < app_list_items_.size(); ++i) { 35 for (size_t i = 0; i < app_list_items_.size(); ++i) {
36 AppListItem* item = app_list_items_[i]; 36 AppListItem* item = app_list_items_[i];
37 if (item->id() == id) { 37 if (item->id() == id) {
38 *index = i; 38 *index = i;
39 return true; 39 return true;
40 } 40 }
41 } 41 }
42 return false; 42 return false;
43 } 43 }
44 44
45 size_t AppListItemList::AddItem(AppListItem* item) {
46 CHECK(std::find(app_list_items_.begin(), app_list_items_.end(), item)
47 == app_list_items_.end());
48 EnsureValidItemPosition(item);
49 size_t index = GetItemSortOrderIndex(item->position(), item->id());
50 app_list_items_.insert(app_list_items_.begin() + index, item);
51 FOR_EACH_OBSERVER(AppListItemListObserver,
52 observers_,
53 OnListItemAdded(index, item));
54 return index;
55 }
56
57 void AppListItemList::InsertItemAt(AppListItem* item, size_t index) {
58 DCHECK_LE(index, item_count());
59 if (item_count() == 0) {
60 AddItem(item);
61 return;
62 }
63
64 AppListItem* prev = index > 0 ? app_list_items_[index - 1] : NULL;
65 AppListItem* next = index <= app_list_items_.size() - 1 ?
66 app_list_items_[index] : NULL;
67 CHECK_NE(prev, next);
68
69 if (prev && next && prev->position().Equals(next->position()))
70 prev = NULL;
71
72 if (!prev)
73 item->set_position(next->position().CreateBefore());
74 else if (!next)
75 item->set_position(prev->position().CreateAfter());
76 else
77 item->set_position(prev->position().CreateBetween(next->position()));
78
79 app_list_items_.insert(app_list_items_.begin() + index, item);
80
81 FOR_EACH_OBSERVER(AppListItemListObserver,
82 observers_,
83 OnListItemAdded(index, item));
84 }
85
86 void AppListItemList::DeleteItem(const std::string& id) {
87 scoped_ptr<AppListItem> item = RemoveItem(id);
88 // |item| will be deleted on destruction.
89 }
90
91 void AppListItemList::DeleteItemsByType(const char* type) {
92 for (int i = static_cast<int>(app_list_items_.size()) - 1;
93 i >= 0; --i) {
94 AppListItem* item = app_list_items_[i];
95 if (!type || item->GetItemType() == type)
96 DeleteItemAt(i);
97 }
98 }
99
100 scoped_ptr<AppListItem> AppListItemList::RemoveItem(
101 const std::string& id) {
102 size_t index;
103 if (FindItemIndex(id, &index))
104 return RemoveItemAt(index);
105
106 return scoped_ptr<AppListItem>();
107 }
108
109 scoped_ptr<AppListItem> AppListItemList::RemoveItemAt(size_t index) {
110 DCHECK_LT(index, item_count());
111 AppListItem* item = app_list_items_[index];
112 app_list_items_.weak_erase(app_list_items_.begin() + index);
113 FOR_EACH_OBSERVER(AppListItemListObserver,
114 observers_,
115 OnListItemRemoved(index, item));
116 return make_scoped_ptr<AppListItem>(item);
117 }
118
119 void AppListItemList::MoveItem(size_t from_index, size_t to_index) { 45 void AppListItemList::MoveItem(size_t from_index, size_t to_index) {
120 DCHECK_LT(from_index, item_count()); 46 DCHECK_LT(from_index, item_count());
121 DCHECK_LT(to_index, item_count()); 47 DCHECK_LT(to_index, item_count());
122 if (from_index == to_index) 48 if (from_index == to_index)
123 return; 49 return;
124 50
125 AppListItem* target_item = app_list_items_[from_index]; 51 AppListItem* target_item = app_list_items_[from_index];
126 app_list_items_.weak_erase(app_list_items_.begin() + from_index); 52 app_list_items_.weak_erase(app_list_items_.begin() + from_index);
127 app_list_items_.insert(app_list_items_.begin() + to_index, target_item); 53 app_list_items_.insert(app_list_items_.begin() + to_index, target_item);
128 54
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 VLOG(2) << "SetItemPosition: " << item->id().substr(0, 8) 105 VLOG(2) << "SetItemPosition: " << item->id().substr(0, 8)
180 << " -> " << new_position.ToDebugString() 106 << " -> " << new_position.ToDebugString()
181 << " From: " << from_index << " To: " << to_index; 107 << " From: " << from_index << " To: " << to_index;
182 item->set_position(new_position); 108 item->set_position(new_position);
183 app_list_items_.insert(app_list_items_.begin() + to_index, item); 109 app_list_items_.insert(app_list_items_.begin() + to_index, item);
184 FOR_EACH_OBSERVER(AppListItemListObserver, 110 FOR_EACH_OBSERVER(AppListItemListObserver,
185 observers_, 111 observers_,
186 OnListItemMoved(from_index, to_index, item)); 112 OnListItemMoved(from_index, to_index, item));
187 } 113 }
188 114
115 // AppListItemList protected
116
117 size_t AppListItemList::AddItem(AppListItem* item) {
118 CHECK(std::find(app_list_items_.begin(), app_list_items_.end(), item)
119 == app_list_items_.end());
120 EnsureValidItemPosition(item);
121 size_t index = GetItemSortOrderIndex(item->position(), item->id());
122 app_list_items_.insert(app_list_items_.begin() + index, item);
123 FOR_EACH_OBSERVER(AppListItemListObserver,
124 observers_,
125 OnListItemAdded(index, item));
126 return index;
127 }
128
129 void AppListItemList::DeleteItem(const std::string& id) {
130 scoped_ptr<AppListItem> item = RemoveItem(id);
131 // |item| will be deleted on destruction.
132 }
133
134 scoped_ptr<AppListItem> AppListItemList::RemoveItem(
135 const std::string& id) {
136 size_t index;
137 if (FindItemIndex(id, &index))
138 return RemoveItemAt(index);
139
140 return scoped_ptr<AppListItem>();
141 }
142
143 scoped_ptr<AppListItem> AppListItemList::RemoveItemAt(size_t index) {
144 DCHECK_LT(index, item_count());
145 AppListItem* item = app_list_items_[index];
146 app_list_items_.weak_erase(app_list_items_.begin() + index);
147 FOR_EACH_OBSERVER(AppListItemListObserver,
148 observers_,
149 OnListItemRemoved(index, item));
150 return make_scoped_ptr<AppListItem>(item);
151 }
152
189 // AppListItemList private 153 // AppListItemList private
190 154
191 void AppListItemList::DeleteItemAt(size_t index) { 155 void AppListItemList::DeleteItemAt(size_t index) {
192 scoped_ptr<AppListItem> item = RemoveItemAt(index); 156 scoped_ptr<AppListItem> item = RemoveItemAt(index);
193 // |item| will be deleted on destruction. 157 // |item| will be deleted on destruction.
194 } 158 }
195 159
196 void AppListItemList::EnsureValidItemPosition(AppListItem* item) { 160 void AppListItemList::EnsureValidItemPosition(AppListItem* item) {
197 syncer::StringOrdinal position = item->position(); 161 syncer::StringOrdinal position = item->position();
198 if (position.IsValid()) 162 if (position.IsValid())
(...skipping 15 matching lines...) Expand all
214 if (position.LessThan(app_list_items_[index]->position()) || 178 if (position.LessThan(app_list_items_[index]->position()) ||
215 (position.Equals(app_list_items_[index]->position()) && 179 (position.Equals(app_list_items_[index]->position()) &&
216 (id < app_list_items_[index]->id()))) { 180 (id < app_list_items_[index]->id()))) {
217 return index; 181 return index;
218 } 182 }
219 } 183 }
220 return app_list_items_.size(); 184 return app_list_items_.size();
221 } 185 }
222 186
223 } // namespace app_list 187 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698