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

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

Issue 2765563002: Move and rename Chrome's AppLauncherId to ash::AppLaunchId. (Closed)
Patch Set: Add example commment. Created 3 years, 9 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 <set> 9 #include <set>
10 10
11 #include "ash/public/cpp/app_launch_id.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "chrome/browser/app_mode/app_mode_utils.h" 16 #include "chrome/browser/app_mode/app_mode_utils.h"
16 #include "chrome/browser/chromeos/arc/arc_support_host.h" 17 #include "chrome/browser/chromeos/arc/arc_support_host.h"
17 #include "chrome/browser/prefs/pref_service_syncable_util.h" 18 #include "chrome/browser/prefs/pref_service_syncable_util.h"
18 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" 19 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
19 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" 20 #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
20 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" 21 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
21 #include "chrome/browser/ui/ash/app_launcher_id.h"
22 #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h" 22 #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h"
23 #include "chrome/common/extensions/extension_constants.h" 23 #include "chrome/common/extensions/extension_constants.h"
24 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
25 #include "components/pref_registry/pref_registry_syncable.h" 25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "components/prefs/pref_service.h" 26 #include "components/prefs/pref_service.h"
27 #include "components/prefs/scoped_user_pref_update.h" 27 #include "components/prefs/scoped_user_pref_update.h"
28 #include "components/sync/model/string_ordinal.h" 28 #include "components/sync/model/string_ordinal.h"
29 #include "components/sync_preferences/pref_service_syncable.h" 29 #include "components/sync_preferences/pref_service_syncable.h"
30 #include "ui/display/display.h" 30 #include "ui/display/display.h"
31 #include "ui/display/screen.h" 31 #include "ui/display/screen.h"
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 // If no user-set value exists at |local_path|, the value from |synced_path| is 219 // If no user-set value exists at |local_path|, the value from |synced_path| is
220 // copied to |local_path|. 220 // copied to |local_path|.
221 void PropagatePrefToLocalIfNotSet( 221 void PropagatePrefToLocalIfNotSet(
222 sync_preferences::PrefServiceSyncable* pref_service, 222 sync_preferences::PrefServiceSyncable* pref_service,
223 const char* local_path, 223 const char* local_path,
224 const char* synced_path) { 224 const char* synced_path) {
225 if (!pref_service->FindPreference(local_path)->HasUserSetting()) 225 if (!pref_service->FindPreference(local_path)->HasUserSetting())
226 pref_service->SetString(local_path, pref_service->GetString(synced_path)); 226 pref_service->SetString(local_path, pref_service->GetString(synced_path));
227 } 227 }
228 228
229 std::vector<AppLauncherId> AppIdsToAppLauncherIds( 229 std::vector<AppLaunchId> AppIdsToAppLaunchIds(
230 const std::vector<std::string> app_ids) { 230 const std::vector<std::string> app_ids) {
231 std::vector<AppLauncherId> app_launcher_ids(app_ids.size(), AppLauncherId()); 231 std::vector<AppLaunchId> app_launch_ids(app_ids.size(), AppLaunchId());
232 for (size_t i = 0; i < app_ids.size(); ++i) 232 for (size_t i = 0; i < app_ids.size(); ++i)
233 app_launcher_ids[i] = AppLauncherId(app_ids[i]); 233 app_launch_ids[i] = AppLaunchId(app_ids[i]);
234 return app_launcher_ids; 234 return app_launch_ids;
235 } 235 }
236 236
237 struct PinInfo { 237 struct PinInfo {
238 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal) 238 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal)
239 : app_id(app_id), item_ordinal(item_ordinal) {} 239 : app_id(app_id), item_ordinal(item_ordinal) {}
240 240
241 std::string app_id; 241 std::string app_id;
242 syncer::StringOrdinal item_ordinal; 242 syncer::StringOrdinal item_ordinal;
243 }; 243 };
244 244
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 // Check if it is present but not in legacy pin. 591 // Check if it is present but not in legacy pin.
592 if (helper->IsValidIDForCurrentUser(app_id)) 592 if (helper->IsValidIDForCurrentUser(app_id))
593 continue; 593 continue;
594 app_service->SetPinPosition(app_id, last_position); 594 app_service->SetPinPosition(app_id, last_position);
595 last_position = last_position.CreateAfter(); 595 last_position = last_position.CreateAfter();
596 } 596 }
597 597
598 return legacy_pins_valid; 598 return legacy_pins_valid;
599 } 599 }
600 600
601 std::vector<AppLauncherId> GetPinnedAppsFromPrefs( 601 std::vector<AppLaunchId> GetPinnedAppsFromPrefs(
602 const PrefService* prefs, 602 const PrefService* prefs,
603 LauncherControllerHelper* helper) { 603 LauncherControllerHelper* helper) {
604 app_list::AppListSyncableService* app_service = 604 app_list::AppListSyncableService* app_service =
605 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); 605 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile());
606 // 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.
607 if (!app_service || !app_service->IsInitialized()) 607 if (!app_service || !app_service->IsInitialized())
608 return std::vector<AppLauncherId>(); 608 return std::vector<AppLaunchId>();
609 609
610 std::vector<PinInfo> pin_infos; 610 std::vector<PinInfo> pin_infos;
611 611
612 // 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
613 // 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.
614 bool first_run = true; 614 bool first_run = true;
615 615
616 for (const auto& sync_peer : app_service->sync_items()) { 616 for (const auto& sync_peer : app_service->sync_items()) {
617 if (!sync_peer.second->item_pin_ordinal.IsValid()) 617 if (!sync_peer.second->item_pin_ordinal.IsValid())
618 continue; 618 continue;
619 619
620 first_run = false; 620 first_run = false;
621 // Don't include apps that currently do not exist on device. 621 // Don't include apps that currently do not exist on device.
622 if (sync_peer.first != extension_misc::kChromeAppId && 622 if (sync_peer.first != extension_misc::kChromeAppId &&
623 !helper->IsValidIDForCurrentUser(sync_peer.first)) { 623 !helper->IsValidIDForCurrentUser(sync_peer.first)) {
624 continue; 624 continue;
625 } 625 }
626 626
627 pin_infos.push_back( 627 pin_infos.push_back(
628 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal)); 628 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal));
629 } 629 }
630 630
631 if (first_run) { 631 if (first_run) {
632 // Return default apps in case profile is not synced yet. 632 // Return default apps in case profile is not synced yet.
633 sync_preferences::PrefServiceSyncable* const pref_service_syncable = 633 sync_preferences::PrefServiceSyncable* const pref_service_syncable =
634 PrefServiceSyncableFromProfile(helper->profile()); 634 PrefServiceSyncableFromProfile(helper->profile());
635 if (!pref_service_syncable->IsSyncing()) 635 if (!pref_service_syncable->IsSyncing())
636 return AppIdsToAppLauncherIds( 636 return AppIdsToAppLaunchIds(
637 GetPinnedAppsFromPrefsLegacy(prefs, helper, true)); 637 GetPinnedAppsFromPrefsLegacy(prefs, helper, true));
638 638
639 // 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
640 // model. 640 // model.
641 return AppIdsToAppLauncherIds(ImportLegacyPinnedApps(prefs, helper)); 641 return AppIdsToAppLaunchIds(ImportLegacyPinnedApps(prefs, helper));
642 } 642 }
643 643
644 // Sort pins according their ordinals. 644 // Sort pins according their ordinals.
645 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); 645 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo());
646 646
647 AppTracker policy_apps; 647 AppTracker policy_apps;
648 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps); 648 GetAppsPinnedByPolicy(prefs, helper, true, &policy_apps);
649 649
650 // 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.
651 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); 651 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile());
(...skipping 25 matching lines...) Expand all
677 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { 677 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) {
678 const syncer::StringOrdinal arc_host_position = 678 const syncer::StringOrdinal arc_host_position =
679 GetLastPinPosition(helper->profile()); 679 GetLastPinPosition(helper->profile());
680 pin_infos.insert(pin_infos.begin(), 680 pin_infos.insert(pin_infos.begin(),
681 PinInfo(ArcSupportHost::kHostAppId, arc_host_position)); 681 PinInfo(ArcSupportHost::kHostAppId, arc_host_position));
682 app_service->SetPinPosition(ArcSupportHost::kHostAppId, 682 app_service->SetPinPosition(ArcSupportHost::kHostAppId,
683 arc_host_position); 683 arc_host_position);
684 } 684 }
685 } 685 }
686 686
687 // Convert to AppLauncherId array. 687 // Convert to AppLaunchId array.
688 std::vector<std::string> pins(pin_infos.size()); 688 std::vector<std::string> pins(pin_infos.size());
689 for (size_t i = 0; i < pin_infos.size(); ++i) 689 for (size_t i = 0; i < pin_infos.size(); ++i)
690 pins[i] = pin_infos[i].app_id; 690 pins[i] = pin_infos[i].app_id;
691 691
692 return AppIdsToAppLauncherIds(pins); 692 return AppIdsToAppLaunchIds(pins);
693 } 693 }
694 694
695 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { 695 void RemovePinPosition(Profile* profile, const AppLaunchId& app_launch_id) {
696 DCHECK(profile); 696 DCHECK(profile);
697 697
698 const std::string& app_id = app_launcher_id.app_id(); 698 const std::string& app_id = app_launch_id.app_id();
699 if (!app_launcher_id.launch_id().empty()) { 699 if (!app_launch_id.launch_id().empty()) {
700 VLOG(2) << "Syncing remove pin for '" << app_id 700 VLOG(2) << "Syncing remove pin for '" << app_id
701 << "' with non-empty launch id '" << app_launcher_id.launch_id() 701 << "' with non-empty launch id '" << app_launch_id.launch_id()
702 << "' is not supported."; 702 << "' is not supported.";
703 return; 703 return;
704 } 704 }
705 DCHECK(!app_id.empty()); 705 DCHECK(!app_id.empty());
706 706
707 app_list::AppListSyncableService* app_service = 707 app_list::AppListSyncableService* app_service =
708 app_list::AppListSyncableServiceFactory::GetForProfile(profile); 708 app_list::AppListSyncableServiceFactory::GetForProfile(profile);
709 app_service->SetPinPosition(app_id, syncer::StringOrdinal()); 709 app_service->SetPinPosition(app_id, syncer::StringOrdinal());
710 } 710 }
711 711
712 void SetPinPosition(Profile* profile, 712 void SetPinPosition(Profile* profile,
713 const AppLauncherId& app_launcher_id, 713 const AppLaunchId& app_launch_id,
714 const AppLauncherId& app_launcher_id_before, 714 const AppLaunchId& app_launch_id_before,
715 const std::vector<AppLauncherId>& app_launcher_ids_after) { 715 const std::vector<AppLaunchId>& app_launch_ids_after) {
716 DCHECK(profile); 716 DCHECK(profile);
717 717
718 const std::string& app_id = app_launcher_id.app_id(); 718 const std::string& app_id = app_launch_id.app_id();
719 if (!app_launcher_id.launch_id().empty()) { 719 if (!app_launch_id.launch_id().empty()) {
720 VLOG(2) << "Syncing set pin for '" << app_id 720 VLOG(2) << "Syncing set pin for '" << app_id
721 << "' with non-empty launch id '" << app_launcher_id.launch_id() 721 << "' with non-empty launch id '" << app_launch_id.launch_id()
722 << "' is not supported."; 722 << "' is not supported.";
723 return; 723 return;
724 } 724 }
725 725
726 const std::string& app_id_before = app_launcher_id_before.app_id(); 726 const std::string& app_id_before = app_launch_id_before.app_id();
727 727
728 DCHECK(!app_id.empty()); 728 DCHECK(!app_id.empty());
729 DCHECK_NE(app_id, app_id_before); 729 DCHECK_NE(app_id, app_id_before);
730 730
731 app_list::AppListSyncableService* app_service = 731 app_list::AppListSyncableService* app_service =
732 app_list::AppListSyncableServiceFactory::GetForProfile(profile); 732 app_list::AppListSyncableServiceFactory::GetForProfile(profile);
733 // Some unit tests may not have this service. 733 // Some unit tests may not have this service.
734 if (!app_service) 734 if (!app_service)
735 return; 735 return;
736 736
737 syncer::StringOrdinal position_before = 737 syncer::StringOrdinal position_before =
738 app_id_before.empty() ? syncer::StringOrdinal() 738 app_id_before.empty() ? syncer::StringOrdinal()
739 : app_service->GetPinPosition(app_id_before); 739 : app_service->GetPinPosition(app_id_before);
740 syncer::StringOrdinal position_after; 740 syncer::StringOrdinal position_after;
741 for (const auto& app_launcher_id_after : app_launcher_ids_after) { 741 for (const auto& app_launch_id_after : app_launch_ids_after) {
742 const std::string& app_id_after = app_launcher_id_after.app_id(); 742 const std::string& app_id_after = app_launch_id_after.app_id();
743 DCHECK_NE(app_id_after, app_id); 743 DCHECK_NE(app_id_after, app_id);
744 DCHECK_NE(app_id_after, app_id_before); 744 DCHECK_NE(app_id_after, app_id_before);
745 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after); 745 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after);
746 DCHECK(position.IsValid()); 746 DCHECK(position.IsValid());
747 if (!position.IsValid()) { 747 if (!position.IsValid()) {
748 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;
749 continue; 749 continue;
750 } 750 }
751 if (!position_before.IsValid() || !position.Equals(position_before)) { 751 if (!position_before.IsValid() || !position.Equals(position_before)) {
752 position_after = position; 752 position_after = position;
753 break; 753 break;
754 } 754 }
755 } 755 }
756 756
757 syncer::StringOrdinal pin_position; 757 syncer::StringOrdinal pin_position;
758 if (position_before.IsValid() && position_after.IsValid()) 758 if (position_before.IsValid() && position_after.IsValid())
759 pin_position = position_before.CreateBetween(position_after); 759 pin_position = position_before.CreateBetween(position_after);
760 else if (position_before.IsValid()) 760 else if (position_before.IsValid())
761 pin_position = position_before.CreateAfter(); 761 pin_position = position_before.CreateAfter();
762 else if (position_after.IsValid()) 762 else if (position_after.IsValid())
763 pin_position = position_after.CreateBefore(); 763 pin_position = position_after.CreateBefore();
764 else 764 else
765 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); 765 pin_position = syncer::StringOrdinal::CreateInitialOrdinal();
766 app_service->SetPinPosition(app_id, pin_position); 766 app_service->SetPinPosition(app_id, pin_position);
767 } 767 }
768 768
769 } // namespace launcher 769 } // namespace launcher
770 } // namespace ash 770 } // namespace ash
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/chrome_launcher_prefs.h ('k') | chrome/browser/ui/ash/launcher/app_shortcut_launcher_item_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698