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