File: messaging_backend_service_impl.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 (1837 lines) | stat: -rw-r--r-- 70,717 bytes parent folder | download | duplicates (3)
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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
// Copyright 2024 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/collaboration/internal/messaging/messaging_backend_service_impl.h"

#include <sys/types.h>

#include <memory>
#include <optional>
#include <string>
#include <unordered_set>
#include <vector>

#include "base/check.h"
#include "base/check_is_test.h"
#include "base/containers/contains.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/break_iterator.h"
#include "base/i18n/message_formatter.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/collaboration/internal/messaging/data_sharing_change_notifier_impl.h"
#include "components/collaboration/internal/messaging/storage/collaboration_message_util.h"
#include "components/collaboration/internal/messaging/storage/messaging_backend_store.h"
#include "components/collaboration/internal/messaging/storage/protocol/message.pb.h"
#include "components/collaboration/internal/messaging/tab_group_change_notifier.h"
#include "components/collaboration/public/messaging/activity_log.h"
#include "components/collaboration/public/messaging/message.h"
#include "components/data_sharing/public/group_data.h"
#include "components/saved_tab_groups/public/saved_tab_group.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/elide_url.h"
#include "ui/base/l10n/l10n_util.h"

namespace collaboration::messaging {
namespace {
collaboration_pb::Message CreateMessage(
    const data_sharing::GroupId& collaboration_group_id,
    collaboration_pb::EventType event_type,
    DirtyType dirty_type,
    const base::Time& event_time) {
  collaboration_pb::Message message;
  message.set_uuid(base::Uuid::GenerateRandomV4().AsLowercaseString());
  message.set_collaboration_id(collaboration_group_id.value());
  message.set_event_type(event_type);
  message.set_dirty(static_cast<int>(dirty_type));
  message.set_event_timestamp(event_time.ToTimeT());
  return message;
}

std::u16string TruncateTabTitle(const std::u16string& original_title) {
  constexpr int kMaxTabTitleCharacters = 28;
  constexpr char16_t kEllipsis = u'\u2026';
  std::u16string trimmed;
  base::TrimWhitespace(original_title, base::TrimPositions::TRIM_ALL, &trimmed);

  // If the size of the text is already smaller than the max size without
  // grapheme counting, then we can just return the text untrimmed.
  if (trimmed.size() <= kMaxTabTitleCharacters) {
    return trimmed;
  }

  // Count the number of graphemes, stopping when we hit the max size.
  // Copy the string_view contents over to the result string.
  std::u16string result;
  base::i18n::BreakIterator iter(trimmed,
                                 base::i18n::BreakIterator::BREAK_CHARACTER);
  iter.Init();
  int seen = 0;
  while (seen < kMaxTabTitleCharacters && iter.Advance()) {
    ++seen;

    // GetString returns the span that Advance() moved forwards on.
    auto span = iter.GetString();
    result.append(span.data(), span.size());
  }

  // If seen count hit the max characters and there are still more, add the
  // ellipsis.
  if (iter.Advance()) {
    result.push_back(kEllipsis);
  }
  return result;
}

collaboration_pb::Message CreateTabGroupMessage(
    data_sharing::GroupId collaboration_group_id,
    const tab_groups::SavedTabGroup& tab_group,
    collaboration_pb::EventType event_type,
    DirtyType dirty_type) {
  collaboration_pb::Message message = CreateMessage(
      collaboration_group_id, event_type, dirty_type, tab_group.update_time());
  message.mutable_tab_group_data()->set_sync_tab_group_id(
      tab_group.saved_guid().AsLowercaseString());
  message.mutable_tab_group_data()->set_title(
      base::UTF16ToUTF8(tab_group.title()));
  switch (event_type) {
    case collaboration_pb::TAB_GROUP_ADDED:
      message.set_triggering_user_gaia_id(
          tab_group.shared_attribution().created_by.ToString());
      break;
    case collaboration_pb::TAB_GROUP_REMOVED:
    case collaboration_pb::TAB_GROUP_NAME_UPDATED:
    case collaboration_pb::TAB_GROUP_COLOR_UPDATED:
      message.set_triggering_user_gaia_id(
          tab_group.shared_attribution().updated_by.ToString());
      break;
    default:
      break;
  }
  return message;
}

collaboration_pb::Message CreateTabMessage(
    data_sharing::GroupId collaboration_group_id,
    const tab_groups::SavedTabGroupTab& tab,
    collaboration_pb::EventType event_type,
    DirtyType dirty_type) {
  collaboration_pb::Message message = CreateMessage(
      collaboration_group_id, event_type, dirty_type,
      event_type == collaboration_pb::TAB_ADDED ? tab.creation_time()
                                                : tab.update_time());
  message.mutable_tab_data()->set_sync_tab_id(
      tab.saved_tab_guid().AsLowercaseString());
  message.mutable_tab_data()->set_sync_tab_group_id(
      tab.saved_group_guid().AsLowercaseString());
  message.mutable_tab_data()->set_last_url(tab.url().spec());
  switch (event_type) {
    case collaboration_pb::TAB_ADDED:
      message.set_triggering_user_gaia_id(
          tab.shared_attribution().created_by.ToString());
      break;
    case collaboration_pb::TAB_UPDATED:
    case collaboration_pb::TAB_REMOVED:
      message.set_triggering_user_gaia_id(
          tab.shared_attribution().updated_by.ToString());
      break;
    default:
      break;
  }
  return message;
}

CollaborationEvent ToCollaborationEvent(
    collaboration_pb::EventType event_type) {
  switch (event_type) {
    case collaboration_pb::TAB_ADDED:
      return CollaborationEvent::TAB_ADDED;
    case collaboration_pb::TAB_REMOVED:
      return CollaborationEvent::TAB_REMOVED;
    case collaboration_pb::TAB_UPDATED:
      return CollaborationEvent::TAB_UPDATED;
    case collaboration_pb::TAB_GROUP_ADDED:
      return CollaborationEvent::TAB_GROUP_ADDED;
    case collaboration_pb::TAB_GROUP_REMOVED:
      return CollaborationEvent::TAB_GROUP_REMOVED;
    case collaboration_pb::TAB_GROUP_NAME_UPDATED:
      return CollaborationEvent::TAB_GROUP_NAME_UPDATED;
    case collaboration_pb::TAB_GROUP_COLOR_UPDATED:
      return CollaborationEvent::TAB_GROUP_COLOR_UPDATED;
    case collaboration_pb::COLLABORATION_ADDED:
      return CollaborationEvent::COLLABORATION_ADDED;
    case collaboration_pb::COLLABORATION_REMOVED:
      return CollaborationEvent::COLLABORATION_REMOVED;
    case collaboration_pb::COLLABORATION_MEMBER_ADDED:
      return CollaborationEvent::COLLABORATION_MEMBER_ADDED;
    case collaboration_pb::COLLABORATION_MEMBER_REMOVED:
      return CollaborationEvent::COLLABORATION_MEMBER_REMOVED;
    default:
      return CollaborationEvent::UNDEFINED;
  }
}

RecentActivityAction GetRecentActivityActionFromCollaborationEvent(
    CollaborationEvent event) {
  switch (event) {
    case CollaborationEvent::TAB_ADDED:
    case CollaborationEvent::TAB_UPDATED:
      return RecentActivityAction::kFocusTab;
    case CollaborationEvent::TAB_REMOVED:
      return RecentActivityAction::kReopenTab;
    case CollaborationEvent::TAB_GROUP_ADDED:
    case CollaborationEvent::TAB_GROUP_REMOVED:
      return RecentActivityAction::kNone;
    case CollaborationEvent::TAB_GROUP_NAME_UPDATED:
    case CollaborationEvent::TAB_GROUP_COLOR_UPDATED:
      return RecentActivityAction::kOpenTabGroupEditDialog;
    case CollaborationEvent::COLLABORATION_ADDED:
    case CollaborationEvent::COLLABORATION_REMOVED:
      return RecentActivityAction::kNone;
    case CollaborationEvent::COLLABORATION_MEMBER_ADDED:
    case CollaborationEvent::COLLABORATION_MEMBER_REMOVED:
      return RecentActivityAction::kManageSharing;
    case CollaborationEvent::UNDEFINED:
      return RecentActivityAction::kNone;
  }
}

// Enum used to show the correct selector variant of the string template.
enum class TimeDimension {
  kMinutes = 0,
  kHours = 1,
  kDays = 2,
  kMaxValue = kDays,
};

// Gets the string representation of the given time delta. Time is
// binned into minutes, hours, or days.
std::u16string GetElapsedTimeText(base::TimeDelta time_delta) {
  TimeDimension dimension;
  int number;
  if (time_delta < base::Minutes(1)) {
    return l10n_util::GetStringUTF16(IDS_DATA_SHARING_RECENT_ACTIVITY_JUST_NOW);
  } else if (time_delta < base::Hours(1)) {
    dimension = TimeDimension::kMinutes;
    number = time_delta.InMinutes();
  } else if (time_delta < base::Days(1)) {
    dimension = TimeDimension::kHours;
    number = time_delta.InHours();
  } else {
    dimension = TimeDimension::kDays;
    number = time_delta.InDays();
  }
  return base::i18n::MessageFormatter::FormatWithNumberedArgs(
      l10n_util::GetStringFUTF16(
          IDS_DATA_SHARING_RECENT_ACTIVITY_TIME_DELTA,
          base::UTF8ToUTF16(base::NumberToString(number))),
      static_cast<int>(dimension));
}

std::optional<GaiaId> GetGaiaIdFromMessage(
    const collaboration_pb::Message& message) {
  switch (GetMessageCategory(message)) {
    case MessageCategory::kTab:
    case MessageCategory::kTabGroup:
      if (message.triggering_user_gaia_id().empty()) {
        return std::nullopt;
      }
      return GaiaId(message.triggering_user_gaia_id());
    case MessageCategory::kCollaboration:
      if (message.affected_user_gaia_id().empty()) {
        return std::nullopt;
      }
      return GaiaId(message.affected_user_gaia_id());
    default:
      return std::nullopt;
  }
}

std::optional<data_sharing::GroupId> GroupIdForTabGroup(
    const tab_groups::SavedTabGroup& tab_group) {
  if (!tab_group.collaboration_id()) {
    return std::nullopt;
  }
  return data_sharing::GroupId(tab_group.collaboration_id().value().value());
}

tab_groups::CollaborationId ToCollaborationId(
    const data_sharing::GroupId& group_id) {
  return tab_groups::CollaborationId(group_id.value());
}

TabGroupMessageMetadata CreateTabGroupMessageMetadata(
    const tab_groups::SavedTabGroup& tab_group) {
  TabGroupMessageMetadata metadata;
  metadata.local_tab_group_id = tab_group.local_group_id();
  metadata.sync_tab_group_id = tab_group.saved_guid();
  metadata.last_known_title = base::UTF16ToUTF8(tab_group.title());
  if (metadata.last_known_title->empty()) {
    metadata.last_known_title = l10n_util::GetPluralStringFUTF8(
        IDS_DATA_SHARING_TAB_GROUP_DEFAULT_TITLE_TABS_COUNT,
        tab_group.saved_tabs().size());
  }
  metadata.last_known_color = tab_group.color();
  return metadata;
}

TabMessageMetadata CreateTabMessageMetadata(
    const tab_groups::SavedTabGroupTab& tab) {
  auto tab_metadata = TabMessageMetadata();
  tab_metadata.local_tab_id = tab.local_tab_id();
  tab_metadata.sync_tab_id = tab.saved_tab_guid();
  tab_metadata.last_known_url = tab.url().spec();
  tab_metadata.last_known_title = base::UTF16ToUTF8(tab.title());
  return tab_metadata;
}

TabMessageMetadata CreateTabMessageMetadataFromMessageOrTab(
    const collaboration_pb::Message& message,
    std::optional<tab_groups::SavedTabGroupTab> tab) {
  if (tab) {
    return CreateTabMessageMetadata(*tab);
  }

  // Tab no longer available, so fill in what we can.
  TabMessageMetadata tab_metadata = TabMessageMetadata();
  tab_metadata.last_known_url = message.tab_data().last_url();
  tab_metadata.sync_tab_id =
      base::Uuid::ParseLowercase(message.tab_data().sync_tab_id());
  return tab_metadata;
}

std::optional<tab_groups::SavedTabGroupTab> GetTabFromGroup(
    const collaboration_pb::Message& message,
    std::optional<tab_groups::SavedTabGroup> tab_group) {
  if (!tab_group) {
    return std::nullopt;
  }

  const tab_groups::SavedTabGroupTab* tab = tab_group->GetTab(
      base::Uuid::ParseCaseInsensitive(message.tab_data().sync_tab_id()));
  if (tab) {
    return std::make_optional(*tab);
  }
  return std::nullopt;
}

std::u16string GetTitleForTabRemovedMessage(const InstantMessage& message) {
  const auto& attribution = message.attributions[0];
  std::optional<data_sharing::GroupMember> user = attribution.triggering_user;
  std::optional<TabMessageMetadata> tab_metadata = attribution.tab_metadata;
  const bool has_title =
      tab_metadata.has_value() && tab_metadata->last_known_title.has_value();
  if (!user.has_value() || !has_title) {
    return std::u16string();
  }

  return l10n_util::GetStringFUTF16(
      IDS_DATA_SHARING_TOAST_TAB_REMOVED, base::UTF8ToUTF16(user->given_name),
      TruncateTabTitle(
          base::UTF8ToUTF16(tab_metadata->last_known_title.value())));
}

std::u16string GetTitleForTabUpdatedMessage(const InstantMessage& message) {
  const auto& attribution = message.attributions[0];
  std::optional<data_sharing::GroupMember> user = attribution.triggering_user;
  std::optional<TabMessageMetadata> tab_metadata = attribution.tab_metadata;
  const bool has_title =
      tab_metadata.has_value() && tab_metadata->last_known_title.has_value();
  if (!user.has_value() || !has_title) {
    return std::u16string();
  }

  return l10n_util::GetStringFUTF16(
      IDS_DATA_SHARING_TOAST_TAB_UPDATED, base::UTF8ToUTF16(user->given_name),
      TruncateTabTitle(
          base::UTF8ToUTF16(tab_metadata->last_known_title.value())));
}

std::u16string GetTitleForMemberAddedMessage(const InstantMessage& message) {
  const auto& attribution = message.attributions[0];
  std::optional<data_sharing::GroupMember> user = attribution.affected_user;
  std::optional<TabGroupMessageMetadata> tab_group_metadata =
      attribution.tab_group_metadata;
  const bool has_group_title = tab_group_metadata.has_value() &&
                               tab_group_metadata->last_known_title.has_value();
  if (!user.has_value() || !has_group_title) {
    return std::u16string();
  }

  return l10n_util::GetStringFUTF16(
      IDS_DATA_SHARING_TOAST_NEW_MEMBER, base::UTF8ToUTF16(user->given_name),
      TruncateTabTitle(
          base::UTF8ToUTF16(tab_group_metadata->last_known_title.value())));
}

std::u16string GetTitleForTabGroupRemovedMessage(
    const InstantMessage& message) {
  const auto& attribution = message.attributions[0];
  std::optional<TabGroupMessageMetadata> tab_group_metadata =
      attribution.tab_group_metadata;
  const bool has_group_title = tab_group_metadata.has_value() &&
                               tab_group_metadata->last_known_title.has_value();
  if (!has_group_title) {
    return std::u16string();
  }

  return l10n_util::GetStringFUTF16(
      IDS_DATA_SHARING_TOAST_BLOCK_LEAVE,
      TruncateTabTitle(
          base::UTF8ToUTF16(tab_group_metadata->last_known_title.value())));
}

DirtyType GetDirtyTypeFromPersistentNotificationTypeForQuery(
    std::optional<PersistentNotificationType> type) {
  if (!type) {
    // Ask for all dirty messages.
    return DirtyType::kAll;
  }
  if (*type == PersistentNotificationType::DIRTY_TAB) {
    return DirtyType::kDot;
  } else if (*type == PersistentNotificationType::CHIP) {
    return DirtyType::kChip;
  } else if (*type == PersistentNotificationType::TOMBSTONED) {
    return DirtyType::kTombstoned;
  } else if (*type == PersistentNotificationType::INSTANT_MESSAGE) {
    return DirtyType::kMessageOnly;
  } else {
    // Ask for all dirty messages.
    return DirtyType::kAll;
  }
}

std::vector<PersistentMessage> RemoveDuplicateDirtyTabGroupMessages(
    const std::vector<PersistentMessage>& messages) {
  std::unordered_set<data_sharing::GroupId> dirty_tab_groups;
  std::vector<PersistentMessage> result;
  for (const auto& message : messages) {
    if (message.type == PersistentNotificationType::DIRTY_TAB_GROUP) {
      // We only want one DIRTY_TAB_GROUP per collaboration.
      if (dirty_tab_groups.find(message.attribution.collaboration_id) ==
          dirty_tab_groups.end()) {
        // This is the first one, so we add it.
        dirty_tab_groups.emplace(message.attribution.collaboration_id);
        result.emplace_back(message);
      }
    } else {
      // If this is not a dirty tab group, add it to the result
      result.emplace_back(message);
    }
  }
  return result;
}

std::vector<PersistentMessage> CreatePersistentMessagesForTypes(
    PersistentMessage base_message,
    const std::vector<PersistentNotificationType>& types) {
  std::vector<PersistentMessage> messages;
  for (const PersistentNotificationType& type : types) {
    PersistentMessage message = base_message;
    message.type = type;
    messages.emplace_back(message);
  }
  return messages;
}

bool IsMemberOwner(const data_sharing::GroupData& group_data,
                   const GaiaId& member_gaia_id) {
  for (const data_sharing::GroupMember& member : group_data.members) {
    if (member.gaia_id == member_gaia_id) {
      return member.role == data_sharing::MemberRole::kOwner;
    }
  }

  return false;
}

bool IsMemberCurrentUser(const signin::IdentityManager* identity_manager,
                         const GaiaId& gaia_id) {
  CoreAccountInfo account =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
  if (account.IsEmpty()) {
    return false;
  }

  return account.gaia == gaia_id;
}

bool IsMemberSelfOrOwner(const signin::IdentityManager* identity_manager,
                         const data_sharing::GroupData& group_data,
                         const GaiaId& member_gaia_id) {
  return IsMemberCurrentUser(identity_manager, member_gaia_id) ||
         IsMemberOwner(group_data, member_gaia_id);
}

bool IsCurrentUserOwner(const signin::IdentityManager* identity_manager,
                        const data_sharing::GroupData& group_data) {
  CoreAccountInfo account =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
  if (account.IsEmpty()) {
    return false;
  }

  return IsMemberOwner(group_data, account.gaia);
}

bool HasSeenTabUpdate(const tab_groups::SavedTabGroupTab& tab) {
  return tab.last_seen_time() >= tab.navigation_time();
}

}  // namespace

MessagingBackendServiceImpl::MessagingBackendServiceImpl(
    const MessagingBackendConfiguration& configuration,
    std::unique_ptr<TabGroupChangeNotifier> tab_group_change_notifier,
    std::unique_ptr<DataSharingChangeNotifier> data_sharing_change_notifier,
    std::unique_ptr<MessagingBackendStore> messaging_backend_store,
    std::unique_ptr<InstantMessageProcessor> instant_message_processor,
    tab_groups::TabGroupSyncService* tab_group_sync_service,
    data_sharing::DataSharingService* data_sharing_service,
    signin::IdentityManager* identity_manager)
    : configuration_(configuration),
      tab_group_change_notifier_(std::move(tab_group_change_notifier)),
      data_sharing_change_notifier_(std::move(data_sharing_change_notifier)),
      store_(std::move(messaging_backend_store)),
      instant_message_processor_(std::move(instant_message_processor)),
      tab_group_sync_service_(tab_group_sync_service),
      data_sharing_service_(data_sharing_service),
      identity_manager_(identity_manager) {
  instant_message_processor_->SetMessagingBackendService(this);
  store_->Initialize(
      base::BindOnce(&MessagingBackendServiceImpl::OnStoreInitialized,
                     weak_ptr_factory_.GetWeakPtr()));
}

MessagingBackendServiceImpl::~MessagingBackendServiceImpl() = default;

void MessagingBackendServiceImpl::SetInstantMessageDelegate(
    InstantMessageDelegate* instant_message_delegate) {
  instant_message_processor_->SetInstantMessageDelegate(
      instant_message_delegate);
}

void MessagingBackendServiceImpl::AddPersistentMessageObserver(
    PersistentMessageObserver* observer) {
  persistent_message_observers_.AddObserver(observer);
  if (IsInitialized()) {
    // We invoke the observer here in a re-entrant manner (documented in the
    // public API), because at any time after adding the observer, new messages
    // can come in, and they could arrive before the posted task with the
    // callback executes, which means they could get calls to Display/Hide of
    // PersistentMessages before they get the call to
    // OnMessagingBackendServiceInitialized().
    observer->OnMessagingBackendServiceInitialized();
  }
}

void MessagingBackendServiceImpl::RemovePersistentMessageObserver(
    PersistentMessageObserver* observer) {
  persistent_message_observers_.RemoveObserver(observer);
}

bool MessagingBackendServiceImpl::IsInitialized() {
  return initialized_;
}

std::vector<PersistentMessage> MessagingBackendServiceImpl::GetMessagesForTab(
    tab_groups::EitherTabID tab_id,
    std::optional<PersistentNotificationType> type) {
  std::optional<tab_groups::SavedTabGroupTab> tab = GetTabFromTabId(tab_id);
  if (!tab) {
    // Unable to find tab.
    return {};
  }

  std::optional<tab_groups::SavedTabGroup> tab_group =
      tab_group_sync_service_->GetGroup(tab->saved_group_guid());
  if (!tab_group) {
    // Unable to find group.
    return {};
  }

  std::optional<data_sharing::GroupId> collaboration_group_id =
      GroupIdForTabGroup(*tab_group);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID.
    return {};
  }

