File: bookmark_model_merger_comparison_metrics.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 (686 lines) | stat: -rw-r--r-- 26,115 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
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_bookmarks/bookmark_model_merger_comparison_metrics.h"

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/stl_util.h"
#include "base/strings/strcat.h"
#include "base/uuid.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/sync/base/previously_syncing_gaia_id_info_for_metrics.h"
#include "components/sync/protocol/bookmark_specifics.pb.h"
#include "components/sync_bookmarks/bookmark_model_merger.h"
#include "components/sync_bookmarks/bookmark_model_view.h"
#include "components/sync_bookmarks/bookmark_specifics_conversions.h"

namespace sync_bookmarks::metrics {
namespace {

// Constants forked from bookmark_model_merger.cc.
const char kBookmarkBarTag[] = "bookmark_bar";
const char kMobileBookmarksTag[] = "synced_bookmarks";
const char kOtherBookmarksTag[] = "other_bookmarks";

// Constants used when the parent's title is relevant during grouping. They
// resemble the real titles but the precise value isn't important, as long as
// they are unlikely to collide with user-generated folders.
constexpr char16_t kBookmarkBarFolderName[] = u"__Bookmarks bar__";
constexpr char16_t kOtherBookmarksFolderName[] = u"__Other bookmarks__";
constexpr char16_t kMobileBookmarksFolderName[] = u"__Mobile bookmarks__";

using RemoteForest = BookmarkModelMerger::RemoteForest;
using RemoteTreeNode = BookmarkModelMerger::RemoteTreeNode;

// Enum representing the number of URL bookmarks stored locally, bucketized into
// a few notable ranges. This enum is used as suffix when recording metrics.
enum BookmarkCountSuffix {
  kZeroLocalUrlBookmarks,
  kBetween1And19LocalUrlBookmarks,
  kBetween20and999LocalUrlBookmarks,
  k1000OrMoreLocalUrlBookmarks,
};

std::string_view SubtreeSelectionToInfix(SubtreeSelection value) {
  // LINT.IfChange(BookmarkComparisonSubtreeSelection)
  switch (value) {
    case SubtreeSelection::kConsideringAllBookmarks:
      return "ConsideringAllBookmarks";
    case SubtreeSelection::kUnderBookmarksBar:
      return "UnderBookmarksBar";
  }
  // LINT.ThenChange(/tools/metrics/histograms/metadata/sync/histograms.xml:BookmarkComparisonSubtreeSelection)
  NOTREACHED();
}

std::string_view GroupingKeyInfixToString(GroupingKeyInfix value) {
  // LINT.IfChange(BookmarkComparisonGroupingKey)
  switch (value) {
    case GroupingKeyInfix::kByUrl:
      return "ByUrl";
    case GroupingKeyInfix::kByUrlAndTitle:
      return "ByUrlAndTitle";
    case GroupingKeyInfix::kByUrlAndUuid:
      return "ByUrlAndUuid";
    case GroupingKeyInfix::kByUrlAndTitleAndPath:
      return "ByUrlAndTitleAndPath";
    case GroupingKeyInfix::kByUrlAndTitleAndPathAndUuid:
      return "ByUrlAndTitleAndPathAndUuid";
  }
  // LINT.ThenChange(/tools/metrics/histograms/metadata/sync/histograms.xml:BookmarkComparisonGroupingKey)
  NOTREACHED();
}

std::string_view BookmarkCountSuffixToString(BookmarkCountSuffix value) {
  // LINT.IfChange(BookmarkComparisonBookmarkCount)
  switch (value) {
    case kZeroLocalUrlBookmarks:
      return ".ZeroLocalUrlBookmarks";
    case kBetween1And19LocalUrlBookmarks:
      return ".Between1And19LocalUrlBookmarks";
    case kBetween20and999LocalUrlBookmarks:
      return ".Between20and999LocalUrlBookmarks";
    case k1000OrMoreLocalUrlBookmarks:
      return ".1000OrMoreLocalUrlBookmarks";
  }
  // LINT.ThenChange(/tools/metrics/histograms/metadata/sync/histograms.xml:BookmarkComparisonBookmarkCount)
  NOTREACHED();
}

std::string_view PreviouslySyncingGaiaIdInfoToInfix(
    syncer::PreviouslySyncingGaiaIdInfoForMetrics value) {
  // LINT.IfChange(BookmarkComparisonPreviouslySyncingGaiaId)
  switch (value) {
    case syncer::PreviouslySyncingGaiaIdInfoForMetrics::kUnspecified:
      NOTREACHED();
    case syncer::PreviouslySyncingGaiaIdInfoForMetrics::
        kSyncFeatureNeverPreviouslyTurnedOn:
      return ".NoPreviousGaiaId";
    case syncer::PreviouslySyncingGaiaIdInfoForMetrics::
        kCurrentGaiaIdMatchesPreviousWithSyncFeatureOn:
      return ".MatchesPreviousGaiaId";
    case syncer::PreviouslySyncingGaiaIdInfoForMetrics::
        kCurrentGaiaIdIfDiffersPreviousWithSyncFeatureOn:
      return ".DiffersPreviousGaiaId";
  }
  // LINT.ThenChange(/tools/metrics/histograms/metadata/sync/histograms.xml:BookmarkComparisonPreviouslySyncingGaiaId)
  NOTREACHED();
}

std::u16string_view GetBookmarkNodeTitle(const bookmarks::BookmarkNode* node) {
  CHECK(node);
  switch (node->type()) {
    case bookmarks::BookmarkNode::URL:
    case bookmarks::BookmarkNode::FOLDER:
      return node->GetTitle();
    case bookmarks::BookmarkNode::BOOKMARK_BAR:
      return kBookmarkBarFolderName;
    case bookmarks::BookmarkNode::OTHER_NODE:
      return kOtherBookmarksFolderName;
    case bookmarks::BookmarkNode::MOBILE:
      return kMobileBookmarksFolderName;
  }
  NOTREACHED();
}

std::u16string NodeTitleFromEntityData(const syncer::EntityData& entity_data) {
  if (entity_data.server_defined_unique_tag == kBookmarkBarTag) {
    return kBookmarkBarFolderName;
  } else if (entity_data.server_defined_unique_tag == kOtherBookmarksTag) {
    return kOtherBookmarksFolderName;
  } else if (entity_data.server_defined_unique_tag == kMobileBookmarksTag) {
    return kMobileBookmarksFolderName;
  } else {
    return NodeTitleFromSpecifics(entity_data.specifics.bookmark());
  }
}

// Returns the subset of top-level permanent bookmark folders in
// `all_local_data` as selected by `subtree_selection`.
std::vector<const bookmarks::BookmarkNode*> GetRelevantLocalSubtrees(
    const BookmarkModelView& all_local_data,
    SubtreeSelection subtree_selection) {
  switch (subtree_selection) {
    case SubtreeSelection::kConsideringAllBookmarks:
      return {all_local_data.bookmark_bar_node(), all_local_data.other_node(),
              all_local_data.mobile_node()};
    case SubtreeSelection::kUnderBookmarksBar:
      return {all_local_data.bookmark_bar_node()};
  }
}

// Returns the subset of top-level permanent bookmark folder updates in
// `all_account_data` as selected by `subtree_selection`. Note that these
// updates also contain updates for their descendants.
std::vector<const RemoteTreeNode*> GetRelevantAccountSubtrees(
    const RemoteForest& all_account_data,
    SubtreeSelection subtree_selection) {
  std::vector<std::string> relevant_tags;
  switch (subtree_selection) {
    case SubtreeSelection::kConsideringAllBookmarks:
      relevant_tags.push_back(kMobileBookmarksTag);
      relevant_tags.push_back(kOtherBookmarksTag);
      [[fallthrough]];
    case SubtreeSelection::kUnderBookmarksBar:
      relevant_tags.push_back(kBookmarkBarTag);
      break;
  }
  std::vector<const RemoteTreeNode*> result;
  for (const std::string& tag : relevant_tags) {
    auto it = all_account_data.find(tag);
    if (it != all_account_data.end()) {
      result.push_back(&it->second);
    }
  }
  return result;
}

size_t CountUrlNodesInSubtree(const bookmarks::BookmarkNode* node) {
  CHECK(node);
  if (node->is_url()) {
    return 1;
  }
  size_t result = 0;
  for (const std::unique_ptr<bookmarks::BookmarkNode>& child :
       node->children()) {
    result += CountUrlNodesInSubtree(child.get());
  }
  return result;
}

BookmarkCountSuffix CountLocalBookmarks(
    const std::vector<const bookmarks::BookmarkNode*>& local_data) {
  size_t num_url_nodes = 0;
  for (const bookmarks::BookmarkNode* permanent_node : local_data) {
    num_url_nodes += CountUrlNodesInSubtree(permanent_node);
  }
  if (num_url_nodes == 0) {
    return kZeroLocalUrlBookmarks;
  }
  if (num_url_nodes < 20) {
    return kBetween1And19LocalUrlBookmarks;
  }
  if (num_url_nodes < 1000) {
    return kBetween20and999LocalUrlBookmarks;
  }
  return k1000OrMoreLocalUrlBookmarks;
}

// Function template declaration that must be specialized per supported grouping
// criterion that allows mapping local data to keys used for grouping.
template <typename Key>
Key GroupingKeyFromLocalData(const bookmarks::BookmarkNode* node);

// Same as above but for account data.
template <typename Key>
Key GroupingKeyFromLocalData(const bookmarks::BookmarkNode* node,
                             std::u16string path);

template <typename Key>
Key GroupingKeyFromAccountData(const sync_pb::BookmarkSpecifics& specifics,
                               std::u16string path);

template <>
UrlOnly GroupingKeyFromLocalData(const bookmarks::BookmarkNode* node,
                                 std::u16string path) {
  UrlOnly key;
  key.url = node->url();
  // `path` ignored but required in signture for template code.
  return key;
}

template <>
UrlOnly GroupingKeyFromAccountData(const sync_pb::BookmarkSpecifics& specifics,
                                   std::u16string path) {
  UrlOnly key;
  key.url = GURL(specifics.url());
  // `path` ignored but required in signture for template code.
  return key;
}

template <>
UrlAndTitle GroupingKeyFromLocalData(const bookmarks::BookmarkNode* node,
                                     std::u16string path) {
  UrlAndTitle key;
  key.url = node->url();
  key.title = node->GetTitle();
  // `path` ignored but required in signture for template code.
  return key;
}

template <>
UrlAndTitle GroupingKeyFromAccountData(
    const sync_pb::BookmarkSpecifics& specifics,
    std::u16string path) {
  UrlAndTitle key;
  key.url = GURL(specifics.url());
  key.title = NodeTitleFromSpecifics(specifics);
  // `path` ignored but required in signture for template code.
  return key;
}

template <>
UrlAndUuid GroupingKeyFromLocalData(const bookmarks::BookmarkNode* node,
                                    std::u16string path) {
  UrlAndUuid key;
  key.url = node->url();
  key.uuid = node->uuid();
  // `path` ignored but required in signture for template code.
  return key;
}

template <>
UrlAndUuid GroupingKeyFromAccountData(
    const sync_pb::BookmarkSpecifics& specifics,
    std::u16string path) {
  UrlAndUuid key;
  key.url = GURL(specifics.url());
  key.uuid = base::Uuid::ParseLowercase(specifics.guid());
  // `path` ignored but required in signture for template code.
  return key;
}

template <>
UrlAndTitleAndPath GroupingKeyFromLocalData(const bookmarks::BookmarkNode* node,
                                            std::u16string path) {
  UrlAndTitleAndPath key;
  key.url = node->url();
  key.title = node->GetTitle();
  key.path = std::move(path);
  return key;
}

template <>
UrlAndTitleAndPath GroupingKeyFromAccountData(
    const sync_pb::BookmarkSpecifics& specifics,
    std::u16string path) {
  UrlAndTitleAndPath key;
  key.url = GURL(specifics.url());
  key.title = NodeTitleFromSpecifics(specifics);
  key.path = std::move(path);
  return key;
}

template <>
UrlAndTitleAndPathAndUuid GroupingKeyFromLocalData(
    const bookmarks::BookmarkNode* node,
    std::u16string path) {
  UrlAndTitleAndPathAndUuid key;
  key.url = node->url();
  key.title = node->GetTitle();
  key.path = std::move(path);
  key.uuid = node->uuid();
  return key;
}

template <>
UrlAndTitleAndPathAndUuid GroupingKeyFromAccountData(
    const sync_pb::BookmarkSpecifics& specifics,
    std::u16string path) {
  UrlAndTitleAndPathAndUuid key;
  key.url = GURL(specifics.url());
  key.title = NodeTitleFromSpecifics(specifics);
  key.path = std::move(path);
  key.uuid = base::Uuid::ParseLowercase(specifics.guid());
  return key;
}

// Given two sets `set1` and `set2`, returns the number of values that exist in
// both.
template <typename Key>
size_t GetSetIntersectionSize(const base::flat_set<Key>& set1,
                              const base::flat_set<Key>& set2) {
  size_t intersection_size = 0;
  auto it1 = set1.begin();
  auto it2 = set2.begin();
  while (it1 != set1.end() && it2 != set2.end()) {
    if (*it1 == *it2) {
      ++intersection_size;
      ++it1;
      ++it2;
    } else if (*it1 < *it2) {
      ++it1;
    } else {
      ++it2;
    }
  }
  // The implementation above could be replaced the size resulting of calling
  // base::STLSetIntersection(), but this is avoided for performance reasons,
  // because base::STLSetIntersection() would compute a full set.
  DCHECK_EQ(intersection_size,
            base::STLSetIntersection<base::flat_set<Key>>(set1, set2).size());
  return intersection_size;
}

// Given two sets `account_data` and `local_data`, it returns how the two
// relate to each other as represented in enum `SetComparisonOutcome`.
template <typename Key>
SetComparisonOutcome CompareSets(const base::flat_set<Key>& account_data,
                                 const base::flat_set<Key>& local_data) {
  const size_t account_data_set_size = account_data.size();
  const size_t local_data_set_size = local_data.size();
  const size_t intersection_size =
      GetSetIntersectionSize(local_data, account_data);

  CHECK_LE(intersection_size, local_data_set_size);
  CHECK_LE(intersection_size, account_data_set_size);

  if (local_data_set_size == 0 && account_data_set_size == 0) {
    return SetComparisonOutcome::kBothEmpty;
  } else if (local_data_set_size == 0) {
    return SetComparisonOutcome::kLocalDataEmpty;
  } else if (account_data_set_size == 0) {
    return SetComparisonOutcome::kAccountDataEmpty;
  } else if (local_data_set_size == intersection_size) {
    if (account_data_set_size == intersection_size) {
      return SetComparisonOutcome::kExactMatchNonEmpty;
    } else {
      return SetComparisonOutcome::kLocalDataIsStrictSubsetOfAccountData;
    }
  }

  // Produce a notion of similarity based on which fraction of the total data
  // items (union) is included in the intersection.
  const size_t union_size =
      local_data_set_size + account_data_set_size - intersection_size;
  DCHECK_EQ(
      union_size,
      base::STLSetUnion<base::flat_set<Key>>(account_data, local_data).size());

  CHECK_LT(intersection_size, union_size);
  const double intersecting_fraction = 1.0 * intersection_size / union_size;

  if (intersecting_fraction >= 0.99) {
    return SetComparisonOutcome::kIntersectionBetween99And100Percent;
  } else if (intersecting_fraction >= 0.95) {
    return SetComparisonOutcome::kIntersectionBetween95And99Percent;
  } else if (intersecting_fraction >= 0.90) {
    return SetComparisonOutcome::kIntersectionBetween90And95Percent;
  } else if (intersecting_fraction >= 0.50) {
    return SetComparisonOutcome::kIntersectionBetween50And90Percent;
  } else if (intersecting_fraction >= 0.10) {
    return SetComparisonOutcome::kIntersectionBetween10And50Percent;
  } else if (intersecting_fraction > 0) {
    return SetComparisonOutcome::kIntersectionBelow10PercentExcludingZero;
  } else {
    return SetComparisonOutcome::kIntersectionEmpty;
  }
}

// Recursive function used to implement `ExtractLocalDataSet()` below.
template <typename Key>
void ExtractLocalDataSetRecursive(std::u16string path,
                                  const bookmarks::BookmarkNode* node,
                                  std::vector<Key>& keys) {
  CHECK(node);
  if (node->is_url()) {
    keys.emplace_back(GroupingKeyFromLocalData<Key>(node, std::move(path)));
    return;
  }
  for (const std::unique_ptr<bookmarks::BookmarkNode>& child :
       node->children()) {
    ExtractLocalDataSetRecursive(
        base::StrCat({path, u"/", GetBookmarkNodeTitle(node)}), child.get(),
        keys);
  }
}

// Returns unique data items representing URL bookmarks in
// `relevant_local_subtrees` after being grouped by `Key`.
template <typename Key>
base::flat_set<Key> ExtractLocalDataSet(
    const std::vector<const bookmarks::BookmarkNode*>&
        relevant_local_subtrees) {
  std::vector<Key> keys;
  for (const bookmarks::BookmarkNode* node : relevant_local_subtrees) {
    ExtractLocalDataSetRecursive(/*path=*/u"", node, keys);
  }
  return base::flat_set<Key>(std::move(keys));
}

// Recursive function used to implement `ExtractAccountDataSet()` below.
template <typename Key>
void ExtractAccountDataSetRecursive(std::u16string path,
                                    const RemoteTreeNode& node,
                                    std::vector<Key>& keys) {
  if (node.entity().specifics.bookmark().type() ==
      sync_pb::BookmarkSpecifics::URL) {
    keys.emplace_back(GroupingKeyFromAccountData<Key>(
        node.entity().specifics.bookmark(), std::move(path)));
    return;
  }
  for (const RemoteTreeNode& child : node.children()) {
    ExtractAccountDataSetRecursive(
        base::StrCat({path, u"/", NodeTitleFromEntityData(node.entity())}),
        child, keys);
  }
}

// Returns unique data items representing URL bookmarks in
// `relevant_account_subtrees` after being grouped by `Key`.
template <typename Key>
base::flat_set<Key> ExtractAccountDataSet(
    const std::vector<const RemoteTreeNode*>& relevant_account_subtrees) {
  std::vector<Key> keys;
  for (const RemoteTreeNode* node : relevant_account_subtrees) {
    CHECK(node);
    ExtractAccountDataSetRecursive(/*path=*/u"", *node, keys);
  }
  return base::flat_set<Key>(std::move(keys));
}

template <typename Key>
void CompareAndLogHistogramsWithKey(
    SubtreeSelection subtree_selection,
    syncer::PreviouslySyncingGaiaIdInfoForMetrics
        previously_syncing_gaia_id_info,
    BookmarkCountSuffix bookmark_count_suffix,
    const std::vector<const bookmarks::BookmarkNode*>& relevant_local_subtrees,
    const std::vector<const RemoteTreeNode*>& relevant_account_subtrees) {
  const base::flat_set<Key> local_data_set =
      ExtractLocalDataSet<Key>(relevant_local_subtrees);
  const base::flat_set<Key> account_data_set =
      ExtractAccountDataSet<Key>(relevant_account_subtrees);

  // When recording the metric, always record four metrics, resulting from
  // the combinatorial cases for:
  // 1. With and without the infix representing
  //    PreviouslySyncingGaiaIdInfoForMetrics.
  // 2. With and without the suffix representing the number of local URL
  //    bookmarks.
  for (std::string_view optional_previously_syncing_gaia_id_info_infix :
       {std::string_view(),
        PreviouslySyncingGaiaIdInfoToInfix(previously_syncing_gaia_id_info)}) {
    for (std::string_view optional_bookmark_count_suffix :
         {std::string_view(),
          BookmarkCountSuffixToString(bookmark_count_suffix)}) {
      const std::string legacy_histogram_name =
          base::StrCat({"Sync.BookmarkModelMerger.Comparison",
                        optional_previously_syncing_gaia_id_info_infix, ".",
                        SubtreeSelectionToInfix(subtree_selection), ".",
                        GroupingKeyInfixToString(Key::kGroupingKeyInfix),
                        optional_bookmark_count_suffix});

      // The call below to CompareSets() mixes up local data with account data.
      // Such implementation was accidental, but the resulting metric is anyway
      // meaningful, and the resulting behavior documented in histograms.xml.
      // For a fixed version of this, see `fixed_histogram_name` immediately
      // below.
      // TODO(crbug.com/424551547): Clean up the legacy metric, if not both,
      // in upcoming milestones (M139 or M140).
      base::UmaHistogramEnumeration(
          legacy_histogram_name, CompareSets(/*account_data=*/local_data_set,
                                             /*local_data=*/account_data_set));

      const std::string fixed_histogram_name =
          base::StrCat({"Sync.BookmarkModelMerger.Comparison2",
                        optional_previously_syncing_gaia_id_info_infix, ".",
                        SubtreeSelectionToInfix(subtree_selection), ".",
                        GroupingKeyInfixToString(Key::kGroupingKeyInfix),
                        optional_bookmark_count_suffix});

      base::UmaHistogramEnumeration(
          fixed_histogram_name, CompareSets(/*account_data=*/account_data_set,
                                            /*local_data=*/local_data_set));
    }
  }
}

}  // namespace

UrlAndTitleAndPathAndUuid::UrlAndTitleAndPathAndUuid() = default;

UrlAndTitleAndPathAndUuid::UrlAndTitleAndPathAndUuid(
    const GURL& url,
    const std::u16string& title,
    const std::u16string& path,
    const base::Uuid& uuid)
    : url(url), title(title), path(path), uuid(uuid) {}

UrlAndTitleAndPathAndUuid::UrlAndTitleAndPathAndUuid(
    const UrlAndTitleAndPathAndUuid&) = default;

UrlAndTitleAndPathAndUuid::UrlAndTitleAndPathAndUuid(
    UrlAndTitleAndPathAndUuid&&) = default;

UrlAndTitleAndPathAndUuid::~UrlAndTitleAndPathAndUuid() = default;

UrlAndTitleAndPathAndUuid& UrlAndTitleAndPathAndUuid::operator=(
    const UrlAndTitleAndPathAndUuid&) = default;

UrlAndTitleAndPathAndUuid& UrlAndTitleAndPathAndUuid::operator=(
    UrlAndTitleAndPathAndUuid&&) = default;

base::flat_set<UrlOnly> ExtractUniqueLocalNodesByUrlForTesting(
    const BookmarkModelView& all_local_data,
    SubtreeSelection subtree_selection) {
  return ExtractLocalDataSet<UrlOnly>(
      GetRelevantLocalSubtrees(all_local_data, subtree_selection));
}

base::flat_set<UrlAndTitle> ExtractUniqueLocalNodesByUrlAndTitleForTesting(
    const BookmarkModelView& all_local_data,
    SubtreeSelection subtree_selection) {
  return ExtractLocalDataSet<UrlAndTitle>(
      GetRelevantLocalSubtrees(all_local_data, subtree_selection));
}

base::flat_set<UrlAndUuid> ExtractUniqueLocalNodesByUrlAndUuidForTesting(
    const BookmarkModelView& all_local_data,
    SubtreeSelection subtree_selection) {
  return ExtractLocalDataSet<UrlAndUuid>(
      GetRelevantLocalSubtrees(all_local_data, subtree_selection));
}

base::flat_set<UrlAndTitleAndPath>
ExtractUniqueLocalNodesByUrlAndTitleAndPathForTesting(
    const BookmarkModelView& all_local_data,
    SubtreeSelection subtree_selection) {
  return ExtractLocalDataSet<UrlAndTitleAndPath>(
      GetRelevantLocalSubtrees(all_local_data, subtree_selection));
}

base::flat_set<UrlAndTitleAndPathAndUuid>
ExtractUniqueLocalNodesByUrlAndTitleAndPathAndUuidForTesting(
    const BookmarkModelView& all_local_data,
    SubtreeSelection subtree_selection) {
  return ExtractLocalDataSet<UrlAndTitleAndPathAndUuid>(
      GetRelevantLocalSubtrees(all_local_data, subtree_selection));
}

base::flat_set<UrlOnly> ExtractUniqueAccountNodesByUrlForTesting(
    const BookmarkModelMerger::RemoteForest& all_account_data,
    SubtreeSelection subtree_selection) {
  return ExtractAccountDataSet<UrlOnly>(
      GetRelevantAccountSubtrees(all_account_data, subtree_selection));
}

base::flat_set<UrlAndTitle> ExtractUniqueAccountNodesByUrlAndTitleForTesting(
    const BookmarkModelMerger::RemoteForest& all_account_data,
    SubtreeSelection subtree_selection) {
  return ExtractAccountDataSet<UrlAndTitle>(
      GetRelevantAccountSubtrees(all_account_data, subtree_selection));
}

base::flat_set<UrlAndUuid> ExtractUniqueAccountNodesByUrlAndUuidForTesting(
    const BookmarkModelMerger::RemoteForest& all_account_data,
    SubtreeSelection subtree_selection) {
  return ExtractAccountDataSet<UrlAndUuid>(
      GetRelevantAccountSubtrees(all_account_data, subtree_selection));
}

base::flat_set<UrlAndTitleAndPath>
ExtractUniqueAccountNodesByUrlAndTitleAndPathForTesting(
    const BookmarkModelMerger::RemoteForest& all_account_data,
    SubtreeSelection subtree_selection) {
  return ExtractAccountDataSet<UrlAndTitleAndPath>(
      GetRelevantAccountSubtrees(all_account_data, subtree_selection));
}

base::flat_set<UrlAndTitleAndPathAndUuid>
ExtractUniqueAccountNodesByUrlAndTitleAndPathAndUuidForTesting(
    const BookmarkModelMerger::RemoteForest& all_account_data,
    SubtreeSelection subtree_selection) {
  return ExtractAccountDataSet<UrlAndTitleAndPathAndUuid>(
      GetRelevantAccountSubtrees(all_account_data, subtree_selection));
}

SetComparisonOutcome CompareSetsForTesting(
    const base::flat_set<int>& account_data,
    const base::flat_set<int>& local_data) {
  return CompareSets<int>(account_data, local_data);
}

void CompareBookmarkModelAndLogHistograms(
    const BookmarkModelView& all_local_data,
    const BookmarkModelMerger::RemoteForest& all_account_data,
    syncer::PreviouslySyncingGaiaIdInfoForMetrics
        previously_syncing_gaia_id_info) {
  CHECK_NE(previously_syncing_gaia_id_info,
           syncer::PreviouslySyncingGaiaIdInfoForMetrics::kUnspecified);

  for (SubtreeSelection subtree_selection :
       {SubtreeSelection::kConsideringAllBookmarks,
        SubtreeSelection::kUnderBookmarksBar}) {
    const std::vector<const bookmarks::BookmarkNode*> relevant_local_subtrees =
        GetRelevantLocalSubtrees(all_local_data, subtree_selection);
    const std::vector<const RemoteTreeNode*> relevant_account_subtrees =
        GetRelevantAccountSubtrees(all_account_data, subtree_selection);

    const BookmarkCountSuffix bookmark_count_suffix =
        CountLocalBookmarks(relevant_local_subtrees);

    CompareAndLogHistogramsWithKey<UrlOnly>(
        subtree_selection, previously_syncing_gaia_id_info,
        bookmark_count_suffix, relevant_local_subtrees,
        relevant_account_subtrees);
    CompareAndLogHistogramsWithKey<UrlAndTitle>(
        subtree_selection, previously_syncing_gaia_id_info,
        bookmark_count_suffix, relevant_local_subtrees,
        relevant_account_subtrees);
    CompareAndLogHistogramsWithKey<UrlAndUuid>(
        subtree_selection, previously_syncing_gaia_id_info,
        bookmark_count_suffix, relevant_local_subtrees,
        relevant_account_subtrees);
    CompareAndLogHistogramsWithKey<UrlAndTitleAndPath>(
        subtree_selection, previously_syncing_gaia_id_info,
        bookmark_count_suffix, relevant_local_subtrees,
        relevant_account_subtrees);
    CompareAndLogHistogramsWithKey<UrlAndTitleAndPathAndUuid>(
        subtree_selection, previously_syncing_gaia_id_info,
        bookmark_count_suffix, relevant_local_subtrees,
        relevant_account_subtrees);
  }
}

}  // namespace sync_bookmarks::metrics