File: web_app_sync_bridge.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1019 lines) | stat: -rw-r--r-- 38,746 bytes parent folder | download | duplicates (4)
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
// Copyright 2019 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/web_applications/web_app_sync_bridge.h"

#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <type_traits>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/check_is_test.h"
#include "base/check_op.h"
#include "base/containers/flat_set.h"
#include "base/containers/flat_tree.h"
#include "base/dcheck_is_on.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/to_string.h"
#include "base/types/expected.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/user_display_mode.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_chromeos_data.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_database.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_management_type.h"
#include "chrome/browser/web_applications/web_app_proto_utils.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_features.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/deletion_origin.h"
#include "components/sync/base/report_unrecoverable_error.h"
#include "components/sync/model/client_tag_based_data_type_processor.h"
#include "components/sync/model/data_type_local_change_processor.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/model_error.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/model/string_ordinal.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/web_app_specifics.pb.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/uninstall_result_code.h"
#include "components/webapps/common/web_app_id.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace web_app {
namespace {

bool g_disable_resume_sync_install_and_missing_os_integration_for_testing =
    false;

// Returns the manifest id from the sync entity. Does not validate whether the
// manifest_id is valid.
base::expected<webapps::ManifestId, StorageKeyParseResult>
ParseManifestIdFromSyncEntity(const sync_pb::WebAppSpecifics& specifics) {
  // Validate the entity is not corrupt.
  if (!specifics.has_start_url()) {
    return base::unexpected(StorageKeyParseResult::kNoStartUrl);
  }

  const GURL start_url = GURL(specifics.start_url());
  if (!start_url.is_valid()) {
    return base::unexpected(StorageKeyParseResult::kInvalidStartUrl);
  }

  // Set the manifest id first, as ApplySyncDataToApp verifies that the
  // computed manifest ids match.
  webapps::ManifestId manifest_id;
  if (specifics.has_relative_manifest_id()) {
    manifest_id =
        GenerateManifestIdUnsafe(specifics.relative_manifest_id(), start_url);
  } else {
    manifest_id = GenerateManifestIdFromStartUrlOnly(start_url);
  }
  if (!manifest_id.is_valid()) {
    return base::unexpected(StorageKeyParseResult::kInvalidManifestId);
  }
  return base::ok(manifest_id);
}

base::expected<webapps::ManifestId, ManifestIdParseResult>
ValidateManifestIdFromParsableSyncEntity(
    const sync_pb::WebAppSpecifics& specifics,
    const WebApp* existing_web_app) {
  base::expected<webapps::ManifestId, StorageKeyParseResult> manifest_id =
      ParseManifestIdFromSyncEntity(specifics);
  // These are guaranteed to be true, as it is checked in IsEntityDataValid,
  // which prevents the entity from ever being given to our system.
  CHECK(manifest_id.has_value());
  CHECK(manifest_id->is_valid());
  GURL start_url = GURL(specifics.start_url());
  CHECK(start_url.is_valid());

  if (!url::IsSameOriginWith(start_url, manifest_id.value())) {
    return base::unexpected(
        ManifestIdParseResult::kManifestIdResolutionFailure);
  }

  if (existing_web_app && existing_web_app->manifest_id() != manifest_id) {
    return base::unexpected(
        ManifestIdParseResult::kManifestIdDoesNotMatchLocalData);
  }

  return base::ok(manifest_id.value());
}

}  // namespace

BASE_FEATURE(kDeleteBadWebAppSyncEntitites,
             "DeleteBadWebAppSyncEntitites",
             base::FEATURE_DISABLED_BY_DEFAULT);

std::unique_ptr<syncer::EntityData> CreateSyncEntityData(const WebApp& app) {
  // The Sync System doesn't allow empty entity_data name.
  DCHECK(!app.untranslated_name().empty());

  auto entity_data = std::make_unique<syncer::EntityData>();
  entity_data->name = app.untranslated_name();
  // TODO(crbug.com/40139320): Remove this fallback later.
  if (entity_data->name.empty())
    entity_data->name = app.start_url().spec();

  *(entity_data->specifics.mutable_web_app()) = app.sync_proto();
  return entity_data;
}

