File: webapk_sync_bridge.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (642 lines) | stat: -rw-r--r-- 22,984 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 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/android/webapk/webapk_sync_bridge.h"

#include <memory>
#include <optional>
#include <vector>

#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "chrome/browser/android/webapk/webapk_database.h"
#include "chrome/browser/android/webapk/webapk_helpers.h"
#include "chrome/browser/android/webapk/webapk_registry_update.h"
#include "chrome/browser/android/webapk/webapk_restore_task.h"
#include "chrome/browser/android/webapk/webapk_specifics_fetcher.h"
#include "chrome/common/channel_info.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_store.h"
#include "components/sync/model/data_type_store_service.h"
#include "components/sync/model/metadata_batch.h"
#include "components/sync/model/metadata_change_list.h"
#include "components/sync/model/mutable_data_batch.h"
#include "components/sync/protocol/web_apk_specifics.pb.h"
#include "components/webapps/browser/android/shortcut_info.h"
#include "components/webapps/browser/android/webapps_icon_utils.h"
#include "components/webapps/common/web_app_id.h"
#include "url/gurl.h"

namespace webapk {

std::unique_ptr<syncer::EntityData> CreateSyncEntityDataFromSpecifics(
    const sync_pb::WebApkSpecifics& app) {
  // The Sync System doesn't allow empty entity_data name.
  CHECK(!app.name().empty());

  auto entity_data = std::make_unique<syncer::EntityData>();
  entity_data->name = app.name();

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

std::unique_ptr<syncer::EntityData> CreateSyncEntityData(
    const WebApkProto& app) {
  return CreateSyncEntityDataFromSpecifics(app.sync_data());
}

webapps::AppId ManifestIdStrToAppId(const std::string& manifest_id) {
  GURL manifest_id_gurl(manifest_id);
  if (!manifest_id_gurl.is_valid()) {
    LOG(ERROR) << "Invalid manifest_id: " << manifest_id;
    return "";
  }
  return GenerateAppIdFromManifestId(manifest_id_gurl.GetWithoutRef());
}

namespace {

constexpr base::TimeDelta kRecentAppMaxAge = base::Days(30);
constexpr char kSyncedWebApkAdditionHistogramName[] =
    "WebApk.Sync.SyncedWebApkAddition";

const WebApkProto* GetAppById(const Registry& registry,
                              const webapps::AppId& app_id) {
  auto it = registry.find(app_id);
  if (it != registry.end()) {
    return it->second.get();
  }

  return nullptr;
}

WebApkProto* GetAppByIdMutable(const Registry& registry,
                               const webapps::AppId& app_id) {
  return const_cast<WebApkProto*>(GetAppById(registry, app_id));
}

std::unique_ptr<WebApkProto> WebApkProtoFromSpecifics(
    const sync_pb::WebApkSpecifics* app,
    bool installed) {
  std::unique_ptr<WebApkProto> app_proto = std::make_unique<WebApkProto>();
  app_proto->set_is_locally_installed(installed);

  sync_pb::WebApkSpecifics* mutable_specifics = app_proto->mutable_sync_data();
  *mutable_specifics = *app;

  return app_proto;
}

std::unique_ptr<WebApkProto> CloneWebApkProto(const WebApkProto& app) {
  std::unique_ptr<WebApkProto> clone = std::make_unique<WebApkProto>();

  *clone = app;

  sync_pb::WebApkSpecifics* mutable_specifics = clone->mutable_sync_data();
  *mutable_specifics = app.sync_data();

  return clone;
}

// Returns true if the specifics' timestamp is at most kRecentAppMaxAge before
// |time|. In other words, if |time| is Now, then this returns whether the
// specifics is at most kRecentAppMaxAge old.
bool AppWasUsedRecentlyComparedTo(const sync_pb::WebApkSpecifics* specifics,
                                  const base::Time time) {
  base::Time app_last_used = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(specifics->last_used_time_windows_epoch_micros()));
  return time - app_last_used < kRecentAppMaxAge;
}

// Create a |webapps::ShortcutInfo| from the synced |webapk_specifics|.
// If the data is invalid, returns nullptr.
std::unique_ptr<webapps::ShortcutInfo> CreateShortcutInfoFromSpecifics(
    const sync_pb::WebApkSpecifics& webapk_specifics) {
  GURL start_url(GURL(webapk_specifics.start_url()));
  if (!start_url.is_valid()) {
    return nullptr;
  }
  auto shortcut_info = std::make_unique<webapps::ShortcutInfo>(start_url);
  GURL manifest_id(webapk_specifics.manifest_id());
  if (manifest_id.is_valid()) {
    shortcut_info->manifest_id = manifest_id;
  }
  GURL scope(webapk_specifics.scope());
  if (scope.is_valid()) {
    shortcut_info->scope = scope;
  }
  std::u16string name = base::UTF8ToUTF16(webapk_specifics.name());
  shortcut_info->user_title = name;
  shortcut_info->name = name;
  shortcut_info->short_name = name;
  if (webapk_specifics.icon_infos().size() > 0) {
    shortcut_info->best_primary_icon_url =
        GURL(webapk_specifics.icon_infos(0).url());
    shortcut_info->is_primary_icon_maskable =
        webapk_specifics.icon_infos(0).purpose() ==
            sync_pb::WebApkIconInfo_Purpose_MASKABLE;
  } else {
    // If there is no icon url in sync data, put |start_url| as a place holder
    // for primary icon url. Download icon will fallback to generated icon.
    shortcut_info->best_primary_icon_url = start_url;
  }
  return shortcut_info;
}

// Legacy (pre-manifest-id) WebAPKs can have empty manifest_ids. These, in turn,
// get translated into empty app_ids via ManifestIdStrToAppId(). If we end up
// with an empty app_id, generally we need to abort Sync-handling and ignore
// that WebAPK for Sync purposes.
bool IsLegacyAppId(webapps::AppId app_id) {
  return app_id.empty();
}

}  // anonymous namespace

WebApkSyncBridge::WebApkSyncBridge(
    syncer::DataTypeStoreService* data_type_store_service,
    base::OnceClosure on_initialized)
    : WebApkSyncBridge(
          data_type_store_service,
          std::move(on_initialized),
          std::make_unique<syncer::ClientTagBasedDataTypeProcessor>(
              syncer::WEB_APKS,
              base::BindRepeating(&syncer::ReportUnrecoverableError,
                                  chrome::GetChannel())),
          std::make_unique<base::DefaultClock>(),
          std::make_unique<WebApkSpecificsFetcher>()) {}

WebApkSyncBridge::WebApkSyncBridge(
    syncer::DataTypeStoreService* data_type_store_service,
    base::OnceClosure on_initialized,
    std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor,
    std::unique_ptr<base::Clock> clock,
    std::unique_ptr<AbstractWebApkSpecificsFetcher> specifics_fetcher)
    : syncer::DataTypeSyncBridge(std::move(change_processor)),
      database_(
          data_type_store_service,
          base::BindRepeating(&WebApkSyncBridge::ReportErrorToChangeProcessor,
                              base::Unretained(this))),
      clock_(std::move(clock)),
      webapk_specifics_fetcher_(std::move(specifics_fetcher)) {
  database_.OpenDatabase(base::BindOnce(&WebApkSyncBridge::OnDatabaseOpened,
                                        weak_ptr_factory_.GetWeakPtr(),
                                        std::move(on_initialized)));
}

WebApkSyncBridge::~WebApkSyncBridge() = default;

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

void WebApkSyncBridge::OnDatabaseOpened(
    base::OnceClosure 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));

  registry_ = std::move(registry);
  std::move(callback).Run();
  for (auto& task : init_done_callback_) {
    std::move(task).Run(/* initialized= */ true);
  }
}

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

