File: chrome_shelf_prefs.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (677 lines) | stat: -rw-r--r-- 25,894 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/ash/shelf/chrome_shelf_prefs.h"

#include <stddef.h>

#include <algorithm>
#include <map>
#include <memory>
#include <ostream>
#include <utility>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/webui/projector_app/public/cpp/projector_app_constants.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/extend.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_piece_forward.h"
#include "base/values.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/policy_util.h"
#include "chrome/browser/ash/app_list/app_list_syncable_service.h"
#include "chrome/browser/ash/app_list/app_list_syncable_service_factory.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/crosapi/browser_util.h"
#include "chrome/browser/ash/file_manager/app_id.h"
#include "chrome/browser/ash/file_manager/prefs_migration_uma.h"
#include "chrome/browser/ash/login/demo_mode/demo_session.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/prefs/pref_service_syncable_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/ui/ash/default_pinned_apps.h"
#include "chrome/browser/ui/ash/shelf/shelf_controller_helper.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/common/pref_names.h"
#include "components/app_constants/constants.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/model/string_ordinal.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "extensions/common/constants.h"

namespace {

// Returns pinned app position even if app is not currently visible on device
// that is leftmost item on the shelf. If |exclude_chrome| is true then Chrome
// app is not processed. if nothing pinned found, returns an invalid ordinal.
syncer::StringOrdinal GetFirstPinnedAppPosition(
    app_list::AppListSyncableService* syncable_service,
    bool exclude_chrome) {
  syncer::StringOrdinal position;
  for (const auto& [item_id, sync_item] : syncable_service->sync_items()) {
    if (!sync_item->item_pin_ordinal.IsValid()) {
      continue;
    }
    if (exclude_chrome && (item_id == app_constants::kChromeAppId ||
                           item_id == app_constants::kLacrosAppId ||
                           item_id == app_constants::kAshDebugBrowserAppId)) {
      continue;
    }
    if (!position.IsValid() || sync_item->item_pin_ordinal.LessThan(position)) {
      position = sync_item->item_pin_ordinal;
    }
  }
  return position;
}

// Helper to create pin position that stays before any synced app, even if
// app is not currently visible on a device.
syncer::StringOrdinal CreateFirstPinPosition(
    app_list::AppListSyncableService* syncable_service) {
  const syncer::StringOrdinal position =
      GetFirstPinnedAppPosition(syncable_service, false /* exclude_chrome */);
  return position.IsValid() ? position.CreateBefore()
                            : syncer::StringOrdinal::CreateInitialOrdinal();
}

// Ensures |app_id| is pinned. If it is not pinned, makes it pinned in the first
// position.
void EnsurePinnedOrMakeFirst(
    const std::string& app_id,
    app_list::AppListSyncableService* syncable_service) {
  // This piece prevents accidental side-effects to the SetPinPosition() call
  // below.
  CHECK(app_id == app_constants::kChromeAppId ||
        app_id == app_constants::kLacrosAppId);
  syncer::StringOrdinal position = syncable_service->GetPinPosition(app_id);
  if (!position.IsValid()) {
    position = CreateFirstPinPosition(syncable_service);
    syncable_service->SetPinPosition(app_id, position,
                                     /*is_policy_initiated=*/false);
  }
}

constexpr char kDefaultPinnedAppsKey[] = "default";

bool skip_pinned_apps_from_sync_for_test = false;

bool should_add_default_apps_for_test = false;

struct PinInfo {
  PinInfo(const std::string& app_id, const syncer::StringOrdinal& item_ordinal)
      : app_id(app_id), item_ordinal(item_ordinal) {}