void ApplySyncDataToApp(const sync_pb::WebAppSpecifics& sync_proto,
                        WebApp* app) {
  app->AddSource(WebAppManagement::kSync);

  sync_pb::WebAppSpecifics modified_sync_proto = sync_proto;

  std::string relative_manifest_id_path =
      RelativeManifestIdPath(app->manifest_id());
  if (modified_sync_proto.has_relative_manifest_id() &&
      modified_sync_proto.relative_manifest_id() != relative_manifest_id_path) {
    modified_sync_proto.set_relative_manifest_id(relative_manifest_id_path);
    // Record when this happens. When it is rare enough we could remove the
    // logic here and instead drop incoming sync data with fragment parts in the
    // manifest_id_path.
    base::UmaHistogramBoolean("WebApp.ApplySyncDataToApp.ManifestIdMatch",
                              false);
  } else {
    // Record success for comparison.
    base::UmaHistogramBoolean("WebApp.ApplySyncDataToApp.ManifestIdMatch",
                              true);
  }

  // Prevent incoming sync data from clearing recently-added fields in our local
  // copy. This ensures new sync fields are preserved despite old (pre-M125)
  // clients incorrectly clearing unknown fields. Any new fields added to the
  // sync proto should also be added here (if we don't want them to be cleared
  // by old clients) until this block can be removed. This can be removed when
  // there are few <M125 clients remaining.
  if (app->sync_proto().has_user_display_mode_cros() &&
      !modified_sync_proto.has_user_display_mode_cros()) {
    modified_sync_proto.set_user_display_mode_cros(
        app->sync_proto().user_display_mode_cros());
  }
  if (app->sync_proto().has_user_display_mode_default() &&
      !modified_sync_proto.has_user_display_mode_default()) {
    modified_sync_proto.set_user_display_mode_default(
        app->sync_proto().user_display_mode_default());
  }

  // Ensure the current platform's UserDisplayMode is set.
  // Conditional to avoid clobbering an unknown new UDM with a fallback one.
  if (!HasCurrentPlatformUserDisplayMode(modified_sync_proto)) {
    auto udm = ResolvePlatformSpecificUserDisplayMode(modified_sync_proto);
    SetPlatformSpecificUserDisplayMode(udm, &modified_sync_proto);
  }
  app->SetSyncProto(std::move(modified_sync_proto));
  CHECK(HasCurrentPlatformUserDisplayMode(app->sync_proto()));
}

// static
base::AutoReset<bool>
WebAppSyncBridge::DisableResumeSyncInstallAndMissingOsIntegrationForTesting() {
  CHECK_IS_TEST();
  return base::AutoReset<bool>(
      &g_disable_resume_sync_install_and_missing_os_integration_for_testing,
      true);
}

WebAppSyncBridge::WebAppSyncBridge(WebAppRegistrarMutable* registrar)
    : WebAppSyncBridge(
          registrar,
          std::make_unique<syncer::ClientTagBasedDataTypeProcessor>(
              syncer::WEB_APPS,
              base::BindRepeating(&syncer::ReportUnrecoverableError,
                                  chrome::GetChannel()))) {}

WebAppSyncBridge::WebAppSyncBridge(
    WebAppRegistrarMutable* registrar,
    std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor)
    : syncer::DataTypeSyncBridge(std::move(change_processor)),
      registrar_(registrar) {
  DCHECK(registrar_);
}

WebAppSyncBridge::~WebAppSyncBridge() = default;

void WebAppSyncBridge::SetSubsystems(
    AbstractWebAppDatabaseFactory* database_factory,
    WebAppCommandManager* command_manager,
    WebAppCommandScheduler* command_scheduler,
    WebAppInstallManager* install_manager) {
  DCHECK(database_factory);
  database_ = std::make_unique<WebAppDatabase>(
      database_factory,
      base::BindRepeating(&WebAppSyncBridge::ReportErrorToChangeProcessor,
                          base::Unretained(this)));
  command_manager_ = command_manager;
  command_scheduler_ = command_scheduler;
  install_manager_ = install_manager;
}

[[nodiscard]] ScopedRegistryUpdate WebAppSyncBridge::BeginUpdate(
    CommitCallback callback) {
  DCHECK(database_->is_opened());

  DCHECK(!is_in_update_);
  is_in_update_ = true;

  return ScopedRegistryUpdate(
      base::PassKey<WebAppSyncBridge>(),
      std::make_unique<WebAppRegistryUpdate>(registrar_,
                                             base::PassKey<WebAppSyncBridge>()),
      base::BindOnce(&WebAppSyncBridge::CommitUpdate,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void WebAppSyncBridge::Init(base::OnceClosure initialized_callback) {
  database_->OpenDatabase(base::BindOnce(&WebAppSyncBridge::OnDatabaseOpened,
                                         weak_ptr_factory_.GetWeakPtr(),
                                         std::move(initialized_callback)));
}

void WebAppSyncBridge::SetAppUserDisplayModeForTesting(
    const webapps::AppId& app_id,
    mojom::UserDisplayMode user_display_mode) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (web_app) {
      web_app->SetUserDisplayMode(user_display_mode);
    }
  }

  registrar_->NotifyWebAppUserDisplayModeChanged(app_id, user_display_mode);
}

void WebAppSyncBridge::SetAppWindowControlsOverlayEnabled(
    const webapps::AppId& app_id,
    bool enabled) {
  ScopedRegistryUpdate update = BeginUpdate();
  WebApp* web_app = update->UpdateApp(app_id);
  if (web_app) {
    web_app->SetWindowControlsOverlayEnabled(enabled);
  }
}

void WebAppSyncBridge::SetAppIsDisabled(AppLock& lock,
                                        const webapps::AppId& app_id,
                                        bool is_disabled) {
  if (!IsChromeOsDataMandatory()) {
    return;
  }

  bool notify = false;
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (!web_app) {
      return;
    }

    std::optional<WebAppChromeOsData> cros_data = web_app->chromeos_data();
    DCHECK(cros_data.has_value());

    if (cros_data->is_disabled != is_disabled) {
      cros_data->is_disabled = is_disabled;
      web_app->SetWebAppChromeOsData(std::move(cros_data));
      notify = true;
    }
  }

  if (notify) {
    registrar_->NotifyWebAppDisabledStateChanged(app_id, is_disabled);
  }
}

