OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/ui/app_list/app_list_syncable_service.h" | 5 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/apps/drive/drive_app_provider.h" | 8 #include "chrome/browser/apps/drive/drive_app_provider.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 } | 298 } |
299 | 299 |
300 void AppListSyncableService::AddItem(scoped_ptr<AppListItem> app_item) { | 300 void AppListSyncableService::AddItem(scoped_ptr<AppListItem> app_item) { |
301 SyncItem* sync_item = FindOrAddSyncItem(app_item.get()); | 301 SyncItem* sync_item = FindOrAddSyncItem(app_item.get()); |
302 if (!sync_item) | 302 if (!sync_item) |
303 return; // Item is not valid. | 303 return; // Item is not valid. |
304 | 304 |
305 std::string folder_id; | 305 std::string folder_id; |
306 if (app_list::switches::IsFolderUIEnabled()) { | 306 if (app_list::switches::IsFolderUIEnabled()) { |
307 if (AppIsOem(app_item->id())) { | 307 if (AppIsOem(app_item->id())) { |
308 folder_id = FindOrCreateOemFolder(app_item->id()); | 308 folder_id = FindOrCreateOemFolder(); |
309 VLOG_IF(2, !folder_id.empty()) | 309 VLOG_IF(2, !folder_id.empty()) |
310 << this << ": AddItem to OEM folder: " << sync_item->ToString(); | 310 << this << ": AddItem to OEM folder: " << sync_item->ToString(); |
311 } else { | 311 } else { |
312 folder_id = sync_item->parent_id; | 312 folder_id = sync_item->parent_id; |
313 } | 313 } |
314 } | 314 } |
315 VLOG(2) << this << ": AddItem: " << sync_item->ToString() | 315 VLOG(2) << this << ": AddItem: " << sync_item->ToString() |
316 << " Folder: '" << folder_id << "'"; | 316 << " Folder: '" << folder_id << "'"; |
317 model_->AddItemToFolder(app_item.Pass(), folder_id); | 317 model_->AddItemToFolder(app_item.Pass(), folder_id); |
318 } | 318 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 iter != sync_items_.end(); ++iter) { | 469 iter != sync_items_.end(); ++iter) { |
470 SyncItem* sync_item = iter->second; | 470 SyncItem* sync_item = iter->second; |
471 if (sync_item->item_type != sync_pb::AppListSpecifics::TYPE_FOLDER) | 471 if (sync_item->item_type != sync_pb::AppListSpecifics::TYPE_FOLDER) |
472 continue; | 472 continue; |
473 AppListItem* app_item = model_->FindItem(sync_item->item_id); | 473 AppListItem* app_item = model_->FindItem(sync_item->item_id); |
474 if (!app_item) | 474 if (!app_item) |
475 continue; | 475 continue; |
476 UpdateAppItemFromSyncItem(sync_item, app_item); | 476 UpdateAppItemFromSyncItem(sync_item, app_item); |
477 } | 477 } |
478 | 478 |
479 // Create the OEM folder if necessary. | 479 // Move the OEM folder if one exists and we have not synced its position. |
480 if (oem_folder_item_ids_.empty()) | 480 AppListFolderItem* oem_folder = model_->FindFolderItem(kOemFolderId); |
481 return; | 481 if (oem_folder && !FindSyncItem(kOemFolderId)) { |
482 std::string oem_folder_id = FindOrCreateOemFolder(""); | 482 model_->SetItemPosition(oem_folder, GetOemFolderPos()); |
483 DCHECK(!oem_folder_id.empty()); | 483 DVLOG(1) << "Creating new OEM folder sync item: " |
484 for (std::vector<std::string>::iterator iter = oem_folder_item_ids_.begin(); | 484 << oem_folder->position().ToDebugString(); |
485 iter != oem_folder_item_ids_.end(); ++iter) { | 485 CreateSyncItemFromAppItem(oem_folder); |
486 AppListItem* app_item = model_->FindItem(*iter); | |
487 if (!app_item) | |
488 continue; | |
489 VLOG(2) << this << ": MoveItem to OEM folder: " | |
490 << app_item->ToDebugString(); | |
491 model_->MoveItemToFolder(app_item, oem_folder_id); | |
492 } | 486 } |
493 } | 487 } |
494 | 488 |
495 void AppListSyncableService::PruneEmptySyncFolders() { | 489 void AppListSyncableService::PruneEmptySyncFolders() { |
496 if (!app_list::switches::IsFolderUIEnabled()) | 490 if (!app_list::switches::IsFolderUIEnabled()) |
497 return; | 491 return; |
498 | 492 |
499 std::set<std::string> parent_ids; | 493 std::set<std::string> parent_ids; |
500 for (SyncItemMap::iterator iter = sync_items_.begin(); | 494 for (SyncItemMap::iterator iter = sync_items_.begin(); |
501 iter != sync_items_.end(); ++iter) { | 495 iter != sync_items_.end(); ++iter) { |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 iter->second->item_type; | 816 iter->second->item_type; |
823 VLOG(2) << this << " <- SYNC DELETE: " << iter->second->ToString(); | 817 VLOG(2) << this << " <- SYNC DELETE: " << iter->second->ToString(); |
824 delete iter->second; | 818 delete iter->second; |
825 sync_items_.erase(iter); | 819 sync_items_.erase(iter); |
826 // Only delete apps from the model. Folders will be deleted when all | 820 // Only delete apps from the model. Folders will be deleted when all |
827 // children have been deleted. | 821 // children have been deleted. |
828 if (item_type == sync_pb::AppListSpecifics::TYPE_APP) | 822 if (item_type == sync_pb::AppListSpecifics::TYPE_APP) |
829 model_->DeleteItem(item_id); | 823 model_->DeleteItem(item_id); |
830 } | 824 } |
831 | 825 |
832 std::string AppListSyncableService::FindOrCreateOemFolder( | 826 std::string AppListSyncableService::FindOrCreateOemFolder() { |
833 const std::string& item_id) { | |
834 AppListFolderItem* oem_folder = model_->FindFolderItem(kOemFolderId); | 827 AppListFolderItem* oem_folder = model_->FindFolderItem(kOemFolderId); |
835 if (!oem_folder) { | 828 if (!oem_folder) { |
836 if (!SyncStarted()) { | |
837 // We need sync to have started to know where to place the OEM folder. | |
838 // If sync is not started, add |item_id| (which should never be empty | |
839 // when called before sync is started) to oem_folder_item_ids_. | |
840 DCHECK(!item_id.empty()); | |
841 oem_folder_item_ids_.push_back(item_id); | |
842 return ""; | |
843 } | |
844 scoped_ptr<AppListFolderItem> new_folder(new AppListFolderItem( | 829 scoped_ptr<AppListFolderItem> new_folder(new AppListFolderItem( |
845 kOemFolderId, AppListFolderItem::FOLDER_TYPE_OEM)); | 830 kOemFolderId, AppListFolderItem::FOLDER_TYPE_OEM)); |
846 oem_folder = static_cast<AppListFolderItem*>( | 831 oem_folder = static_cast<AppListFolderItem*>( |
847 model_->AddItem(new_folder.PassAs<app_list::AppListItem>())); | 832 model_->AddItem(new_folder.PassAs<app_list::AppListItem>())); |
848 SyncItem* oem_sync_item = FindSyncItem(kOemFolderId); | 833 SyncItem* oem_sync_item = FindSyncItem(kOemFolderId); |
849 if (oem_sync_item) { | 834 if (oem_sync_item) { |
850 DVLOG(1) << "Creating OEM folder from existing sync item: " | 835 DVLOG(1) << "Creating OEM folder from existing sync item: " |
851 << oem_sync_item->item_ordinal.ToDebugString(); | 836 << oem_sync_item->item_ordinal.ToDebugString(); |
852 model_->SetItemPosition(oem_folder, oem_sync_item->item_ordinal); | 837 model_->SetItemPosition(oem_folder, oem_sync_item->item_ordinal); |
853 } else { | 838 } else { |
854 model_->SetItemPosition(oem_folder, GetOemFolderPos()); | 839 model_->SetItemPosition(oem_folder, GetOemFolderPos()); |
855 DVLOG(1) << "Creating new OEM folder sync item: " | 840 // Do not create a sync item for the OEM folder here, do it in |
856 << oem_folder->position().ToDebugString(); | 841 // ResolveFolderPositions() when the item position is finalized. |
857 CreateSyncItemFromAppItem(oem_folder); | |
858 } | 842 } |
859 } | 843 } |
860 model_->SetItemName(oem_folder, oem_folder_name_); | 844 model_->SetItemName(oem_folder, oem_folder_name_); |
861 return oem_folder->id(); | 845 return oem_folder->id(); |
862 } | 846 } |
863 | 847 |
864 syncer::StringOrdinal AppListSyncableService::GetOemFolderPos() { | 848 syncer::StringOrdinal AppListSyncableService::GetOemFolderPos() { |
865 VLOG(1) << "GetOemFolderPos: " << first_app_list_sync_; | 849 VLOG(1) << "GetOemFolderPos: " << first_app_list_sync_; |
866 if (!first_app_list_sync_) { | 850 if (!first_app_list_sync_) { |
867 DVLOG(1) << "Sync items exist, placing OEM folder at end."; | 851 DVLOG(1) << "Sync items exist, placing OEM folder at end."; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 } else { | 901 } else { |
918 res += " { " + item_name + " }"; | 902 res += " { " + item_name + " }"; |
919 res += " [" + item_ordinal.ToDebugString() + "]"; | 903 res += " [" + item_ordinal.ToDebugString() + "]"; |
920 if (!parent_id.empty()) | 904 if (!parent_id.empty()) |
921 res += " <" + parent_id.substr(0, 8) + ">"; | 905 res += " <" + parent_id.substr(0, 8) + ">"; |
922 } | 906 } |
923 return res; | 907 return res; |
924 } | 908 } |
925 | 909 |
926 } // namespace app_list | 910 } // namespace app_list |
OLD | NEW |