  DirtyType dirty_type =
      GetDirtyTypeFromPersistentNotificationTypeForQuery(type);

  std::optional<collaboration_pb::Message> message =
      store_->GetDirtyMessageForTab(*collaboration_group_id,
                                    tab->saved_tab_guid(), dirty_type);
  if (!message) {
    return {};
  }
  return ConvertMessageToPersistentMessages(
      *message, dirty_type, type, /*allow_dirty_tab_group_message=*/false);
}

std::vector<PersistentMessage> MessagingBackendServiceImpl::GetMessagesForGroup(
    tab_groups::EitherGroupID group_id,
    std::optional<PersistentNotificationType> type) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GetCollaborationGroupId(group_id);
  if (!collaboration_group_id) {
    // Unable to find collaboration.
    return {};
  }

  DirtyType dirty_type =
      GetDirtyTypeFromPersistentNotificationTypeForQuery(type);

  std::vector<collaboration_pb::Message> messages =
      store_->GetDirtyMessagesForGroup(*collaboration_group_id, dirty_type);
  return RemoveDuplicateDirtyTabGroupMessages(
      ConvertMessagesToPersistentMessages(messages, dirty_type, type));
}

std::vector<PersistentMessage> MessagingBackendServiceImpl::GetMessages(
    std::optional<PersistentNotificationType> type) {
  DirtyType dirty_type =
      GetDirtyTypeFromPersistentNotificationTypeForQuery(type);

  std::vector<collaboration_pb::Message> messages =
      store_->GetDirtyMessages(dirty_type);
  return RemoveDuplicateDirtyTabGroupMessages(
      ConvertMessagesToPersistentMessages(messages, dirty_type, type));
}

