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