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

Side by Side Diff: ui/base/models/simple_menu_model.cc

Issue 10458045: Cleanup: Remove vector::at() calls in SimpleMenuModel. Do CHECKs instead. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 6 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
« no previous file with comments | « ui/base/models/simple_menu_model.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/base/models/simple_menu_model.h" 5 #include "ui/base/models/simple_menu_model.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "ui/base/l10n/l10n_util.h" 9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/gfx/image/image_skia.h" 10 #include "ui/gfx/image/image_skia.h"
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 InsertItemAtIndex(item, index); 178 InsertItemAtIndex(item, index);
179 } 179 }
180 180
181 void SimpleMenuModel::InsertSubMenuWithStringIdAt( 181 void SimpleMenuModel::InsertSubMenuWithStringIdAt(
182 int index, int command_id, int string_id, MenuModel* model) { 182 int index, int command_id, int string_id, MenuModel* model) {
183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id), 183 InsertSubMenuAt(index, command_id, l10n_util::GetStringUTF16(string_id),
184 model); 184 model);
185 } 185 }
186 186
187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) { 187 void SimpleMenuModel::SetIcon(int index, const gfx::ImageSkia& icon) {
188 ValidateItemIndex(index);
188 items_[index].icon = icon; 189 items_[index].icon = icon;
189 } 190 }
190 191
191 void SimpleMenuModel::Clear() { 192 void SimpleMenuModel::Clear() {
192 items_.clear(); 193 items_.clear();
193 } 194 }
194 195
195 int SimpleMenuModel::GetIndexOfCommandId(int command_id) { 196 int SimpleMenuModel::GetIndexOfCommandId(int command_id) {
196 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) { 197 for (ItemVector::iterator i = items_.begin(); i != items_.end(); ++i) {
197 if ((*i).command_id == command_id) { 198 if (i->command_id == command_id) {
198 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i))); 199 return FlipIndex(static_cast<int>(std::distance(items_.begin(), i)));
199 } 200 }
200 } 201 }
201 return -1; 202 return -1;
202 } 203 }
203 204
204 //////////////////////////////////////////////////////////////////////////////// 205 ////////////////////////////////////////////////////////////////////////////////
205 // SimpleMenuModel, MenuModel implementation: 206 // SimpleMenuModel, MenuModel implementation:
206 207
207 bool SimpleMenuModel::HasIcons() const { 208 bool SimpleMenuModel::HasIcons() const {
208 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) { 209 for (ItemVector::const_iterator i = items_.begin(); i != items_.end(); ++i) {
209 if (!i->icon.isNull()) 210 if (!i->icon.isNull())
210 return true; 211 return true;
211 } 212 }
212 213
213 return false; 214 return false;
214 } 215 }
215 216
216 int SimpleMenuModel::GetItemCount() const { 217 int SimpleMenuModel::GetItemCount() const {
217 return static_cast<int>(items_.size()); 218 return static_cast<int>(items_.size());
218 } 219 }
219 220
220 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const { 221 MenuModel::ItemType SimpleMenuModel::GetTypeAt(int index) const {
221 return items_.at(FlipIndex(index)).type; 222 int item_index = FlipIndex(index);
223 ValidateItemIndex(item_index);
224 return items_[item_index].type;
222 } 225 }
223 226
224 int SimpleMenuModel::GetCommandIdAt(int index) const { 227 int SimpleMenuModel::GetCommandIdAt(int index) const {
225 return items_.at(FlipIndex(index)).command_id; 228 int item_index = FlipIndex(index);
229 ValidateItemIndex(item_index);
230 return items_[item_index].command_id;
226 } 231 }
227 232
228 string16 SimpleMenuModel::GetLabelAt(int index) const { 233 string16 SimpleMenuModel::GetLabelAt(int index) const {
229 if (IsItemDynamicAt(index)) 234 if (IsItemDynamicAt(index))
230 return delegate_->GetLabelForCommandId(GetCommandIdAt(index)); 235 return delegate_->GetLabelForCommandId(GetCommandIdAt(index));
231 return items_.at(FlipIndex(index)).label; 236 int item_index = FlipIndex(index);
237 ValidateItemIndex(item_index);
238 return items_[item_index].label;
232 } 239 }
233 240
234 bool SimpleMenuModel::IsItemDynamicAt(int index) const { 241 bool SimpleMenuModel::IsItemDynamicAt(int index) const {
235 if (delegate_) 242 if (delegate_)
236 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index)); 243 return delegate_->IsItemForCommandIdDynamic(GetCommandIdAt(index));
237 return false; 244 return false;
238 } 245 }
239 246
240 bool SimpleMenuModel::GetAcceleratorAt(int index, 247 bool SimpleMenuModel::GetAcceleratorAt(int index,
241 ui::Accelerator* accelerator) const { 248 ui::Accelerator* accelerator) const {
242 if (delegate_) { 249 if (delegate_) {
243 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index), 250 return delegate_->GetAcceleratorForCommandId(GetCommandIdAt(index),
244 accelerator); 251 accelerator);
245 } 252 }
246 return false; 253 return false;
247 } 254 }
248 255
249 bool SimpleMenuModel::IsItemCheckedAt(int index) const { 256 bool SimpleMenuModel::IsItemCheckedAt(int index) const {
250 if (!delegate_) 257 if (!delegate_)
251 return false; 258 return false;
252 int item_index = FlipIndex(index); 259 MenuModel::ItemType item_type = GetTypeAt(index);
253 MenuModel::ItemType item_type = items_[item_index].type;
254 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ? 260 return (item_type == TYPE_CHECK || item_type == TYPE_RADIO) ?
255 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false; 261 delegate_->IsCommandIdChecked(GetCommandIdAt(index)) : false;
256 } 262 }
257 263
258 int SimpleMenuModel::GetGroupIdAt(int index) const { 264 int SimpleMenuModel::GetGroupIdAt(int index) const {
259 return items_.at(FlipIndex(index)).group_id; 265 int item_index = FlipIndex(index);
266 ValidateItemIndex(item_index);
267 return items_[item_index].group_id;
260 } 268 }
261 269
262 bool SimpleMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) { 270 bool SimpleMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) {
263 if (IsItemDynamicAt(index)) 271 if (IsItemDynamicAt(index))
264 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon); 272 return delegate_->GetIconForCommandId(GetCommandIdAt(index), icon);
265 273
274 ValidateItemIndex(index);
266 if (items_[index].icon.isNull()) 275 if (items_[index].icon.isNull())
267 return false; 276 return false;
268 277
269 *icon = items_[index].icon; 278 *icon = items_[index].icon;
270 return true; 279 return true;
271 } 280 }
272 281
273 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const { 282 ButtonMenuItemModel* SimpleMenuModel::GetButtonMenuItemAt(int index) const {
274 return items_.at(FlipIndex(index)).button_model; 283 int item_index = FlipIndex(index);
284 ValidateItemIndex(item_index);
285 return items_[item_index].button_model;
275 } 286 }
276 287
277 bool SimpleMenuModel::IsEnabledAt(int index) const { 288 bool SimpleMenuModel::IsEnabledAt(int index) const {
278 int command_id = GetCommandIdAt(index); 289 int command_id = GetCommandIdAt(index);
279 if (!delegate_ || command_id == kSeparatorId || 290 if (!delegate_ || command_id == kSeparatorId || GetButtonMenuItemAt(index))
280 items_.at(FlipIndex(index)).button_model)
281 return true; 291 return true;
282 return delegate_->IsCommandIdEnabled(command_id); 292 return delegate_->IsCommandIdEnabled(command_id);
283 } 293 }
284 294
285 bool SimpleMenuModel::IsVisibleAt(int index) const { 295 bool SimpleMenuModel::IsVisibleAt(int index) const {
286 int command_id = GetCommandIdAt(index); 296 int command_id = GetCommandIdAt(index);
287 if (!delegate_ || command_id == kSeparatorId || 297 if (!delegate_ || command_id == kSeparatorId || GetButtonMenuItemAt(index))
288 items_.at(FlipIndex(index)).button_model)
289 return true; 298 return true;
290 return delegate_->IsCommandIdVisible(command_id); 299 return delegate_->IsCommandIdVisible(command_id);
291 } 300 }
292 301
293 void SimpleMenuModel::HighlightChangedTo(int index) { 302 void SimpleMenuModel::HighlightChangedTo(int index) {
294 if (delegate_) 303 if (delegate_)
295 delegate_->CommandIdHighlighted(GetCommandIdAt(index)); 304 delegate_->CommandIdHighlighted(GetCommandIdAt(index));
296 } 305 }
297 306
298 void SimpleMenuModel::ActivatedAt(int index) { 307 void SimpleMenuModel::ActivatedAt(int index) {
299 if (delegate_) 308 if (delegate_)
300 delegate_->ExecuteCommand(GetCommandIdAt(index)); 309 delegate_->ExecuteCommand(GetCommandIdAt(index));
301 } 310 }
302 311
303 void SimpleMenuModel::ActivatedAt(int index, int event_flags) { 312 void SimpleMenuModel::ActivatedAt(int index, int event_flags) {
304 if (delegate_) 313 if (delegate_)
305 delegate_->ExecuteCommand(GetCommandIdAt(index), event_flags); 314 delegate_->ExecuteCommand(GetCommandIdAt(index), event_flags);
306 } 315 }
307 316
308 MenuModel* SimpleMenuModel::GetSubmenuModelAt(int index) const { 317 MenuModel* SimpleMenuModel::GetSubmenuModelAt(int index) const {
309 return items_.at(FlipIndex(index)).submenu; 318 int item_index = FlipIndex(index);
319 ValidateItemIndex(item_index);
320 return items_[item_index].submenu;
310 } 321 }
311 322
312 void SimpleMenuModel::MenuWillShow() { 323 void SimpleMenuModel::MenuWillShow() {
313 if (delegate_) 324 if (delegate_)
314 delegate_->MenuWillShow(this); 325 delegate_->MenuWillShow(this);
315 } 326 }
316 327
317 void SimpleMenuModel::MenuClosed() { 328 void SimpleMenuModel::MenuClosed() {
318 // Due to how menus work on the different platforms, ActivatedAt will be 329 // Due to how menus work on the different platforms, ActivatedAt will be
319 // called after this. It's more convenient for the delegate to be called 330 // called after this. It's more convenient for the delegate to be called
(...skipping 13 matching lines...) Expand all
333 delegate_->MenuClosed(this); 344 delegate_->MenuClosed(this);
334 } 345 }
335 346
336 int SimpleMenuModel::FlipIndex(int index) const { 347 int SimpleMenuModel::FlipIndex(int index) const {
337 return index; 348 return index;
338 } 349 }
339 350
340 //////////////////////////////////////////////////////////////////////////////// 351 ////////////////////////////////////////////////////////////////////////////////
341 // SimpleMenuModel, Private: 352 // SimpleMenuModel, Private:
342 353
354 void SimpleMenuModel::ValidateItemIndex(int index) const {
Peter Kasting 2012/05/31 04:49:25 Nit: If you make this method return its argument r
Lei Zhang 2012/05/31 06:22:05 Done.
355 CHECK_LE(0, index);
Peter Kasting 2012/05/31 04:49:25 Nit: For inequalities other than NE, go ahead and
Lei Zhang 2012/05/31 06:22:05 Done. I always forget you only have to do that wit
356 CHECK_GT(items_.size(), static_cast<size_t>(index));
357 }
358
343 void SimpleMenuModel::AppendItem(const Item& item) { 359 void SimpleMenuModel::AppendItem(const Item& item) {
344 ValidateItem(item); 360 ValidateItem(item);
345 items_.push_back(item); 361 items_.push_back(item);
346 } 362 }
347 363
348 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) { 364 void SimpleMenuModel::InsertItemAtIndex(const Item& item, int index) {
349 ValidateItem(item); 365 ValidateItem(item);
350 items_.insert(items_.begin() + FlipIndex(index), item); 366 items_.insert(items_.begin() + FlipIndex(index), item);
351 } 367 }
352 368
353 void SimpleMenuModel::ValidateItem(const Item& item) { 369 void SimpleMenuModel::ValidateItem(const Item& item) {
354 #ifndef NDEBUG 370 #ifndef NDEBUG
355 if (item.type == TYPE_SEPARATOR) { 371 if (item.type == TYPE_SEPARATOR) {
356 DCHECK_EQ(item.command_id, kSeparatorId); 372 DCHECK_EQ(item.command_id, kSeparatorId);
357 } else { 373 } else {
358 DCHECK_GE(item.command_id, 0); 374 DCHECK_GE(item.command_id, 0);
359 } 375 }
360 #endif // NDEBUG 376 #endif // NDEBUG
361 } 377 }
362 378
363 } // namespace ui 379 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/models/simple_menu_model.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698