std::vector<ActivityLogItem> MessagingBackendServiceImpl::GetActivityLog(
    const ActivityLogQueryParams& params) {
  if (activity_log_for_testing_.contains(params.collaboration_id)) {
    CHECK_IS_TEST();
    return activity_log_for_testing_.at(params.collaboration_id);
  }

  const bool show_activity_for_single_tab = params.local_tab_id.has_value();
  std::vector<ActivityLogItem> result;
  std::vector<collaboration_pb::Message> messages =
      store_->GetRecentMessagesForGroup(params.collaboration_id);
  int message_count = 0;
  for (const auto& message : messages) {
    std::optional<ActivityLogItem> activity_log_item =
        ConvertMessageToActivityLogItem(message, show_activity_for_single_tab);
    if (!activity_log_item) {
      continue;
    }
    // If local_tab_id was supplied, filter for activity on this tab.
    if (show_activity_for_single_tab) {
      if (!activity_log_item->activity_metadata.tab_metadata.has_value()) {
        continue;
      }
      if (params.local_tab_id !=
          activity_log_item->activity_metadata.tab_metadata->local_tab_id) {
        continue;
      }
    }
    result.emplace_back(*activity_log_item);
    if (params.result_length == 0) {
      continue;
    }
    if (++message_count >= params.result_length) {
      break;
    }
  }
  return result;
}

void MessagingBackendServiceImpl::ClearDirtyTabMessagesForGroup(
    const data_sharing::GroupId& collaboration_group_id,
    const std::optional<tab_groups::SavedTabGroup>& tab_group) {
  // Clear the dirty bits from the storage.
  auto cleared_messages =
      store_->ClearDirtyTabMessagesForGroup(collaboration_group_id);
  if (!tab_group) {
    return;
  }

  std::vector<base::Uuid> cleared_tab_ids;

  // Since the dirty bits are cleared from DB, hide any dirty dots from the tabs
  // and tab groups if they are already showing.
  for (auto& message : cleared_messages) {
    PersistentMessage persistent_message;
    persistent_message.collaboration_event =
        ToCollaborationEvent(message.event_type());
    persistent_message.attribution =
        CreateMessageAttributionForTabUpdates(message, tab_group, std::nullopt);
    NotifyHidePersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::CHIP,
                             PersistentNotificationType::DIRTY_TAB});
    if (persistent_message.attribution.tab_metadata.has_value() &&
        persistent_message.attribution.tab_metadata->sync_tab_id.has_value()) {
      cleared_tab_ids.emplace_back(
          persistent_message.attribution.tab_metadata->sync_tab_id.value());
    }

    if (persistent_message.attribution.tab_group_metadata &&
        persistent_message.attribution.tab_group_metadata->sync_tab_group_id) {
      base::Uuid tab_group_id =
          persistent_message.attribution.tab_group_metadata->sync_tab_group_id
              .value();
      DisplayOrHideTabGroupDirtyDotForTabGroup(collaboration_group_id,
                                               tab_group_id);
    }
  }

  for (const base::Uuid& tab_id : cleared_tab_ids) {
    tab_group_sync_service_->UpdateTabLastSeenTime(
        tab_group->saved_guid(), tab_id, tab_groups::TriggerSource::LOCAL);
  }
}

