Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Side by Side Diff: chrome/browser/ui/ash/chrome_launcher_prefs.cc

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

Powered by Google App Engine
This is Rietveld 408576698