File: saved_tab_group_proto_conversions.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 (398 lines) | stat: -rw-r--r-- 15,802 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
// 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/saved_tab_groups/internal/saved_tab_group_proto_conversions.h"

#include <algorithm>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/saved_tab_groups/public/pref_names.h"
#include "components/saved_tab_groups/public/saved_tab_group.h"
#include "components/saved_tab_groups/public/saved_tab_group_tab.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/saved_tab_groups/public/utils.h"
#include "components/sync/base/data_type.h"
#include "components/sync/protocol/saved_tab_group_specifics.pb.h"

namespace tab_groups {
namespace {

base::Time TimeFromWindowsEpochMicros(int64_t time_windows_epoch_micros) {
  return base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(time_windows_epoch_micros));
}

std::optional<sync_pb::AttributionMetadata::Attribution>
GetAttributionFromSpecifics(const sync_pb::SavedTabGroupSpecifics& specific,
                            bool for_creation) {
  if (specific.has_attribution_metadata()) {
    const auto& attribution_metadata = specific.attribution_metadata();
    if (for_creation) {
      if (attribution_metadata.has_created()) {
        return attribution_metadata.created();
      }
    } else {
      if (attribution_metadata.has_updated()) {
        return attribution_metadata.updated();
      }
    }
  }

  return std::nullopt;
}

std::optional<std::string> GetCacheGuidFromSpecifics(
    const sync_pb::SavedTabGroupSpecifics& specific,
    bool is_created) {
  auto attribution = GetAttributionFromSpecifics(specific, is_created);
  if (attribution.has_value()) {
    if (attribution->has_device_info()) {
      const auto& device_info = attribution->device_info();
      if (device_info.has_cache_guid()) {
        return device_info.cache_guid();
      }
    }
  }

  return std::nullopt;
}

}  // namespace

std::optional<size_t> GroupPositionFromSpecifics(
    const sync_pb::SavedTabGroupSpecifics& specifics) {
  // We leave the position unset if the proto is not set for unpinned tab
  // groups.
  if (specifics.group().has_pinned_position()) {
    return specifics.group().pinned_position();
  }
  return std::nullopt;
}

tab_groups::TabGroupColorId SyncColorToTabGroupColor(
    const sync_pb::SavedTabGroup::SavedTabGroupColor color) {
  switch (color) {
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_GREY:
      return tab_groups::TabGroupColorId::kGrey;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_BLUE:
      return tab_groups::TabGroupColorId::kBlue;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_RED:
      return tab_groups::TabGroupColorId::kRed;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_YELLOW:
      return tab_groups::TabGroupColorId::kYellow;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_GREEN:
      return tab_groups::TabGroupColorId::kGreen;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_PINK:
      return tab_groups::TabGroupColorId::kPink;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_PURPLE:
      return tab_groups::TabGroupColorId::kPurple;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_CYAN:
      return tab_groups::TabGroupColorId::kCyan;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_ORANGE:
      return tab_groups::TabGroupColorId::kOrange;
    case sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_UNSPECIFIED:
      return tab_groups::TabGroupColorId::kGrey;
  }
}

sync_pb::SavedTabGroup_SavedTabGroupColor TabGroupColorToSyncColor(
    const tab_groups::TabGroupColorId color) {
  switch (color) {
    case tab_groups::TabGroupColorId::kGrey:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_GREY;
    case tab_groups::TabGroupColorId::kBlue:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_BLUE;
    case tab_groups::TabGroupColorId::kRed:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_RED;
    case tab_groups::TabGroupColorId::kYellow:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_YELLOW;
    case tab_groups::TabGroupColorId::kGreen:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_GREEN;
    case tab_groups::TabGroupColorId::kPink:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_PINK;
    case tab_groups::TabGroupColorId::kPurple:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_PURPLE;
    case tab_groups::TabGroupColorId::kCyan:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_CYAN;
    case tab_groups::TabGroupColorId::kOrange:
      return sync_pb::SavedTabGroup::SAVED_TAB_GROUP_COLOR_ORANGE;
    case tab_groups::TabGroupColorId::kNumEntries:
      NOTREACHED() << "kNumEntries is not a supported color enum.";
  }

  NOTREACHED() << "No known conversion for the supplied color.";
}

