Chromium Code Reviews| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 const char kDriveAppSyncIdPrefix[] = "drive-app-"; | 59 const char kDriveAppSyncIdPrefix[] = "drive-app-"; |
| 60 | 60 |
| 61 void UpdateSyncItemFromSync(const sync_pb::AppListSpecifics& specifics, | 61 void UpdateSyncItemFromSync(const sync_pb::AppListSpecifics& specifics, |
| 62 AppListSyncableService::SyncItem* item) { | 62 AppListSyncableService::SyncItem* item) { |
| 63 DCHECK_EQ(item->item_id, specifics.item_id()); | 63 DCHECK_EQ(item->item_id, specifics.item_id()); |
| 64 item->item_type = specifics.item_type(); | 64 item->item_type = specifics.item_type(); |
| 65 item->item_name = specifics.item_name(); | 65 item->item_name = specifics.item_name(); |
| 66 item->parent_id = specifics.parent_id(); | 66 item->parent_id = specifics.parent_id(); |
| 67 if (!specifics.item_ordinal().empty()) | 67 if (!specifics.item_ordinal().empty()) |
| 68 item->item_ordinal = syncer::StringOrdinal(specifics.item_ordinal()); | 68 item->item_ordinal = syncer::StringOrdinal(specifics.item_ordinal()); |
| 69 item->item_pin_ordinal = syncer::StringOrdinal(specifics.item_pin_ordinal()); | |
|
stevenjb
2016/06/14 22:44:08
An unfortunate side effect of this is that if an o
khmel
2016/06/15 17:01:19
That is great point. I missed this possibility, th
| |
| 69 } | 70 } |
| 70 | 71 |
| 71 bool UpdateSyncItemFromAppItem(const AppListItem* app_item, | 72 bool UpdateSyncItemFromAppItem(const AppListItem* app_item, |
| 72 AppListSyncableService::SyncItem* sync_item) { | 73 AppListSyncableService::SyncItem* sync_item) { |
| 73 DCHECK_EQ(sync_item->item_id, app_item->id()); | 74 DCHECK_EQ(sync_item->item_id, app_item->id()); |
| 74 bool changed = false; | 75 bool changed = false; |
| 75 if (app_list::switches::IsFolderUIEnabled() && | 76 if (app_list::switches::IsFolderUIEnabled() && |
| 76 sync_item->parent_id != app_item->folder_id()) { | 77 sync_item->parent_id != app_item->folder_id()) { |
| 77 sync_item->parent_id = app_item->folder_id(); | 78 sync_item->parent_id = app_item->folder_id(); |
| 78 changed = true; | 79 changed = true; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 89 return changed; | 90 return changed; |
| 90 } | 91 } |
| 91 | 92 |
| 92 void GetSyncSpecificsFromSyncItem(const AppListSyncableService::SyncItem* item, | 93 void GetSyncSpecificsFromSyncItem(const AppListSyncableService::SyncItem* item, |
| 93 sync_pb::AppListSpecifics* specifics) { | 94 sync_pb::AppListSpecifics* specifics) { |
| 94 DCHECK(specifics); | 95 DCHECK(specifics); |
| 95 specifics->set_item_id(item->item_id); | 96 specifics->set_item_id(item->item_id); |
| 96 specifics->set_item_type(item->item_type); | 97 specifics->set_item_type(item->item_type); |
| 97 specifics->set_item_name(item->item_name); | 98 specifics->set_item_name(item->item_name); |
| 98 specifics->set_parent_id(item->parent_id); | 99 specifics->set_parent_id(item->parent_id); |
| 99 if (item->item_ordinal.IsValid()) | 100 specifics->set_item_ordinal(item->item_ordinal.IsValid() ? |
| 100 specifics->set_item_ordinal(item->item_ordinal.ToInternalValue()); | 101 item->item_ordinal.ToInternalValue() : std::string()); |
| 102 specifics->set_item_pin_ordinal(item->item_pin_ordinal.IsValid() ? | |
| 103 item->item_pin_ordinal.ToInternalValue() : std::string()); | |
| 101 } | 104 } |
| 102 | 105 |
| 103 syncer::SyncData GetSyncDataFromSyncItem( | 106 syncer::SyncData GetSyncDataFromSyncItem( |
| 104 const AppListSyncableService::SyncItem* item) { | 107 const AppListSyncableService::SyncItem* item) { |
| 105 sync_pb::EntitySpecifics specifics; | 108 sync_pb::EntitySpecifics specifics; |
| 106 GetSyncSpecificsFromSyncItem(item, specifics.mutable_app_list()); | 109 GetSyncSpecificsFromSyncItem(item, specifics.mutable_app_list()); |
| 107 return syncer::SyncData::CreateLocalData(item->item_id, | 110 return syncer::SyncData::CreateLocalData(item->item_id, |
| 108 item->item_id, | 111 item->item_id, |
| 109 specifics); | 112 specifics); |
| 110 } | 113 } |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 #endif | 294 #endif |
| 292 } | 295 } |
| 293 | 296 |
| 294 model_pref_updater_.reset( | 297 model_pref_updater_.reset( |
| 295 new ModelPrefUpdater(AppListPrefs::Get(profile_), model_.get())); | 298 new ModelPrefUpdater(AppListPrefs::Get(profile_), model_.get())); |
| 296 | 299 |
| 297 if (app_list::switches::IsDriveAppsInAppListEnabled()) | 300 if (app_list::switches::IsDriveAppsInAppListEnabled()) |
| 298 drive_app_provider_.reset(new DriveAppProvider(profile_, this)); | 301 drive_app_provider_.reset(new DriveAppProvider(profile_, this)); |
| 299 } | 302 } |
| 300 | 303 |
| 304 void AppListSyncableService::AddObserverAndStart(Observer* observer) { | |
| 305 observer_list_.AddObserver(observer); | |
| 306 SyncStarted(); | |
| 307 } | |
| 308 | |
| 309 void AppListSyncableService::RemoveObserver(Observer* observer) { | |
| 310 observer_list_.RemoveObserver(observer); | |
| 311 } | |
| 312 | |
| 313 void AppListSyncableService::NotifyObserversSyncUpdated() { | |
| 314 FOR_EACH_OBSERVER(Observer, observer_list_, OnSyncModelUpdated()); | |
| 315 } | |
| 316 | |
| 301 size_t AppListSyncableService::GetNumSyncItemsForTest() { | 317 size_t AppListSyncableService::GetNumSyncItemsForTest() { |
| 302 // If the model isn't built yet, there will be no sync items. | 318 // If the model isn't built yet, there will be no sync items. |
| 303 GetModel(); | 319 GetModel(); |
| 304 | 320 |
| 305 return sync_items_.size(); | 321 return sync_items_.size(); |
| 306 } | 322 } |
| 307 | 323 |
| 308 void AppListSyncableService::ResetDriveAppProviderForTest() { | 324 void AppListSyncableService::ResetDriveAppProviderForTest() { |
| 309 drive_app_provider_.reset(); | 325 drive_app_provider_.reset(); |
| 310 } | 326 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 sync_pb::AppListSpecifics::AppListItemType type; | 431 sync_pb::AppListSpecifics::AppListItemType type; |
| 416 if (!GetAppListItemType(app_item, &type)) | 432 if (!GetAppListItemType(app_item, &type)) |
| 417 return NULL; | 433 return NULL; |
| 418 VLOG(2) << this << " CreateSyncItemFromAppItem:" << app_item->ToDebugString(); | 434 VLOG(2) << this << " CreateSyncItemFromAppItem:" << app_item->ToDebugString(); |
| 419 SyncItem* sync_item = CreateSyncItem(app_item->id(), type); | 435 SyncItem* sync_item = CreateSyncItem(app_item->id(), type); |
| 420 UpdateSyncItemFromAppItem(app_item, sync_item); | 436 UpdateSyncItemFromAppItem(app_item, sync_item); |
| 421 SendSyncChange(sync_item, SyncChange::ACTION_ADD); | 437 SendSyncChange(sync_item, SyncChange::ACTION_ADD); |
| 422 return sync_item; | 438 return sync_item; |
| 423 } | 439 } |
| 424 | 440 |
| 441 syncer::StringOrdinal AppListSyncableService::GetPinPosition( | |
| 442 const std::string& app_id) { | |
| 443 SyncItem* sync_item = FindSyncItem(app_id); | |
| 444 if (!sync_item) | |
| 445 return syncer::StringOrdinal(); | |
| 446 return sync_item->item_pin_ordinal; | |
| 447 } | |
| 448 | |
| 449 void AppListSyncableService::SetPinPosition( | |
| 450 const std::string& app_id, | |
| 451 const syncer::StringOrdinal& item_pin_ordinal) { | |
| 452 SyncItem* sync_item = FindSyncItem(app_id); | |
| 453 SyncChange::SyncChangeType sync_change_type; | |
| 454 if (sync_item) { | |
| 455 sync_change_type = SyncChange::ACTION_UPDATE; | |
| 456 } else { | |
| 457 sync_item = CreateSyncItem(app_id, sync_pb::AppListSpecifics::TYPE_APP); | |
| 458 sync_change_type = SyncChange::ACTION_ADD; | |
| 459 } | |
| 460 | |
| 461 sync_item->item_pin_ordinal = item_pin_ordinal; | |
| 462 SendSyncChange(sync_item, sync_change_type); | |
| 463 } | |
| 464 | |
| 425 void AppListSyncableService::AddOrUpdateFromSyncItem(AppListItem* app_item) { | 465 void AppListSyncableService::AddOrUpdateFromSyncItem(AppListItem* app_item) { |
| 426 // Do not create a sync item for the OEM folder here, do that in | 466 // Do not create a sync item for the OEM folder here, do that in |
| 427 // ResolveFolderPositions once the position has been resolved. | 467 // ResolveFolderPositions once the position has been resolved. |
| 428 if (app_item->id() == kOemFolderId) | 468 if (app_item->id() == kOemFolderId) |
| 429 return; | 469 return; |
| 430 | 470 |
| 431 SyncItem* sync_item = FindSyncItem(app_item->id()); | 471 SyncItem* sync_item = FindSyncItem(app_item->id()); |
| 432 if (sync_item) { | 472 if (sync_item) { |
| 433 UpdateAppItemFromSyncItem(sync_item, app_item); | 473 UpdateAppItemFromSyncItem(sync_item, app_item); |
| 434 return; | 474 return; |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 661 } | 701 } |
| 662 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); | 702 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
| 663 | 703 |
| 664 // Adding items may have created folders without setting their positions | 704 // Adding items may have created folders without setting their positions |
| 665 // since we haven't started observing the item list yet. Resolve those. | 705 // since we haven't started observing the item list yet. Resolve those. |
| 666 ResolveFolderPositions(); | 706 ResolveFolderPositions(); |
| 667 | 707 |
| 668 // Start observing app list model changes. | 708 // Start observing app list model changes. |
| 669 model_observer_.reset(new ModelObserver(this)); | 709 model_observer_.reset(new ModelObserver(this)); |
| 670 | 710 |
| 711 NotifyObserversSyncUpdated(); | |
| 712 | |
| 671 return result; | 713 return result; |
| 672 } | 714 } |
| 673 | 715 |
| 674 void AppListSyncableService::StopSyncing(syncer::ModelType type) { | 716 void AppListSyncableService::StopSyncing(syncer::ModelType type) { |
| 675 DCHECK_EQ(type, syncer::APP_LIST); | 717 DCHECK_EQ(type, syncer::APP_LIST); |
| 676 | 718 |
| 677 sync_processor_.reset(); | 719 sync_processor_.reset(); |
| 678 sync_error_handler_.reset(); | 720 sync_error_handler_.reset(); |
| 679 model_->SetFoldersEnabled(false); | 721 model_->SetFoldersEnabled(false); |
| 680 } | 722 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 719 } else if (change.change_type() == SyncChange::ACTION_DELETE) { | 761 } else if (change.change_type() == SyncChange::ACTION_DELETE) { |
| 720 DeleteSyncItemSpecifics(change.sync_data().GetSpecifics().app_list()); | 762 DeleteSyncItemSpecifics(change.sync_data().GetSpecifics().app_list()); |
| 721 } else { | 763 } else { |
| 722 LOG(ERROR) << "Invalid sync change"; | 764 LOG(ERROR) << "Invalid sync change"; |
| 723 } | 765 } |
| 724 } | 766 } |
| 725 | 767 |
| 726 // Continue observing app list model changes. | 768 // Continue observing app list model changes. |
| 727 model_observer_.reset(new ModelObserver(this)); | 769 model_observer_.reset(new ModelObserver(this)); |
| 728 | 770 |
| 771 NotifyObserversSyncUpdated(); | |
| 772 | |
| 729 return syncer::SyncError(); | 773 return syncer::SyncError(); |
| 730 } | 774 } |
| 731 | 775 |
| 732 // AppListSyncableService private | 776 // AppListSyncableService private |
| 733 | 777 |
| 734 bool AppListSyncableService::ProcessSyncItemSpecifics( | 778 bool AppListSyncableService::ProcessSyncItemSpecifics( |
| 735 const sync_pb::AppListSpecifics& specifics) { | 779 const sync_pb::AppListSpecifics& specifics) { |
| 736 const std::string& item_id = specifics.item_id(); | 780 const std::string& item_id = specifics.item_id(); |
| 737 if (item_id.empty()) { | 781 if (item_id.empty()) { |
| 738 LOG(ERROR) << "AppList item with empty ID"; | 782 LOG(ERROR) << "AppList item with empty ID"; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 827 VLOG(2) << " Moving Item To Folder: " << sync_item->parent_id; | 871 VLOG(2) << " Moving Item To Folder: " << sync_item->parent_id; |
| 828 model_->MoveItemToFolder(app_item, sync_item->parent_id); | 872 model_->MoveItemToFolder(app_item, sync_item->parent_id); |
| 829 } | 873 } |
| 830 UpdateAppItemFromSyncItem(sync_item, app_item); | 874 UpdateAppItemFromSyncItem(sync_item, app_item); |
| 831 } | 875 } |
| 832 | 876 |
| 833 void AppListSyncableService::UpdateAppItemFromSyncItem( | 877 void AppListSyncableService::UpdateAppItemFromSyncItem( |
| 834 const AppListSyncableService::SyncItem* sync_item, | 878 const AppListSyncableService::SyncItem* sync_item, |
| 835 AppListItem* app_item) { | 879 AppListItem* app_item) { |
| 836 VLOG(2) << this << " UpdateAppItemFromSyncItem: " << sync_item->ToString(); | 880 VLOG(2) << this << " UpdateAppItemFromSyncItem: " << sync_item->ToString(); |
| 837 if (!app_item->position().Equals(sync_item->item_ordinal)) | 881 if (sync_item->item_ordinal.IsValid() && |
| 882 !app_item->position().Equals(sync_item->item_ordinal)) { | |
| 838 model_->SetItemPosition(app_item, sync_item->item_ordinal); | 883 model_->SetItemPosition(app_item, sync_item->item_ordinal); |
| 884 } | |
| 839 // Only update the item name if it is a Folder or the name is empty. | 885 // Only update the item name if it is a Folder or the name is empty. |
| 840 if (sync_item->item_name != app_item->name() && | 886 if (sync_item->item_name != app_item->name() && |
| 841 sync_item->item_id != kOemFolderId && | 887 sync_item->item_id != kOemFolderId && |
| 842 (app_item->GetItemType() == AppListFolderItem::kItemType || | 888 (app_item->GetItemType() == AppListFolderItem::kItemType || |
| 843 app_item->name().empty())) { | 889 app_item->name().empty())) { |
| 844 model_->SetItemName(app_item, sync_item->item_name); | 890 model_->SetItemName(app_item, sync_item->item_name); |
| 845 } | 891 } |
| 846 } | 892 } |
| 847 | 893 |
| 848 bool AppListSyncableService::SyncStarted() { | 894 bool AppListSyncableService::SyncStarted() { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1006 | 1052 |
| 1007 std::string AppListSyncableService::SyncItem::ToString() const { | 1053 std::string AppListSyncableService::SyncItem::ToString() const { |
| 1008 std::string res = item_id.substr(0, 8); | 1054 std::string res = item_id.substr(0, 8); |
| 1009 if (item_type == sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) { | 1055 if (item_type == sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) { |
| 1010 res += " { RemoveDefault }"; | 1056 res += " { RemoveDefault }"; |
| 1011 } else { | 1057 } else { |
| 1012 res += " { " + item_name + " }"; | 1058 res += " { " + item_name + " }"; |
| 1013 res += " [" + item_ordinal.ToDebugString() + "]"; | 1059 res += " [" + item_ordinal.ToDebugString() + "]"; |
| 1014 if (!parent_id.empty()) | 1060 if (!parent_id.empty()) |
| 1015 res += " <" + parent_id.substr(0, 8) + ">"; | 1061 res += " <" + parent_id.substr(0, 8) + ">"; |
| 1062 res += " [" + item_pin_ordinal.ToDebugString() + "]"; | |
| 1016 } | 1063 } |
| 1017 return res; | 1064 return res; |
| 1018 } | 1065 } |
| 1019 | 1066 |
| 1020 } // namespace app_list | 1067 } // namespace app_list |
| OLD | NEW |