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

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: 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"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "chrome/browser/apps/drive/drive_app_provider.h" 13 #include "chrome/browser/apps/drive/drive_app_provider.h"
14 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/app_list/app_list_prefs.h" 16 #include "chrome/browser/ui/app_list/app_list_prefs.h"
17 #include "chrome/browser/ui/app_list/app_list_service.h" 17 #include "chrome/browser/ui/app_list/app_list_service.h"
18 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
18 #include "chrome/browser/ui/app_list/extension_app_item.h" 19 #include "chrome/browser/ui/app_list/extension_app_item.h"
19 #include "chrome/browser/ui/app_list/extension_app_model_builder.h" 20 #include "chrome/browser/ui/app_list/extension_app_model_builder.h"
20 #include "chrome/browser/ui/app_list/model_pref_updater.h" 21 #include "chrome/browser/ui/app_list/model_pref_updater.h"
21 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/extensions/extension_constants.h" 23 #include "chrome/common/extensions/extension_constants.h"
23 #include "chrome/grit/generated_resources.h" 24 #include "chrome/grit/generated_resources.h"
24 #include "extensions/browser/extension_prefs.h" 25 #include "extensions/browser/extension_prefs.h"
25 #include "extensions/browser/extension_system.h" 26 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/uninstall_reason.h" 27 #include "extensions/browser/uninstall_reason.h"
27 #include "extensions/common/constants.h" 28 #include "extensions/common/constants.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 const char kDriveAppSyncIdPrefix[] = "drive-app-"; 60 const char kDriveAppSyncIdPrefix[] = "drive-app-";
60 61
61 void UpdateSyncItemFromSync(const sync_pb::AppListSpecifics& specifics, 62 void UpdateSyncItemFromSync(const sync_pb::AppListSpecifics& specifics,
62 AppListSyncableService::SyncItem* item) { 63 AppListSyncableService::SyncItem* item) {
63 DCHECK_EQ(item->item_id, specifics.item_id()); 64 DCHECK_EQ(item->item_id, specifics.item_id());
64 item->item_type = specifics.item_type(); 65 item->item_type = specifics.item_type();
65 item->item_name = specifics.item_name(); 66 item->item_name = specifics.item_name();
66 item->parent_id = specifics.parent_id(); 67 item->parent_id = specifics.parent_id();
67 if (!specifics.item_ordinal().empty()) 68 if (!specifics.item_ordinal().empty())
68 item->item_ordinal = syncer::StringOrdinal(specifics.item_ordinal()); 69 item->item_ordinal = syncer::StringOrdinal(specifics.item_ordinal());
70 item->item_pin_ordinal = syncer::StringOrdinal(specifics.item_pin_ordinal());
71 item->item_pin_by_policy = specifics.item_pin_by_policy();
69 } 72 }
70 73
71 bool UpdateSyncItemFromAppItem(const AppListItem* app_item, 74 bool UpdateSyncItemFromAppItem(const AppListItem* app_item,
72 AppListSyncableService::SyncItem* sync_item) { 75 AppListSyncableService::SyncItem* sync_item) {
73 DCHECK_EQ(sync_item->item_id, app_item->id()); 76 DCHECK_EQ(sync_item->item_id, app_item->id());
74 bool changed = false; 77 bool changed = false;
75 if (app_list::switches::IsFolderUIEnabled() && 78 if (app_list::switches::IsFolderUIEnabled() &&
76 sync_item->parent_id != app_item->folder_id()) { 79 sync_item->parent_id != app_item->folder_id()) {
77 sync_item->parent_id = app_item->folder_id(); 80 sync_item->parent_id = app_item->folder_id();
78 changed = true; 81 changed = true;
(...skipping 12 matching lines...) Expand all
91 94
92 void GetSyncSpecificsFromSyncItem(const AppListSyncableService::SyncItem* item, 95 void GetSyncSpecificsFromSyncItem(const AppListSyncableService::SyncItem* item,
93 sync_pb::AppListSpecifics* specifics) { 96 sync_pb::AppListSpecifics* specifics) {
94 DCHECK(specifics); 97 DCHECK(specifics);
95 specifics->set_item_id(item->item_id); 98 specifics->set_item_id(item->item_id);
96 specifics->set_item_type(item->item_type); 99 specifics->set_item_type(item->item_type);
97 specifics->set_item_name(item->item_name); 100 specifics->set_item_name(item->item_name);
98 specifics->set_parent_id(item->parent_id); 101 specifics->set_parent_id(item->parent_id);
99 if (item->item_ordinal.IsValid()) 102 if (item->item_ordinal.IsValid())
100 specifics->set_item_ordinal(item->item_ordinal.ToInternalValue()); 103 specifics->set_item_ordinal(item->item_ordinal.ToInternalValue());
104 specifics->set_item_pin_ordinal(item->item_pin_ordinal.IsValid() ?
105 item->item_pin_ordinal.ToInternalValue() : std::string());
106 specifics->set_item_pin_by_policy(item->item_pin_by_policy);
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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 172 }
167 173
168 } // namespace 174 } // namespace
169 175
170 // AppListSyncableService::SyncItem 176 // AppListSyncableService::SyncItem
171 177
172 AppListSyncableService::SyncItem::SyncItem( 178 AppListSyncableService::SyncItem::SyncItem(
173 const std::string& id, 179 const std::string& id,
174 sync_pb::AppListSpecifics::AppListItemType type) 180 sync_pb::AppListSpecifics::AppListItemType type)
175 : item_id(id), 181 : item_id(id),
176 item_type(type) { 182 item_type(type),
183 item_pin_by_policy(false) {
177 } 184 }
178 185
179 AppListSyncableService::SyncItem::~SyncItem() { 186 AppListSyncableService::SyncItem::~SyncItem() {
180 } 187 }
181 188
182 // AppListSyncableService::ModelObserver 189 // AppListSyncableService::ModelObserver
183 190
184 class AppListSyncableService::ModelObserver : public AppListModelObserver { 191 class AppListSyncableService::ModelObserver : public AppListModelObserver {
185 public: 192 public:
186 explicit ModelObserver(AppListSyncableService* owner) 193 explicit ModelObserver(AppListSyncableService* owner)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 } 235 }
229 236
230 AppListSyncableService* owner_; 237 AppListSyncableService* owner_;
231 AppListItem* adding_item_; // Unowned pointer to item being added. 238 AppListItem* adding_item_; // Unowned pointer to item being added.
232 239
233 DISALLOW_COPY_AND_ASSIGN(ModelObserver); 240 DISALLOW_COPY_AND_ASSIGN(ModelObserver);
234 }; 241 };
235 242
236 // AppListSyncableService 243 // AppListSyncableService
237 244
245 // static
246 AppListSyncableService* AppListSyncableService::Get(Profile* profile) {
247 return AppListSyncableServiceFactory::GetForProfile(profile);
248 }
249
250
238 AppListSyncableService::AppListSyncableService( 251 AppListSyncableService::AppListSyncableService(
239 Profile* profile, 252 Profile* profile,
240 extensions::ExtensionSystem* extension_system) 253 extensions::ExtensionSystem* extension_system)
241 : profile_(profile), 254 : profile_(profile),
242 extension_system_(extension_system), 255 extension_system_(extension_system),
243 model_(new AppListModel), 256 model_(new AppListModel),
244 initial_sync_data_processed_(false), 257 initial_sync_data_processed_(false),
245 first_app_list_sync_(true) { 258 first_app_list_sync_(true) {
246 if (!extension_system) { 259 if (!extension_system) {
247 LOG(ERROR) << "AppListSyncableService created with no ExtensionSystem"; 260 LOG(ERROR) << "AppListSyncableService created with no ExtensionSystem";
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 #endif 304 #endif
292 } 305 }
293 306
294 model_pref_updater_.reset( 307 model_pref_updater_.reset(
295 new ModelPrefUpdater(AppListPrefs::Get(profile_), model_.get())); 308 new ModelPrefUpdater(AppListPrefs::Get(profile_), model_.get()));
296 309
297 if (app_list::switches::IsDriveAppsInAppListEnabled()) 310 if (app_list::switches::IsDriveAppsInAppListEnabled())
298 drive_app_provider_.reset(new DriveAppProvider(profile_, this)); 311 drive_app_provider_.reset(new DriveAppProvider(profile_, this));
299 } 312 }
300 313
314 void AppListSyncableService::AddObserverAndStart(Observer* observer) {
315 observer_list_.AddObserver(observer);
316 SyncStarted();
317 }
318
319 void AppListSyncableService::RemoveObserver(Observer* observer) {
320 observer_list_.RemoveObserver(observer);
321 }
322
323 void AppListSyncableService::NotifyObserversSyncUpdated() {
324 FOR_EACH_OBSERVER(Observer, observer_list_, OnSyncModelUpdated());
325 }
326
301 size_t AppListSyncableService::GetNumSyncItemsForTest() { 327 size_t AppListSyncableService::GetNumSyncItemsForTest() {
302 // If the model isn't built yet, there will be no sync items. 328 // If the model isn't built yet, there will be no sync items.
303 GetModel(); 329 GetModel();
304 330
305 return sync_items_.size(); 331 return sync_items_.size();
306 } 332 }
307 333
308 void AppListSyncableService::ResetDriveAppProviderForTest() { 334 void AppListSyncableService::ResetDriveAppProviderForTest() {
309 drive_app_provider_.reset(); 335 drive_app_provider_.reset();
310 } 336 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 sync_pb::AppListSpecifics::AppListItemType type; 441 sync_pb::AppListSpecifics::AppListItemType type;
416 if (!GetAppListItemType(app_item, &type)) 442 if (!GetAppListItemType(app_item, &type))
417 return NULL; 443 return NULL;
418 VLOG(2) << this << " CreateSyncItemFromAppItem:" << app_item->ToDebugString(); 444 VLOG(2) << this << " CreateSyncItemFromAppItem:" << app_item->ToDebugString();
419 SyncItem* sync_item = CreateSyncItem(app_item->id(), type); 445 SyncItem* sync_item = CreateSyncItem(app_item->id(), type);
420 UpdateSyncItemFromAppItem(app_item, sync_item); 446 UpdateSyncItemFromAppItem(app_item, sync_item);
421 SendSyncChange(sync_item, SyncChange::ACTION_ADD); 447 SendSyncChange(sync_item, SyncChange::ACTION_ADD);
422 return sync_item; 448 return sync_item;
423 } 449 }
424 450
451 bool AppListSyncableService::GetPinByPolicy(const std::string& app_id) {
452 SyncItem* sync_item = FindSyncItem(app_id);
453 if (!sync_item)
454 return false;
455 return sync_item->item_pin_by_policy;
456 }
457
458 syncer::StringOrdinal AppListSyncableService::GetPinPosition(
459 const std::string& app_id) {
460 SyncItem* sync_item = FindSyncItem(app_id);
461 if (!sync_item)
462 return syncer::StringOrdinal();
463 return sync_item->item_pin_ordinal;
464 }
465
466 void AppListSyncableService::SetPinPosition(
467 const std::string& app_id,
468 const syncer::StringOrdinal& item_pin_ordinal,
469 bool pinned_by_policy) {
470 SyncItem* sync_item = FindSyncItem(app_id);
471 SyncChange::SyncChangeType sync_change_type;
472 if (sync_item) {
473 sync_change_type = SyncChange::ACTION_UPDATE;
474 } else {
475 sync_item = CreateSyncItem(app_id, sync_pb::AppListSpecifics::TYPE_APP);
476 sync_change_type = SyncChange::ACTION_ADD;
477 }
478
479 sync_item->item_pin_ordinal = item_pin_ordinal;
480 sync_item->item_pin_by_policy = pinned_by_policy;
481 SendSyncChange(sync_item, sync_change_type);
482 }
483
425 void AppListSyncableService::AddOrUpdateFromSyncItem(AppListItem* app_item) { 484 void AppListSyncableService::AddOrUpdateFromSyncItem(AppListItem* app_item) {
426 // Do not create a sync item for the OEM folder here, do that in 485 // Do not create a sync item for the OEM folder here, do that in
427 // ResolveFolderPositions once the position has been resolved. 486 // ResolveFolderPositions once the position has been resolved.
428 if (app_item->id() == kOemFolderId) 487 if (app_item->id() == kOemFolderId)
429 return; 488 return;
430 489
431 SyncItem* sync_item = FindSyncItem(app_item->id()); 490 SyncItem* sync_item = FindSyncItem(app_item->id());
432 if (sync_item) { 491 if (sync_item) {
433 UpdateAppItemFromSyncItem(sync_item, app_item); 492 UpdateAppItemFromSyncItem(sync_item, app_item);
434 return; 493 return;
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 } 720 }
662 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); 721 sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
663 722
664 // Adding items may have created folders without setting their positions 723 // Adding items may have created folders without setting their positions
665 // since we haven't started observing the item list yet. Resolve those. 724 // since we haven't started observing the item list yet. Resolve those.
666 ResolveFolderPositions(); 725 ResolveFolderPositions();
667 726
668 // Start observing app list model changes. 727 // Start observing app list model changes.
669 model_observer_.reset(new ModelObserver(this)); 728 model_observer_.reset(new ModelObserver(this));
670 729
730 NotifyObserversSyncUpdated();
731
671 return result; 732 return result;
672 } 733 }
673 734
674 void AppListSyncableService::StopSyncing(syncer::ModelType type) { 735 void AppListSyncableService::StopSyncing(syncer::ModelType type) {
675 DCHECK_EQ(type, syncer::APP_LIST); 736 DCHECK_EQ(type, syncer::APP_LIST);
676 737
677 sync_processor_.reset(); 738 sync_processor_.reset();
678 sync_error_handler_.reset(); 739 sync_error_handler_.reset();
679 model_->SetFoldersEnabled(false); 740 model_->SetFoldersEnabled(false);
680 } 741 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 } else if (change.change_type() == SyncChange::ACTION_DELETE) { 780 } else if (change.change_type() == SyncChange::ACTION_DELETE) {
720 DeleteSyncItemSpecifics(change.sync_data().GetSpecifics().app_list()); 781 DeleteSyncItemSpecifics(change.sync_data().GetSpecifics().app_list());
721 } else { 782 } else {
722 LOG(ERROR) << "Invalid sync change"; 783 LOG(ERROR) << "Invalid sync change";
723 } 784 }
724 } 785 }
725 786
726 // Continue observing app list model changes. 787 // Continue observing app list model changes.
727 model_observer_.reset(new ModelObserver(this)); 788 model_observer_.reset(new ModelObserver(this));
728 789
790 NotifyObserversSyncUpdated();
791
729 return syncer::SyncError(); 792 return syncer::SyncError();
730 } 793 }
731 794
732 // AppListSyncableService private 795 // AppListSyncableService private
733 796
734 bool AppListSyncableService::ProcessSyncItemSpecifics( 797 bool AppListSyncableService::ProcessSyncItemSpecifics(
735 const sync_pb::AppListSpecifics& specifics) { 798 const sync_pb::AppListSpecifics& specifics) {
736 const std::string& item_id = specifics.item_id(); 799 const std::string& item_id = specifics.item_id();
737 if (item_id.empty()) { 800 if (item_id.empty()) {
738 LOG(ERROR) << "AppList item with empty ID"; 801 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; 890 VLOG(2) << " Moving Item To Folder: " << sync_item->parent_id;
828 model_->MoveItemToFolder(app_item, sync_item->parent_id); 891 model_->MoveItemToFolder(app_item, sync_item->parent_id);
829 } 892 }
830 UpdateAppItemFromSyncItem(sync_item, app_item); 893 UpdateAppItemFromSyncItem(sync_item, app_item);
831 } 894 }
832 895
833 void AppListSyncableService::UpdateAppItemFromSyncItem( 896 void AppListSyncableService::UpdateAppItemFromSyncItem(
834 const AppListSyncableService::SyncItem* sync_item, 897 const AppListSyncableService::SyncItem* sync_item,
835 AppListItem* app_item) { 898 AppListItem* app_item) {
836 VLOG(2) << this << " UpdateAppItemFromSyncItem: " << sync_item->ToString(); 899 VLOG(2) << this << " UpdateAppItemFromSyncItem: " << sync_item->ToString();
837 if (!app_item->position().Equals(sync_item->item_ordinal)) 900 if (sync_item->item_ordinal.IsValid() &&
901 !app_item->position().Equals(sync_item->item_ordinal))
838 model_->SetItemPosition(app_item, sync_item->item_ordinal); 902 model_->SetItemPosition(app_item, sync_item->item_ordinal);
839 // Only update the item name if it is a Folder or the name is empty. 903 // Only update the item name if it is a Folder or the name is empty.
840 if (sync_item->item_name != app_item->name() && 904 if (sync_item->item_name != app_item->name() &&
841 sync_item->item_id != kOemFolderId && 905 sync_item->item_id != kOemFolderId &&
842 (app_item->GetItemType() == AppListFolderItem::kItemType || 906 (app_item->GetItemType() == AppListFolderItem::kItemType ||
843 app_item->name().empty())) { 907 app_item->name().empty())) {
844 model_->SetItemName(app_item, sync_item->item_name); 908 model_->SetItemName(app_item, sync_item->item_name);
845 } 909 }
846 } 910 }
847 911
(...skipping 19 matching lines...) Expand all
867 if (!initial_sync_data_processed_ && 931 if (!initial_sync_data_processed_ &&
868 sync_change_type == SyncChange::ACTION_ADD) { 932 sync_change_type == SyncChange::ACTION_ADD) {
869 // This can occur if an initial item is created before its folder item. 933 // This can occur if an initial item is created before its folder item.
870 // A sync item should already exist for the folder, so we do not want to 934 // A sync item should already exist for the folder, so we do not want to
871 // send an ADD event, since that would trigger a CHECK in the sync code. 935 // send an ADD event, since that would trigger a CHECK in the sync code.
872 DCHECK(sync_item->item_type == sync_pb::AppListSpecifics::TYPE_FOLDER); 936 DCHECK(sync_item->item_type == sync_pb::AppListSpecifics::TYPE_FOLDER);
873 DVLOG(2) << this << " - SendSyncChange: ADD before initial data processed: " 937 DVLOG(2) << this << " - SendSyncChange: ADD before initial data processed: "
874 << sync_item->ToString(); 938 << sync_item->ToString();
875 return; 939 return;
876 } 940 }
877 if (sync_change_type == SyncChange::ACTION_ADD) 941 if (sync_change_type == SyncChange::ACTION_ADD) {
878 VLOG(2) << this << " -> SYNC ADD: " << sync_item->ToString(); 942 VLOG(2) << this << " -> SYNC ADD: " << sync_item->ToString();
879 else 943 } else {
880 VLOG(2) << this << " -> SYNC UPDATE: " << sync_item->ToString(); 944 VLOG(2) << this << " -> SYNC UPDATE: " << sync_item->ToString();
945 }
881 SyncChange sync_change(FROM_HERE, sync_change_type, 946 SyncChange sync_change(FROM_HERE, sync_change_type,
882 GetSyncDataFromSyncItem(sync_item)); 947 GetSyncDataFromSyncItem(sync_item));
883 sync_processor_->ProcessSyncChanges( 948 sync_processor_->ProcessSyncChanges(
884 FROM_HERE, syncer::SyncChangeList(1, sync_change)); 949 FROM_HERE, syncer::SyncChangeList(1, sync_change));
885 } 950 }
886 951
887 AppListSyncableService::SyncItem* 952 AppListSyncableService::SyncItem*
888 AppListSyncableService::FindSyncItem(const std::string& item_id) { 953 AppListSyncableService::FindSyncItem(const std::string& item_id) {
889 SyncItemMap::iterator iter = sync_items_.find(item_id); 954 SyncItemMap::iterator iter = sync_items_.find(item_id);
890 if (iter == sync_items_.end()) 955 if (iter == sync_items_.end())
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 1071
1007 std::string AppListSyncableService::SyncItem::ToString() const { 1072 std::string AppListSyncableService::SyncItem::ToString() const {
1008 std::string res = item_id.substr(0, 8); 1073 std::string res = item_id.substr(0, 8);
1009 if (item_type == sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) { 1074 if (item_type == sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) {
1010 res += " { RemoveDefault }"; 1075 res += " { RemoveDefault }";
1011 } else { 1076 } else {
1012 res += " { " + item_name + " }"; 1077 res += " { " + item_name + " }";
1013 res += " [" + item_ordinal.ToDebugString() + "]"; 1078 res += " [" + item_ordinal.ToDebugString() + "]";
1014 if (!parent_id.empty()) 1079 if (!parent_id.empty())
1015 res += " <" + parent_id.substr(0, 8) + ">"; 1080 res += " <" + parent_id.substr(0, 8) + ">";
1081 res += " [" + item_pin_ordinal.ToDebugString() + "]";
1016 } 1082 }
1017 return res; 1083 return res;
1018 } 1084 }
1019 1085
1020 } // namespace app_list 1086 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698