void MessagingBackendServiceImpl::ClearDirtyTabMessagesForGroup(
    const data_sharing::GroupId& collaboration_group_id) {
  ClearDirtyTabMessagesForGroup(
      collaboration_group_id,
      GetTabGroupFromCollaborationId(collaboration_group_id.value()));
}

void MessagingBackendServiceImpl::OnStoreInitialized(bool success) {
  if (!success) {
    DVLOG(2) << "Failed to initialize MessagingBackendServiceImpl.";
    return;
  }
  data_sharing_change_notifier_observer_.Observe(
      data_sharing_change_notifier_.get());
  data_sharing_flush_callback_ = data_sharing_change_notifier_->Initialize();
}

void MessagingBackendServiceImpl::OnDataSharingChangeNotifierInitialized() {
  tab_group_change_notifier_observer_.Observe(tab_group_change_notifier_.get());
  tab_group_change_notifier_->Initialize();
}

void MessagingBackendServiceImpl::OnTabGroupChangeNotifierInitialized() {
  initialized_ = true;
  for (auto& observer : persistent_message_observers_) {
    observer.OnMessagingBackendServiceInitialized();
  }
  CHECK(data_sharing_flush_callback_);
  std::move(data_sharing_flush_callback_).Run();
}

void MessagingBackendServiceImpl::OnSyncDisabled() {
  store_->RemoveAllMessages();
}

void MessagingBackendServiceImpl::OnTabGroupAdded(
    const tab_groups::SavedTabGroup& added_group,
    tab_groups::TriggerSource source) {
  if (source == tab_groups::TriggerSource::LOCAL) {
    // No message to show or clear if a tab group was added locally.
    return;
  }

  std::optional<data_sharing::GroupId> collaboration_group_id =
      GroupIdForTabGroup(added_group);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab group.
    return;
  }

  collaboration_pb::Message message = CreateTabGroupMessage(
      *collaboration_group_id, added_group, collaboration_pb::TAB_GROUP_ADDED,
      DirtyType::kNone);
  store_->AddMessage(message);
}

void MessagingBackendServiceImpl::OnTabGroupRemoved(
    tab_groups::SavedTabGroup removed_group,
    tab_groups::TriggerSource source) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GroupIdForTabGroup(removed_group);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab group.
    return;
  }

  // Clear any the dirty persistent messages related to the group that are
  // already showing in UI. This is important in unshare flow since the tab
  // group continues to exist in the UI. This will also clear the dirty bits in
  // the DB and notify all the observers to update the UI.
  ClearDirtyTabMessagesForGroup(*collaboration_group_id, removed_group);

  // Remove all messages from the DB related to this tab group (including the
  // ones that were just cleared from dirty state). The only message that will
  // stay will be the group removal message which will be added in the next
  // section.
  std::vector<collaboration_pb::Message> messages =
      store_->GetRecentMessagesForGroup(*collaboration_group_id);
  std::set<std::string> message_uuid_strings;
  std::set<base::Uuid> message_uuids;
  for (auto& message : messages) {
    message_uuid_strings.insert(message.uuid());
    message_uuids.insert(base::Uuid::ParseLowercase(message.uuid()));
  }
  store_->RemoveMessages(message_uuid_strings);

  // Regardless of whether the user is leaving or deleting the group and
  // regardless of whether it happened from a remote event or a local event,
  // we should hide any instant messages related to the group.
  instant_message_processor_->HideInstantMessage(message_uuids);

  if (source == tab_groups::TriggerSource::LOCAL) {
    return;
  }

  // If the user themselves are trying to leave or delete the group, they don't
  // need to be notified of anything. Note that although real event source is
  // local, it appears to be a remote event since the leave / delete attempt and
  // tab group removal is processed only after a commit happens to the server
  // side.
  if (data_sharing_service_->IsLeavingOrDeletingGroup(
          *collaboration_group_id)) {
    return;
  }

  // If the current user is the owner of the group, they might be unsharing the
  // group. Ignore the message.
  std::optional<data_sharing::GroupData> group_data =
      data_sharing_service_->ReadGroup(*collaboration_group_id);
  if (!group_data) {
    group_data =
        data_sharing_service_->GetPossiblyRemovedGroup(*collaboration_group_id);
  }

  if (group_data.has_value() &&
      IsCurrentUserOwner(identity_manager_, *group_data)) {
    return;
  }

  collaboration_pb::Message message =
      CreateTabGroupMessage(*collaboration_group_id, removed_group,
                            collaboration_pb::TAB_GROUP_REMOVED,
                            DirtyType::kTombstonedAndInstantMessage);
  store_->AddMessage(message);

  PersistentMessage persistent_message =
      CreatePersistentMessage(message, removed_group, std::nullopt,
                              PersistentNotificationType::TOMBSTONED);
  persistent_message.collaboration_event =
      CollaborationEvent::TAB_GROUP_REMOVED;
  NotifyDisplayPersistentMessagesForTypes(
      persistent_message, {PersistentNotificationType::TOMBSTONED});

  if (instant_message_processor_->IsEnabled()) {
    InstantMessage instant_message =
        CreateInstantMessage(message, removed_group, /*tab=*/std::nullopt);
    instant_message.type = InstantNotificationType::UNDEFINED;
    instant_message.localized_message =
        GetTitleForTabGroupRemovedMessage(instant_message);
    DisplayInstantMessage(base::Uuid::ParseLowercase(message.uuid()),
                          instant_message, {InstantNotificationLevel::BROWSER});
  }
}

void MessagingBackendServiceImpl::OnTabGroupNameUpdated(
    const tab_groups::SavedTabGroup& updated_group,
    tab_groups::TriggerSource source) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GroupIdForTabGroup(updated_group);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab group.
    return;
  }

  if (source == tab_groups::TriggerSource::LOCAL) {
    return;
  }

  collaboration_pb::Message message = CreateTabGroupMessage(
      *collaboration_group_id, updated_group,
      collaboration_pb::TAB_GROUP_NAME_UPDATED, DirtyType::kNone);
  store_->AddMessage(message);
}

void MessagingBackendServiceImpl::OnTabGroupColorUpdated(
    const tab_groups::SavedTabGroup& updated_group,
    tab_groups::TriggerSource source) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GroupIdForTabGroup(updated_group);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab group.
    return;
  }
  if (source == tab_groups::TriggerSource::LOCAL) {
    return;
  }

  collaboration_pb::Message message = CreateTabGroupMessage(
      *collaboration_group_id, updated_group,
      collaboration_pb::TAB_GROUP_COLOR_UPDATED, DirtyType::kNone);
  store_->AddMessage(message);
}

void MessagingBackendServiceImpl::OnTabAdded(
    const tab_groups::SavedTabGroupTab& added_tab,
    tab_groups::TriggerSource source) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GetCollaborationGroupIdForTab(added_tab);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab.
    return;
  }

  bool is_local = source == tab_groups::TriggerSource::LOCAL;
  bool triggering_user_is_self = IsMemberCurrentUser(
      identity_manager_, added_tab.shared_attribution().created_by);
  DirtyType dirty_type = (is_local || triggering_user_is_self)
                             ? DirtyType::kNone
                             : DirtyType::kDotAndChip;

  if (HasSeenTabUpdate(added_tab)) {
    dirty_type = DirtyType::kNone;
  }

  collaboration_pb::Message message =
      CreateTabMessage(*collaboration_group_id, added_tab,
                       collaboration_pb::TAB_ADDED, dirty_type);
  store_->AddMessage(message);

  if (dirty_type != DirtyType::kNone) {
    PersistentMessage persistent_message =
        CreatePersistentMessage(message, std::nullopt, added_tab, std::nullopt);

    NotifyDisplayPersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::CHIP,
                             PersistentNotificationType::DIRTY_TAB});
  }

  DisplayOrHideTabGroupDirtyDotForTabGroup(*collaboration_group_id,
                                           added_tab.saved_group_guid());
}

void MessagingBackendServiceImpl::OnTabRemoved(
    tab_groups::SavedTabGroupTab removed_tab,
    tab_groups::TriggerSource source,
    bool is_selected) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GetCollaborationGroupIdForTab(removed_tab);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab.
    return;
  }

  bool is_local = source == tab_groups::TriggerSource::LOCAL;
  bool triggering_user_is_self = IsMemberCurrentUser(
      identity_manager_, removed_tab.shared_attribution().updated_by);
  DirtyType dirty_type = (is_local || triggering_user_is_self)
                             ? DirtyType::kNone
                             : DirtyType::kTombstoned;
  collaboration_pb::Message message =
      CreateTabMessage(*collaboration_group_id, removed_tab,
                       collaboration_pb::TAB_REMOVED, dirty_type);
  store_->AddMessage(message);

  // Tab is no longer available, so should not contribute to any dirty tab
  // groups.
  store_->ClearDirtyMessageForTab(*collaboration_group_id,
                                  removed_tab.saved_tab_guid(),
                                  DirtyType::kDotAndChip);

  // Hide any existing persistent dot or chip messages already showing.
  PersistentMessage persistent_message =
      CreatePersistentMessage(message, std::nullopt, removed_tab, std::nullopt);

  NotifyHidePersistentMessagesForTypes(persistent_message,
                                       {PersistentNotificationType::CHIP,
                                        PersistentNotificationType::DIRTY_TAB});

  // Hide any dirty dot on the tab group.
  DisplayOrHideTabGroupDirtyDotForTabGroup(*collaboration_group_id,
                                           removed_tab.saved_group_guid());

  if (dirty_type == DirtyType::kNone) {
    return;
  }

  if (is_selected && instant_message_processor_->IsEnabled()) {
    InstantMessage instant_message =
        CreateInstantMessage(message, /*tab_group=*/std::nullopt, removed_tab);
    instant_message.type = InstantNotificationType::CONFLICT_TAB_REMOVED;
    instant_message.localized_message =
        GetTitleForTabRemovedMessage(instant_message);

    // TODO(crbug.com/390794240): Remove the id argument to
    // DisplayInstantMessage as it's now contained inside the
    // MessageAttribution.
    DisplayInstantMessage(base::Uuid::ParseLowercase(message.uuid()),
                          instant_message, {InstantNotificationLevel::BROWSER});
  }
}