bool WebApkSyncBridge::AppWasUsedRecently(
    const sync_pb::WebApkSpecifics* specifics) const {
  return AppWasUsedRecentlyComparedTo(specifics, clock_->Now());
}

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

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

void WebApkSyncBridge::ApplyIncrementalSyncChangesToRegistry(
    std::unique_ptr<RegistryUpdateData> update_data) {
  if (update_data->isEmpty()) {
    return;
  }

  for (auto& app : update_data->apps_to_create) {
    webapps::AppId app_id =
        ManifestIdStrToAppId(app->sync_data().manifest_id());
    if (IsLegacyAppId(app_id)) {
      continue;
    }

    auto it = registry_.find(app_id);
    if (it != registry_.end()) {
      registry_.erase(it);
    }
    registry_.emplace(std::move(app_id), std::move(app));
  }

  for (const webapps::AppId& app_id : update_data->apps_to_delete) {
    auto it = registry_.find(app_id);
    CHECK(it != registry_.end());
    registry_.erase(it);
  }
}

bool WebApkSyncBridge::SyncDataContainsNewApps(
    const std::vector<std::unique_ptr<sync_pb::WebApkSpecifics>>&
        installed_apps,
    const syncer::EntityChangeList& sync_changes) const {
  std::set<webapps::AppId> sync_update_from_installed_set;
  for (const std::unique_ptr<sync_pb::WebApkSpecifics>& sync_update :
       installed_apps) {
    webapps::AppId app_id = ManifestIdStrToAppId(sync_update->manifest_id());
    if (!IsLegacyAppId(app_id)) {
      sync_update_from_installed_set.insert(app_id);
    }
  }

  for (const auto& sync_change : sync_changes) {
    if (sync_update_from_installed_set.count(sync_change->storage_key()) != 0) {
      continue;
    }

    if (sync_change->type() == syncer::EntityChange::ACTION_DELETE) {
      continue;
    }

    // There are changes from sync that aren't installed on the device.
    return true;
  }

  return false;
}

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

  const std::vector<std::unique_ptr<sync_pb::WebApkSpecifics>> installed_apps =
      webapk_specifics_fetcher_->GetWebApkSpecifics();

  WebappRegistry::SetNeedsPwaRestore(
      SyncDataContainsNewApps(installed_apps, entity_changes));

  // Since we're using "account-only" semantics for Transport Mode, we just call
  // through to ApplyIncrementalSyncChanges().
  return ApplyIncrementalSyncChanges(std::move(metadata_change_list),
                                     std::move(entity_changes));
}