SavedTabGroup DataToSavedTabGroup(const proto::SavedTabGroupData& data) {
  const auto& specific = data.specifics();
  CHECK(specific.has_group());

  const tab_groups::TabGroupColorId color =
      SyncColorToTabGroupColor(specific.group().color());
  const std::u16string& title = base::UTF8ToUTF16(specific.group().title());
  std::optional<size_t> position = GroupPositionFromSpecifics(specific);
  const base::Uuid guid = base::Uuid::ParseLowercase(specific.guid());
  const base::Time creation_time =
      TimeFromWindowsEpochMicros(specific.creation_time_windows_epoch_micros());
  const base::Time update_time =
      TimeFromWindowsEpochMicros(specific.update_time_windows_epoch_micros());

  std::optional<LocalTabGroupID> local_group_id;
  if (data.has_local_tab_group_data() &&
      data.local_tab_group_data().has_local_group_id()) {
    if (AreLocalIdsPersisted()) {
      local_group_id = LocalTabGroupIDFromString(
          data.local_tab_group_data().local_group_id());
    }
  }

  std::optional<std::string> creator_cache_guid =
      GetCreatorCacheGuidFromSpecifics(specific);
  std::optional<std::string> last_updater_cache_guid =
      GetLastUpdaterCacheGuidFromSpecifics(specific);

  bool created_before_syncing_tab_groups = false;
  base::Time last_user_interaction_time;
  base::Uuid originating_tab_group_guid;
  bool is_hidden = false;
  std::optional<base::Time> archival_time;
  if (data.has_local_tab_group_data()) {
    created_before_syncing_tab_groups =
        data.local_tab_group_data().created_before_syncing_tab_groups();
    last_user_interaction_time = TimeFromWindowsEpochMicros(
        data.local_tab_group_data()
            .last_user_interaction_time_windows_epoch_micros());
    if (data.local_tab_group_data().has_originating_tab_group_guid()) {
      originating_tab_group_guid = base::Uuid::ParseLowercase(
          data.local_tab_group_data().originating_tab_group_guid());
    }
    is_hidden = data.local_tab_group_data().is_group_hidden();
    if (data.local_tab_group_data().has_archival_time_windows_epoch_micros()) {
      archival_time = TimeFromWindowsEpochMicros(
          data.local_tab_group_data().archival_time_windows_epoch_micros());
    }
  }

  SavedTabGroup group = SavedTabGroup(
      title, color, {}, position, guid, local_group_id,
      std::move(creator_cache_guid), std::move(last_updater_cache_guid),
      created_before_syncing_tab_groups, creation_time);
  group.SetUpdateTime(update_time);
  group.SetLastUserInteractionTime(last_user_interaction_time);
  if (originating_tab_group_guid.is_valid()) {
    // The user is always an owner of saved tab groups.
    group.SetOriginatingTabGroupGuid(std::move(originating_tab_group_guid),
                                     /*use_originating_tab_group_guid=*/true);
  }
  group.SetIsHidden(is_hidden);
  group.SetArchivalTime(archival_time);

  return group;
}

