| OLD | NEW |
| 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 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // App ID of default pinned apps. | 38 // App ID of default pinned apps. |
| 39 const char* kDefaultPinnedApps[] = { | 39 const char* kDefaultPinnedApps[] = { |
| 40 extension_misc::kGmailAppId, extension_misc::kGoogleDocAppId, | 40 extension_misc::kGmailAppId, extension_misc::kGoogleDocAppId, |
| 41 extension_misc::kYoutubeAppId, ArcSupportHost::kHostAppId}; | 41 extension_misc::kYoutubeAppId, ArcSupportHost::kHostAppId}; |
| 42 | 42 |
| 43 base::ListValue* CreateDefaultPinnedAppsList() { | 43 base::ListValue* CreateDefaultPinnedAppsList() { |
| 44 std::unique_ptr<base::ListValue> apps(new base::ListValue); | 44 std::unique_ptr<base::ListValue> apps(new base::ListValue); |
| 45 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) | 45 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) |
| 46 apps->Append(CreateAppDict(kDefaultPinnedApps[i])); | 46 apps->Append(CreateAppDict(AppLauncherId(kDefaultPinnedApps[i]))); |
| 47 | 47 |
| 48 return apps.release(); | 48 return apps.release(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Returns the preference value for the display with the given |display_id|. | 51 // Returns the preference value for the display with the given |display_id|. |
| 52 // The pref value is stored in |local_path| and |path|, but the pref service may | 52 // The pref value is stored in |local_path| and |path|, but the pref service may |
| 53 // have per-display preferences and the value can be specified by policy. | 53 // have per-display preferences and the value can be specified by policy. |
| 54 // Here is the priority: | 54 // Here is the priority: |
| 55 // * A value managed by policy. This is a single value that applies to all | 55 // * A value managed by policy. This is a single value that applies to all |
| 56 // displays. | 56 // displays. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // copied to |local_path|. | 200 // copied to |local_path|. |
| 201 void PropagatePrefToLocalIfNotSet( | 201 void PropagatePrefToLocalIfNotSet( |
| 202 syncable_prefs::PrefServiceSyncable* pref_service, | 202 syncable_prefs::PrefServiceSyncable* pref_service, |
| 203 const char* local_path, | 203 const char* local_path, |
| 204 const char* synced_path) { | 204 const char* synced_path) { |
| 205 if (!pref_service->FindPreference(local_path)->HasUserSetting()) | 205 if (!pref_service->FindPreference(local_path)->HasUserSetting()) |
| 206 pref_service->SetString(local_path, pref_service->GetString(synced_path)); | 206 pref_service->SetString(local_path, pref_service->GetString(synced_path)); |
| 207 } | 207 } |
| 208 | 208 |
| 209 struct PinInfo { | 209 struct PinInfo { |
| 210 PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal) | 210 PinInfo(const AppLauncherId& app_launcher_id, |
| 211 : app_id(app_id), item_ordinal(item_ordinal) {} | 211 const syncer::StringOrdinal& item_ordinal) |
| 212 : app_launcher_id(app_launcher_id), item_ordinal(item_ordinal) {} |
| 212 | 213 |
| 213 std::string app_id; | 214 AppLauncherId app_launcher_id; |
| 214 syncer::StringOrdinal item_ordinal; | 215 syncer::StringOrdinal item_ordinal; |
| 215 }; | 216 }; |
| 216 | 217 |
| 217 struct ComparePinInfo { | 218 struct ComparePinInfo { |
| 218 bool operator()(const PinInfo& pin1, const PinInfo& pin2) { | 219 bool operator()(const PinInfo& pin1, const PinInfo& pin2) { |
| 219 return pin1.item_ordinal.LessThan(pin2.item_ordinal); | 220 return pin1.item_ordinal.LessThan(pin2.item_ordinal); |
| 220 } | 221 } |
| 221 }; | 222 }; |
| 222 | 223 |
| 223 // Helper class to keep apps in order of appearance and to provide fast way | 224 // Helper class to keep apps in order of appearance and to provide fast way |
| 224 // to check if app exists in the list. | 225 // to check if app exists in the list. |
| 225 class AppTracker { | 226 class AppTracker { |
| 226 public: | 227 public: |
| 227 bool HasApp(const std::string& app_id) const { | 228 bool HasApp(const AppLauncherId& app_launcher_id) const { |
| 228 return app_set_.find(app_id) != app_set_.end(); | 229 return app_set_.find(app_launcher_id) != app_set_.end(); |
| 229 } | 230 } |
| 230 | 231 |
| 231 void AddApp(const std::string& app_id) { | 232 void AddApp(const AppLauncherId& app_launcher_id) { |
| 232 if (HasApp(app_id)) | 233 if (HasApp(app_launcher_id)) |
| 233 return; | 234 return; |
| 234 app_list_.push_back(app_id); | 235 app_list_.push_back(app_launcher_id); |
| 235 app_set_.insert(app_id); | 236 app_set_.insert(app_launcher_id); |
| 236 } | 237 } |
| 237 | 238 |
| 238 void MaybeAddApp(const std::string& app_id, | 239 void MaybeAddApp(const AppLauncherId& app_launcher_id, |
| 239 const LauncherControllerHelper* helper) { | 240 const LauncherControllerHelper* helper) { |
| 240 DCHECK_NE(kPinnedAppsPlaceholder, app_id); | 241 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); |
| 241 if (!helper->IsValidIDForCurrentUser(app_id)) | 242 if (!helper->IsValidIDForCurrentUser(app_launcher_id.ToString())) |
| 242 return; | 243 return; |
| 243 AddApp(app_id); | 244 AddApp(app_launcher_id); |
| 244 } | 245 } |
| 245 | 246 |
| 246 void MaybeAddAppFromPref(const base::DictionaryValue* app_pref, | 247 void MaybeAddAppFromPref(const base::DictionaryValue* app_pref, |
| 247 const LauncherControllerHelper* helper) { | 248 const LauncherControllerHelper* helper) { |
| 248 std::string app_id; | 249 std::string app_id; |
| 249 if (!app_pref->GetString(kPinnedAppsPrefAppIDPath, &app_id)) { | 250 if (!app_pref->GetString(kPinnedAppsPrefAppIDPath, &app_id)) { |
| 250 LOG(ERROR) << "Cannot get app id from app pref entry."; | 251 LOG(ERROR) << "Cannot get app id from app pref entry."; |
| 251 return; | 252 return; |
| 252 } | 253 } |
| 253 | 254 |
| 254 if (app_id == kPinnedAppsPlaceholder) | 255 if (app_id == kPinnedAppsPlaceholder) |
| 255 return; | 256 return; |
| 256 | 257 |
| 257 bool pinned_by_policy = false; | 258 bool pinned_by_policy = false; |
| 258 if (app_pref->GetBoolean(kPinnedAppsPrefPinnedByPolicy, | 259 if (app_pref->GetBoolean(kPinnedAppsPrefPinnedByPolicy, |
| 259 &pinned_by_policy) && | 260 &pinned_by_policy) && |
| 260 pinned_by_policy) { | 261 pinned_by_policy) { |
| 261 return; | 262 return; |
| 262 } | 263 } |
| 263 | 264 |
| 264 MaybeAddApp(app_id, helper); | 265 MaybeAddApp(AppLauncherId(app_id), helper); |
| 265 } | 266 } |
| 266 | 267 |
| 267 const std::vector<std::string>& app_list() const { return app_list_; } | 268 const std::vector<AppLauncherId>& app_list() const { return app_list_; } |
| 268 | 269 |
| 269 private: | 270 private: |
| 270 std::vector<std::string> app_list_; | 271 std::vector<AppLauncherId> app_list_; |
| 271 std::set<std::string> app_set_; | 272 std::set<AppLauncherId> app_set_; |
| 272 }; | 273 }; |
| 273 | 274 |
| 274 } // namespace | 275 } // namespace |
| 275 | 276 |
| 276 const char kPinnedAppsPrefAppIDPath[] = "id"; | 277 const char kPinnedAppsPrefAppIDPath[] = "id"; |
| 277 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy"; | 278 const char kPinnedAppsPrefPinnedByPolicy[] = "pinned_by_policy"; |
| 278 const char kPinnedAppsPlaceholder[] = "AppShelfIDPlaceholder--------"; | 279 const char kPinnedAppsPlaceholder[] = "AppShelfIDPlaceholder--------"; |
| 279 | 280 |
| 280 const char kShelfAutoHideBehaviorAlways[] = "Always"; | 281 const char kShelfAutoHideBehaviorAlways[] = "Always"; |
| 281 const char kShelfAutoHideBehaviorNever[] = "Never"; | 282 const char kShelfAutoHideBehaviorNever[] = "Never"; |
| 282 | 283 |
| 283 const char kShelfAlignmentBottom[] = "Bottom"; | 284 const char kShelfAlignmentBottom[] = "Bottom"; |
| 284 const char kShelfAlignmentLeft[] = "Left"; | 285 const char kShelfAlignmentLeft[] = "Left"; |
| 285 const char kShelfAlignmentRight[] = "Right"; | 286 const char kShelfAlignmentRight[] = "Right"; |
| 286 | 287 |
| 288 AppLauncherId::AppLauncherId(const std::string& app_id, |
| 289 const std::string& launch_id) |
| 290 : app_id_(app_id), launch_id_(launch_id) { |
| 291 DCHECK(!app_id.empty()); |
| 292 } |
| 293 |
| 294 AppLauncherId::AppLauncherId(const std::string& app_id) : app_id_(app_id) { |
| 295 DCHECK(!app_id.empty()); |
| 296 } |
| 297 |
| 298 AppLauncherId::AppLauncherId() {} |
| 299 |
| 300 AppLauncherId::~AppLauncherId() {} |
| 301 |
| 302 std::string AppLauncherId::ToString() const { |
| 303 return app_id_ + launch_id_; |
| 304 } |
| 305 |
| 306 bool AppLauncherId::operator<(const AppLauncherId& other) const { |
| 307 return ToString() < other.ToString(); |
| 308 } |
| 309 |
| 287 void RegisterChromeLauncherUserPrefs( | 310 void RegisterChromeLauncherUserPrefs( |
| 288 user_prefs::PrefRegistrySyncable* registry) { | 311 user_prefs::PrefRegistrySyncable* registry) { |
| 289 // TODO: If we want to support multiple profiles this will likely need to be | 312 // TODO: If we want to support multiple profiles this will likely need to be |
| 290 // pushed to local state and we'll need to track profile per item. | 313 // pushed to local state and we'll need to track profile per item. |
| 291 registry->RegisterIntegerPref( | 314 registry->RegisterIntegerPref( |
| 292 prefs::kShelfChromeIconIndex, | 315 prefs::kShelfChromeIconIndex, |
| 293 0, | 316 0, |
| 294 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 317 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 295 registry->RegisterListPref(prefs::kPinnedLauncherApps, | 318 registry->RegisterListPref(prefs::kPinnedLauncherApps, |
| 296 CreateDefaultPinnedAppsList(), | 319 CreateDefaultPinnedAppsList(), |
| 297 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 320 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 298 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); | 321 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); |
| 299 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, | 322 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, |
| 300 kShelfAutoHideBehaviorNever, | 323 kShelfAutoHideBehaviorNever, |
| 301 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 324 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 302 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, | 325 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, |
| 303 std::string()); | 326 std::string()); |
| 304 registry->RegisterStringPref(prefs::kShelfAlignment, | 327 registry->RegisterStringPref(prefs::kShelfAlignment, |
| 305 kShelfAlignmentBottom, | 328 kShelfAlignmentBottom, |
| 306 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 329 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 307 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); | 330 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); |
| 308 registry->RegisterDictionaryPref(prefs::kShelfPreferences); | 331 registry->RegisterDictionaryPref(prefs::kShelfPreferences); |
| 309 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); | 332 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); |
| 310 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); | 333 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); |
| 311 } | 334 } |
| 312 | 335 |
| 313 std::unique_ptr<base::DictionaryValue> CreateAppDict( | 336 std::unique_ptr<base::DictionaryValue> CreateAppDict( |
| 314 const std::string& app_id) { | 337 const AppLauncherId& app_launcher_id) { |
| 315 auto app_value = base::MakeUnique<base::DictionaryValue>(); | 338 auto app_value = base::MakeUnique<base::DictionaryValue>(); |
| 316 app_value->SetString(kPinnedAppsPrefAppIDPath, app_id); | 339 app_value->SetString(kPinnedAppsPrefAppIDPath, app_launcher_id.ToString()); |
| 317 return app_value; | 340 return app_value; |
| 318 } | 341 } |
| 319 | 342 |
| 320 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, | 343 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, |
| 321 int64_t display_id) { | 344 int64_t display_id) { |
| 322 DCHECK_NE(display_id, display::Display::kInvalidDisplayID); | 345 DCHECK_NE(display_id, display::Display::kInvalidDisplayID); |
| 323 | 346 |
| 324 // Don't show the shelf in app mode. | 347 // Don't show the shelf in app mode. |
| 325 if (chrome::IsRunningInAppMode()) | 348 if (chrome::IsRunningInAppMode()) |
| 326 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN; | 349 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 if (!arc_app_list_pref) | 426 if (!arc_app_list_pref) |
| 404 continue; | 427 continue; |
| 405 | 428 |
| 406 // We are dealing with package name, not with 32 characters ID. | 429 // We are dealing with package name, not with 32 characters ID. |
| 407 const std::string& arc_package = app_id; | 430 const std::string& arc_package = app_id; |
| 408 const std::vector<std::string> activities = GetActivitiesForPackage( | 431 const std::vector<std::string> activities = GetActivitiesForPackage( |
| 409 arc_package, all_arc_app_ids, *arc_app_list_pref); | 432 arc_package, all_arc_app_ids, *arc_app_list_pref); |
| 410 for (const auto& activity : activities) { | 433 for (const auto& activity : activities) { |
| 411 const std::string arc_app_id = | 434 const std::string arc_app_id = |
| 412 ArcAppListPrefs::GetAppId(arc_package, activity); | 435 ArcAppListPrefs::GetAppId(arc_package, activity); |
| 413 apps->MaybeAddApp(arc_app_id, helper); | 436 apps->MaybeAddApp(AppLauncherId(arc_app_id), helper); |
| 414 } | 437 } |
| 415 } else { | 438 } else { |
| 416 apps->MaybeAddApp(app_id, helper); | 439 apps->MaybeAddApp(AppLauncherId(app_id), helper); |
| 417 } | 440 } |
| 418 } | 441 } |
| 419 } | 442 } |
| 420 | 443 |
| 421 std::vector<std::string> GetPinnedAppsFromPrefsLegacy( | 444 std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( |
| 422 const PrefService* prefs, | 445 const PrefService* prefs, |
| 423 const LauncherControllerHelper* helper) { | 446 const LauncherControllerHelper* helper) { |
| 424 // Adding the app list item to the list of items requires that the ID is not | 447 // Adding the app list item to the list of items requires that the ID is not |
| 425 // a valid and known ID for the extension system. The ID was constructed that | 448 // a valid and known ID for the extension system. The ID was constructed that |
| 426 // way - but just to make sure... | 449 // way - but just to make sure... |
| 427 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); | 450 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); |
| 428 | 451 |
| 429 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); | 452 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); |
| 430 | 453 |
| 431 // Get the sanitized preference value for the index of the Chrome app icon. | 454 // Get the sanitized preference value for the index of the Chrome app icon. |
| 432 const size_t chrome_icon_index = std::max<size_t>( | 455 const size_t chrome_icon_index = std::max<size_t>( |
| 433 0, std::min<size_t>(pinned_apps->GetSize(), | 456 0, std::min<size_t>(pinned_apps->GetSize(), |
| 434 prefs->GetInteger(prefs::kShelfChromeIconIndex))); | 457 prefs->GetInteger(prefs::kShelfChromeIconIndex))); |
| 435 | 458 |
| 436 // Check if Chrome is in either of the the preferences lists. | 459 // Check if Chrome is in either of the the preferences lists. |
| 437 std::unique_ptr<base::Value> chrome_app( | 460 std::unique_ptr<base::Value> chrome_app( |
| 438 CreateAppDict(extension_misc::kChromeAppId)); | 461 CreateAppDict(AppLauncherId(extension_misc::kChromeAppId))); |
| 439 | 462 |
| 440 AppTracker apps; | 463 AppTracker apps; |
| 441 GetAppsPinnedByPolicy(prefs, helper, &apps); | 464 GetAppsPinnedByPolicy(prefs, helper, &apps); |
| 442 | 465 |
| 443 std::string app_id; | 466 std::string app_id; |
| 444 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) { | 467 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) { |
| 445 // We need to position the chrome icon relative to its place in the pinned | 468 // We need to position the chrome icon relative to its place in the pinned |
| 446 // preference list - even if an item of that list isn't shown yet. | 469 // preference list - even if an item of that list isn't shown yet. |
| 447 if (i == chrome_icon_index) | 470 if (i == chrome_icon_index) |
| 448 apps.AddApp(extension_misc::kChromeAppId); | 471 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); |
| 449 const base::DictionaryValue* app_pref = nullptr; | 472 const base::DictionaryValue* app_pref = nullptr; |
| 450 if (!pinned_apps->GetDictionary(i, &app_pref)) { | 473 if (!pinned_apps->GetDictionary(i, &app_pref)) { |
| 451 LOG(ERROR) << "There is no dictionary for app entry."; | 474 LOG(ERROR) << "There is no dictionary for app entry."; |
| 452 continue; | 475 continue; |
| 453 } | 476 } |
| 454 apps.MaybeAddAppFromPref(app_pref, helper); | 477 apps.MaybeAddAppFromPref(app_pref, helper); |
| 455 } | 478 } |
| 456 | 479 |
| 457 // If not added yet, the chrome item will be the last item in the list. | 480 // If not added yet, the chrome item will be the last item in the list. |
| 458 apps.AddApp(extension_misc::kChromeAppId); | 481 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); |
| 459 return apps.app_list(); | 482 return apps.app_list(); |
| 460 } | 483 } |
| 461 | 484 |
| 462 // static | 485 // static |
| 463 std::unique_ptr<ChromeLauncherPrefsObserver> | 486 std::unique_ptr<ChromeLauncherPrefsObserver> |
| 464 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { | 487 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { |
| 465 syncable_prefs::PrefServiceSyncable* prefs = | 488 syncable_prefs::PrefServiceSyncable* prefs = |
| 466 PrefServiceSyncableFromProfile(profile); | 489 PrefServiceSyncableFromProfile(profile); |
| 467 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || | 490 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || |
| 468 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) | 491 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 if (!position.IsValid() || | 550 if (!position.IsValid() || |
| 528 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { | 551 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { |
| 529 position = sync_peer.second->item_pin_ordinal; | 552 position = sync_peer.second->item_pin_ordinal; |
| 530 } | 553 } |
| 531 } | 554 } |
| 532 | 555 |
| 533 return position.IsValid() ? position.CreateAfter() | 556 return position.IsValid() ? position.CreateAfter() |
| 534 : syncer::StringOrdinal::CreateInitialOrdinal(); | 557 : syncer::StringOrdinal::CreateInitialOrdinal(); |
| 535 } | 558 } |
| 536 | 559 |
| 537 std::vector<std::string> ImportLegacyPinnedApps( | 560 std::vector<AppLauncherId> ImportLegacyPinnedApps( |
| 538 const PrefService* prefs, | 561 const PrefService* prefs, |
| 539 LauncherControllerHelper* helper, | 562 LauncherControllerHelper* helper, |
| 540 const AppTracker& policy_apps) { | 563 const AppTracker& policy_apps) { |
| 541 std::vector<std::string> legacy_pins = | 564 std::vector<AppLauncherId> legacy_pins = |
| 542 GetPinnedAppsFromPrefsLegacy(prefs, helper); | 565 GetPinnedAppsFromPrefsLegacy(prefs, helper); |
| 543 DCHECK(!legacy_pins.empty()); | 566 DCHECK(!legacy_pins.empty()); |
| 544 | 567 |
| 545 app_list::AppListSyncableService* app_service = | 568 app_list::AppListSyncableService* app_service = |
| 546 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); | 569 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); |
| 547 | 570 |
| 548 syncer::StringOrdinal last_position = | 571 syncer::StringOrdinal last_position = |
| 549 syncer::StringOrdinal::CreateInitialOrdinal(); | 572 syncer::StringOrdinal::CreateInitialOrdinal(); |
| 550 // Convert to sync item record. | 573 // Convert to sync item record. |
| 551 for (const auto& app_id : legacy_pins) { | 574 for (const auto& app_launcher_id : legacy_pins) { |
| 552 DCHECK_NE(kPinnedAppsPlaceholder, app_id); | 575 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.ToString()); |
| 553 app_service->SetPinPosition(app_id, last_position); | 576 app_service->SetPinPosition(app_launcher_id.ToString(), last_position); |
| 554 last_position = last_position.CreateAfter(); | 577 last_position = last_position.CreateAfter(); |
| 555 } | 578 } |
| 556 | 579 |
| 557 // Now process default apps. | 580 // Now process default apps. |
| 558 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { | 581 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { |
| 559 const std::string& app_id = kDefaultPinnedApps[i]; | 582 const std::string& app_id = kDefaultPinnedApps[i]; |
| 560 // Check if it is already imported. | 583 // Check if it is already imported. |
| 561 if (app_service->GetPinPosition(app_id).IsValid()) | 584 if (app_service->GetPinPosition(app_id).IsValid()) |
| 562 continue; | 585 continue; |
| 563 // Check if it is present but not in legacy pin. | 586 // Check if it is present but not in legacy pin. |
| 564 if (helper->IsValidIDForCurrentUser(app_id)) | 587 if (helper->IsValidIDForCurrentUser(app_id)) |
| 565 continue; | 588 continue; |
| 566 app_service->SetPinPosition(app_id, last_position); | 589 app_service->SetPinPosition(app_id, last_position); |
| 567 last_position = last_position.CreateAfter(); | 590 last_position = last_position.CreateAfter(); |
| 568 } | 591 } |
| 569 | 592 |
| 570 return legacy_pins; | 593 return legacy_pins; |
| 571 } | 594 } |
| 572 | 595 |
| 573 std::vector<std::string> GetPinnedAppsFromPrefs( | 596 std::vector<AppLauncherId> GetPinnedAppsFromPrefs( |
| 574 const PrefService* prefs, | 597 const PrefService* prefs, |
| 575 LauncherControllerHelper* helper) { | 598 LauncherControllerHelper* helper) { |
| 576 app_list::AppListSyncableService* app_service = | 599 app_list::AppListSyncableService* app_service = |
| 577 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); | 600 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); |
| 578 // Some unit tests may not have it. | 601 // Some unit tests may not have it. |
| 579 if (!app_service) | 602 if (!app_service) |
| 580 return std::vector<std::string>(); | 603 return std::vector<AppLauncherId>(); |
| 581 | 604 |
| 582 std::vector<PinInfo> pin_infos; | 605 std::vector<PinInfo> pin_infos; |
| 583 | 606 |
| 584 AppTracker policy_apps; | 607 AppTracker policy_apps; |
| 585 GetAppsPinnedByPolicy(prefs, helper, &policy_apps); | 608 GetAppsPinnedByPolicy(prefs, helper, &policy_apps); |
| 586 | 609 |
| 587 // Empty pins indicates that sync based pin model is used for the first | 610 // Empty pins indicates that sync based pin model is used for the first |
| 588 // time. In normal workflow we have at least Chrome browser pin info. | 611 // time. In normal workflow we have at least Chrome browser pin info. |
| 589 bool first_run = true; | 612 bool first_run = true; |
| 590 | 613 |
| 591 for (const auto& sync_peer : app_service->sync_items()) { | 614 for (const auto& sync_peer : app_service->sync_items()) { |
| 592 if (!sync_peer.second->item_pin_ordinal.IsValid()) | 615 if (!sync_peer.second->item_pin_ordinal.IsValid()) |
| 593 continue; | 616 continue; |
| 594 | 617 |
| 595 first_run = false; | 618 first_run = false; |
| 596 // Don't include apps that currently do not exist on device. | 619 // Don't include apps that currently do not exist on device. |
| 597 if (sync_peer.first != extension_misc::kChromeAppId && | 620 if (sync_peer.first != extension_misc::kChromeAppId && |
| 598 !helper->IsValidIDForCurrentUser(sync_peer.first)) { | 621 !helper->IsValidIDForCurrentUser(sync_peer.first)) { |
| 599 continue; | 622 continue; |
| 600 } | 623 } |
| 601 | 624 |
| 602 pin_infos.push_back( | 625 pin_infos.push_back(PinInfo(AppLauncherId(sync_peer.first), |
| 603 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal)); | 626 sync_peer.second->item_pin_ordinal)); |
| 604 } | 627 } |
| 605 | 628 |
| 606 if (first_run) { | 629 if (first_run) { |
| 607 // We need to import legacy pins model and convert it to sync based | 630 // We need to import legacy pins model and convert it to sync based |
| 608 // model. | 631 // model. |
| 609 return ImportLegacyPinnedApps(prefs, helper, policy_apps); | 632 return ImportLegacyPinnedApps(prefs, helper, policy_apps); |
| 610 } | 633 } |
| 611 | 634 |
| 612 // Sort pins according their ordinals. | 635 // Sort pins according their ordinals. |
| 613 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); | 636 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); |
| 614 | 637 |
| 615 // Pinned by policy apps appear first, if they were not shown before. | 638 // Pinned by policy apps appear first, if they were not shown before. |
| 616 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); | 639 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); |
| 617 std::vector<std::string>::const_reverse_iterator it; | 640 std::vector<AppLauncherId>::const_reverse_iterator it; |
| 618 for (it = policy_apps.app_list().rbegin(); | 641 for (it = policy_apps.app_list().rbegin(); |
| 619 it != policy_apps.app_list().rend(); ++it) { | 642 it != policy_apps.app_list().rend(); ++it) { |
| 620 const std::string& app_id = *it; | 643 const std::string& app_id = (*it).ToString(); |
| 621 if (app_id == kPinnedAppsPlaceholder) | 644 if (app_id == kPinnedAppsPlaceholder) |
| 622 continue; | 645 continue; |
| 623 | 646 |
| 624 // Check if we already processed current app. | 647 // Check if we already processed current app. |
| 625 if (app_service->GetPinPosition(app_id).IsValid()) | 648 if (app_service->GetPinPosition(app_id).IsValid()) |
| 626 continue; | 649 continue; |
| 627 | 650 |
| 628 // Now move it to the front. | 651 // Now move it to the front. |
| 629 pin_infos.insert(pin_infos.begin(), PinInfo(app_id, front_position)); | 652 pin_infos.insert(pin_infos.begin(), |
| 653 PinInfo(AppLauncherId(app_id), front_position)); |
| 630 app_service->SetPinPosition(app_id, front_position); | 654 app_service->SetPinPosition(app_id, front_position); |
| 631 front_position = front_position.CreateBefore(); | 655 front_position = front_position.CreateBefore(); |
| 632 } | 656 } |
| 633 | 657 |
| 634 // Now insert Chrome browser app if needed. | 658 // Now insert Chrome browser app if needed. |
| 635 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { | 659 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { |
| 636 pin_infos.insert(pin_infos.begin(), | 660 pin_infos.insert( |
| 637 PinInfo(extension_misc::kChromeAppId, front_position)); | 661 pin_infos.begin(), |
| 662 PinInfo(AppLauncherId(extension_misc::kChromeAppId), front_position)); |
| 638 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); | 663 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); |
| 639 } | 664 } |
| 640 | 665 |
| 641 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { | 666 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { |
| 642 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { | 667 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { |
| 643 const syncer::StringOrdinal arc_host_position = | 668 const syncer::StringOrdinal arc_host_position = |
| 644 GetLastPinPosition(helper->profile()); | 669 GetLastPinPosition(helper->profile()); |
| 645 pin_infos.insert(pin_infos.begin(), | 670 pin_infos.insert(pin_infos.begin(), |
| 646 PinInfo(ArcSupportHost::kHostAppId, arc_host_position)); | 671 PinInfo(AppLauncherId(ArcSupportHost::kHostAppId), |
| 672 arc_host_position)); |
| 647 app_service->SetPinPosition(ArcSupportHost::kHostAppId, | 673 app_service->SetPinPosition(ArcSupportHost::kHostAppId, |
| 648 arc_host_position); | 674 arc_host_position); |
| 649 } | 675 } |
| 650 } | 676 } |
| 651 | 677 |
| 652 // Convert to string array. | 678 // Convert to AppLauncherId array. |
| 653 std::vector<std::string> pins(pin_infos.size()); | 679 std::vector<AppLauncherId> pins(pin_infos.size(), AppLauncherId()); |
| 654 for (size_t i = 0; i < pin_infos.size(); ++i) | 680 for (size_t i = 0; i < pin_infos.size(); ++i) |
| 655 pins[i] = pin_infos[i].app_id; | 681 pins[i] = pin_infos[i].app_launcher_id; |
| 656 | 682 |
| 657 return pins; | 683 return pins; |
| 658 } | 684 } |
| 659 | 685 |
| 660 void RemovePinPosition(Profile* profile, const std::string& app_id) { | 686 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { |
| 661 DCHECK(profile); | 687 DCHECK(profile); |
| 662 DCHECK(!app_id.empty()); | |
| 663 app_list::AppListSyncableService* app_service = | 688 app_list::AppListSyncableService* app_service = |
| 664 app_list::AppListSyncableServiceFactory::GetForProfile(profile); | 689 app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
| 665 app_service->SetPinPosition(app_id, syncer::StringOrdinal()); | 690 app_service->SetPinPosition(app_launcher_id.ToString(), |
| 691 syncer::StringOrdinal()); |
| 666 } | 692 } |
| 667 | 693 |
| 668 void SetPinPosition(Profile* profile, | 694 void SetPinPosition(Profile* profile, |
| 669 const std::string& app_id, | 695 const AppLauncherId& app_launcher_id, |
| 670 const std::string& app_id_before, | 696 const AppLauncherId& app_launcher_id_before, |
| 671 const std::vector<std::string>& app_ids_after) { | 697 const std::vector<AppLauncherId>& app_launcher_ids_after) { |
| 672 DCHECK(profile); | 698 DCHECK(profile); |
| 673 DCHECK(!app_id.empty()); | 699 const std::string app_launcher_id_str = app_launcher_id.ToString(); |
| 674 DCHECK_NE(app_id, app_id_before); | 700 const std::string app_launcher_id_before_str = |
| 701 app_launcher_id_before.ToString(); |
| 702 DCHECK_NE(app_launcher_id_str, app_launcher_id_before_str); |
| 675 | 703 |
| 676 app_list::AppListSyncableService* app_service = | 704 app_list::AppListSyncableService* app_service = |
| 677 app_list::AppListSyncableServiceFactory::GetForProfile(profile); | 705 app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
| 678 // Some unit tests may not have this service. | 706 // Some unit tests may not have this service. |
| 679 if (!app_service) | 707 if (!app_service) |
| 680 return; | 708 return; |
| 681 | 709 |
| 682 syncer::StringOrdinal position_before = | 710 syncer::StringOrdinal position_before = |
| 683 app_id_before.empty() ? syncer::StringOrdinal() | 711 app_launcher_id_before_str.empty() |
| 684 : app_service->GetPinPosition(app_id_before); | 712 ? syncer::StringOrdinal() |
| 713 : app_service->GetPinPosition(app_launcher_id_before_str); |
| 685 syncer::StringOrdinal position_after; | 714 syncer::StringOrdinal position_after; |
| 686 for (const auto& app_id_after : app_ids_after) { | 715 for (const auto& app_launcher_id_after : app_launcher_ids_after) { |
| 687 DCHECK_NE(app_id_after, app_id); | 716 const std::string app_launcher_id_after_str = |
| 688 DCHECK_NE(app_id_after, app_id_before); | 717 app_launcher_id_after.ToString(); |
| 689 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after); | 718 DCHECK_NE(app_launcher_id_after_str, app_launcher_id_str); |
| 719 DCHECK_NE(app_launcher_id_after_str, app_launcher_id_before_str); |
| 720 syncer::StringOrdinal position = |
| 721 app_service->GetPinPosition(app_launcher_id_after_str); |
| 690 DCHECK(position.IsValid()); | 722 DCHECK(position.IsValid()); |
| 691 if (!position.IsValid()) { | 723 if (!position.IsValid()) { |
| 692 LOG(ERROR) << "Sync pin position was not found for " << app_id_after; | 724 LOG(ERROR) << "Sync pin position was not found for " |
| 725 << app_launcher_id_after_str; |
| 693 continue; | 726 continue; |
| 694 } | 727 } |
| 695 if (!position_before.IsValid() || !position.Equals(position_before)) { | 728 if (!position_before.IsValid() || !position.Equals(position_before)) { |
| 696 position_after = position; | 729 position_after = position; |
| 697 break; | 730 break; |
| 698 } | 731 } |
| 699 } | 732 } |
| 700 | 733 |
| 701 syncer::StringOrdinal pin_position; | 734 syncer::StringOrdinal pin_position; |
| 702 if (position_before.IsValid() && position_after.IsValid()) | 735 if (position_before.IsValid() && position_after.IsValid()) |
| 703 pin_position = position_before.CreateBetween(position_after); | 736 pin_position = position_before.CreateBetween(position_after); |
| 704 else if (position_before.IsValid()) | 737 else if (position_before.IsValid()) |
| 705 pin_position = position_before.CreateAfter(); | 738 pin_position = position_before.CreateAfter(); |
| 706 else if (position_after.IsValid()) | 739 else if (position_after.IsValid()) |
| 707 pin_position = position_after.CreateBefore(); | 740 pin_position = position_after.CreateBefore(); |
| 708 else | 741 else |
| 709 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); | 742 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); |
| 710 app_service->SetPinPosition(app_id, pin_position); | 743 app_service->SetPinPosition(app_launcher_id_str, pin_position); |
| 711 } | 744 } |
| 712 | 745 |
| 713 } // namespace launcher | 746 } // namespace launcher |
| 714 } // namespace ash | 747 } // namespace ash |
| OLD | NEW |