  std::string app_id;
  syncer::StringOrdinal item_ordinal;
};

// Helper function that returns the right pref string based on device type.
// This is required because tablet form factor devices do not sync app
// positions and pin preferences.
std::string GetShelfDefaultPinLayoutPref() {
  if (ash::switches::IsTabletFormFactor())
    return prefs::kShelfDefaultPinLayoutRollsForTabletFormFactor;

  return prefs::kShelfDefaultPinLayoutRolls;
}

// Returns true in case default pin layout configuration could be applied
// safely. That means all required components are synced or worked in local
// mode.
bool IsSafeToApplyDefaultPinLayout(Profile* profile) {
  const syncer::SyncService* sync_service =
      SyncServiceFactory::GetForProfile(profile);
  // No |sync_service| in incognito mode.
  if (!sync_service) {
    return true;
  }

  // Tablet form-factor devices do not have position sync.
  if (ash::switches::IsTabletFormFactor()) {
    return true;
  }

  // Some browser tests don't start sync fully as there is no server to download
  // the initial data from. This prevents applying the default pin layout,
  // required in some tests. To support this, the behavior can be overridden via
  // command-line flags.
  if (ash::switches::ShouldAllowDefaultShelfPinLayoutIgnoringSync()) {
    return true;
  }

  const syncer::SyncUserSettings* settings = sync_service->GetUserSettings();

  // If App sync is not yet started, don't apply default pin apps once synced
  // apps is likely override it. There is a case when App sync is disabled and
  // in last case local cache is available immediately.
  if (sync_service->IsSyncFeatureEnabled() &&
      settings->GetSelectedOsTypes().Has(
          syncer::UserSelectableOsType::kOsApps) &&
      !app_list::AppListSyncableServiceFactory::GetForProfile(profile)
           ->IsSyncing()) {
    return false;
  }

  // If shelf pin layout rolls preference is not started yet then we cannot say
  // if we rolled layout or not.
  if (sync_service->IsSyncFeatureEnabled() &&
      settings->GetSelectedOsTypes().Has(
          syncer::UserSelectableOsType::kOsPreferences) &&
      !PrefServiceSyncableFromProfile(profile)->AreOsPrefsSyncing()) {
    return false;
  }
  return true;
}

bool IsOnlyPolicyPinned(app_list::AppListSyncableService::SyncItem* sync_item) {
  return sync_item->is_user_pinned.has_value() &&
         !sync_item->is_user_pinned.value() &&
         ash::features::IsRemoveStalePolicyPinnedAppsFromShelfEnabled();
}

// In order to ensure that the chrome icon in the shelf is consistent across
// devices, we must apply the following rules:
// (1) If lacros is the only web-browser (lacros_only), transform [sync id]
// kChromeAppId <-> [shelf id] kLacrosAppId
// (2) If lacros is the only web-browser and ash debug browser is enabled,
// transform [sync id] kAshDebugBrowserAppId <-> [shelf id] kChromeAppId
std::string GetShelfId(const std::string& sync_id) {
  if (!crosapi::browser_util::IsAshWebBrowserEnabled()) {
    if (sync_id == app_constants::kChromeAppId) {
      return app_constants::kLacrosAppId;
    }
    if (ash::switches::IsAshDebugBrowserEnabled() &&
        sync_id == app_constants::kAshDebugBrowserAppId) {
      return app_constants::kChromeAppId;
    }
  }

  return sync_id;
}

// In order to ensure that the chrome icon in the shelf is consistent across
// devices, we must apply the following rules:
// (1) If lacros is the only web-browser (lacros_only), transform [shelf id]
// kLacrosAppId <-> [sync id] kChromeAppId
// (2) If lacros is the only web-browser and ash debug browser is enabled,
// transform [shelf id] kChromeAppId <-> [sync id] kAshDebugBrowserAppId
std::string GetSyncId(const std::string& shelf_id) {
  if (!crosapi::browser_util::IsAshWebBrowserEnabled()) {
    if (shelf_id == app_constants::kLacrosAppId) {
      return app_constants::kChromeAppId;
    }
    if (ash::switches::IsAshDebugBrowserEnabled() &&
        shelf_id == app_constants::kChromeAppId) {
      return app_constants::kAshDebugBrowserAppId;
    }
  }

  return shelf_id;
}

}  // namespace

ChromeShelfPrefs::ChromeShelfPrefs(Profile* profile) : profile_(profile) {}

ChromeShelfPrefs::~ChromeShelfPrefs() = default;

void ChromeShelfPrefs::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterListPref(prefs::kPolicyPinnedLauncherApps);
  registry->RegisterListPref(
      prefs::kShelfDefaultPinLayoutRolls,
      user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
  registry->RegisterListPref(
      prefs::kShelfDefaultPinLayoutRollsForTabletFormFactor,
      PrefRegistry::NO_REGISTRATION_FLAGS);
}