void WebApkSyncBridge::RegisterDoneInitializingCallback(
    base::OnceCallback<void(bool)> init_done_callback) {
  if (database_.is_opened()) {
    std::move(init_done_callback).Run(/* initialized= */ true);
    return;
  }

  init_done_callback_.push_back(std::move(init_done_callback));
}

void WebApkSyncBridge::MergeSyncDataForTesting(
    std::vector<std::vector<std::string>> app_vector,
    std::vector<int> last_used_days_vector) {
  CHECK(database_.is_opened());
  CHECK(app_vector.size() == last_used_days_vector.size());

  std::unique_ptr<syncer::MetadataChangeList> metadata_change_list =
      syncer::DataTypeStore::WriteBatch::CreateMetadataChangeList();
  std::unique_ptr<webapk::RegistryUpdateData> registry_update =
      std::make_unique<webapk::RegistryUpdateData>();

  int i = 0;
  for (auto const& app : app_vector) {
    std::unique_ptr<sync_pb::WebApkSpecifics> specifics =
        std::make_unique<sync_pb::WebApkSpecifics>();
    specifics->set_start_url(app[0]);
    specifics->set_manifest_id(app[0]);
    specifics->set_name(app[1]);

    const std::string icon_url = app[2];
    const int32_t icon_size_in_px = 256;
    const sync_pb::WebApkIconInfo_Purpose icon_purpose =
        sync_pb::WebApkIconInfo_Purpose_ANY;
    sync_pb::WebApkIconInfo* icon_info = specifics->add_icon_infos();
    icon_info->set_size_in_px(icon_size_in_px);
    icon_info->set_url(icon_url);
    icon_info->set_purpose(icon_purpose);

    base::Time time = base::Time::Now() - base::Days(last_used_days_vector[i]);
    specifics->set_last_used_time_windows_epoch_micros(
        time.ToDeltaSinceWindowsEpoch().InMicroseconds());
    registry_update->apps_to_create.push_back(
        WebApkProtoFromSpecifics(specifics.get(), false));
    i++;
  }

  database_.Write(
      *registry_update, std::move(metadata_change_list),
      base::BindOnce(&WebApkSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistry(std::move(registry_update));
}

void WebApkSyncBridge::PrepareRegistryUpdateFromSyncApps(
    const syncer::EntityChangeList& sync_changes,
    RegistryUpdateData* registry_update_from_sync) const {
  for (const auto& sync_change : sync_changes) {
    if (sync_change->type() == syncer::EntityChange::ACTION_DELETE) {
      // There's no need to queue up a deletion if the app doesn't exist in the
      // registry in the first place.
      if (GetAppByIdMutable(registry_, sync_change->storage_key()) != nullptr) {
        registry_update_from_sync->apps_to_delete.push_back(
            sync_change->storage_key());
      }
      continue;
    }

    CHECK(sync_change->data().specifics.has_web_apk());
    registry_update_from_sync->apps_to_create.push_back(
        WebApkProtoFromSpecifics(&sync_change->data().specifics.web_apk(),
                                 false /* installed */));
  }
}

std::optional<syncer::ModelError> WebApkSyncBridge::ApplyIncrementalSyncChanges(
    std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
    syncer::EntityChangeList entity_changes) {
  std::unique_ptr<RegistryUpdateData> registry_update_from_sync =
      std::make_unique<RegistryUpdateData>();
  PrepareRegistryUpdateFromSyncApps(entity_changes,
                                    registry_update_from_sync.get());

  database_.Write(
      *registry_update_from_sync, std::move(metadata_change_list),
      base::BindOnce(&WebApkSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistry(std::move(registry_update_from_sync));

  return std::nullopt;
}

void WebApkSyncBridge::OnWebApkUsed(
    std::unique_ptr<sync_pb::WebApkSpecifics> app_specifics,
    bool is_install) {
  if (!change_processor()->IsTrackingMetadata()) {
    return;
  }

  AddOrModifyAppInSync(
      WebApkProtoFromSpecifics(app_specifics.get(), true /* installed */),
      is_install);
}

void WebApkSyncBridge::OnWebApkUninstalled(const std::string& manifest_id) {
  if (!change_processor()->IsTrackingMetadata()) {
    return;
  }

  webapps::AppId app_id = ManifestIdStrToAppId(manifest_id);
  if (IsLegacyAppId(app_id)) {
    return;
  }
  WebApkProto* app = GetAppByIdMutable(registry_, app_id);

  if (app == nullptr) {
    return;
  }

  if (!AppWasUsedRecently(&app->sync_data())) {
    DeleteAppsFromSync(std::vector<webapps::AppId>{app_id},
                       database_.is_opened());
    return;
  }

  // This updates the registry entry directly, so we don't need to call
  // ApplyIncrementalSyncChangesToRegistry() later.
  app->set_is_locally_installed(false);

  // We don't need to update Sync, since this change only affects the
  // non-Specifics part of the proto.
  std::unique_ptr<RegistryUpdateData> registry_update =
      std::make_unique<RegistryUpdateData>();
  registry_update->apps_to_create.push_back(CloneWebApkProto(*app));

  database_.Write(
      *registry_update,
      syncer::DataTypeStore::WriteBatch::CreateMetadataChangeList(),
      base::BindOnce(&WebApkSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));
}

std::vector<WebApkRestoreData> WebApkSyncBridge::GetRestorableAppsShortcutInfo()
    const {
  std::vector<WebApkRestoreData> results;
  for (auto const& [appId, proto] : registry_) {
    if (!proto->is_locally_installed() &&
        AppWasUsedRecently(&proto->sync_data())) {
      auto restore_info = CreateShortcutInfoFromSpecifics(proto->sync_data());
      if (restore_info) {
        results.emplace_back(WebApkRestoreData(
            appId, std::move(restore_info),
            base::Time::FromDeltaSinceWindowsEpoch(base::Microseconds(
                proto->sync_data().last_used_time_windows_epoch_micros()))));
      }
    }
  }
  return results;
}

const WebApkProto* WebApkSyncBridge::GetWebApkByAppId(
    webapps::AppId app_id) const {
  return GetAppById(registry_, app_id);
}

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

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

  return data_batch;
}

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

  for (const auto& appListing : registry_) {
    const webapps::AppId app_id = appListing.first;
    const WebApkProto& app = *appListing.second;
    data_batch->Put(app_id, CreateSyncEntityData(app));
  }

  return data_batch;
}

// GetClientTag and GetStorageKey must return the same thing for a given AppId
// as the dPWA implementation in
// chrome/browser/web_applications/web_app_sync_bridge.cc's
// WebAppSyncBridge::GetClientTag().
std::string WebApkSyncBridge::GetClientTag(
    const syncer::EntityData& entity_data) const {
  DCHECK(entity_data.specifics.has_web_apk());

  return ManifestIdStrToAppId(entity_data.specifics.web_apk().manifest_id());
}

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

void WebApkSyncBridge::ApplyDisableSyncChanges(
    std::unique_ptr<syncer::MetadataChangeList> delete_metadata_change_list) {
  database_.DeleteAllDataAndMetadata(base::DoNothing());

  registry_.clear();
}

void WebApkSyncBridge::RemoveOldWebAPKsFromSync(
    int64_t current_time_ms_since_unix_epoch) {
  std::vector<webapps::AppId> app_ids;
  for (const auto& appListing : registry_) {
    const webapps::AppId app_id = appListing.first;
    const WebApkProto& app = *appListing.second;
    if (!AppWasUsedRecentlyComparedTo(
            &app.sync_data(), base::Time::FromMillisecondsSinceUnixEpoch(
                                  current_time_ms_since_unix_epoch))) {
      app_ids.push_back(app_id);
    }
  }

  RegisterDoneInitializingCallback(
      base::BindOnce(&WebApkSyncBridge::DeleteAppsFromSync,
                     weak_ptr_factory_.GetWeakPtr(), app_ids));
}

void WebApkSyncBridge::AddOrModifyAppInSync(std::unique_ptr<WebApkProto> app,
                                            bool is_install) {
  webapps::AppId app_id = ManifestIdStrToAppId(app->sync_data().manifest_id());
  if (IsLegacyAppId(app_id)) {
    return;
  }
  RecordSyncedWebApkAdditionHistogram(is_install, registry_.count(app_id) > 0);

  std::unique_ptr<syncer::EntityData> entity_data =
      CreateSyncEntityDataFromSpecifics(app->sync_data());

  std::unique_ptr<syncer::MetadataChangeList> metadata_change_list =
      syncer::DataTypeStore::WriteBatch::CreateMetadataChangeList();
  change_processor()->Put(app_id, std::move(entity_data),
                          metadata_change_list.get());

  std::unique_ptr<RegistryUpdateData> registry_update =
      std::make_unique<RegistryUpdateData>();
  registry_update->apps_to_create.push_back(std::move(app));

  database_.Write(
      *registry_update, std::move(metadata_change_list),
      base::BindOnce(&WebApkSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistry(std::move(registry_update));
}

void WebApkSyncBridge::DeleteAppsFromSync(
    const std::vector<webapps::AppId>& app_ids,
    bool database_opened) {
  if (app_ids.size() == 0 || !database_opened) {
    return;
  }

  RecordSyncedWebApkRemovalCountHistogram(app_ids.size());

  std::unique_ptr<syncer::MetadataChangeList> metadata_change_list =
      syncer::DataTypeStore::WriteBatch::CreateMetadataChangeList();
  std::unique_ptr<RegistryUpdateData> registry_update =
      std::make_unique<RegistryUpdateData>();

  for (const webapps::AppId& app_id : app_ids) {
    change_processor()->Delete(app_id, syncer::DeletionOrigin::Unspecified(),
                               metadata_change_list.get());
    registry_update->apps_to_delete.push_back(app_id);
  }

  database_.Write(
      *registry_update, std::move(metadata_change_list),
      base::BindOnce(&WebApkSyncBridge::OnDataWritten,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));

  ApplyIncrementalSyncChangesToRegistry(std::move(registry_update));
}

void WebApkSyncBridge::SetClockForTesting(std::unique_ptr<base::Clock> clock) {
  clock_ = std::move(clock);
}

const Registry& WebApkSyncBridge::GetRegistryForTesting() const {
  return registry_;
}

base::WeakPtr<syncer::DataTypeControllerDelegate>
WebApkSyncBridge::GetDataTypeControllerDelegate() {
  return change_processor()->GetControllerDelegate();
}

void WebApkSyncBridge::RecordSyncedWebApkAdditionHistogram(
    bool is_install,
    bool already_exists_in_sync) const {
  if (is_install && !already_exists_in_sync) {
    base::UmaHistogramEnumeration(
        kSyncedWebApkAdditionHistogramName,
        AddOrModifyType::kNewInstallOnDeviceAndNewAddToSync);
  } else if (is_install && already_exists_in_sync) {
    base::UmaHistogramEnumeration(
        kSyncedWebApkAdditionHistogramName,
        AddOrModifyType::kNewInstallOnDeviceAndModificationToSync);
  } else if (!is_install && !already_exists_in_sync) {
    base::UmaHistogramEnumeration(
        kSyncedWebApkAdditionHistogramName,
        AddOrModifyType::kLaunchOnDeviceAndNewAddToSync);
  } else {
    base::UmaHistogramEnumeration(
        kSyncedWebApkAdditionHistogramName,
        AddOrModifyType::kLaunchOnDeviceAndModificationToSync);
  }
}

void WebApkSyncBridge::RecordSyncedWebApkRemovalCountHistogram(
    int num_web_apks_removed) const {
  base::UmaHistogramExactLinear("WebApk.Sync.SyncedWebApkRemovalCount",
                                num_web_apks_removed, 51 /* max_count */);
}

}  // namespace webapk