void MessagingBackendServiceImpl::OnTabUpdated(
    const tab_groups::SavedTabGroupTab& before,
    const tab_groups::SavedTabGroupTab& after,
    tab_groups::TriggerSource source,
    bool is_selected) {
  const tab_groups::SavedTabGroupTab& updated_tab = after;
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GetCollaborationGroupIdForTab(updated_tab);
  if (!collaboration_group_id) {
    // Unable to find collaboration ID from tab.
    return;
  }

  bool is_local = source == tab_groups::TriggerSource::LOCAL;
  bool triggering_user_is_self = IsMemberCurrentUser(
      identity_manager_, updated_tab.shared_attribution().updated_by);

  DirtyType dirty_type =
      (is_local || triggering_user_is_self)
          ? DirtyType::kNone
          : (is_selected ? DirtyType::kChip : DirtyType::kDotAndChip);
  if (HasSeenTabUpdate(updated_tab)) {
    dirty_type = DirtyType::kNone;
  }

  collaboration_pb::Message message =
      CreateTabMessage(*collaboration_group_id, updated_tab,
                       collaboration_pb::TAB_UPDATED, dirty_type);
  store_->AddMessage(message);

  if (dirty_type == DirtyType::kNone) {
    // For local updates, hide any dirty messages for tab from storage and
    // dismiss any messages already being displayed for tab.
    store_->ClearDirtyMessageForTab(*collaboration_group_id,
                                    updated_tab.saved_tab_guid(),
                                    DirtyType::kDotAndChip);
  }

  PersistentMessage persistent_message =
      CreatePersistentMessage(message, std::nullopt, updated_tab, std::nullopt);

  if (dirty_type == DirtyType::kNone) {
    NotifyHidePersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::CHIP,
                             PersistentNotificationType::DIRTY_TAB});
  } else {
    std::vector<PersistentNotificationType> persistent_notification_types(
        {PersistentNotificationType::CHIP});
    if (!is_selected) {
      persistent_notification_types.emplace_back(
          PersistentNotificationType::DIRTY_TAB);
    }
    // For remote updates, show the message on UI.
    NotifyDisplayPersistentMessagesForTypes(persistent_message,
                                            persistent_notification_types);
  }

  DisplayOrHideTabGroupDirtyDotForTabGroup(*collaboration_group_id,
                                           updated_tab.saved_group_guid());

  if (dirty_type != DirtyType::kNone && is_selected &&
      instant_message_processor_->IsEnabled()) {
    InstantMessage instant_message_base;
    auto message_attribution = CreateMessageAttributionForTabUpdates(
        message, std::nullopt, updated_tab);
    message_attribution.tab_metadata->previous_url = before.url().spec();
    instant_message_base.attributions.emplace_back(message_attribution);

    instant_message_base.collaboration_event = CollaborationEvent::TAB_UPDATED;
    // TODO(crbug.com/391941212): CONFLICT_TAB_REMOVED and UNDEFINED don't seem
    // to be used. In that case, remove them.
    instant_message_base.type = InstantNotificationType::UNDEFINED;
    instant_message_base.localized_message =
        GetTitleForTabUpdatedMessage(instant_message_base);

    DisplayInstantMessage(base::Uuid::ParseLowercase(message.uuid()),
                          instant_message_base,
                          {InstantNotificationLevel::BROWSER});
  }
}

void MessagingBackendServiceImpl::OnTabSelectionChanged(
    const tab_groups::LocalTabID& tab_id,
    bool is_selected) {
  std::optional<tab_groups::SavedTabGroupTab> tab;
  std::optional<data_sharing::GroupId> collaboration_group_id;
  for (const auto& group : tab_group_sync_service_->GetAllGroups()) {
    if (group.is_shared_tab_group() && group.GetTab(tab_id)) {
      tab = *(group.GetTab(tab_id));
      collaboration_group_id =
          data_sharing::GroupId(group.collaboration_id().value().value());
      break;
    }
  }

  if (!tab) {
    return;
  }

  if (!is_selected && !configuration_.clear_chip_on_tab_selection) {
    CHECK(collaboration_group_id);
    // When we do not clear the chip on selection, we instead clear the chip
    // when a user selects a different tab after having selected one.
    store_->ClearDirtyMessageForTab(*collaboration_group_id,
                                    tab->saved_tab_guid(), DirtyType::kChip);

    // Specialized handling of creating a PersistentMessage, since we do not
    // have a stored collaboration_pb::Message available.
    PersistentMessage persistent_message =
        CreatePersistentMessageFromTabGroupAndTab(
            *collaboration_group_id, *tab, CollaborationEvent::UNDEFINED);

    NotifyHidePersistentMessagesForTypes(persistent_message,
                                         {PersistentNotificationType::CHIP});
  }

  if (!is_selected) {
    return;
  }

  if (configuration_.clear_chip_on_tab_selection) {
    store_->ClearDirtyMessageForTab(
        *collaboration_group_id, tab->saved_tab_guid(), DirtyType::kDotAndChip);
  } else {
    store_->ClearDirtyMessageForTab(*collaboration_group_id,
                                    tab->saved_tab_guid(), DirtyType::kDot);
  }

  PersistentMessage persistent_message =
      CreatePersistentMessageFromTabGroupAndTab(*collaboration_group_id, *tab,
                                                CollaborationEvent::UNDEFINED);

  if (configuration_.clear_chip_on_tab_selection) {
    NotifyHidePersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::CHIP,
                             PersistentNotificationType::DIRTY_TAB});
  } else {
    NotifyHidePersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::DIRTY_TAB});
  }

  DisplayOrHideTabGroupDirtyDotForTabGroup(*collaboration_group_id,
                                           tab->saved_group_guid());
}

void MessagingBackendServiceImpl::OnTabLastSeenTimeChanged(
    const base::Uuid& tab_id,
    tab_groups::TriggerSource source) {
  // Only remote changes need to update the notification states.
  if (source != tab_groups::TriggerSource::REMOTE) {
    return;
  }

  std::optional<tab_groups::SavedTabGroupTab> tab;
  std::optional<data_sharing::GroupId> collaboration_group_id;
  for (const auto& group : tab_group_sync_service_->GetAllGroups()) {
    if (group.is_shared_tab_group() && group.GetTab(tab_id)) {
      tab = *(group.GetTab(tab_id));
      collaboration_group_id =
          data_sharing::GroupId(group.collaboration_id().value().value());
      break;
    }
  }

  if (!tab) {
    return;
  }

  if (!HasSeenTabUpdate(*tab)) {
    return;
  }

  store_->ClearDirtyMessageForTab(
      *collaboration_group_id, tab->saved_tab_guid(), DirtyType::kDotAndChip);

  // Hide any existing persistent dot or chip messages already showing.
  PersistentMessage persistent_message =
      CreatePersistentMessageFromTabGroupAndTab(*collaboration_group_id, *tab,
                                                CollaborationEvent::UNDEFINED);
  NotifyHidePersistentMessagesForTypes(persistent_message,
                                       {PersistentNotificationType::CHIP,
                                        PersistentNotificationType::DIRTY_TAB});

  DisplayOrHideTabGroupDirtyDotForTabGroup(*collaboration_group_id,
                                           tab->saved_group_guid());
}

void MessagingBackendServiceImpl::OnTabGroupOpened(
    const tab_groups::SavedTabGroup& tab_group) {
  std::optional<data_sharing::GroupId> collaboration_group_id =
      GroupIdForTabGroup(tab_group);
  if (!collaboration_group_id) {
    return;
  }

  // Redeliver instant messages for the open group.
  for (auto& message : store_->GetDirtyMessagesForGroup(
           *collaboration_group_id, DirtyType::kMessageOnly)) {
    InstantMessage instant_message =
        CreateInstantMessage(message, tab_group, /*tab=*/std::nullopt);
    DisplayInstantMessage(base::Uuid::ParseLowercase(message.uuid()),
                          instant_message, {InstantNotificationLevel::BROWSER});
  }

  // Show all the persistent messages in the group.
  std::vector<PersistentMessage> messages = GetMessagesForGroup(
      tab_group.saved_guid(), PersistentNotificationType::DIRTY_TAB);
  for (auto& message : messages) {
    NotifyDisplayPersistentMessagesForTypes(
        message, {PersistentNotificationType::CHIP,
                  PersistentNotificationType::DIRTY_TAB});
  }

  DisplayOrHideTabGroupDirtyDotForTabGroup(*collaboration_group_id,
                                           tab_group.saved_guid());
}