void WebAppSyncBridge::UpdateAppsDisableMode() {
  if (!IsChromeOsDataMandatory()) {
    return;
  }

  registrar_->NotifyWebAppsDisabledModeChanged();
}

void WebAppSyncBridge::SetAppLastBadgingTime(const webapps::AppId& app_id,
                                             const base::Time& time) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (web_app) {
      web_app->SetLastBadgingTime(time);
    }
  }
  registrar_->NotifyWebAppLastBadgingTimeChanged(app_id, time);
}

void WebAppSyncBridge::SetAppLastLaunchTime(const webapps::AppId& app_id,
                                            const base::Time& time) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (web_app) {
      web_app->SetLastLaunchTime(time);
    }
  }
  registrar_->NotifyWebAppLastLaunchTimeChanged(app_id, time);
}

void WebAppSyncBridge::SetAppFirstInstallTime(const webapps::AppId& app_id,
                                              const base::Time& time) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (web_app) {
      web_app->SetFirstInstallTime(time);
    }
  }
  registrar_->NotifyWebAppFirstInstallTimeChanged(app_id, time);
}

void WebAppSyncBridge::SetAppManifestUpdateTime(const webapps::AppId& app_id,
                                                const base::Time& time) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (web_app) {
      web_app->SetManifestUpdateTime(time);
    }
  }
}

void WebAppSyncBridge::SetUserPageOrdinal(const webapps::AppId& app_id,
                                          syncer::StringOrdinal page_ordinal) {
  CHECK(page_ordinal.IsValid());
  ScopedRegistryUpdate update = BeginUpdate();
  WebApp* web_app = update->UpdateApp(app_id);
  // Due to the extensions sync system setting ordinals on sync, this can get
  // called before the app is installed in the web apps system. Until apps are
  // no longer double-installed on both systems, ignore this case.
  // https://crbug.com/1101781
  if (!registrar_->IsInRegistrar(app_id)) {
    return;
  }
  if (web_app) {
    sync_pb::WebAppSpecifics mutable_sync_proto = web_app->sync_proto();
    mutable_sync_proto.set_user_page_ordinal(page_ordinal.ToInternalValue());
    web_app->SetSyncProto(std::move(mutable_sync_proto));
  }
}

void WebAppSyncBridge::SetUserLaunchOrdinal(
    const webapps::AppId& app_id,
    syncer::StringOrdinal launch_ordinal) {
  CHECK(launch_ordinal.IsValid());
  ScopedRegistryUpdate update = BeginUpdate();
  // Due to the extensions sync system setting ordinals on sync, this can get
  // called before the app is installed in the web apps system. Until apps are
  // no longer double-installed on both systems, ignore this case.
  // https://crbug.com/1101781
  if (!registrar_->IsInRegistrar(app_id)) {
    return;
  }
  WebApp* web_app = update->UpdateApp(app_id);
  if (web_app) {
    sync_pb::WebAppSpecifics mutable_sync_proto = web_app->sync_proto();
    mutable_sync_proto.set_user_launch_ordinal(
        launch_ordinal.ToInternalValue());
    web_app->SetSyncProto(std::move(mutable_sync_proto));
  }
}

#if BUILDFLAG(IS_MAC)
void WebAppSyncBridge::SetAlwaysShowToolbarInFullscreen(
    const webapps::AppId& app_id,
    bool show) {
  if (!registrar_->IsInstallState(
          app_id, {proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE,
                   proto::InstallState::INSTALLED_WITHOUT_OS_INTEGRATION,
                   proto::InstallState::INSTALLED_WITH_OS_INTEGRATION})) {
    return;
  }
  {
    ScopedRegistryUpdate update = BeginUpdate();
    update->UpdateApp(app_id)->SetAlwaysShowToolbarInFullscreen(show);
  }
  registrar_->NotifyAlwaysShowToolbarInFullscreenChanged(app_id, show);
}
#endif

