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

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

Issue 600393002: AppListModel / AppsGridView: Better error handling for corner cases. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@appsgridview-static-casts
Patch Set: Created 6 years, 2 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
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/app_list/app_list_model.h" 5 #include "ui/app_list/app_list_model.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "ui/app_list/app_list_folder_item.h" 9 #include "ui/app_list/app_list_folder_item.h"
10 #include "ui/app_list/app_list_item.h" 10 #include "ui/app_list/app_list_item.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 return AddItemToFolderItemAndNotify(dest_folder, item.Pass()); 84 return AddItemToFolderItemAndNotify(dest_folder, item.Pass());
85 } 85 }
86 86
87 const std::string AppListModel::MergeItems(const std::string& target_item_id, 87 const std::string AppListModel::MergeItems(const std::string& target_item_id,
88 const std::string& source_item_id) { 88 const std::string& source_item_id) {
89 if (!folders_enabled()) { 89 if (!folders_enabled()) {
90 LOG(ERROR) << "MergeItems called with folders disabled."; 90 LOG(ERROR) << "MergeItems called with folders disabled.";
91 return ""; 91 return "";
92 } 92 }
93 DVLOG(2) << "MergeItems: " << source_item_id << " -> " << target_item_id; 93 DVLOG(2) << "MergeItems: " << source_item_id << " -> " << target_item_id;
94
95 if (target_item_id == source_item_id) {
96 LOG(WARNING) << "MergeItems tried to drop item onto itself ("
calamity 2014/09/26 03:55:18 These can probably stand to be DCHECKs.
Matt Giuca 2014/09/29 03:05:03 Hmm. If this is a DCHECK and the one in apps_grid_
Matt Giuca 2014/09/29 03:34:34 As discussed, it's reasonable to leave the model's
97 << source_item_id << " -> " << target_item_id << ").";
98 return "";
99 }
100
94 // Find the target item. 101 // Find the target item.
95 AppListItem* target_item = FindItem(target_item_id); 102 AppListItem* target_item = top_level_item_list_->FindItem(target_item_id);
96 if (!target_item) { 103 if (!target_item) {
97 LOG(ERROR) << "MergeItems: Target no longer exists."; 104 LOG(ERROR) << "MergeItems: Target no longer exists.";
98 return ""; 105 return "";
99 } 106 }
100 CHECK(target_item->folder_id().empty());
101 107
102 AppListItem* source_item = FindItem(source_item_id); 108 AppListItem* source_item = FindItem(source_item_id);
103 if (!source_item) { 109 if (!source_item) {
104 LOG(ERROR) << "MergeItems: Source no longer exists."; 110 LOG(ERROR) << "MergeItems: Source no longer exists.";
105 return ""; 111 return "";
106 } 112 }
107 113
108 // If the target item is a folder, just add the source item to it. 114 // If the target item is a folder, just add the source item to it.
109 if (target_item->GetItemType() == AppListFolderItem::kItemType) { 115 if (target_item->GetItemType() == AppListFolderItem::kItemType) {
110 AppListFolderItem* target_folder = 116 AppListFolderItem* target_folder =
111 static_cast<AppListFolderItem*>(target_item); 117 static_cast<AppListFolderItem*>(target_item);
112 if (target_folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) { 118 if (target_folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) {
113 LOG(WARNING) << "MergeItems called with OEM folder as target"; 119 LOG(WARNING) << "MergeItems called with OEM folder as target";
114 return ""; 120 return "";
115 } 121 }
116 scoped_ptr<AppListItem> source_item_ptr = RemoveItem(source_item); 122 scoped_ptr<AppListItem> source_item_ptr = RemoveItem(source_item);
117 source_item_ptr->set_position( 123 source_item_ptr->set_position(
118 target_folder->item_list()->CreatePositionBefore( 124 target_folder->item_list()->CreatePositionBefore(
119 syncer::StringOrdinal())); 125 syncer::StringOrdinal()));
120 AddItemToFolderItemAndNotify(target_folder, source_item_ptr.Pass()); 126 AddItemToFolderItemAndNotify(target_folder, source_item_ptr.Pass());
121 return target_folder->id(); 127 return target_folder->id();
122 } 128 }
123 129
124 // Otherwise remove the source item and target item from their current 130 // Otherwise remove the source item and target item from their current
125 // location, they will become owned by the new folder. 131 // location, they will become owned by the new folder.
126 scoped_ptr<AppListItem> source_item_ptr = RemoveItem(source_item); 132 scoped_ptr<AppListItem> source_item_ptr = RemoveItem(source_item);
127 CHECK(source_item_ptr); 133 CHECK(source_item_ptr);
134 // Note: This would fail if |target_item_id == source_item_id|, except we
135 // checked that they are distinct at the top of this method.
128 scoped_ptr<AppListItem> target_item_ptr = 136 scoped_ptr<AppListItem> target_item_ptr =
129 top_level_item_list_->RemoveItem(target_item_id); 137 top_level_item_list_->RemoveItem(target_item_id);
130 CHECK(target_item_ptr); 138 CHECK(target_item_ptr);
131 139
132 // Create a new folder in the same location as the target item. 140 // Create a new folder in the same location as the target item.
133 std::string new_folder_id = AppListFolderItem::GenerateId(); 141 std::string new_folder_id = AppListFolderItem::GenerateId();
134 DVLOG(2) << "Creating folder for merge: " << new_folder_id; 142 DVLOG(2) << "Creating folder for merge: " << new_folder_id;
135 scoped_ptr<AppListItem> new_folder_ptr(new AppListFolderItem( 143 scoped_ptr<AppListItem> new_folder_ptr(new AppListFolderItem(
136 new_folder_id, AppListFolderItem::FOLDER_TYPE_NORMAL)); 144 new_folder_id, AppListFolderItem::FOLDER_TYPE_NORMAL));
137 new_folder_ptr->set_position(target_item_ptr->position()); 145 new_folder_ptr->set_position(target_item_ptr->position());
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 scoped_ptr<AppListItem> result = folder->item_list()->RemoveItem(item->id()); 392 scoped_ptr<AppListItem> result = folder->item_list()->RemoveItem(item->id());
385 result->set_folder_id(""); 393 result->set_folder_id("");
386 if (folder->item_list()->item_count() == 0) { 394 if (folder->item_list()->item_count() == 0) {
387 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString(); 395 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString();
388 DeleteItem(folder_id); 396 DeleteItem(folder_id);
389 } 397 }
390 return result.Pass(); 398 return result.Pass();
391 } 399 }
392 400
393 } // namespace app_list 401 } // namespace app_list
OLDNEW
« no previous file with comments | « no previous file | ui/app_list/app_list_model_unittest.cc » ('j') | ui/app_list/views/apps_grid_view.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698