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

Side by Side Diff: chrome/browser/ui/app_list/app_list_syncable_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698