void WebAppSyncBridge::SetAppFileHandlerApprovalState(
    const webapps::AppId& app_id,
    ApiApprovalState state) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    update->UpdateApp(app_id)->SetFileHandlerApprovalState(state);
  }
  registrar_->NotifyWebAppFileHandlerApprovalStateChanged(app_id);
}

void WebAppSyncBridge::CommitUpdate(
    CommitCallback callback,
    std::unique_ptr<WebAppRegistryUpdate> update) {
  DCHECK(is_in_update_);
  is_in_update_ = false;

  if (update == nullptr) {
    std::move(callback).Run(/*success*/ true);
    return;
  }

  std::unique_ptr<RegistryUpdateData> update_data =
      update->TakeUpdateData(base::PassKey<WebAppSyncBridge>());

  // Remove all unchanged apps.
  RegistryUpdateData::Apps changed_apps_to_update;
  for (std::unique_ptr<WebApp>& app_to_update : update_data->apps_to_update) {
    const webapps::AppId& app_id = app_to_update->app_id();
    if (*app_to_update != *registrar().GetAppById(app_id)) {
      changed_apps_to_update.push_back(std::move(app_to_update));
    }
  }
  update_data->apps_to_update = std::move(changed_apps_to_update);

  if (update_data->IsEmpty()) {
    std::move(callback).Run(/*success*/ true);
    return;
  }

  if (!disable_checks_for_testing_) {
    CheckRegistryUpdateData(*update_data);
  }

  std::unique_ptr<syncer::MetadataChangeList> metadata_change_list =
      CreateMetadataChangeList();

  UpdateSync(*update_data, metadata_change_list.get());

  database_->Write(
      *update_data, std::move(metadata_change_list),
      base::BindOnce(&WebAppSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));

  UpdateRegistrar(std::move(update_data));
}

void WebAppSyncBridge::CheckRegistryUpdateData(
    const RegistryUpdateData& update_data) const {
#if DCHECK_IS_ON()
  for (const std::unique_ptr<WebApp>& web_app : update_data.apps_to_create) {
    DCHECK(!registrar_->GetAppById(web_app->app_id()));
    DCHECK(!web_app->untranslated_name().empty());
    DCHECK(web_app->manifest_id().is_valid());
  }

  for (const std::unique_ptr<WebApp>& web_app : update_data.apps_to_update) {
    DCHECK(registrar_->GetAppById(web_app->app_id()));
    DCHECK(!web_app->untranslated_name().empty());
    DCHECK(web_app->manifest_id().is_valid());
  }

  for (const webapps::AppId& app_id : update_data.apps_to_delete) {
    DCHECK(registrar_->GetAppById(app_id));
  }
#endif
}

void WebAppSyncBridge::UpdateRegistrar(
    std::unique_ptr<RegistryUpdateData> update_data) {
  registrar_->CountMutation();

  for (std::unique_ptr<WebApp>& web_app : update_data->apps_to_create) {
    webapps::AppId app_id = web_app->app_id();
    DCHECK(!registrar_->GetAppById(app_id));
    registrar_->registry().emplace(std::move(app_id), std::move(web_app));
  }

  for (std::unique_ptr<WebApp>& web_app : update_data->apps_to_update) {
    WebApp* original_web_app = registrar_->GetAppByIdMutable(web_app->app_id());
    DCHECK(original_web_app);
    DCHECK_EQ(web_app->IsSystemApp(), original_web_app->IsSystemApp());
    // Commit previously created copy into original. Preserve original web_app
    // object pointer value (the object's identity) to support stored pointers.
    *original_web_app = std::move(*web_app);
  }
  for (const webapps::AppId& app_id : update_data->apps_to_delete) {
    auto it = registrar_->registry().find(app_id);
    CHECK(it != registrar_->registry().end());
    registrar_->registry().erase(it);
  }
}