proto::SavedTabGroupData SavedTabGroupToData(
    const SavedTabGroup& group,
    const sync_pb::SavedTabGroupSpecifics& base_specifics) {
  proto::SavedTabGroupData pb_data;
  auto* pb_specific = pb_data.mutable_specifics();
  pb_specific->CopyFrom(base_specifics);

  // WARNING: all fields need to be set or cleared explicitly.
  // WARNING: if you are adding support for new `SavedTabGroupSpecifics`
  // fields, you need to update the following functions accordingly:
  // `TrimAllSupportedFieldsFromRemoteSpecifics`.
  pb_specific->set_guid(group.saved_guid().AsLowercaseString());
  pb_specific->set_creation_time_windows_epoch_micros(
      group.creation_time().ToDeltaSinceWindowsEpoch().InMicroseconds());
  pb_specific->set_update_time_windows_epoch_micros(
      group.update_time().ToDeltaSinceWindowsEpoch().InMicroseconds());

  sync_pb::SavedTabGroup* pb_group = pb_specific->mutable_group();
  pb_group->set_color(TabGroupColorToSyncColor(group.color()));
  pb_group->set_title(base::UTF16ToUTF8(group.title()));
  if (group.creator_cache_guid().has_value()) {
    pb_specific->mutable_attribution_metadata()
        ->mutable_created()
        ->mutable_device_info()
        ->set_cache_guid(group.creator_cache_guid().value());
  } else {
    if (pb_specific->has_attribution_metadata() &&
        pb_specific->attribution_metadata().has_created() &&
        pb_specific->attribution_metadata().created().has_device_info()) {
      pb_specific->mutable_attribution_metadata()
          ->mutable_created()
          ->mutable_device_info()
          ->clear_cache_guid();
    }
  }

  if (group.last_updater_cache_guid().has_value()) {
    pb_specific->mutable_attribution_metadata()
        ->mutable_updated()
        ->mutable_device_info()
        ->set_cache_guid(group.last_updater_cache_guid().value());
  } else {
    if (pb_specific->has_attribution_metadata() &&
        pb_specific->attribution_metadata().has_updated() &&
        pb_specific->attribution_metadata().updated().has_device_info()) {
      pb_specific->mutable_attribution_metadata()
          ->mutable_updated()
          ->mutable_device_info()
          ->clear_cache_guid();
    }
  }

  if (group.position().has_value()) {
    pb_group->set_pinned_position(group.position().value());
  } else {
    pb_group->clear_pinned_position();
  }

  // Local only fields.
  if (AreLocalIdsPersisted()) {
    const auto& local_group_id = group.local_group_id();
    if (local_group_id.has_value()) {
      pb_data.mutable_local_tab_group_data()->set_local_group_id(
          LocalTabGroupIDToString(local_group_id.value()));
    }
  }

  pb_data.mutable_local_tab_group_data()->set_created_before_syncing_tab_groups(
      group.created_before_syncing_tab_groups());
  proto::LocalTabGroupData* local_data = pb_data.mutable_local_tab_group_data();
  local_data->set_last_user_interaction_time_windows_epoch_micros(
      group.last_user_interaction_time()
          .ToDeltaSinceWindowsEpoch()
          .InMicroseconds());

  if (group.GetOriginatingTabGroupGuid().has_value()) {
    local_data->set_originating_tab_group_guid(
        group.GetOriginatingTabGroupGuid().value().AsLowercaseString());
  }
  local_data->set_is_group_hidden(group.is_hidden());
  if (group.archival_time().has_value()) {
    local_data->set_archival_time_windows_epoch_micros(
        group.archival_time()
            .value()
            .ToDeltaSinceWindowsEpoch()
            .InMicroseconds());
  }

  // Version fields.
  pb_specific->set_version(kCurrentSavedTabGroupSpecificsProtoVersion);
  pb_data.set_version(kCurrentSavedTabGroupDataProtoVersion);

  // Note: When adding a new syncable field, also update IsSyncEquivalent().

  return pb_data;
}