void MessagingBackendServiceImpl::OnTabGroupClosed(
    const tab_groups::SavedTabGroup& tab_group) {
  // TODO(crbug.com/389948628): Handle hide persistence messages if needed.
}

void MessagingBackendServiceImpl::OnGroupMemberAdded(
    const data_sharing::GroupData& group_data,
    const GaiaId& member_gaia_id,
    const base::Time& event_time) {
  if (IsMemberSelfOrOwner(identity_manager_, group_data, member_gaia_id)) {
    return;
  }

  std::optional<tab_groups::SavedTabGroup> tab_group =
      GetTabGroupFromCollaborationId(group_data.group_token.group_id.value());
  if (!tab_group) {
    // The tab group may be deleted or not synced.
    // TODO(386420717): Maybe persist the message to disk in case the tab group
    // gets synced at a later time. If this is persisted, then it may never get
    // cleared if the group was deleted.
    return;
  }

  collaboration_pb::Message message =
      CreateMessage(group_data.group_token.group_id,
                    collaboration_pb::COLLABORATION_MEMBER_ADDED,
                    DirtyType::kMessageOnly, event_time);
  message.set_affected_user_gaia_id(member_gaia_id.ToString());
  store_->AddMessage(message);

  if (instant_message_processor_->IsEnabled()) {
    InstantMessage instant_message =
        CreateInstantMessage(message, tab_group, /*tab=*/std::nullopt);
    instant_message.localized_message =
        GetTitleForMemberAddedMessage(instant_message);
    DisplayInstantMessage(
        base::Uuid::ParseLowercase(message.uuid()), instant_message,
        {InstantNotificationLevel::SYSTEM, InstantNotificationLevel::BROWSER});
  }
}

void MessagingBackendServiceImpl::OnGroupMemberRemoved(
    const data_sharing::GroupData& group_data,
    const GaiaId& member_gaia_id,
    const base::Time& event_time) {
  if (IsMemberSelfOrOwner(identity_manager_, group_data, member_gaia_id)) {
    return;
  }

  collaboration_pb::Message message =
      CreateMessage(group_data.group_token.group_id,
                    collaboration_pb::COLLABORATION_MEMBER_REMOVED,
                    DirtyType::kNone, event_time);
  message.set_affected_user_gaia_id(member_gaia_id.ToString());
  store_->AddMessage(message);
}

// static
std::u16string MessagingBackendServiceImpl::GetTruncatedTabTitleForTesting(
    const std::u16string& original_title) {
  return TruncateTabTitle(original_title);
}

void MessagingBackendServiceImpl::ClearPersistentMessage(
    const base::Uuid& message_id,
    std::optional<PersistentNotificationType> type) {
  store_->ClearDirtyMessage(
      message_id, GetDirtyTypeFromPersistentNotificationTypeForQuery(type));
}

void MessagingBackendServiceImpl::RemoveMessages(
    const std::vector<base::Uuid>& message_ids) {
  std::set<std::string> message_uuids;
  for (const base::Uuid& message_id : message_ids) {
    message_uuids.insert(message_id.AsLowercaseString());
  }
  store_->RemoveMessages(message_uuids);
}

void MessagingBackendServiceImpl::AddActivityLogForTesting(
    data_sharing::GroupId collaboration_id,
    const std::vector<ActivityLogItem>& activity_log) {
  CHECK_IS_TEST();
  activity_log_for_testing_.emplace(collaboration_id, activity_log);
}

std::optional<std::string>
MessagingBackendServiceImpl::GetDisplayNameForUserInGroup(
    const data_sharing::GroupId& group_id,
    const GaiaId& gaia_id) {
  std::optional<data_sharing::GroupMemberPartialData> group_member_data =
      data_sharing_service_->GetPossiblyRemovedGroupMember(group_id, gaia_id);
  // Try given name from live data first.
  if (group_member_data && !group_member_data->given_name.empty()) {
    return group_member_data->given_name;
  }

  // Then try display name from live data.
  if (group_member_data && !group_member_data->display_name.empty()) {
    return group_member_data->display_name;
  }

  return std::nullopt;
}

int GetTitleStringRes(CollaborationEvent collaboration_event,
                      bool is_tab_activity) {
  switch (collaboration_event) {
    case CollaborationEvent::TAB_ADDED:
      return is_tab_activity
                 ? IDS_DATA_SHARING_RECENT_ACTIVITY_MEMBER_ADDED_THIS_TAB
                 : IDS_DATA_SHARING_RECENT_ACTIVITY_TAB_ADDED;
    case CollaborationEvent::TAB_REMOVED:
      return IDS_DATA_SHARING_RECENT_ACTIVITY_TAB_REMOVED;
    case CollaborationEvent::TAB_UPDATED:
      return is_tab_activity
                 ? IDS_DATA_SHARING_RECENT_ACTIVITY_MEMBER_CHANGED_THIS_TAB
                 : IDS_DATA_SHARING_RECENT_ACTIVITY_TAB_UPDATED;
    case CollaborationEvent::TAB_GROUP_NAME_UPDATED:
      return IDS_DATA_SHARING_RECENT_ACTIVITY_TAB_GROUP_NAME_UPDATED;
    case CollaborationEvent::TAB_GROUP_COLOR_UPDATED:
      return IDS_DATA_SHARING_RECENT_ACTIVITY_TAB_GROUP_COLOR_UPDATED;
    case CollaborationEvent::COLLABORATION_MEMBER_ADDED:
      return IDS_DATA_SHARING_RECENT_ACTIVITY_USER_JOINED_GROUP;
    case CollaborationEvent::COLLABORATION_MEMBER_REMOVED:
      return IDS_DATA_SHARING_RECENT_ACTIVITY_USER_LEFT_GROUP;
    case CollaborationEvent::TAB_GROUP_ADDED:
    case CollaborationEvent::TAB_GROUP_REMOVED:
    case CollaborationEvent::COLLABORATION_ADDED:
    case CollaborationEvent::COLLABORATION_REMOVED:
    case CollaborationEvent::UNDEFINED:
      CHECK(false) << "No string res for collaboration event "
                   << static_cast<size_t>(collaboration_event);
  }
  return 0;
}

std::optional<ActivityLogItem>
MessagingBackendServiceImpl::ConvertMessageToActivityLogItem(
    const collaboration_pb::Message& message,
    bool is_tab_activity) {
  switch (message.event_type()) {
    case collaboration_pb::TAB_GROUP_ADDED:
    case collaboration_pb::TAB_GROUP_REMOVED:
    case collaboration_pb::COLLABORATION_ADDED:
    case collaboration_pb::COLLABORATION_REMOVED:
      return std::nullopt;
    default:
      break;
  }
  ActivityLogItem item;
  item.collaboration_event = ToCollaborationEvent(message.event_type());
  data_sharing::GroupId collaboration_group_id(message.collaboration_id());

  std::optional<GaiaId> gaia_id = GetGaiaIdFromMessage(message);
  std::optional<data_sharing::GroupMember> group_member =
      GetGroupMemberFromGaiaId(collaboration_group_id, gaia_id);

  std::optional<std::string> user_name_for_display;
  if (gaia_id) {
    user_name_for_display =
        GetDisplayNameForUserInGroup(collaboration_group_id, *gaia_id);
  }

  bool is_self =
      gaia_id.has_value() && IsMemberCurrentUser(identity_manager_, *gaia_id);
  std::u16string user_to_show =
      is_self ? l10n_util::GetStringUTF16(
                    IDS_DATA_SHARING_RECENT_ACTIVITY_USER_SELF)
              : (user_name_for_display
                     ? base::UTF8ToUTF16(*user_name_for_display)
                     : l10n_util::GetStringUTF16(
                           IDS_DATA_SHARING_RECENT_ACTIVITY_UNKNOWN_USER));

  item.title_text = l10n_util::GetStringFUTF16(
      GetTitleStringRes(item.collaboration_event, is_tab_activity),
      user_to_show);

  // By default, we use an empty description. This is special cased below.
  item.description_text = u"";
  base::TimeDelta time_delta =
      base::Time::Now() - base::Time::FromTimeT(message.event_timestamp());
  item.time_delta_text = GetElapsedTimeText(time_delta);
  item.action =
      GetRecentActivityActionFromCollaborationEvent(item.collaboration_event);

  item.activity_metadata = MessageAttribution();
  item.activity_metadata.id = base::Uuid::ParseLowercase(message.uuid());
  item.activity_metadata.collaboration_id = collaboration_group_id;

  std::optional<tab_groups::SavedTabGroup> tab_group =
      GetTabGroupFromCollaborationId(message.collaboration_id());
  item.activity_metadata.tab_group_metadata =
      CreateTabGroupMessageMetadataFromMessageOrTabGroup(message, tab_group);

  // The code below needs to fill in `activity_metadata`, and optionally
  // `show_favicon` if it is true.
  switch (GetMessageCategory(message)) {
    case MessageCategory::kTab: {
      item.show_favicon = true;
      item.activity_metadata.tab_metadata =
          CreateTabMessageMetadataFromMessageOrTab(
              message, GetTabFromGroup(message, tab_group));
      // We are guaranteed to have a value for `last_known_url`.
      GURL url = GURL(*item.activity_metadata.tab_metadata->last_known_url);
      item.activity_metadata.triggering_user = group_member;
      item.activity_metadata.triggering_user_is_self = is_self;

      item.description_text =
          url_formatter::FormatUrlForDisplayOmitSchemePathAndTrivialSubdomains(
              url);

      break;
    }
    case MessageCategory::kTabGroup: {
      item.activity_metadata.triggering_user = group_member;
      item.activity_metadata.triggering_user_is_self = is_self;

      // Only tab group name changes have specialized description.
      if (message.event_type() == collaboration_pb::TAB_GROUP_NAME_UPDATED) {
        if (item.activity_metadata.tab_group_metadata->last_known_title) {
          item.description_text = base::UTF8ToUTF16(
              *item.activity_metadata.tab_group_metadata->last_known_title);
        }
      }

      break;
    }
    case MessageCategory::kCollaboration:
      item.activity_metadata.affected_user = group_member;
      if (group_member) {
        item.description_text = base::UTF8ToUTF16(group_member->email);
      }
      item.activity_metadata.affected_user_is_self = is_self;
      break;
    default:
      break;
  }
  return item;
}