void WebAppSyncBridge::UpdateSync(
    const RegistryUpdateData& update_data,
    syncer::MetadataChangeList* metadata_change_list) {
  // We don't block web app subsystems on WebAppSyncBridge::MergeFullSyncData:
  // we call WebAppProvider::OnRegistryControllerReady() right after
  // change_processor()->ModelReadyToSync. As a result, subsystems may produce
  // some local changes between OnRegistryControllerReady and MergeFullSyncData.
  // Return early in this case. The processor cannot do any useful metadata
  // tracking until MergeFullSyncData is called:
  if (!change_processor()->IsTrackingMetadata())
    return;

  for (const std::unique_ptr<WebApp>& new_app : update_data.apps_to_create) {
    if (new_app->IsSynced()) {
      CHECK(new_app->manifest_id().is_valid());
      change_processor()->Put(new_app->app_id(), CreateSyncEntityData(*new_app),
                              metadata_change_list);
    }
  }

  for (const std::unique_ptr<WebApp>& new_state : update_data.apps_to_update) {
    const webapps::AppId& app_id = new_state->app_id();
    // Find the current state of the app to be overritten.
    const WebApp* current_state = registrar_->GetAppById(app_id);
    DCHECK(current_state);

    // Include the app in the sync "view" if IsSynced flag becomes true. Update
    // the app if IsSynced flag stays true. Exclude the app from the sync "view"
    // if IsSynced flag becomes false.
    if (new_state->IsSynced()) {
      CHECK(new_state->manifest_id().is_valid());
      // Only call 'Put' if it wasn't synced, or if the sync data has changed.
      // TODO(https://crbug.com/409867622): We can remove this optimization
      // after tests are updated to use a Fake version instead of the Mock
      // version of the processor.
      if (!current_state->IsSynced() ||
          (current_state->sync_proto().SerializeAsString() !=
           new_state->sync_proto().SerializeAsString())) {
        change_processor()->Put(app_id, CreateSyncEntityData(*new_state),
                                metadata_change_list);
      }
    } else if (current_state->IsSynced()) {
      change_processor()->Delete(app_id, syncer::DeletionOrigin::Unspecified(),
                                 metadata_change_list);
    }
  }

  for (const webapps::AppId& app_id_to_delete : update_data.apps_to_delete) {
    const WebApp* current_state = registrar_->GetAppById(app_id_to_delete);
    DCHECK(current_state);
    // Exclude the app from the sync "view" if IsSynced flag was true.
    if (current_state->IsSynced())
      change_processor()->Delete(app_id_to_delete,
                                 syncer::DeletionOrigin::Unspecified(),
                                 metadata_change_list);
  }
}

void WebAppSyncBridge::OnDatabaseOpened(
    base::OnceClosure initialized_callback,
    Registry registry,
    std::unique_ptr<syncer::MetadataBatch> metadata_batch) {
  DCHECK(database_->is_opened());

  // Provide sync metadata to the processor _before_ any local changes occur.
  change_processor()->ModelReadyToSync(std::move(metadata_batch));

  registrar_->InitRegistry(std::move(registry));

  // Database migrations happen inside WebAppDatabase::MigrateDatabase.

  std::move(initialized_callback).Run();

  // Already have data stored in web app system and shouldn't expect further
  // callbacks once `IsTrackingMetadata` is true.
  if (!on_sync_connected_.is_signaled() &&
      change_processor()->IsTrackingMetadata()) {
    on_sync_connected_.Signal();
  }

  MaybeUninstallAppsPendingUninstall();
  MaybeInstallAppsFromSyncAndPendingInstallOrSyncOsIntegration();
}

void WebAppSyncBridge::OnDataWritten(CommitCallback callback, bool success) {
  if (!success)
    DLOG(ERROR) << "WebAppSyncBridge commit failed";

  base::UmaHistogramBoolean("WebApp.Database.WriteResult", success);
  std::move(callback).Run(success);
}

void WebAppSyncBridge::OnWebAppUninstallComplete(
    const webapps::AppId& app,
    webapps::UninstallResultCode code) {
  base::UmaHistogramBoolean("Webapp.SyncInitiatedUninstallResult",
                            UninstallSucceeded(code));
}

void WebAppSyncBridge::ReportErrorToChangeProcessor(
    const syncer::ModelError& error) {
  change_processor()->ReportError(error);
}

void WebAppSyncBridge::MergeLocalAppsToSync(
    const syncer::EntityChangeList& entity_data,
    syncer::MetadataChangeList* metadata_change_list) {
  auto sync_server_apps = base::MakeFlatSet<webapps::AppId>(
      entity_data, {}, &syncer::EntityChange::storage_key);

  for (const WebApp& app : registrar_->GetAppsIncludingStubs()) {
    if (!app.IsSynced())
      continue;

    bool exists_remotely = sync_server_apps.contains(app.app_id());
    if (!exists_remotely) {
      change_processor()->Put(app.app_id(), CreateSyncEntityData(app),
                              metadata_change_list);
    }
  }
}