void ChromeShelfPrefs::InitLocalPref(PrefService* prefs,
                                     const char* local,
                                     const char* synced) {
  // Ash's prefs *should* have been propagated to Chrome by now, but maybe not.
  // This belongs in Ash, but it can't observe syncing changes: crbug.com/774657
  if (prefs->FindPreference(local) && prefs->FindPreference(synced) &&
      !prefs->FindPreference(local)->HasUserSetting()) {
    prefs->SetString(local, prefs->GetString(synced));
  }
}

// Helper that extracts app list from policy preferences.
std::vector<std::string> ChromeShelfPrefs::GetAppsPinnedByPolicy(
    Profile* profile) {
  CHECK(profile);
  const base::Value::List& policy_apps =
      profile->GetPrefs()->GetList(prefs::kPolicyPinnedLauncherApps);
  if (policy_apps.empty()) {
    return {};
  }

  std::vector<std::string> policy_entries;
  for (const auto& policy_app : policy_apps) {
    if (!policy_app.is_dict()) {
      continue;
    }
    const std::string* policy_entry = policy_app.GetDict().FindString(
        ChromeShelfPrefs::kPinnedAppsPrefAppIDKey);
    if (!policy_entry) {
      LOG(ERROR) << "Cannot extract policy app info from prefs.";
      continue;
    }

    if (ash::DemoSession::Get() &&
        !ash::DemoSession::Get()->ShouldShowAndroidOrChromeAppInShelf(
            *policy_entry)) {
      continue;
    }

    policy_entries.push_back(apps_util::TransformRawPolicyId(*policy_entry));
  }

  if (policy_entries.empty()) {
    return {};
  }

  std::vector<std::string> results;
  for (const auto& policy_entry : policy_entries) {
    std::vector<std::string> app_ids =
        apps_util::GetAppIdsFromPolicyId(profile, policy_entry);
    if (app_ids.empty()) {
      LOG(WARNING) << "No matching app(s) found for |policy_entry| = "
                   << policy_entry;
      continue;
    }
    base::Extend(results, std::move(app_ids));
  }

  return results;
}

// Helper to create and insert pins on the shelf for the set of apps defined in
// |app_ids| after Chrome in the first position and before any other pinned app.
// If Chrome is not the first pinned app then apps are pinned before any other
// app.
void InsertPinsAfterChromeAndBeforeFirstPinnedApp(
    app_list::AppListSyncableService* syncable_service,
    const std::vector<std::string>& app_ids,
    bool is_policy_initiated) {
  // Chrome must be pinned at this point.
  syncer::StringOrdinal chrome_position =
      syncable_service->GetPinPosition(app_constants::kChromeAppId);
  DCHECK(chrome_position.IsValid());

  // New pins are inserted after this position.
  syncer::StringOrdinal after;
  // New pins are inserted before this position.
  syncer::StringOrdinal before =
      GetFirstPinnedAppPosition(syncable_service, /*exclude_chrome=*/true);

  if (!before.IsValid()) {
    before = chrome_position.CreateAfter();
  }

  if (before.GreaterThan(chrome_position)) {
    // Perfect case, Chrome is the first pinned app and we have next pinned app.
    after = chrome_position;
  } else {
    after = before.CreateBefore();
  }

  for (const auto& app_id : app_ids) {
    // Check if we already processed the current app.
    auto* sync_item = syncable_service->GetSyncItem(app_id);
    if (sync_item && sync_item->item_pin_ordinal.IsValid()) {
      // If `is_user_pinned` is currently unknown but the incoming pin is
      // triggered by a change to policy, set `is_user_pinned` to false.
      if (is_policy_initiated && !sync_item->is_user_pinned.has_value() &&
          ash::features::IsRemoveStalePolicyPinnedAppsFromShelfEnabled()) {
        syncable_service->SetIsPolicyPinned(app_id);
      }
      continue;
    }
    const syncer::StringOrdinal position = after.CreateBetween(before);
    syncable_service->SetPinPosition(app_id, position, is_policy_initiated);

    // Shift after position, next policy pin position will be created after
    // current item.
    after = position;
  }
}