SavedTabGroupTab DataToSavedTabGroupTab(const proto::SavedTabGroupData& data) {
  const auto& specific = data.specifics();
  CHECK(specific.has_tab());

  const base::Time creation_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(specific.creation_time_windows_epoch_micros()));
  const base::Time update_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(specific.update_time_windows_epoch_micros()));

  std::optional<std::string> creator_cache_guid =
      GetCreatorCacheGuidFromSpecifics(specific);
  std::optional<std::string> last_updater_cache_guid =
      GetLastUpdaterCacheGuidFromSpecifics(specific);

  SavedTabGroupTab tab(
      GURL(specific.tab().url()), base::UTF8ToUTF16(specific.tab().title()),
      base::Uuid::ParseLowercase(specific.tab().group_guid()),
      specific.tab().position(), base::Uuid::ParseLowercase(specific.guid()),
      std::nullopt, std::move(creator_cache_guid),
      std::move(last_updater_cache_guid), creation_time, update_time,
      /*favicon=*/std::nullopt,
      /*is_pending_ntp=*/false);
  return tab;
}

proto::SavedTabGroupData SavedTabGroupTabToData(
    const SavedTabGroupTab& tab,
    const sync_pb::SavedTabGroupSpecifics& base_specifics) {
  proto::SavedTabGroupData pb_data;
  auto* pb_specific = pb_data.mutable_specifics();
  pb_specific->CopyFrom(base_specifics);

  // WARNING: all fields need to be set or cleared explicitly.
  // WARNING: if you are adding support for new `SavedTabGroupSpecifics`
  // fields, you need to update the following functions accordingly:
  // `TrimAllSupportedFieldsFromRemoteSpecifics`.
  pb_specific->set_guid(tab.saved_tab_guid().AsLowercaseString());
  pb_specific->set_creation_time_windows_epoch_micros(
      tab.creation_time().ToDeltaSinceWindowsEpoch().InMicroseconds());
  pb_specific->set_update_time_windows_epoch_micros(
      tab.update_time().ToDeltaSinceWindowsEpoch().InMicroseconds());

  if (tab.creator_cache_guid().has_value()) {
    pb_specific->mutable_attribution_metadata()
        ->mutable_created()
        ->mutable_device_info()
        ->set_cache_guid(tab.creator_cache_guid().value());
  } else {
    if (pb_specific->has_attribution_metadata() &&
        pb_specific->attribution_metadata().has_created() &&
        pb_specific->attribution_metadata().created().has_device_info()) {
      pb_specific->mutable_attribution_metadata()
          ->mutable_created()
          ->mutable_device_info()
          ->clear_cache_guid();
    }
  }

  if (tab.last_updater_cache_guid().has_value()) {
    pb_specific->mutable_attribution_metadata()
        ->mutable_updated()
        ->mutable_device_info()
        ->set_cache_guid(tab.last_updater_cache_guid().value());
  } else {
    if (pb_specific->has_attribution_metadata() &&
        pb_specific->attribution_metadata().has_updated() &&
        pb_specific->attribution_metadata().updated().has_device_info()) {
      pb_specific->mutable_attribution_metadata()
          ->mutable_updated()
          ->mutable_device_info()
          ->clear_cache_guid();
    }
  }

  sync_pb::SavedTabGroupTab* pb_tab = pb_specific->mutable_tab();
  pb_tab->set_url(tab.url().spec());
  pb_tab->set_group_guid(tab.saved_group_guid().AsLowercaseString());
  pb_tab->set_title(base::UTF16ToUTF8(tab.title()));
  pb_tab->set_position(tab.position().value());
  // Note: When adding a new syncable field, also update IsSyncEquivalent().

  // Version fields.
  pb_specific->set_version(kCurrentSavedTabGroupSpecificsProtoVersion);
  pb_data.set_version(kCurrentSavedTabGroupDataProtoVersion);

  return pb_data;
}

std::optional<std::string> GetCreatorCacheGuidFromSpecifics(
    const sync_pb::SavedTabGroupSpecifics& specific) {
  return GetCacheGuidFromSpecifics(specific, /*is_created=*/true);
}

std::optional<std::string> GetLastUpdaterCacheGuidFromSpecifics(
    const sync_pb::SavedTabGroupSpecifics& specific) {
  return GetCacheGuidFromSpecifics(specific, /*is_created=*/false);
}

}  // namespace tab_groups