ManifestIdParseResult WebAppSyncBridge::PrepareLocalUpdateFromSyncChange(
    const syncer::EntityChange& change,
    RegistryUpdateData* update_local_data,
    std::vector<webapps::AppId>& apps_display_mode_changed) {
  // app_id is storage key.
  const webapps::AppId& app_id = change.storage_key();

  const WebApp* existing_web_app = registrar_->GetAppById(app_id);

  // Handle deletion first.
  if (change.type() == syncer::EntityChange::ACTION_DELETE) {
    if (!existing_web_app) {
      DLOG(ERROR) << "ApplySyncDataChange error: no app to delete";
      return ManifestIdParseResult::kSuccess;
    }
    auto app_copy = std::make_unique<WebApp>(*existing_web_app);
    app_copy->RemoveSource(WebAppManagement::kSync);
    // Currently removing an app from sync will uninstall the app on all
    // profiles that are synced to it; we could consider not removing the
    // kUserInstalled source in this case.
    app_copy->RemoveSource(WebAppManagement::kUserInstalled);
    if (!app_copy->HasAnySources()) {
      // Uninstallation from the local database is a two-phase commit. Setting
      // this flag to true signals that uninstallation should occur, and then
      // when all asynchronous uninstallation tasks are complete then the entity
      // is deleted from the database.
      app_copy->SetIsUninstalling(true);
    } else {
      install_manager_->NotifyWebAppSourceRemoved(app_id);
    }
    update_local_data->apps_to_update.push_back(std::move(app_copy));
    return ManifestIdParseResult::kSuccess;
  }

  // Handle EntityChange::ACTION_ADD and EntityChange::ACTION_UPDATE.
  CHECK(change.data().specifics.has_web_app());
  const sync_pb::WebAppSpecifics& specifics = change.data().specifics.web_app();

  base::expected<webapps::ManifestId, ManifestIdParseResult> manifest_id =
      ValidateManifestIdFromParsableSyncEntity(specifics, existing_web_app);

  if (!manifest_id.has_value()) {
    base::UmaHistogramEnumeration("WebApp.Sync.CorruptSyncEntity",
                                  manifest_id.error());
    return manifest_id.error();
  }
  base::UmaHistogramEnumeration("WebApp.Sync.CorruptSyncEntity",
                                ManifestIdParseResult::kSuccess);

  std::unique_ptr<WebApp> web_app;

  if (!existing_web_app) {
    // Any remote entities that don’t exist locally must be written to local
    // storage.
    web_app = std::make_unique<WebApp>(app_id);
    web_app->SetStartUrl(GURL(specifics.start_url()));
    web_app->SetManifestId(manifest_id.value());

    // Request a followup sync-initiated install for this stub app to fetch
    // full local data and all the icons.
    web_app->SetIsFromSyncAndPendingInstallation(true);
    // The sync system requires non-empty name, populate temp name from
    // the fallback sync data name.
    if (specifics.name().empty()) {
      web_app->SetName(change.data().name);
    } else {
      web_app->SetName(specifics.name());
    }
    // For a new app, automatically choose if we want to install it locally.
    web_app->SetInstallState(
        AreAppsLocallyInstalledBySync()
            ? proto::InstallState::INSTALLED_WITH_OS_INTEGRATION
            : proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE);
  } else {
    web_app = std::make_unique<WebApp>(*existing_web_app);
  }

  ApplySyncDataToApp(specifics, web_app.get());

  if (existing_web_app) {
    if (existing_web_app->user_display_mode() != web_app->user_display_mode()) {
      apps_display_mode_changed.push_back(app_id);
    }
    update_local_data->apps_to_update.push_back(std::move(web_app));
  } else {
    update_local_data->apps_to_create.push_back(std::move(web_app));
  }
  return ManifestIdParseResult::kSuccess;
}

void WebAppSyncBridge::ApplyIncrementalSyncChangesToRegistrar(
    std::unique_ptr<RegistryUpdateData> update_local_data,
    const std::vector<webapps::AppId>& apps_display_mode_changed) {
  if (update_local_data->IsEmpty())
    return;

  // Notify observers that web apps will be updated.
  // Prepare a short living read-only "view" to support const correctness:
  // observers must not modify the |new_apps_state|.
  if (!update_local_data->apps_to_update.empty()) {
    std::vector<const WebApp*> new_apps_state;
    new_apps_state.reserve(update_local_data->apps_to_update.size());
    for (const std::unique_ptr<WebApp>& new_web_app_state :
         update_local_data->apps_to_update) {
      new_apps_state.push_back(new_web_app_state.get());
    }
    registrar_->NotifyWebAppsWillBeUpdatedFromSync(new_apps_state);
  }

  for (const auto& web_app : update_local_data->apps_to_create) {
    // Commands cannot start synchronously, so this is safe.
    command_scheduler_->InstallFromSync(*web_app, base::DoNothing());
  }

  UpdateRegistrar(std::move(update_local_data));

  for (const webapps::AppId& app_id : apps_display_mode_changed) {
    const WebApp* app = registrar_->GetAppById(app_id);
    registrar_->NotifyWebAppUserDisplayModeChanged(app_id,
                                                   app->user_display_mode());
  }

  std::vector<webapps::AppId> apps_to_delete;
  for (const WebApp& app : registrar_->GetAppsIncludingStubs()) {
    if (app.is_uninstalling())
      apps_to_delete.push_back(app.app_id());
  }

  // Initiate any uninstall actions to clean up os integration, disk data, etc.
  if (!apps_to_delete.empty()) {
    auto callback =
        base::BindRepeating(&WebAppSyncBridge::OnWebAppUninstallComplete,
                            weak_ptr_factory_.GetWeakPtr());
    for (const webapps::AppId& app_id : apps_to_delete) {
      command_scheduler_->RemoveAllManagementTypesAndUninstall(
          base::PassKey<WebAppSyncBridge>(), app_id,
          webapps::WebappUninstallSource::kSync,
          base::BindOnce(callback, app_id));
    }
  }
}