std::vector<ash::ShelfID> ChromeShelfPrefs::GetPinnedAppsFromSync(
    ShelfControllerHelper* helper) {
  auto* syncable_service =
      app_list::AppListSyncableServiceFactory::GetForProfile(profile_);

  // Some unit tests may not have it or service may not be initialized.
  if (!syncable_service || !syncable_service->IsInitialized() ||
      skip_pinned_apps_from_sync_for_test) {
    return std::vector<ash::ShelfID>();
  }

  if (!sync_service_observer_.IsObserving()) {
    sync_service_observer_.Observe(syncable_service);
  }

  if (ShouldPerformConsistencyMigrations()) {
    needs_consistency_migrations_ = false;
    MigrateFilesChromeAppToSWA();
    EnsureChromePinned();
    EnsureProjectorShelfPinConsistency();
  }

  // This migration must be run outside of the consistency migrations block
  // since the timing can occur later, after apps have been synced.
  if (!DidAddDefaultApps() && ShouldAddDefaultApps()) {
    AddDefaultApps();
  }

  // Handle pins, forced by policy. In case Chrome is first app they are added
  // after Chrome, otherwise they are added to the front. Note, we handle apps
  // that may not be currently on device. At this case pin position would be
  // preallocated and apps will appear on shelf in deterministic order, even if
  // their install order differ.
  std::vector<std::string> policy_pinned_apps = GetAppsPinnedByPolicy(profile_);
  InsertPinsAfterChromeAndBeforeFirstPinnedApp(syncable_service,
                                               policy_pinned_apps,
                                               /*is_policy_initiated=*/true);

  // If Lacros is enabled and allowed for this user type, ensure the Lacros icon
  // is pinned. Lacros doesn't support multi-signin, so only add the icon for
  // the primary user.
  if (crosapi::browser_util::IsLacrosEnabled() &&
      ash::ProfileHelper::IsPrimaryProfile(profile_)) {
    syncer::StringOrdinal lacros_position =
        syncable_service->GetPinPosition(app_constants::kLacrosAppId);
    if (!lacros_position.IsValid()) {
      // If Lacros isn't already pinned, add it to the right of the Chrome icon.
      InsertPinsAfterChromeAndBeforeFirstPinnedApp(
          syncable_service, {app_constants::kLacrosAppId},
          /*is_policy_initiated=*/false);
    }
  }

  std::vector<std::string> policy_delta_remove_from_shelf;

  std::vector<PinInfo> pin_infos;

  // Empty pins indicates that sync based pin model is used for the first
  // time. In the normal workflow we have at least Chrome browser pin info.
  for (const auto& [item_id, sync_item] : syncable_service->sync_items()) {
    // A null ordinal means the item has been unpinned.
    if (!sync_item->item_pin_ordinal.IsValid()) {
      continue;
    }

    // kChromeAppId is the only valid sync ID for the browser.
    if (item_id == app_constants::kLacrosAppId) {
      continue;
    }

    std::string app_id = GetShelfId(item_id);

    // All sync items must be valid app service apps to be added to the shelf
    // with the exception of ash-chrome, which for legacy reasons does not use
    // the app service.
    bool is_ash_chrome = app_id == app_constants::kChromeAppId;
    if (!is_ash_chrome && !helper->IsValidIDForCurrentUser(app_id) &&
        !ShelfControllerHelper::IsPromiseApp(profile_, app_id)) {
      continue;
    }

    // Prune apps that used to be policy-pinned (`is_user_pinned = false`), but
    // are not a part of the policy anymore.
    if (!is_ash_chrome && IsOnlyPolicyPinned(sync_item.get()) &&
        !base::Contains(policy_pinned_apps, item_id) &&
        !ShelfControllerHelper::IsPromiseApp(profile_, app_id)) {
      policy_delta_remove_from_shelf.push_back(item_id);
      continue;
    }
    pin_infos.emplace_back(std::move(app_id), sync_item->item_pin_ordinal);
  }

  for (const auto& item_id : policy_delta_remove_from_shelf) {
    syncable_service->RemovePinPosition(item_id);
  }

  // Sort pins according their ordinals.
  base::ranges::sort(pin_infos, syncer::StringOrdinal::LessThanFn(),
                     &PinInfo::item_ordinal);

  // Convert to ShelfID array.
  std::vector<ash::ShelfID> pins;
  base::ranges::transform(
      pin_infos, std::back_inserter(pins),
      [](const auto& pin_info) { return ash::ShelfID(pin_info.app_id); });

  return pins;
}

void ChromeShelfPrefs::RemovePinPosition(const ash::ShelfID& shelf_id) {
  DCHECK(profile_);

  const std::string& app_id = shelf_id.app_id;
  if (!shelf_id.launch_id.empty()) {
    VLOG(2) << "Syncing remove pin for '" << app_id
            << "' with non-empty launch id '" << shelf_id.launch_id
            << "' is not supported.";
    return;
  }
  DCHECK(!app_id.empty());
  app_list::AppListSyncableServiceFactory::GetForProfile(profile_)
      ->RemovePinPosition(app_id);
}

