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), app_id_(app_id), launch_id_("") {} | |
James Cook
2016/10/05 15:33:10
nit: no need for "" (or even for listing launch_id
Andra Paraschiv
2016/10/06 07:49:47
Done.
| |
289 | |
290 AppLauncherId::AppLauncherId(const std::string& app_id, | |
291 const std::string& launch_id) | |
292 : app_launcher_id_(app_id + launch_id), | |
293 app_id_(app_id), | |
294 launch_id_(launch_id) {} | |
295 | |
296 AppLauncherId::~AppLauncherId() {} | |
297 | |
298 bool AppLauncherId::operator<(const AppLauncherId& other) const { | |
299 return app_launcher_id_ < other.app_launcher_id_; | |
300 } | |
301 | |
286 void RegisterChromeLauncherUserPrefs( | 302 void RegisterChromeLauncherUserPrefs( |
287 user_prefs::PrefRegistrySyncable* registry) { | 303 user_prefs::PrefRegistrySyncable* registry) { |
288 // TODO: If we want to support multiple profiles this will likely need to be | 304 // 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. | 305 // pushed to local state and we'll need to track profile per item. |
290 registry->RegisterIntegerPref( | 306 registry->RegisterIntegerPref( |
291 prefs::kShelfChromeIconIndex, | 307 prefs::kShelfChromeIconIndex, |
292 0, | 308 0, |
293 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 309 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
294 registry->RegisterListPref(prefs::kPinnedLauncherApps, | 310 registry->RegisterListPref(prefs::kPinnedLauncherApps, |
295 CreateDefaultPinnedAppsList(), | 311 CreateDefaultPinnedAppsList(), |
296 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 312 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
297 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); | 313 registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps); |
298 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, | 314 registry->RegisterStringPref(prefs::kShelfAutoHideBehavior, |
299 kShelfAutoHideBehaviorNever, | 315 kShelfAutoHideBehaviorNever, |
300 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 316 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
301 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, | 317 registry->RegisterStringPref(prefs::kShelfAutoHideBehaviorLocal, |
302 std::string()); | 318 std::string()); |
303 registry->RegisterStringPref(prefs::kShelfAlignment, | 319 registry->RegisterStringPref(prefs::kShelfAlignment, |
304 kShelfAlignmentBottom, | 320 kShelfAlignmentBottom, |
305 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 321 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
306 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); | 322 registry->RegisterStringPref(prefs::kShelfAlignmentLocal, std::string()); |
307 registry->RegisterDictionaryPref(prefs::kShelfPreferences); | 323 registry->RegisterDictionaryPref(prefs::kShelfPreferences); |
308 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); | 324 registry->RegisterIntegerPref(prefs::kLogoutDialogDurationMs, 20000); |
309 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); | 325 registry->RegisterBooleanPref(prefs::kShowLogoutButtonInTray, false); |
310 } | 326 } |
311 | 327 |
312 base::DictionaryValue* CreateAppDict(const std::string& app_id) { | 328 base::DictionaryValue* CreateAppDict(const AppLauncherId& app_launcher_id) { |
313 std::unique_ptr<base::DictionaryValue> app_value(new base::DictionaryValue); | 329 std::unique_ptr<base::DictionaryValue> app_value(new base::DictionaryValue); |
314 app_value->SetString(kPinnedAppsPrefAppIDPath, app_id); | 330 app_value->SetString(kPinnedAppsPrefAppIDPath, app_launcher_id.GetAsString()); |
315 return app_value.release(); | 331 return app_value.release(); |
316 } | 332 } |
317 | 333 |
318 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, | 334 ShelfAutoHideBehavior GetShelfAutoHideBehaviorPref(PrefService* prefs, |
319 int64_t display_id) { | 335 int64_t display_id) { |
320 DCHECK_NE(display_id, display::Display::kInvalidDisplayID); | 336 DCHECK_NE(display_id, display::Display::kInvalidDisplayID); |
321 | 337 |
322 // Don't show the shelf in app mode. | 338 // Don't show the shelf in app mode. |
323 if (chrome::IsRunningInAppMode()) | 339 if (chrome::IsRunningInAppMode()) |
324 return SHELF_AUTO_HIDE_ALWAYS_HIDDEN; | 340 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) | 417 if (!arc_app_list_pref) |
402 continue; | 418 continue; |
403 | 419 |
404 // We are dealing with package name, not with 32 characters ID. | 420 // We are dealing with package name, not with 32 characters ID. |
405 const std::string& arc_package = app_id; | 421 const std::string& arc_package = app_id; |
406 const std::vector<std::string> activities = GetActivitiesForPackage( | 422 const std::vector<std::string> activities = GetActivitiesForPackage( |
407 arc_package, all_arc_app_ids, *arc_app_list_pref); | 423 arc_package, all_arc_app_ids, *arc_app_list_pref); |
408 for (const auto& activity : activities) { | 424 for (const auto& activity : activities) { |
409 const std::string arc_app_id = | 425 const std::string arc_app_id = |
410 ArcAppListPrefs::GetAppId(arc_package, activity); | 426 ArcAppListPrefs::GetAppId(arc_package, activity); |
411 apps->MaybeAddApp(arc_app_id, helper); | 427 apps->MaybeAddApp(AppLauncherId(arc_app_id), helper); |
412 } | 428 } |
413 } else { | 429 } else { |
414 apps->MaybeAddApp(app_id, helper); | 430 apps->MaybeAddApp(AppLauncherId(app_id), helper); |
415 } | 431 } |
416 } | 432 } |
417 } | 433 } |
418 | 434 |
419 std::vector<std::string> GetPinnedAppsFromPrefsLegacy( | 435 std::vector<AppLauncherId> GetPinnedAppsFromPrefsLegacy( |
420 const PrefService* prefs, | 436 const PrefService* prefs, |
421 const LauncherControllerHelper* helper) { | 437 const LauncherControllerHelper* helper) { |
422 // Adding the app list item to the list of items requires that the ID is not | 438 // 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 | 439 // a valid and known ID for the extension system. The ID was constructed that |
424 // way - but just to make sure... | 440 // way - but just to make sure... |
425 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); | 441 DCHECK(!helper->IsValidIDForCurrentUser(kPinnedAppsPlaceholder)); |
426 | 442 |
427 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); | 443 const auto* pinned_apps = prefs->GetList(prefs::kPinnedLauncherApps); |
428 | 444 |
429 // Get the sanitized preference value for the index of the Chrome app icon. | 445 // Get the sanitized preference value for the index of the Chrome app icon. |
430 const size_t chrome_icon_index = std::max<size_t>( | 446 const size_t chrome_icon_index = std::max<size_t>( |
431 0, std::min<size_t>(pinned_apps->GetSize(), | 447 0, std::min<size_t>(pinned_apps->GetSize(), |
432 prefs->GetInteger(prefs::kShelfChromeIconIndex))); | 448 prefs->GetInteger(prefs::kShelfChromeIconIndex))); |
433 | 449 |
434 // Check if Chrome is in either of the the preferences lists. | 450 // Check if Chrome is in either of the the preferences lists. |
435 std::unique_ptr<base::Value> chrome_app( | 451 std::unique_ptr<base::Value> chrome_app( |
436 CreateAppDict(extension_misc::kChromeAppId)); | 452 CreateAppDict(AppLauncherId(extension_misc::kChromeAppId))); |
437 | 453 |
438 AppTracker apps; | 454 AppTracker apps; |
439 GetAppsPinnedByPolicy(prefs, helper, &apps); | 455 GetAppsPinnedByPolicy(prefs, helper, &apps); |
440 | 456 |
441 std::string app_id; | 457 std::string app_id; |
442 for (size_t i = 0; i < pinned_apps->GetSize(); ++i) { | 458 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 | 459 // 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. | 460 // preference list - even if an item of that list isn't shown yet. |
445 if (i == chrome_icon_index) | 461 if (i == chrome_icon_index) |
446 apps.AddApp(extension_misc::kChromeAppId); | 462 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); |
447 const base::DictionaryValue* app_pref = nullptr; | 463 const base::DictionaryValue* app_pref = nullptr; |
448 if (!pinned_apps->GetDictionary(i, &app_pref)) { | 464 if (!pinned_apps->GetDictionary(i, &app_pref)) { |
449 LOG(ERROR) << "There is no dictionary for app entry."; | 465 LOG(ERROR) << "There is no dictionary for app entry."; |
450 continue; | 466 continue; |
451 } | 467 } |
452 apps.MaybeAddAppFromPref(app_pref, helper); | 468 apps.MaybeAddAppFromPref(app_pref, helper); |
453 } | 469 } |
454 | 470 |
455 // If not added yet, the chrome item will be the last item in the list. | 471 // If not added yet, the chrome item will be the last item in the list. |
456 apps.AddApp(extension_misc::kChromeAppId); | 472 apps.AddApp(AppLauncherId(extension_misc::kChromeAppId)); |
457 return apps.app_list(); | 473 return apps.app_list(); |
458 } | 474 } |
459 | 475 |
460 // static | 476 // static |
461 std::unique_ptr<ChromeLauncherPrefsObserver> | 477 std::unique_ptr<ChromeLauncherPrefsObserver> |
462 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { | 478 ChromeLauncherPrefsObserver::CreateIfNecessary(Profile* profile) { |
463 syncable_prefs::PrefServiceSyncable* prefs = | 479 syncable_prefs::PrefServiceSyncable* prefs = |
464 PrefServiceSyncableFromProfile(profile); | 480 PrefServiceSyncableFromProfile(profile); |
465 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || | 481 if (!prefs->FindPreference(prefs::kShelfAlignmentLocal)->HasUserSetting() || |
466 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) | 482 !prefs->FindPreference(prefs::kShelfAutoHideBehaviorLocal) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
525 if (!position.IsValid() || | 541 if (!position.IsValid() || |
526 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { | 542 sync_peer.second->item_pin_ordinal.GreaterThan(position)) { |
527 position = sync_peer.second->item_pin_ordinal; | 543 position = sync_peer.second->item_pin_ordinal; |
528 } | 544 } |
529 } | 545 } |
530 | 546 |
531 return position.IsValid() ? position.CreateAfter() | 547 return position.IsValid() ? position.CreateAfter() |
532 : syncer::StringOrdinal::CreateInitialOrdinal(); | 548 : syncer::StringOrdinal::CreateInitialOrdinal(); |
533 } | 549 } |
534 | 550 |
535 std::vector<std::string> ImportLegacyPinnedApps( | 551 std::vector<AppLauncherId> ImportLegacyPinnedApps( |
536 const PrefService* prefs, | 552 const PrefService* prefs, |
537 LauncherControllerHelper* helper, | 553 LauncherControllerHelper* helper, |
538 const AppTracker& policy_apps) { | 554 const AppTracker& policy_apps) { |
539 std::vector<std::string> legacy_pins = | 555 std::vector<AppLauncherId> legacy_pins = |
540 GetPinnedAppsFromPrefsLegacy(prefs, helper); | 556 GetPinnedAppsFromPrefsLegacy(prefs, helper); |
541 DCHECK(!legacy_pins.empty()); | 557 DCHECK(!legacy_pins.empty()); |
542 | 558 |
543 app_list::AppListSyncableService* app_service = | 559 app_list::AppListSyncableService* app_service = |
544 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); | 560 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); |
545 | 561 |
546 syncer::StringOrdinal last_position = | 562 syncer::StringOrdinal last_position = |
547 syncer::StringOrdinal::CreateInitialOrdinal(); | 563 syncer::StringOrdinal::CreateInitialOrdinal(); |
548 // Convert to sync item record. | 564 // Convert to sync item record. |
549 for (const auto& app_id : legacy_pins) { | 565 for (const auto& app_launcher_id : legacy_pins) { |
550 DCHECK_NE(kPinnedAppsPlaceholder, app_id); | 566 DCHECK_NE(kPinnedAppsPlaceholder, app_launcher_id.GetAsString()); |
551 app_service->SetPinPosition(app_id, last_position); | 567 app_service->SetPinPosition(app_launcher_id.GetAsString(), last_position); |
552 last_position = last_position.CreateAfter(); | 568 last_position = last_position.CreateAfter(); |
553 } | 569 } |
554 | 570 |
555 // Now process default apps. | 571 // Now process default apps. |
556 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { | 572 for (size_t i = 0; i < arraysize(kDefaultPinnedApps); ++i) { |
557 const std::string& app_id = kDefaultPinnedApps[i]; | 573 const std::string& app_id = kDefaultPinnedApps[i]; |
558 // Check if it is already imported. | 574 // Check if it is already imported. |
559 if (app_service->GetPinPosition(app_id).IsValid()) | 575 if (app_service->GetPinPosition(app_id).IsValid()) |
560 continue; | 576 continue; |
561 // Check if it is present but not in legacy pin. | 577 // Check if it is present but not in legacy pin. |
562 if (helper->IsValidIDForCurrentUser(app_id)) | 578 if (helper->IsValidIDForCurrentUser(app_id)) |
563 continue; | 579 continue; |
564 app_service->SetPinPosition(app_id, last_position); | 580 app_service->SetPinPosition(app_id, last_position); |
565 last_position = last_position.CreateAfter(); | 581 last_position = last_position.CreateAfter(); |
566 } | 582 } |
567 | 583 |
568 return legacy_pins; | 584 return legacy_pins; |
569 } | 585 } |
570 | 586 |
571 std::vector<std::string> GetPinnedAppsFromPrefs( | 587 std::vector<AppLauncherId> GetPinnedAppsFromPrefs( |
572 const PrefService* prefs, | 588 const PrefService* prefs, |
573 LauncherControllerHelper* helper) { | 589 LauncherControllerHelper* helper) { |
574 app_list::AppListSyncableService* app_service = | 590 app_list::AppListSyncableService* app_service = |
575 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); | 591 app_list::AppListSyncableServiceFactory::GetForProfile(helper->profile()); |
576 // Some unit tests may not have it. | 592 // Some unit tests may not have it. |
577 if (!app_service) | 593 if (!app_service) |
578 return std::vector<std::string>(); | 594 return std::vector<AppLauncherId>(); |
579 | 595 |
580 std::vector<PinInfo> pin_infos; | 596 std::vector<PinInfo> pin_infos; |
581 | 597 |
582 AppTracker policy_apps; | 598 AppTracker policy_apps; |
583 GetAppsPinnedByPolicy(prefs, helper, &policy_apps); | 599 GetAppsPinnedByPolicy(prefs, helper, &policy_apps); |
584 | 600 |
585 // Empty pins indicates that sync based pin model is used for the first | 601 // 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. | 602 // time. In normal workflow we have at least Chrome browser pin info. |
587 bool first_run = true; | 603 bool first_run = true; |
588 | 604 |
589 for (const auto& sync_peer : app_service->sync_items()) { | 605 for (const auto& sync_peer : app_service->sync_items()) { |
590 if (!sync_peer.second->item_pin_ordinal.IsValid()) | 606 if (!sync_peer.second->item_pin_ordinal.IsValid()) |
591 continue; | 607 continue; |
592 | 608 |
593 first_run = false; | 609 first_run = false; |
594 // Don't include apps that currently do not exist on device. | 610 // Don't include apps that currently do not exist on device. |
595 if (sync_peer.first != extension_misc::kChromeAppId && | 611 if (sync_peer.first != extension_misc::kChromeAppId && |
596 !helper->IsValidIDForCurrentUser(sync_peer.first)) { | 612 !helper->IsValidIDForCurrentUser(sync_peer.first)) { |
597 continue; | 613 continue; |
598 } | 614 } |
599 | 615 |
600 pin_infos.push_back( | 616 pin_infos.push_back(PinInfo(AppLauncherId(sync_peer.first), |
601 PinInfo(sync_peer.first, sync_peer.second->item_pin_ordinal)); | 617 sync_peer.second->item_pin_ordinal)); |
602 } | 618 } |
603 | 619 |
604 if (first_run) { | 620 if (first_run) { |
605 // We need to import legacy pins model and convert it to sync based | 621 // We need to import legacy pins model and convert it to sync based |
606 // model. | 622 // model. |
607 return ImportLegacyPinnedApps(prefs, helper, policy_apps); | 623 return ImportLegacyPinnedApps(prefs, helper, policy_apps); |
608 } | 624 } |
609 | 625 |
610 // Sort pins according their ordinals. | 626 // Sort pins according their ordinals. |
611 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); | 627 std::sort(pin_infos.begin(), pin_infos.end(), ComparePinInfo()); |
612 | 628 |
613 // Pinned by policy apps appear first, if they were not shown before. | 629 // Pinned by policy apps appear first, if they were not shown before. |
614 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); | 630 syncer::StringOrdinal front_position = GetFirstPinPosition(helper->profile()); |
615 std::vector<std::string>::const_reverse_iterator it; | 631 std::vector<AppLauncherId>::const_reverse_iterator it; |
616 for (it = policy_apps.app_list().rbegin(); | 632 for (it = policy_apps.app_list().rbegin(); |
617 it != policy_apps.app_list().rend(); ++it) { | 633 it != policy_apps.app_list().rend(); ++it) { |
618 const std::string& app_id = *it; | 634 const std::string& app_id = (*it).GetAsString(); |
619 if (app_id == kPinnedAppsPlaceholder) | 635 if (app_id == kPinnedAppsPlaceholder) |
620 continue; | 636 continue; |
621 | 637 |
622 // Check if we already processed current app. | 638 // Check if we already processed current app. |
623 if (app_service->GetPinPosition(app_id).IsValid()) | 639 if (app_service->GetPinPosition(app_id).IsValid()) |
624 continue; | 640 continue; |
625 | 641 |
626 // Now move it to the front. | 642 // Now move it to the front. |
627 pin_infos.insert(pin_infos.begin(), PinInfo(app_id, front_position)); | 643 pin_infos.insert(pin_infos.begin(), |
644 PinInfo(AppLauncherId(app_id), front_position)); | |
628 app_service->SetPinPosition(app_id, front_position); | 645 app_service->SetPinPosition(app_id, front_position); |
629 front_position = front_position.CreateBefore(); | 646 front_position = front_position.CreateBefore(); |
630 } | 647 } |
631 | 648 |
632 // Now insert Chrome browser app if needed. | 649 // Now insert Chrome browser app if needed. |
633 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { | 650 if (!app_service->GetPinPosition(extension_misc::kChromeAppId).IsValid()) { |
634 pin_infos.insert(pin_infos.begin(), | 651 pin_infos.insert( |
635 PinInfo(extension_misc::kChromeAppId, front_position)); | 652 pin_infos.begin(), |
653 PinInfo(AppLauncherId(extension_misc::kChromeAppId), front_position)); | |
636 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); | 654 app_service->SetPinPosition(extension_misc::kChromeAppId, front_position); |
637 } | 655 } |
638 | 656 |
639 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { | 657 if (helper->IsValidIDForCurrentUser(ArcSupportHost::kHostAppId)) { |
640 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { | 658 if (!app_service->GetSyncItem(ArcSupportHost::kHostAppId)) { |
641 const syncer::StringOrdinal arc_host_position = | 659 const syncer::StringOrdinal arc_host_position = |
642 GetLastPinPosition(helper->profile()); | 660 GetLastPinPosition(helper->profile()); |
643 pin_infos.insert(pin_infos.begin(), | 661 pin_infos.insert(pin_infos.begin(), |
644 PinInfo(ArcSupportHost::kHostAppId, arc_host_position)); | 662 PinInfo(AppLauncherId(ArcSupportHost::kHostAppId), |
663 arc_host_position)); | |
645 app_service->SetPinPosition(ArcSupportHost::kHostAppId, | 664 app_service->SetPinPosition(ArcSupportHost::kHostAppId, |
646 arc_host_position); | 665 arc_host_position); |
647 } | 666 } |
648 } | 667 } |
649 | 668 |
650 // Convert to string array. | 669 // Convert to AppLauncherId array. |
651 std::vector<std::string> pins(pin_infos.size()); | 670 std::vector<AppLauncherId> pins; |
652 for (size_t i = 0; i < pin_infos.size(); ++i) | 671 for (size_t i = 0; i < pin_infos.size(); ++i) |
653 pins[i] = pin_infos[i].app_id; | 672 pins.push_back(pin_infos[i].app_launcher_id); |
654 | 673 |
655 return pins; | 674 return pins; |
656 } | 675 } |
657 | 676 |
658 void RemovePinPosition(Profile* profile, const std::string& app_id) { | 677 void RemovePinPosition(Profile* profile, const AppLauncherId& app_launcher_id) { |
659 DCHECK(profile); | 678 DCHECK(profile); |
660 DCHECK(!app_id.empty()); | 679 const std::string launcher_id = app_launcher_id.GetAsString(); |
680 DCHECK(!launcher_id.empty()); | |
661 app_list::AppListSyncableService* app_service = | 681 app_list::AppListSyncableService* app_service = |
662 app_list::AppListSyncableServiceFactory::GetForProfile(profile); | 682 app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
663 app_service->SetPinPosition(app_id, syncer::StringOrdinal()); | 683 app_service->SetPinPosition(launcher_id, syncer::StringOrdinal()); |
664 } | 684 } |
665 | 685 |
666 void SetPinPosition(Profile* profile, | 686 void SetPinPosition(Profile* profile, |
667 const std::string& app_id, | 687 const AppLauncherId& app_launcher_id, |
668 const std::string& app_id_before, | 688 const AppLauncherId& app_launcher_id_before, |
669 const std::vector<std::string>& app_ids_after) { | 689 const std::vector<AppLauncherId>& app_launcher_ids_after) { |
670 DCHECK(profile); | 690 DCHECK(profile); |
671 DCHECK(!app_id.empty()); | 691 const std::string launcher_id = app_launcher_id.GetAsString(); |
672 DCHECK_NE(app_id, app_id_before); | 692 DCHECK(!launcher_id.empty()); |
693 const std::string launcher_id_before = app_launcher_id_before.GetAsString(); | |
694 DCHECK_NE(launcher_id, launcher_id_before); | |
673 | 695 |
674 app_list::AppListSyncableService* app_service = | 696 app_list::AppListSyncableService* app_service = |
675 app_list::AppListSyncableServiceFactory::GetForProfile(profile); | 697 app_list::AppListSyncableServiceFactory::GetForProfile(profile); |
676 // Some unit tests may not have this service. | 698 // Some unit tests may not have this service. |
677 if (!app_service) | 699 if (!app_service) |
678 return; | 700 return; |
679 | 701 |
680 syncer::StringOrdinal position_before = | 702 syncer::StringOrdinal position_before = |
681 app_id_before.empty() ? syncer::StringOrdinal() | 703 launcher_id_before.empty() |
682 : app_service->GetPinPosition(app_id_before); | 704 ? syncer::StringOrdinal() |
705 : app_service->GetPinPosition(launcher_id_before); | |
683 syncer::StringOrdinal position_after; | 706 syncer::StringOrdinal position_after; |
684 for (const auto& app_id_after : app_ids_after) { | 707 for (const auto& app_launcher_id_after : app_launcher_ids_after) { |
685 DCHECK_NE(app_id_after, app_id); | 708 const std::string launcher_id_after = app_launcher_id_after.GetAsString(); |
686 DCHECK_NE(app_id_after, app_id_before); | 709 DCHECK_NE(launcher_id_after, launcher_id); |
687 syncer::StringOrdinal position = app_service->GetPinPosition(app_id_after); | 710 DCHECK_NE(launcher_id_after, launcher_id_before); |
711 syncer::StringOrdinal position = | |
712 app_service->GetPinPosition(launcher_id_after); | |
688 DCHECK(position.IsValid()); | 713 DCHECK(position.IsValid()); |
689 if (!position.IsValid()) { | 714 if (!position.IsValid()) { |
690 LOG(ERROR) << "Sync pin position was not found for " << app_id_after; | 715 LOG(ERROR) << "Sync pin position was not found for " << launcher_id_after; |
691 continue; | 716 continue; |
692 } | 717 } |
693 if (!position_before.IsValid() || !position.Equals(position_before)) { | 718 if (!position_before.IsValid() || !position.Equals(position_before)) { |
694 position_after = position; | 719 position_after = position; |
695 break; | 720 break; |
696 } | 721 } |
697 } | 722 } |
698 | 723 |
699 syncer::StringOrdinal pin_position; | 724 syncer::StringOrdinal pin_position; |
700 if (position_before.IsValid() && position_after.IsValid()) | 725 if (position_before.IsValid() && position_after.IsValid()) |
701 pin_position = position_before.CreateBetween(position_after); | 726 pin_position = position_before.CreateBetween(position_after); |
702 else if (position_before.IsValid()) | 727 else if (position_before.IsValid()) |
703 pin_position = position_before.CreateAfter(); | 728 pin_position = position_before.CreateAfter(); |
704 else if (position_after.IsValid()) | 729 else if (position_after.IsValid()) |
705 pin_position = position_after.CreateBefore(); | 730 pin_position = position_after.CreateBefore(); |
706 else | 731 else |
707 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); | 732 pin_position = syncer::StringOrdinal::CreateInitialOrdinal(); |
708 app_service->SetPinPosition(app_id, pin_position); | 733 app_service->SetPinPosition(launcher_id, pin_position); |
709 } | 734 } |
710 | 735 |
711 } // namespace launcher | 736 } // namespace launcher |
712 } // namespace ash | 737 } // namespace ash |
OLD | NEW |