std::unique_ptr<syncer::MetadataChangeList>
WebAppSyncBridge::CreateMetadataChangeList() {
  return syncer::DataTypeStore::WriteBatch::CreateMetadataChangeList();
}

std::optional<syncer::ModelError> WebAppSyncBridge::MergeFullSyncData(
    std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
    syncer::EntityChangeList entity_data) {
  CHECK(change_processor()->IsTrackingMetadata());

  auto update_local_data = std::make_unique<RegistryUpdateData>();
  std::vector<webapps::AppId> apps_display_mode_changed;

  for (const auto& change : entity_data) {
    DCHECK_NE(change->type(), syncer::EntityChange::ACTION_DELETE);
    ManifestIdParseResult result = PrepareLocalUpdateFromSyncChange(
        *change, update_local_data.get(), apps_display_mode_changed);
    if (base::FeatureList::IsEnabled(kDeleteBadWebAppSyncEntitites) &&
        result != ManifestIdParseResult::kSuccess) {
      change_processor()->Delete(GetStorageKey(change->data()),
                                 syncer::DeletionOrigin::Unspecified(),
                                 metadata_change_list.get());
    }
  }

  MergeLocalAppsToSync(entity_data, metadata_change_list.get());

  database_->Write(
      *update_local_data, std::move(metadata_change_list),
      base::BindOnce(&WebAppSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistrar(std::move(update_local_data),
                                         apps_display_mode_changed);

  if (!on_sync_connected_.is_signaled()) {
    on_sync_connected_.Signal();
  }

  return std::nullopt;
}

std::optional<syncer::ModelError> WebAppSyncBridge::ApplyIncrementalSyncChanges(
    std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
    syncer::EntityChangeList entity_changes) {
  // `change_processor()->IsTrackingMetadata()` may be false if the sync
  // metadata is invalid and ClearPersistedMetadataIfInvalid() is resetting it.

  auto update_local_data = std::make_unique<RegistryUpdateData>();
  std::vector<webapps::AppId> apps_display_mode_changed;

  for (const auto& change : entity_changes) {
    PrepareLocalUpdateFromSyncChange(*change, update_local_data.get(),
                                     apps_display_mode_changed);
  }

  database_->Write(
      *update_local_data, std::move(metadata_change_list),
      base::BindOnce(&WebAppSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistrar(std::move(update_local_data),
                                         apps_display_mode_changed);

  if (!on_sync_connected_.is_signaled()) {
    on_sync_connected_.Signal();
  }

  return std::nullopt;
}

void WebAppSyncBridge::ApplyDisableSyncChanges(
    std::unique_ptr<syncer::MetadataChangeList> delete_metadata_change_list) {
  auto update_local_data = std::make_unique<RegistryUpdateData>();

  for (const WebApp& web_app : registrar_->GetAppsIncludingStubs()) {
    if (web_app.GetSources().Has(WebAppManagement::kSync)) {
      auto app_copy = std::make_unique<WebApp>(web_app);
      app_copy->RemoveSource(WebAppManagement::kSync);
      if (!app_copy->HasAnySources()) {
        // Uninstallation from the local database is a two-phase commit. Setting
        // this flag to true signals that uninstallation should occur, and then
        // when all asynchronous uninstallation tasks are complete then the
        // entity is deleted from the database.
        app_copy->SetIsUninstalling(true);
      }
      update_local_data->apps_to_update.push_back(std::move(app_copy));
    }
  }

  database_->Write(
      *update_local_data, std::move(delete_metadata_change_list),
      base::BindOnce(&WebAppSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistrar(std::move(update_local_data),
                                         /*apps_display_mode_changed=*/{});
}

std::unique_ptr<syncer::DataBatch> WebAppSyncBridge::GetDataForCommit(
    StorageKeyList storage_keys) {
  auto data_batch = std::make_unique<syncer::MutableDataBatch>();

  for (const webapps::AppId& app_id : storage_keys) {
    const WebApp* app = registrar_->GetAppById(app_id);
    if (app && app->IsSynced())
      data_batch->Put(app->app_id(), CreateSyncEntityData(*app));
  }

  return data_batch;
}

std::unique_ptr<syncer::DataBatch> WebAppSyncBridge::GetAllDataForDebugging() {
  auto data_batch = std::make_unique<syncer::MutableDataBatch>();

  for (const WebApp& app : registrar_->GetAppsIncludingStubs()) {
    if (app.IsSynced())
      data_batch->Put(app.app_id(), CreateSyncEntityData(app));
  }

  return data_batch;
}

std::string WebAppSyncBridge::GetClientTag(
    const syncer::EntityData& entity_data) const {
  CHECK(entity_data.specifics.has_web_app());
  base::expected<webapps::ManifestId, StorageKeyParseResult> manifest_id =
      ParseManifestIdFromSyncEntity(entity_data.specifics.web_app());
  // This is guaranteed to be true, as the contract for this function is that
  // IsEntityDataValid must be true.
  CHECK(manifest_id.has_value());
  return GenerateAppIdFromManifestId(manifest_id.value());
}

std::string WebAppSyncBridge::GetStorageKey(
    const syncer::EntityData& entity_data) const {
  return GetClientTag(entity_data);
}

bool WebAppSyncBridge::IsEntityDataValid(
    const syncer::EntityData& entity_data) const {
  if (!entity_data.specifics.has_web_app()) {
    return false;
  }
  const sync_pb::WebAppSpecifics& specifics = entity_data.specifics.web_app();

  base::expected<webapps::ManifestId, StorageKeyParseResult> manifest_id =
      ParseManifestIdFromSyncEntity(specifics);
  if (manifest_id.has_value()) {
    base::UmaHistogramEnumeration("WebApp.Sync.InvalidEntity",
                                  StorageKeyParseResult::kSuccess);
    return true;
  }
  // Note: The GetClientTag function relies on this function to always return
  // `false` if the manifest id is not parsable, and otherwise will CHECK-fail.
  base::UmaHistogramEnumeration("WebApp.Sync.InvalidEntity",
                                manifest_id.error());
  DLOG(ERROR) << "Cannot parse sync entity: "
              << base::ToString(manifest_id.error());
  return false;
}

void WebAppSyncBridge::SetAppNotLocallyInstalledForTesting(
    const webapps::AppId& app_id) {
  {
    ScopedRegistryUpdate update = BeginUpdate();
    WebApp* web_app = update->UpdateApp(app_id);
    if (web_app) {
      web_app->SetInstallState(
          proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE);
    }
  }
}

void WebAppSyncBridge::MaybeUninstallAppsPendingUninstall() {
  std::vector<webapps::AppId> apps_uninstalling;

  for (WebApp& app : registrar_->GetAppsIncludingStubs()) {
    if (app.is_uninstalling())
      apps_uninstalling.push_back(app.app_id());
  }

  base::UmaHistogramCounts100("WebApp.Uninstall.NonSyncIncompleteCount",
                              apps_uninstalling.size());

  // Retrying incomplete uninstalls
  if (!apps_uninstalling.empty()) {
    auto callback =
        base::BindRepeating(&WebAppSyncBridge::OnWebAppUninstallComplete,
                            weak_ptr_factory_.GetWeakPtr());
    for (const auto& app_id : apps_uninstalling) {
      command_scheduler_->RemoveAllManagementTypesAndUninstall(
          base::PassKey<WebAppSyncBridge>(), app_id,
          webapps::WebappUninstallSource::kSync,
          base::BindOnce(callback, app_id));
    }
  }
}

void WebAppSyncBridge::
    MaybeInstallAppsFromSyncAndPendingInstallOrSyncOsIntegration() {
  if (g_disable_resume_sync_install_and_missing_os_integration_for_testing) {
    return;
  }
  for (WebApp& app : registrar_->GetAppsIncludingStubs()) {
    if (app.is_from_sync_and_pending_installation()) {
      command_scheduler_->InstallFromSync(app, base::DoNothing());
    } else if (app.install_state() ==
                   proto::InstallState::INSTALLED_WITH_OS_INTEGRATION &&
               !app.current_os_integration_states().has_shortcut()) {
      // Web app installs save the app data to the database before synchronizing
      // the OS integration. Only after the OS integration is complete do we
      // save the current_os_integration_states. Since the system can shut down
      // in between these two steps, we need to synchronize the OS integration
      // for all apps that are installed with OS integration but don't have
      // shortcut fields set to complete this operation.
      command_scheduler_->SynchronizeOsIntegration(
          app.app_id(), base::BindOnce([]() {
            base::UmaHistogramBoolean(
                "WebApp.Install.CompletedOsIntegrationOnStartup", true);
          }));
    }
  }
}

}  // namespace web_app