void ChromeShelfPrefs::SetPinPosition(
    const ash::ShelfID& shelf_id,
    const ash::ShelfID& shelf_id_before,
    const std::vector<ash::ShelfID>& shelf_ids_after,
    bool pinned_by_policy) {
  const std::string app_id = GetSyncId(shelf_id.app_id);

  if (!shelf_id.launch_id.empty()) {
    VLOG(2) << "Syncing set pin for '" << app_id
            << "' with non-empty launch id '" << shelf_id.launch_id
            << "' is not supported.";
    return;
  }

  const std::string app_id_before = GetSyncId(shelf_id_before.app_id);

  DCHECK(!app_id.empty());
  DCHECK_NE(app_id, app_id_before);

  auto* syncable_service =
      app_list::AppListSyncableServiceFactory::GetForProfile(profile_);
  // Some unit tests may not have this service.
  if (!syncable_service)
    return;

  syncer::StringOrdinal position_before =
      app_id_before.empty() ? syncer::StringOrdinal()
                            : syncable_service->GetPinPosition(app_id_before);
  syncer::StringOrdinal position_after;
  for (const auto& shelf_id_after : shelf_ids_after) {
    std::string app_id_after = GetSyncId(shelf_id_after.app_id);
    DCHECK_NE(app_id_after, app_id);
    DCHECK_NE(app_id_after, app_id_before);
    syncer::StringOrdinal position =
        syncable_service->GetPinPosition(app_id_after);
    DCHECK(position.IsValid());
    if (!position.IsValid()) {
      LOG(ERROR) << "Sync pin position was not found for " << app_id_after;
      continue;
    }
    if (!position_before.IsValid() || !position.Equals(position_before)) {
      position_after = position;
      break;
    }
  }

  syncer::StringOrdinal pin_position;
  if (position_before.IsValid() && position_after.IsValid())
    pin_position = position_before.CreateBetween(position_after);
  else if (position_before.IsValid())
    pin_position = position_before.CreateAfter();
  else if (position_after.IsValid())
    pin_position = position_after.CreateBefore();
  else
    pin_position = syncer::StringOrdinal::CreateInitialOrdinal();
  syncable_service->SetPinPosition(app_id, pin_position, pinned_by_policy);
}

void ChromeShelfPrefs::SkipPinnedAppsFromSyncForTest() {
  skip_pinned_apps_from_sync_for_test = true;
}

void ChromeShelfPrefs::SetShouldAddDefaultAppsForTest() {
  should_add_default_apps_for_test = true;
}

void ChromeShelfPrefs::MigrateFilesChromeAppToSWA() {
  PrefService* prefs = profile_->GetPrefs();
  if (prefs->GetBoolean(ash::prefs::kFilesAppUIPrefsMigrated)) {
    return;
  }

  // Avoid migrating the user prefs (even if the migration fails) to avoid
  // overriding preferences that a user may set on the SWA explicitly.
  prefs->SetBoolean(ash::prefs::kFilesAppUIPrefsMigrated, true);

  using MigrationStatus = file_manager::FileManagerPrefsMigrationStatus;
  auto* syncable_service =
      app_list::AppListSyncableServiceFactory::GetForProfile(profile_);
  if (!syncable_service->GetSyncItem(extension_misc::kFilesManagerAppId)) {
    base::UmaHistogramEnumeration(file_manager::kPrefsMigrationStatusUMA,
                                  MigrationStatus::kFailNoExistingPreferences);
    return;
  }
  if (!syncable_service->TransferItemAttributes(
          /*from_app_id=*/extension_misc::kFilesManagerAppId,
          /*to_app_id=*/file_manager::kFileManagerSwaAppId)) {
    base::UmaHistogramEnumeration(file_manager::kPrefsMigrationStatusUMA,
                                  MigrationStatus::kFailMigratingPreferences);
    return;
  }

  base::UmaHistogramEnumeration(file_manager::kPrefsMigrationStatusUMA,
                                MigrationStatus::kSuccess);
}