std::optional<data_sharing::GroupId>
MessagingBackendServiceImpl::GetCollaborationGroupIdForTab(
    const tab_groups::SavedTabGroupTab& tab) {
  // Find tab group using the tab group ID and look up collaboration group ID.
  std::optional<tab_groups::SavedTabGroup> tab_group =
      tab_group_sync_service_->GetGroup(tab.saved_group_guid());
  if (!tab_group) {
    return std::nullopt;
  }
  return GroupIdForTabGroup(*tab_group);
}

TabGroupMessageMetadata
MessagingBackendServiceImpl::CreateTabGroupMessageMetadataFromCollaborationId(
    const collaboration_pb::Message& message,
    std::optional<tab_groups::SavedTabGroup> tab_group,
    std::optional<data_sharing::GroupId> collaboration_group_id) {
  if (tab_group) {
    return CreateTabGroupMessageMetadata(*tab_group);
  }

  TabGroupMessageMetadata tab_group_metadata = TabGroupMessageMetadata();
  if (!collaboration_group_id) {
    return tab_group_metadata;
  }
  std::optional<std::u16string> previous_title =
      tab_group_sync_service_->GetTitleForPreviouslyExistingSharedTabGroup(
          ToCollaborationId(data_sharing::GroupId(*collaboration_group_id)));
  if (previous_title) {
    tab_group_metadata.last_known_title = base::UTF16ToUTF8(*previous_title);
  } else {
    tab_group_metadata.last_known_title = message.tab_group_data().title();
  }
  // TODO(crbug.com/395918345): Should we always use title from DB and ignore
  // the tab group?
  return tab_group_metadata;
}

TabGroupMessageMetadata
MessagingBackendServiceImpl::CreateTabGroupMessageMetadataFromMessageOrTabGroup(
    const collaboration_pb::Message& message,
    const std::optional<tab_groups::SavedTabGroup>& tab_group) {
  if (tab_group) {
    return CreateTabGroupMessageMetadata(*tab_group);
  }

  return CreateTabGroupMessageMetadataFromCollaborationId(
      message, GetTabGroupFromCollaborationId(message.collaboration_id()),
      data_sharing::GroupId(message.collaboration_id()));
}

std::optional<tab_groups::SavedTabGroup>
MessagingBackendServiceImpl::GetTabGroupFromCollaborationId(
    const std::string& collaboration_id) {
  if (collaboration_id.empty()) {
    return std::nullopt;
  }

  tab_groups::CollaborationId collaboration_group_id(collaboration_id);
  for (const auto& group : tab_group_sync_service_->GetAllGroups()) {
    if (group.collaboration_id().has_value() &&
        group.collaboration_id().value() == collaboration_group_id) {
      return group;
    }
  }

  return std::nullopt;
}

std::optional<data_sharing::GroupMember>
MessagingBackendServiceImpl::GetGroupMemberFromGaiaId(
    const data_sharing::GroupId& collaboration_group_id,
    std::optional<GaiaId> gaia_id) {
  if (!gaia_id) {
    return std::nullopt;
  }

  std::optional<data_sharing::GroupMemberPartialData> group_member_data =
      data_sharing_service_->GetPossiblyRemovedGroupMember(
          collaboration_group_id, *gaia_id);
  if (group_member_data) {
    return group_member_data->ToGroupMember();
  }
  return std::nullopt;
}

std::optional<data_sharing::GroupId>
MessagingBackendServiceImpl::GetCollaborationGroupId(
    tab_groups::EitherGroupID group_id) {
  std::optional<tab_groups::SavedTabGroup> tab_group =
      tab_group_sync_service_->GetGroup(group_id);
  if (!tab_group) {
    return std::nullopt;
  }
  return GroupIdForTabGroup(*tab_group);
}

std::optional<tab_groups::SavedTabGroupTab>
MessagingBackendServiceImpl::GetTabFromTabId(tab_groups::EitherTabID tab_id) {
  if (std::holds_alternative<base::Uuid>(tab_id)) {
    base::Uuid sync_tab_id = std::get<base::Uuid>(tab_id);
    for (const auto& group : tab_group_sync_service_->GetAllGroups()) {
      if (group.ContainsTab(sync_tab_id)) {
        return std::make_optional(*group.GetTab(sync_tab_id));
      }
    }
  }
  if (std::holds_alternative<tab_groups::LocalTabID>(tab_id)) {
    tab_groups::LocalTabID local_tab_id =
        std::get<tab_groups::LocalTabID>(tab_id);
    for (const auto& group : tab_group_sync_service_->GetAllGroups()) {
      if (group.ContainsTab(local_tab_id)) {
        return std::make_optional(*group.GetTab(local_tab_id));
      }
    }
  }
  return std::nullopt;
}

std::vector<PersistentMessage>
MessagingBackendServiceImpl::ConvertMessagesToPersistentMessages(
    const std::vector<collaboration_pb::Message>& messages,
    DirtyType lookup_dirty_type,
    const std::optional<PersistentNotificationType>& type) {
  std::vector<PersistentMessage> result;
  for (const auto& message : messages) {
    // Each DB message might result in multiple individual PersistentMessages.
    std::vector<PersistentMessage> converted_messages =
        ConvertMessageToPersistentMessages(
            message, lookup_dirty_type, type,
            /*allow_dirty_tab_group_message=*/true);
    result.insert(result.end(), converted_messages.begin(),
                  converted_messages.end());
  }
  return result;
}

std::vector<PersistentMessage>
MessagingBackendServiceImpl::ConvertMessageToPersistentMessages(
    const collaboration_pb::Message& message,
    DirtyType lookup_dirty_type,
    const std::optional<PersistentNotificationType>& type,
    bool allow_dirty_tab_group_message) {
  std::vector<PersistentMessage> persistent_messages;
  std::optional<tab_groups::SavedTabGroup> tab_group =
      GetTabGroupFromCollaborationId(message.collaboration_id());

  // Special case: First handle if it's of type TOMBSTONED.
  bool has_tombstoned =
      message.dirty() & static_cast<int>(DirtyType::kTombstoned);
  bool looking_for_tombstoned = lookup_dirty_type == DirtyType::kAll ||
                                lookup_dirty_type == DirtyType::kTombstoned;
  if (has_tombstoned && looking_for_tombstoned) {
    persistent_messages.push_back(
        CreatePersistentMessage(message, tab_group, std::nullopt,
                                PersistentNotificationType::TOMBSTONED));
    return persistent_messages;
  }

  // Rest of the persistent messages must be related to tabs.
  if (GetMessageCategory(message) != MessageCategory::kTab) {
    return persistent_messages;
  }

  // Helper local variables to increase readability of code below.
  bool has_dirty_chip = message.dirty() & static_cast<int>(DirtyType::kChip);
  bool looking_for_dirty_chip = lookup_dirty_type == DirtyType::kAll ||
                                lookup_dirty_type == DirtyType::kChip;
  bool has_dirty_dot = message.dirty() & static_cast<int>(DirtyType::kDot);
  bool looking_for_dirty_dot = lookup_dirty_type == DirtyType::kAll ||
                               lookup_dirty_type == DirtyType::kDot;
  bool add_dirty_tab_messages =
      !type || *type == PersistentNotificationType::DIRTY_TAB;
  bool add_dirty_tab_group_messages =
      allow_dirty_tab_group_message &&
      (!type || *type == PersistentNotificationType::DIRTY_TAB_GROUP);
  bool has_dirty_tab_messages_in_group =
      !store_
           ->GetDirtyMessagesForGroup(
               data_sharing::GroupId(message.collaboration_id()),
               DirtyType::kDot)
           .empty();

  if (has_dirty_chip && looking_for_dirty_chip) {
    persistent_messages.push_back(CreatePersistentMessage(
        message, tab_group, std::nullopt, PersistentNotificationType::CHIP));
  }

  if (has_dirty_dot && looking_for_dirty_dot) {
    if (add_dirty_tab_messages) {
      persistent_messages.push_back(
          CreatePersistentMessage(message, tab_group, std::nullopt,
                                  PersistentNotificationType::DIRTY_TAB));
    }

    if (add_dirty_tab_group_messages && has_dirty_tab_messages_in_group) {
      PersistentMessage persistent_message =
          CreatePersistentMessage(message, tab_group, std::nullopt,
                                  PersistentNotificationType::DIRTY_TAB_GROUP);
      // Override collaboration event and tab metadata since this is about
      // a group.
      persistent_message.collaboration_event = CollaborationEvent::UNDEFINED;
      persistent_message.attribution.tab_metadata = TabMessageMetadata();
      persistent_messages.push_back(persistent_message);
    }
  }
  return persistent_messages;
}

