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

Unified Diff: ui/app_list/app_list_model.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, 11 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 side-by-side diff with in-line comments
Download patch
Index: ui/app_list/app_list_model.cc
diff --git a/ui/app_list/app_list_model.cc b/ui/app_list/app_list_model.cc
index c5515033e3d97b905e162d0d54f569eaf90e1333..246ed8bc3899fb5d33838cab73aec7817ff96318 100644
--- a/ui/app_list/app_list_model.cc
+++ b/ui/app_list/app_list_model.cc
@@ -4,6 +4,7 @@
#include "ui/app_list/app_list_model.h"
+#include "ui/app_list/app_list_folder_item.h"
#include "ui/app_list/app_list_item.h"
#include "ui/app_list/app_list_model_observer.h"
#include "ui/app_list/search_box_model.h"
@@ -16,9 +17,11 @@ AppListModel::AppListModel()
search_box_(new SearchBoxModel),
results_(new SearchResults),
status_(STATUS_NORMAL) {
+ item_list_->AddObserver(this);
}
AppListModel::~AppListModel() {
+ item_list_->RemoveObserver(this);
}
void AppListModel::AddObserver(AppListModelObserver* observer) {
@@ -39,4 +42,96 @@ void AppListModel::SetStatus(Status status) {
OnAppListModelStatusChanged());
}
+AppListItem* AppListModel::FindItem(const std::string& id) {
+ return item_list_->FindItem(id);
+}
+
+void AppListModel::AddItem(AppListItem* item) {
+ DCHECK(!item_list()->FindItem(item->id()));
+ AddItemToItemList(item);
+}
+
+const std::string& AppListModel::MergeItems(const std::string& target_item_id,
+ const std::string& source_item_id) {
+ // First, remove the source item from |item_list_|.
+ scoped_ptr<AppListItem> source_item_ptr =
+ item_list_->RemoveItem(source_item_id);
+
+ // Next, find the target item.
+ AppListItem* target_item = item_list_->FindItem(target_item_id);
+ DCHECK(target_item);
+
+ // If the target item is a folder, just add |source_item| to it.
+ if (target_item->GetItemType() == AppListFolderItem::kItemType) {
+ AppListFolderItem* target_folder =
+ static_cast<AppListFolderItem*>(target_item);
+ AddItemToFolder(target_folder, source_item_ptr.release());
+ return target_folder->id();
+ }
+
+ // Otherwise, remove the target item from |item_list_|, it will become owned
+ // by the new folder.
+ scoped_ptr<AppListItem> target_item_ptr =
+ item_list_->RemoveItem(target_item_id);
+
+ // Create a new folder in the same location as the target item.
+ AppListFolderItem* new_folder =
+ new AppListFolderItem(AppListFolderItem::GenerateId());
+ new_folder->set_position(target_item->position());
+ AddItemToItemList(new_folder);
+
+ // Add the items to the new folder.
+ AddItemToFolder(new_folder, target_item_ptr.release());
+ AddItemToFolder(new_folder, source_item_ptr.release());
+
+ return new_folder->id();
+}
+
+void AppListModel::SetItemPosition(AppListItem* item,
+ const syncer::StringOrdinal& new_position) {
+ item_list_->SetItemPosition(item, new_position);
+ // Note: this will trigger OnListItemMoved which will signal observers.
+ // (This is done this way because some View code still moves items within
+ // the item list directly).
+}
+
+void AppListModel::DeleteItem(const std::string& id) {
+ AppListItem* item = FindItem(id);
+ if (!item)
+ return;
+ FOR_EACH_OBSERVER(AppListModelObserver,
+ observers_,
+ OnAppListItemWillBeDeleted(item));
+ item_list_->DeleteItem(id);
+}
+
+// Private methods
+
+void AppListModel::OnListItemMoved(size_t from_index,
+ size_t to_index,
+ AppListItem* item) {
+ FOR_EACH_OBSERVER(AppListModelObserver,
+ observers_,
+ OnAppListItemUpdated(item));
+}
+
+syncer::StringOrdinal AppListModel::GetLastItemPosition() {
+ size_t count = item_list_->item_count();
+ if (count == 0)
+ return syncer::StringOrdinal::CreateInitialOrdinal();
+ return item_list_->item_at(count - 1)->position();
+}
+
+void AppListModel::AddItemToItemList(AppListItem* item) {
+ item_list_->AddItem(item);
+ FOR_EACH_OBSERVER(AppListModelObserver,
+ observers_,
+ OnAppListItemAdded(item));
+}
+
+void AppListModel::AddItemToFolder(AppListFolderItem* folder,
+ AppListItem* item) {
+ folder->item_list()->AddItem(item);
+}
+
} // namespace app_list

Powered by Google App Engine
This is Rietveld 408576698