void ChromeShelfPrefs::EnsureProjectorShelfPinConsistency() {
  PrefService* prefs = profile_->GetPrefs();
  if (prefs->GetBoolean(ash::prefs::kProjectorSWAUIPrefsMigrated)) {
    return;
  }

  prefs->SetBoolean(ash::prefs::kProjectorSWAUIPrefsMigrated, true);
  app_list::AppListSyncableServiceFactory::GetForProfile(profile_)
      ->TransferItemAttributes(ash::kChromeUITrustedProjectorSwaAppIdDeprecated,
                               ash::kChromeUIUntrustedProjectorSwaAppId);
}

void ChromeShelfPrefs::EnsureChromePinned() {
  auto* syncable_service =
      app_list::AppListSyncableServiceFactory::GetForProfile(profile_);
  // If ash is the only web browser or if lacros is the only web browser, ensure
  // that ash-chrome is pinned. The sync<->shelf translation layer ensures that
  // we will use the appropriate shelf id.
  if (!crosapi::browser_util::IsLacrosEnabled() ||
      !crosapi::browser_util::IsAshWebBrowserEnabled()) {
    EnsurePinnedOrMakeFirst(app_constants::kChromeAppId, syncable_service);
    return;
  }

  // Otherwise, we are in a transition situation where both the ash and lacros
  // web browsers are available. To ensure consistency with legacy behavior, we
  // ensure both web browsers are pinned.
  EnsurePinnedOrMakeFirst(app_constants::kLacrosAppId, syncable_service);
  EnsurePinnedOrMakeFirst(app_constants::kChromeAppId, syncable_service);
}

bool ChromeShelfPrefs::DidAddDefaultApps() const {
  const auto& layouts_rolled =
      profile_->GetPrefs()->GetList(GetShelfDefaultPinLayoutPref());
  return !layouts_rolled.empty();
}

bool ChromeShelfPrefs::ShouldAddDefaultApps() const {
  if (should_add_default_apps_for_test) {
    return true;
  }
  // Apply default apps in case profile syncing is done. Otherwise there is a
  // risk that applied default apps would be overwritten by sync once it is
  // completed. prefs::kPolicyPinnedLauncherApps overrides any default layout.
  // This also limits applying experimental configuration only for users who
  // have the default pin layout specified by |kDefaultPinnedApps| or for
  // fresh users who have no pin information at all. Default configuration is
  // not applied if any of experimental layout was rolled.
  return !profile_->GetPrefs()->HasPrefPath(prefs::kPolicyPinnedLauncherApps) &&
         IsSafeToApplyDefaultPinLayout(profile_);
}

void ChromeShelfPrefs::AddDefaultApps() {
  VLOG(1) << "Roll default shelf pin layout " << kDefaultPinnedAppsKey;
  std::vector<std::string> default_app_ids;
  for (const char* default_app_id :
       GetDefaultPinnedAppsForFormFactor(profile_)) {
    default_app_ids.push_back(default_app_id);
  }
  InsertPinsAfterChromeAndBeforeFirstPinnedApp(
      app_list::AppListSyncableServiceFactory::GetForProfile(profile_),
      default_app_ids, /*is_policy_initiated=*/false);
  ScopedListPrefUpdate update(profile_->GetPrefs(),
                              GetShelfDefaultPinLayoutPref());
  update->Append(kDefaultPinnedAppsKey);
}

void ChromeShelfPrefs::AttachProfile(Profile* profile) {
  profile_ = profile;
  needs_consistency_migrations_ = true;
  sync_service_observer_.Reset();
}

std::string ChromeShelfPrefs::GetPromisePackageIdForSyncItem(
    const std::string& app_id) {
  if (!ash::features::ArePromiseIconsEnabled()) {
    return std::string();
  }

  auto* syncable_service =
      app_list::AppListSyncableServiceFactory::GetForProfile(profile_);

  // Some unit tests may not have the service or it may not be initialized.
  if (!syncable_service || !syncable_service->IsInitialized() ||
      skip_pinned_apps_from_sync_for_test) {
    return std::string();
  }

  const app_list::AppListSyncableService::SyncItem* item =
      syncable_service->GetSyncItem(app_id);
  return item->promise_package_id;
}

bool ChromeShelfPrefs::ShouldPerformConsistencyMigrations() const {
  return needs_consistency_migrations_;
}

void ChromeShelfPrefs::OnSyncModelUpdated() {
  needs_consistency_migrations_ = true;
}