PersistentMessage MessagingBackendServiceImpl::CreatePersistentMessage(
    const collaboration_pb::Message& message,
    const std::optional<tab_groups::SavedTabGroup>& tab_group,
    const std::optional<tab_groups::SavedTabGroupTab>& tab,
    const std::optional<PersistentNotificationType>& type) {
  PersistentMessage persistent_message;
  persistent_message.collaboration_event =
      ToCollaborationEvent(message.event_type());
  persistent_message.attribution =
      CreateMessageAttributionForTabUpdates(message, tab_group, tab);
  if (type) {
    persistent_message.type = *type;
  }
  return persistent_message;
}

InstantMessage MessagingBackendServiceImpl::CreateInstantMessage(
    const collaboration_pb::Message& message,
    const std::optional<tab_groups::SavedTabGroup>& tab_group,
    const std::optional<tab_groups::SavedTabGroupTab>& tab) {
  InstantMessage instant_message;
  instant_message.collaboration_event =
      ToCollaborationEvent(message.event_type());
  instant_message.attributions.emplace_back(
      CreateMessageAttributionForTabUpdates(message, tab_group, tab));
  return instant_message;
}

void MessagingBackendServiceImpl::NotifyDisplayPersistentMessagesForTypes(
    const PersistentMessage& base_message,
    const std::vector<PersistentNotificationType>& types) {
  for (const PersistentMessage& message :
       CreatePersistentMessagesForTypes(base_message, types)) {
    persistent_message_observers_.Notify(
        &PersistentMessageObserver::DisplayPersistentMessage, message);
  }
}

void MessagingBackendServiceImpl::NotifyHidePersistentMessagesForTypes(
    const PersistentMessage& base_message,
    const std::vector<PersistentNotificationType>& types) {
  for (const PersistentMessage& message :
       CreatePersistentMessagesForTypes(base_message, types)) {
    persistent_message_observers_.Notify(
        &PersistentMessageObserver::HidePersistentMessage, message);
  }
}

void MessagingBackendServiceImpl::DisplayOrHideTabGroupDirtyDotForTabGroup(
    const data_sharing::GroupId& collaboration_group_id,
    base::Uuid shared_tab_group_id) {
  bool hasDirtyDotMessagesForGroup =
      !store_->GetDirtyMessagesForGroup(collaboration_group_id, DirtyType::kDot)
           .empty();

  std::optional<tab_groups::SavedTabGroup> tab_group =
      tab_group_sync_service_->GetGroup(shared_tab_group_id);
  PersistentMessage persistent_message;

  persistent_message.attribution = MessageAttribution();
  // We leave the attribution.id field empty for synthetic messages, as we don't
  // have a corresponding stored collaboration_pb::Message available.
  persistent_message.attribution.collaboration_id = collaboration_group_id;

  if (tab_group) {
    persistent_message.attribution.tab_group_metadata =
        CreateTabGroupMessageMetadata(*tab_group);
  } else {
    // Unable to find the group, so we fill in what we known.
    persistent_message.attribution.tab_group_metadata =
        TabGroupMessageMetadata();
    persistent_message.attribution.tab_group_metadata->sync_tab_group_id =
        shared_tab_group_id;
    std::optional<std::u16string> previous_title =
        tab_group_sync_service_->GetTitleForPreviouslyExistingSharedTabGroup(
            ToCollaborationId(collaboration_group_id));
    if (previous_title) {
      persistent_message.attribution.tab_group_metadata->last_known_title =
          base::UTF16ToUTF8(*previous_title);
    }
  }

  persistent_message.collaboration_event = CollaborationEvent::UNDEFINED;
  // We do not fill in triggering user or affeted user, because any action
  // related to dirty tabs could have been relevant here.

  if (hasDirtyDotMessagesForGroup) {
    NotifyDisplayPersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::DIRTY_TAB_GROUP});
  } else {
    NotifyHidePersistentMessagesForTypes(
        persistent_message, {PersistentNotificationType::DIRTY_TAB_GROUP});
  }
}

MessageAttribution
MessagingBackendServiceImpl::CreateMessageAttributionForTabUpdates(
    const collaboration_pb::Message& message,
    const std::optional<tab_groups::SavedTabGroup>& tab_group,
    const std::optional<tab_groups::SavedTabGroupTab>& tab) {
  MessageAttribution attribution;
  attribution.id = base::Uuid::ParseLowercase(message.uuid());

  attribution.collaboration_id =
      data_sharing::GroupId(message.collaboration_id());
  std::optional<tab_groups::SavedTabGroup> stack_tab_group = tab_group;
  if (!tab_group && tab) {
    stack_tab_group =
        tab_group_sync_service_->GetGroup(tab->saved_group_guid());
  }
  attribution.tab_group_metadata =
      CreateTabGroupMessageMetadataFromMessageOrTabGroup(message,
                                                         stack_tab_group);
  attribution.tab_metadata = CreateTabMessageMetadataFromMessageOrTab(
      message,
      tab.has_value() ? tab : GetTabFromGroup(message, stack_tab_group));

  // Look for the member in the provided data.
  auto group_data =
      data_sharing_service_->ReadGroup(attribution.collaboration_id);

  if (group_data) {
    // Set affected user if available.
    if (message.has_affected_user_gaia_id()) {
      GaiaId gaia_id(message.affected_user_gaia_id());
      for (const data_sharing::GroupMember& member :
           group_data.value().members) {
        if (member.gaia_id == gaia_id) {
          attribution.affected_user = member;
          attribution.affected_user_is_self =
              IsMemberCurrentUser(identity_manager_, gaia_id);
          break;
        }
      }
    }

    // Set triggering user if available.
    if (message.has_triggering_user_gaia_id()) {
      for (const data_sharing::GroupMember& member :
           group_data.value().members) {
        GaiaId gaia_id(message.triggering_user_gaia_id());
        if (member.gaia_id == gaia_id) {
          attribution.triggering_user = member;
          attribution.triggering_user_is_self =
              IsMemberCurrentUser(identity_manager_, gaia_id);
          break;
        }
      }
    }
  }

  return attribution;
}

void MessagingBackendServiceImpl::DisplayInstantMessage(
    const base::Uuid& db_message_uuid,
    const InstantMessage& base_message,
    const std::vector<InstantNotificationLevel>& levels) {
  for (InstantNotificationLevel level : levels) {
    InstantMessage instant_message = base_message;
    instant_message.level = level;
    instant_message_processor_->DisplayInstantMessage(instant_message);
  }
}

void MessagingBackendServiceImpl::ClearMessageDirtyBit(base::Uuid db_message_id,
                                                       bool success) {
  if (!success) {
    return;
  }

  store_->ClearDirtyMessage(db_message_id, DirtyType::kMessageOnly);
}

PersistentMessage
MessagingBackendServiceImpl::CreatePersistentMessageFromTabGroupAndTab(
    const data_sharing::GroupId& collaboration_group_id,
    const tab_groups::SavedTabGroupTab tab,
    CollaborationEvent collaboration_event) {
  std::optional<tab_groups::SavedTabGroup> tab_group =
      tab_group_sync_service_->GetGroup(tab.saved_group_guid());

  PersistentMessage persistent_message;
  persistent_message.collaboration_event = collaboration_event;
  persistent_message.attribution = MessageAttribution();
  // We leave the attribution.id field empty for synthetic messages, as we don't
  // have a corresponding stored collaboration_pb::Message available.

  persistent_message.attribution.collaboration_id = collaboration_group_id;
  if (tab_group) {
    persistent_message.attribution.tab_group_metadata =
        CreateTabGroupMessageMetadata(*tab_group);
  }
  persistent_message.attribution.tab_metadata = CreateTabMessageMetadata(tab);
  return persistent_message;
}

}  // namespace collaboration::messaging