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

Side by Side Diff: chrome/browser/ui/ash/chrome_launcher_prefs.cc

Issue 2860503002: mash: Replace int ShelfIDs with AppLaunchID strings. (Closed)
Patch Set: Restore AppLaunchId class via using ShelfID = AppLaunchId; cleanup. Created 3 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/ash/chrome_launcher_prefs.h" 5 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <utility> 11 #include <utility>
12 12
13 #include "ash/public/cpp/app_launch_id.h"
14 #include "base/macros.h" 13 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
16 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
17 #include "base/values.h" 16 #include "base/values.h"
18 #include "chrome/browser/app_mode/app_mode_utils.h" 17 #include "chrome/browser/app_mode/app_mode_utils.h"
19 #include "chrome/browser/chromeos/arc/arc_support_host.h" 18 #include "chrome/browser/chromeos/arc/arc_support_host.h"
20 #include "chrome/browser/prefs/pref_service_syncable_util.h" 19 #include "chrome/browser/prefs/pref_service_syncable_util.h"
21 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" 20 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
22 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" 21 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
23 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" 22 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 // If no user-set value exists at |local_path|, the value from |synced_path| is 221 // If no user-set value exists at |local_path|, the value from |synced_path| is
223 // copied to |local_path|. 222 // copied to |local_path|.
224 void PropagatePrefToLocalIfNotSet( 223 void PropagatePrefToLocalIfNotSet(
225 sync_preferences::PrefServiceSyncable* pref_service, 224 sync_preferences::PrefServiceSyncable* pref_service,
226 const char* local_path, 225 const char* local_path,
227 const char* synced_path) { 226 const char* synced_path) {
228 if (!pref_service->FindPreference(local_path)->HasUserSetting()) 227 if (!pref_service->FindPreference(local_path)->HasUserSetting())
229 pref_service->SetString(local_path, pref_service->GetString(synced_path)); 228 pref_service->SetString(local_path, pref_service->GetString(synced_path));
230 } 229 }
231 230
232 std::vector<AppLaunchId> AppIdsToAppLaunchIds( 231 std::vector<ShelfID> AppIdsToShelfIDs(const std::vector<std::string> app_ids) {
233 const std::vector<std::string> app_ids) { 232 std::vector<ShelfID> shelf_ids(app_ids.size());
234 std::vector<AppLaunchId> app_launch_ids(app_ids.size(), AppLaunchId());
235 for (size_t i = 0; i < app_ids.size(); ++i) 233 for (size_t i = 0; i < app_ids.size(); ++i)
236 app_launch_ids[i] = AppLaunchId(app_ids[i]); 234 shelf_ids[i] = ShelfID(app_ids[i]);
237 return app_launch_ids; 235 return shelf_ids;
238 } 236 }
239 237
240 struct PinInfo { 238 struct PinInfo {
241 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal) 239 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal)
242 : app_id(app_id), item_ordinal(item_ordinal) {} 240 : app_id(app_id), item_ordinal(item_ordinal) {}
243 241
244 std::string app_id; 242 std::string app_id;
245 syncer::StringOrdinal item_ordinal; 243 syncer::StringOrdinal item_ordinal;
246 }; 244 };
247 245
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 // Check if it is present but not in legacy pin. 592 // Check if it is present but not in legacy pin.
595 if (helper->IsValidIDForCurrentUser(app_id)) 593 if (helper->IsValidIDForCurrentUser(app_id))
596 continue; 594 continue;
597 app_service->SetPinPosition(app_id, last_position); 595 app_service->SetPinPosition(app_id, last_position);
598 last_position = last_position.CreateAfter(); 596 last_position = last_position.CreateAfter();
599 } 597 }
600 598
601 return legacy_pins_valid; 599 return legacy_pins_valid;
602 } 600 }
603 601
604 std::vector<AppLaunchId> GetPinnedAppsFromPrefs( 602 std::vector<ShelfID> GetPinnedAppsFromPrefs(const PrefService* prefs,
605 const PrefService* prefs, 603 LauncherControllerHelper* helper) {
606 LauncherControllerHelper* helper) {
607 app_list::AppListSyncableService* app_service = 604 app_list::AppListSyncableService* app_service =
608 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); 605 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile());
609 // Some unit tests may not have it or service may not be initialized. 606 // Some unit tests may not have it or service may not be initialized.
610 if (!app_service || !app_service->IsInitialized()) 607 if (!app_service || !app_service->IsInitialized())
611 return std::vector<AppLaunchId>(); 608 return std::vector<ShelfID>();
612 609
613 std::vector<PinInfo> pin_infos; 610 std::vector<PinInfo> pin_infos;
614 611
615 // Empty pins indicates that sync based pin model is used for the first 612 // Empty pins indicates that sync based pin model is used for the first
616 // time. In normal workflow we have at least Chrome browser pin info. 613 // time. In normal workflow we have at least Chrome browser pin info.
617 bool first_run = true; 614 bool first_run = true;
618 615
619 for (const auto& sync_peer : app_service->sync_items()) { 616 for (const auto& sync_peer : app_service->sync_items()) {
620 if (!sync_peer.second->item_pin_ordinal.IsValid()) 617 if (!sync_peer.second->item_pin_ordinal.IsValid())
621 continue; 618 continue;
622 619
623 first_run = false; 620 first_run = false;
624 // Don't include apps that currently do not exist on device. 621 // Don't include apps that currently do not exist on device.
625 if (sync_peer.first != extension_misc::kChromeAppId && 622 if (sync_peer.first != extension_misc::kChromeAppId &&
626 !helper->IsValidIDForCurrentUser(sync_peer.first)) { 623 !helper->IsValidIDForCurrentUser(sync_peer.first)) {
627 continue; 624 continue;
628 } 625 }
629 626
630 pin_infos.push_back( 627 pin_infos.push_back(
631 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal)); 628 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal));
632 } 629 }
633 630
634 if (first_run) { 631 if (first_run) {
635 // Return default apps in case profile is not synced yet. 632 // Return default apps in case profile is not synced yet.
636 sync_preferences::PrefServiceSyncable* const pref_service_syncable = 633 sync_preferences::PrefServiceSyncable* const pref_service_syncable =
637 PrefServiceSyncableFromProfile(helper->profile()); 634 PrefServiceSyncableFromProfile(helper->profile());
638 if (!pref_service_syncable->IsSyncing()) 635 if (!pref_service_syncable->IsSyncing())
639 return AppIdsToAppLaunchIds( 636 return AppIdsToShelfIDs(
640 GetPinnedAppsFromPrefsLegacy(prefs, helper, true)); 637 GetPinnedAppsFromPrefsLegacy(prefs, helper, true));
641 638
642 // We need to import legacy pins model and convert it to sync based 639 // We need to import legacy pins model and convert it to sync based
643 // model. 640 // model.
644 return AppIdsToAppLaunchIds(ImportLegacyPinnedApps(prefs, helper)); 641 return AppIdsToShelfIDs(ImportLegacyPinnedApps(prefs, helper));
645 } 642 }
646 643
647 // Sort pins according their ordinals. 644 // Sort pins according their ordinals.
648 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); 645 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo());
649 646
650 AppTracker policy_apps; 647 AppTracker policy_apps;
651 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps); 648 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps);
652 649
653 // Pinned by policy apps appear first, if they were not shown before. 650 // Pinned by policy apps appear first, if they were not shown before.
654 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); 651 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile());
(...skipping 25 matching lines...) Expand all
680 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { 677 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) {
681 const syncer::StringOrdinal arc_host_position = 678 const syncer::StringOrdinal arc_host_position =
682 GetLastPinPosition(helper->profile()); 679 GetLastPinPosition(helper->profile());
683 pin_infos.insert(pin_infos.begin(), 680 pin_infos.insert(pin_infos.begin(),
684 PinInfo(ArcSupportHost::kHostAppId, arc_host_position)); 681 PinInfo(ArcSupportHost::kHostAppId, arc_host_position));
685 app_service->SetPinPosition(ArcSupportHost::kHostAppId, 682 app_service->SetPinPosition(ArcSupportHost::kHostAppId,
686 arc_host_position); 683 arc_host_position);
687 } 684 }
688 } 685 }
689 686
690 // Convert to AppLaunchId array. 687 // Convert to ShelfID array.
691 std::vector<std::string> pins(pin_infos.size()); 688 std::vector<std::string> pins(pin_infos.size());
692 for (size_t i = 0; i < pin_infos.size(); ++i) 689 for (size_t i = 0; i < pin_infos.size(); ++i)
693 pins[i] = pin_infos[i].app_id; 690 pins[i] = pin_infos[i].app_id;
694 691
695 return AppIdsToAppLaunchIds(pins); 692 return AppIdsToShelfIDs(pins);
696 } 693 }
697 694
698 void RemovePinPosition(Profile* profile, const AppLaunchId& app_launch_id) { 695 void RemovePinPosition(Profile* profile, const ShelfID& shelf_id) {
699 DCHECK(profile); 696 DCHECK(profile);
700 697
701 const std::string& app_id = app_launch_id.app_id(); 698 const std::string& app_id = shelf_id.app_id();
702 if (!app_launch_id.launch_id().empty()) { 699 if (!shelf_id.launch_id().empty()) {
703 VLOG(2) << "Syncing remove pin for '" << app_id 700 VLOG(2) << "Syncing remove pin for '" << app_id
704 << "' with non-empty launch id '" << app_launch_id.launch_id() 701 << "' with non-empty launch id '" << shelf_id.launch_id()
705 << "' is not supported."; 702 << "' is not supported.";
706 return; 703 return;
707 } 704 }
708 DCHECK(!app_id.empty()); 705 DCHECK(!app_id.empty());
709 706
710 app_list::AppListSyncableService* app_service = 707 app_list::AppListSyncableService* app_service =
711 app_list::AppListSyncableServiceFactory::GetForProfile(profile); 708 app_list::AppListSyncableServiceFactory::GetForProfile(profile);
712 app_service->SetPinPosition(app_id, syncer::StringOrdinal()); 709 app_service->SetPinPosition(app_id, syncer::StringOrdinal());
713 } 710 }
714 711
715 void SetPinPosition(Profile* profile, 712 void SetPinPosition(Profile* profile,
716 const AppLaunchId& app_launch_id, 713 const ShelfID& shelf_id,
717 const AppLaunchId& app_launch_id_before, 714 const ShelfID& shelf_id_before,
718 const std::vector<AppLaunchId>& app_launch_ids_after) { 715 const std::vector<ShelfID>& shelf_ids_after) {
719 DCHECK(profile); 716 DCHECK(profile);
720 717
721 const std::string& app_id = app_launch_id.app_id(); 718 const std::string& app_id = shelf_id.app_id();
722 if (!app_launch_id.launch_id().empty()) { 719 if (!shelf_id.launch_id().empty()) {
723 VLOG(2) << "Syncing set pin for '" << app_id 720 VLOG(2) << "Syncing set pin for '" << app_id
724 << "' with non-empty launch id '" << app_launch_id.launch_id() 721 << "' with non-empty launch id '" << shelf_id.launch_id()
725 << "' is not supported."; 722 << "' is not supported.";
726 return; 723 return;
727 } 724 }
728 725
729 const std::string& app_id_before = app_launch_id_before.app_id(); 726 const std::string& app_id_before = shelf_id_before.app_id();
730 727
731 DCHECK(!app_id.empty()); 728 DCHECK(!app_id.empty());
732 DCHECK_NE(app_id, app_id_before); 729 DCHECK_NE(app_id, app_id_before);
733 730
734 app_list::AppListSyncableService* app_service = 731 app_list::AppListSyncableService* app_service =
735 app_list::AppListSyncableServiceFactory::GetForProfile(profile); 732 app_list::AppListSyncableServiceFactory::GetForProfile(profile);
736 // Some unit tests may not have this service. 733 // Some unit tests may not have this service.
737 if (!app_service) 734 if (!app_service)
738 return; 735 return;
739 736
740 syncer::StringOrdinal position_before = 737 syncer::StringOrdinal position_before =
741 app_id_before.empty() ? syncer::StringOrdinal() 738 app_id_before.empty() ? syncer::StringOrdinal()
742 : app_service->GetPinPosition(app_id_before); 739 : app_service->GetPinPosition(app_id_before);
743 syncer::StringOrdinal position_after; 740 syncer::StringOrdinal position_after;
744 for (const auto& app_launch_id_after : app_launch_ids_after) { 741 for (const auto& shelf_id_after : shelf_ids_after) {
745 const std::string& app_id_after = app_launch_id_after.app_id(); 742 const std::string& app_id_after = shelf_id_after.app_id();
746 DCHECK_NE(app_id_after, app_id); 743 DCHECK_NE(app_id_after, app_id);
747 DCHECK_NE(app_id_after, app_id_before); 744 DCHECK_NE(app_id_after, app_id_before);
748 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after); 745 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after);
749 DCHECK(position.IsValid()); 746 DCHECK(position.IsValid());
750 if (!position.IsValid()) { 747 if (!position.IsValid()) {
751 LOG(ERROR) << "Sync pin position was not found for " << app_id_after; 748 LOG(ERROR) << "Sync pin position was not found for " << app_id_after;
752 continue; 749 continue;
753 } 750 }
754 if (!position_before.IsValid() || !position.Equals(position_before)) { 751 if (!position_before.IsValid() || !position.Equals(position_before)) {
755 position_after = position; 752 position_after = position;
756 break; 753 break;
757 } 754 }
758 } 755 }
759 756
760 syncer::StringOrdinal pin_position; 757 syncer::StringOrdinal pin_position;
761 if (position_before.IsValid() && position_after.IsValid()) 758 if (position_before.IsValid() && position_after.IsValid())
762 pin_position = position_before.CreateBetween(position_after); 759 pin_position = position_before.CreateBetween(position_after);
763 else if (position_before.IsValid()) 760 else if (position_before.IsValid())
764 pin_position = position_before.CreateAfter(); 761 pin_position = position_before.CreateAfter();
765 else if (position_after.IsValid()) 762 else if (position_after.IsValid())
766 pin_position = position_after.CreateBefore(); 763 pin_position = position_after.CreateBefore();
767 else 764 else
768 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); 765 pin_position = syncer::StringOrdinal::CreateInitialOrdinal();
769 app_service->SetPinPosition(app_id, pin_position); 766 app_service->SetPinPosition(app_id, pin_position);
770 } 767 }
771 768
772 } // namespace launcher 769 } // namespace launcher
773 } // namespace ash 770 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698