Chromium Code Reviews| Index: chrome/browser/ui/ash/chrome_launcher_prefs.cc |
| diff --git a/chrome/browser/ui/ash/chrome_launcher_prefs.cc b/chrome/browser/ui/ash/chrome_launcher_prefs.cc |
| index 18433a440944af707c305ada1eb0179720e0cd2f..30a0745f599058aa62c7ba4720e3a21cafc059ae 100644 |
| --- a/chrome/browser/ui/ash/chrome_launcher_prefs.cc |
| +++ b/chrome/browser/ui/ash/chrome_launcher_prefs.cc |
| @@ -18,6 +18,7 @@ |
| #include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
| #include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h" |
| #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| +#include "chrome/browser/ui/ash/app_launcher_id.h" |
| #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h" |
| #include "chrome/common/extensions/extension_constants.h" |
| #include "chrome/common/pref_names.h" |
| @@ -35,6 +36,13 @@ namespace launcher { |
| namespace { |
| +std::unique_ptr<base::DictionaryValue> CreateAppDict( |
| + const std::string& app_id) { |
| + auto app_value = base::MakeUnique<base::DictionaryValue>(); |
| + app_value->SetString(kPinnedAppsPrefAppIDPath, app_id); |
| + return app_value; |
| +} |
| + |
| // App ID of default pinned apps. |
| const char* kDefaultPinnedApps[] = { |
| extension_misc::kGmailAppId, extension_misc::kGoogleDocAppId, |
| @@ -43,7 +51,7 @@ const char* kDefaultPinnedApps[] = { |
| base::ListValue* CreateDefaultPinnedAppsList() { |
| std::unique_ptr<base::ListValue> apps(new base::ListValue); |
| for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) |
| - apps->Append(CreateAppDict(AppLauncherId(kDefaultPinnedApps[i]))); |
| + apps->Append(CreateAppDict(kDefaultPinnedApps[i])); |
| return apps.release(); |
| } |
| @@ -218,12 +226,19 @@ void PropagatePrefToLocalIfNotSet( |
| pref_service->SetString(local_path, pref_service->GetString(synced_path)); |
| } |
| +std::vector<AppLauncherId> AppIdsToAppLauncherIds( |
| + const std::vector<std::string> app_ids) { |
| + std::vector<AppLauncherId> app_launcher_ids(app_ids.size(), AppLauncherId()); |
| + for (size_t i = 0; i < app_ids.size(); ++i) |
| + app_launcher_ids[i] = AppLauncherId(app_ids[i]); |
| + return app_launcher_ids; |
| +} |
| + |
| struct PinInfo { |
| - PinInfo(const AppLauncherId& app_launcher_id, |
| - const syncer::StringOrdinal& item_ordinal) |
| - : app_launcher_id(app_launcher_id), item_ordinal(item_ordinal) {} |
| + PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal) |
| + : app_id(app_id), item_ordinal(item_ordinal) {} |
| - AppLauncherId app_launcher_id; |
| + std::string app_id; |
| syncer::StringOrdinal item_ordinal; |
| }; |
| @@ -237,26 +252,25 @@ struct ComparePinInfo { |
| // to check if app exists in the list. |
| class AppTracker { |
| public: |
| - bool HasApp(const AppLauncherId& app_launcher_id) const { |
| - return app_set_.find(app_launcher_id) != app_set_.end(); |
| + bool HasApp(const std::string& app_id) const { |
| + return app_set_.find(app_id) != app_set_.end(); |
| } |
| - void AddApp(const AppLauncherId& app_launcher_id) { |
| - if (HasApp(app_launcher_id)) |
| + void AddApp(const std::string& app_id) { |
| + if (HasApp(app_id)) |
| return; |
| - app_list_.push_back(app_launcher_id); |
| - app_set_.insert(app_launcher_id); |
| + app_list_.push_back(app_id); |
| + app_set_.insert(app_id); |
| } |
| - void MaybeAddApp(const AppLauncherId& app_launcher_id, |
| + void MaybeAddApp(const std::string& app_id, |
| const LauncherControllerHelper* helper, |
| bool check_for_valid_app) { |
| - DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); |
| - if (check_for_valid_app && |
| - !helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) { |
| + DCHECK_NE(kPinnedAppsPlaceholder, app_id); |
| + if (check_for_valid_app && !helper->IsValidIDForCurrentUser(app_id)) { |
| return; |
| } |
| - AddApp(app_launcher_id); |
| + AddApp(app_id); |
| } |
| void MaybeAddAppFromPref(const base::DictionaryValue* app_pref, |
| @@ -278,14 +292,14 @@ class AppTracker { |
| return; |
| } |
| - MaybeAddApp(AppLauncherId(app_id), helper, check_for_valid_app); |
| + MaybeAddApp(app_id, helper, check_for_valid_app); |
| } |
| - const std::vector<AppLauncherId>& app_list() const { return app_list_; } |
| + const std::vector<std::string>& app_list() const { return app_list_; } |
| private: |
| - std::vector<AppLauncherId> app_list_; |
| - std::set<AppLauncherId> app_set_; |
| + std::vector<std::string> app_list_; |
| + std::set<std::string> app_set_; |
| }; |
| } // namespace |
| @@ -301,28 +315,6 @@ const char kShelfAlignmentBottom[] = "Bottom"; |
| const char kShelfAlignmentLeft[] = "Left"; |
| const char kShelfAlignmentRight[] = "Right"; |
| -AppLauncherId::AppLauncherId(const std::string& app_id, |
| - const std::string& launch_id) |
| - : app_id_(app_id), launch_id_(launch_id) { |
| - DCHECK(!app_id.empty()); |
| -} |
| - |
| -AppLauncherId::AppLauncherId(const std::string& app_id) : app_id_(app_id) { |
| - DCHECK(!app_id.empty()); |
| -} |
| - |
| -AppLauncherId::AppLauncherId() {} |
| - |
| -AppLauncherId::~AppLauncherId() {} |
| - |
| -std::string AppLauncherId::ToString() const { |
| - return app_id_ + launch_id_; |
| -} |
| - |
| -bool AppLauncherId::operator<(const AppLauncherId& other) const { |
| - return ToString() < other.ToString(); |
| -} |
| - |
| void RegisterChromeLauncherUserPrefs( |
| user_prefs::PrefRegistrySyncable* registry) { |
| // TODO: If we want to support multiple profiles this will likely need to be |
| @@ -349,13 +341,6 @@ void RegisterChromeLauncherUserPrefs( |
| registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); |
| } |
| -std::unique_ptr<base::DictionaryValue> CreateAppDict( |
| - const AppLauncherId& app_launcher_id) { |
| - auto app_value = base::MakeUnique<base::DictionaryValue>(); |
| - app_value->SetString(kPinnedAppsPrefAppIDPath, app_launcher_id.ToString()); |
| - return app_value; |
| -} |
| - |
| ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, |
| int64_t display_id) { |
| DCHECK_NE(display_id, display::kInvalidDisplayId); |
| @@ -450,16 +435,15 @@ void GetAppsPinnedByPolicy(const PrefService* prefs, |
| for (const auto& activity : activities) { |
| const std::string arc_app_id = |
| ArcAppListPrefs::GetAppId(arc_package, activity); |
| - apps->MaybeAddApp(AppLauncherId(arc_app_id), helper, |
| - check_for_valid_app); |
| + apps->MaybeAddApp(arc_app_id, helper, check_for_valid_app); |
| } |
| } else { |
| - apps->MaybeAddApp(AppLauncherId(app_id), helper, check_for_valid_app); |
| + apps->MaybeAddApp(app_id, helper, check_for_valid_app); |
| } |
| } |
| } |
| -std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( |
| +std::vector<std::string> GetPinnedAppsFromPrefsLegacy( |
| const PrefService* prefs, |
| const LauncherControllerHelper* helper, |
| bool check_for_valid_app) { |
| @@ -477,7 +461,7 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( |
| // Check if Chrome is in either of the the preferences lists. |
| std::unique_ptr<base::Value> chrome_app( |
| - CreateAppDict(AppLauncherId(extension_misc::kChromeAppId))); |
| + CreateAppDict(extension_misc::kChromeAppId)); |
| AppTracker apps; |
| GetAppsPinnedByPolicy(prefs, helper, check_for_valid_app, &apps); |
| @@ -487,7 +471,7 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( |
| // We need to position the chrome icon relative to its place in the pinned |
| // preference list - even if an item of that list isn't shown yet. |
| if (i == chrome_icon_index) |
| - apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); |
| + apps.AddApp(extension_misc::kChromeAppId); |
| const base::DictionaryValue* app_pref = nullptr; |
| if (!pinned_apps->GetDictionary(i, &app_pref)) { |
| LOG(ERROR) << "There is no dictionary for app entry."; |
| @@ -497,7 +481,7 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( |
| } |
| // If not added yet, the chrome item will be the last item in the list. |
| - apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); |
| + apps.AddApp(extension_misc::kChromeAppId); |
| return apps.app_list(); |
| } |
| @@ -576,26 +560,26 @@ syncer::StringOrdinal GetLastPinPosition(Profile* profile) { |
| : syncer::StringOrdinal::CreateInitialOrdinal(); |
| } |
| -std::vector<AppLauncherId> ImportLegacyPinnedApps( |
| +std::vector<std::string> ImportLegacyPinnedApps( |
| const PrefService* prefs, |
| LauncherControllerHelper* helper) { |
| - std::vector<AppLauncherId> legacy_pins_all = |
| + const std::vector<std::string> legacy_pins_all = |
| GetPinnedAppsFromPrefsLegacy(prefs, helper, false); |
| DCHECK(!legacy_pins_all.empty()); |
| app_list::AppListSyncableService* app_service = |
| app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); |
| - std::vector<AppLauncherId> legacy_pins_valid; |
| + std::vector<std::string> legacy_pins_valid; |
| syncer::StringOrdinal last_position = |
| syncer::StringOrdinal::CreateInitialOrdinal(); |
| // Convert to sync item record. |
| - for (const auto& app_launcher_id : legacy_pins_all) { |
| - DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); |
| - app_service->SetPinPosition(app_launcher_id.ToString(), last_position); |
| + for (const auto& app_id : legacy_pins_all) { |
| + DCHECK_NE(kPinnedAppsPlaceholder, app_id); |
| + app_service->SetPinPosition(app_id, last_position); |
| last_position = last_position.CreateAfter(); |
| - if (helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) |
| - legacy_pins_valid.push_back(app_launcher_id); |
| + if (helper->IsValidIDForCurrentUser(app_id)) |
| + legacy_pins_valid.push_back(app_id); |
| } |
| // Now process default apps. |
| @@ -640,8 +624,8 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefs( |
| continue; |
| } |
| - pin_infos.push_back(PinInfo(AppLauncherId(sync_peer.first), |
| - sync_peer.second->item_pin_ordinal)); |
| + pin_infos.push_back( |
| + PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal)); |
| } |
| if (first_run) { |
| @@ -649,11 +633,12 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefs( |
| sync_preferences::PrefServiceSyncable* const pref_service_syncable = |
| PrefServiceSyncableFromProfile(helper->profile()); |
| if (!pref_service_syncable->IsSyncing()) |
| - return GetPinnedAppsFromPrefsLegacy(prefs, helper, true); |
| + return AppIdsToAppLauncherIds( |
| + GetPinnedAppsFromPrefsLegacy(prefs, helper, true)); |
| // We need to import legacy pins model and convert it to sync based |
| // model. |
| - return ImportLegacyPinnedApps(prefs, helper); |
| + return AppIdsToAppLauncherIds(ImportLegacyPinnedApps(prefs, helper)); |
| } |
| // Sort pins according their ordinals. |
| @@ -664,30 +649,27 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefs( |
| // Pinned by policy apps appear first, if they were not shown before. |
| syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); |
| - std::vector<AppLauncherId>::const_reverse_iterator it; |
| + std::vector<std::string>::const_reverse_iterator it; |
| for (it = policy_apps.app_list().rbegin(); |
| it != policy_apps.app_list().rend(); ++it) { |
| - const std::string& app_launcher_id_str = (*it).ToString(); |
| - if (app_launcher_id_str == kPinnedAppsPlaceholder) |
| + const std::string& app_id = *it; |
| + if (app_id == kPinnedAppsPlaceholder) |
| continue; |
| // Check if we already processed current app. |
| - if (app_service->GetPinPosition(app_launcher_id_str).IsValid()) |
| + if (app_service->GetPinPosition(app_id).IsValid()) |
| continue; |
| // Now move it to the front. |
| - pin_infos.insert(pin_infos.begin(), |
| - PinInfo(AppLauncherId((*it).app_id(), (*it).launch_id()), |
| - front_position)); |
| - app_service->SetPinPosition(app_launcher_id_str, front_position); |
| + pin_infos.insert(pin_infos.begin(), PinInfo(*it, front_position)); |
| + app_service->SetPinPosition(app_id, front_position); |
| front_position = front_position.CreateBefore(); |
| } |
| // Now insert Chrome browser app if needed. |
| if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { |
| - pin_infos.insert( |
| - pin_infos.begin(), |
| - PinInfo(AppLauncherId(extension_misc::kChromeAppId), front_position)); |
| + pin_infos.insert(pin_infos.begin(), |
| + PinInfo(extension_misc::kChromeAppId, front_position)); |
| app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); |
| } |
| @@ -696,27 +678,34 @@ std::vector<AppLauncherId> GetPinnedAppsFromPrefs( |
| const syncer::StringOrdinal arc_host_position = |
| GetLastPinPosition(helper->profile()); |
| pin_infos.insert(pin_infos.begin(), |
| - PinInfo(AppLauncherId(ArcSupportHost::kHostAppId), |
| - arc_host_position)); |
| + PinInfo(ArcSupportHost::kHostAppId, arc_host_position)); |
| app_service->SetPinPosition(ArcSupportHost::kHostAppId, |
| arc_host_position); |
| } |
| } |
| // Convert to AppLauncherId array. |
| - std::vector<AppLauncherId> pins(pin_infos.size(), AppLauncherId()); |
| + std::vector<std::string> pins(pin_infos.size()); |
| for (size_t i = 0; i < pin_infos.size(); ++i) |
| - pins[i] = pin_infos[i].app_launcher_id; |
| + pins[i] = pin_infos[i].app_id; |
| - return pins; |
| + return AppIdsToAppLauncherIds(pins); |
| } |
| void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { |
| DCHECK(profile); |
| + |
| + const std::string& app_id = app_launcher_id.app_id(); |
| + if (!app_launcher_id.launch_id().empty()) { |
| + VLOG(2) << "Unpinning app '" << app_id << "' with non-empty launch id '" |
|
stevenjb
2017/02/08 20:32:37
nit: Unpinning works, just syncing does not, maybe
khmel
2017/02/08 21:20:39
That is better, thanks!
|
| + << app_launcher_id.launch_id() << "' is not supported."; |
| + return; |
| + } |
| + DCHECK(!app_id.empty()); |
| + |
| app_list::AppListSyncableService* app_service = |
| app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
| - app_service->SetPinPosition(app_launcher_id.ToString(), |
| - syncer::StringOrdinal()); |
| + app_service->SetPinPosition(app_id, syncer::StringOrdinal()); |
| } |
| void SetPinPosition(Profile* profile, |
| @@ -724,10 +713,18 @@ void SetPinPosition(Profile* profile, |
| const AppLauncherId& app_launcher_id_before, |
| const std::vector<AppLauncherId>& app_launcher_ids_after) { |
| DCHECK(profile); |
| - const std::string app_launcher_id_str = app_launcher_id.ToString(); |
| - const std::string app_launcher_id_before_str = |
| - app_launcher_id_before.ToString(); |
| - DCHECK_NE(app_launcher_id_str, app_launcher_id_before_str); |
| + |
| + const std::string& app_id = app_launcher_id.app_id(); |
| + if (!app_launcher_id.launch_id().empty()) { |
| + VLOG(2) << "Pinning app '" << app_id << "' with non-empty launch id '" |
|
stevenjb
2017/02/08 20:32:37
ditto
khmel
2017/02/08 21:20:40
Done.
|
| + << app_launcher_id.launch_id() << "' is not supported."; |
| + return; |
| + } |
| + |
| + const std::string& app_id_before = app_launcher_id_before.app_id(); |
| + |
| + DCHECK(!app_id.empty()); |
| + DCHECK_NE(app_id, app_id_before); |
| app_list::AppListSyncableService* app_service = |
| app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
| @@ -736,21 +733,17 @@ void SetPinPosition(Profile* profile, |
| return; |
| syncer::StringOrdinal position_before = |
| - app_launcher_id_before_str.empty() |
| - ? syncer::StringOrdinal() |
| - : app_service->GetPinPosition(app_launcher_id_before_str); |
| + app_id_before.empty() ? syncer::StringOrdinal() |
| + : app_service->GetPinPosition(app_id_before); |
| syncer::StringOrdinal position_after; |
| - for (const auto& app_launcher_id_after : app_launcher_ids_after) { |
| - const std::string app_launcher_id_after_str = |
| - app_launcher_id_after.ToString(); |
| - DCHECK_NE(app_launcher_id_after_str, app_launcher_id_str); |
| - DCHECK_NE(app_launcher_id_after_str, app_launcher_id_before_str); |
| - syncer::StringOrdinal position = |
| - app_service->GetPinPosition(app_launcher_id_after_str); |
| + for (const auto& app_launher_id_after : app_launcher_ids_after) { |
|
stevenjb
2017/02/08 20:32:37
app_launcher_id_after
khmel
2017/02/08 21:20:39
Done.
|
| + const std::string& app_id_after = app_launher_id_after.app_id(); |
| + DCHECK_NE(app_id_after, app_id); |
| + DCHECK_NE(app_id_after, app_id_before); |
| + syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after); |
| DCHECK(position.IsValid()); |
| if (!position.IsValid()) { |
| - LOG(ERROR) << "Sync pin position was not found for " |
| - << app_launcher_id_after_str; |
| + LOG(ERROR) << "Sync pin position was not found for " << app_id_after; |
| continue; |
| } |
| if (!position_before.IsValid() || !position.Equals(position_before)) { |
| @@ -768,7 +761,7 @@ void SetPinPosition(Profile* profile, |
| pin_position = position_after.CreateBefore(); |
| else |
| pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); |
| - app_service->SetPinPosition(app_launcher_id_str, pin_position); |
| + app_service->SetPinPosition(app_id, pin_position); |
| } |
| } // namespace launcher |