File: sync_bridge_tab_group_model_wrapper.h

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 (123 lines) | stat: -rw-r--r-- 4,728 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_SYNC_BRIDGE_TAB_GROUP_MODEL_WRAPPER_H_
#define COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_SYNC_BRIDGE_TAB_GROUP_MODEL_WRAPPER_H_

#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/sync/base/data_type.h"
#include "components/tab_groups/tab_group_color.h"
#include "google_apis/gaia/gaia_id.h"

namespace tab_groups {

class SavedTabGroup;
class SavedTabGroupTab;
class SavedTabGroupModel;

// Wrapper of SavedTabGroupModel for sync bridges. Used to enforce the bridges
// working with the expected types of tab groups to mitigate possible unexpected
// changes between the data types.
class SyncBridgeTabGroupModelWrapper {
 public:
  using LoadCallback = base::OnceCallback<void(std::vector<SavedTabGroup>,
                                               std::vector<SavedTabGroupTab>)>;

  // `model` must not be null and must outlive the current object.
  SyncBridgeTabGroupModelWrapper(syncer::DataType sync_data_type,
                                 SavedTabGroupModel* model,
                                 LoadCallback on_load_callback);
  SyncBridgeTabGroupModelWrapper(const SyncBridgeTabGroupModelWrapper&) =
      delete;
  SyncBridgeTabGroupModelWrapper& operator=(
      const SyncBridgeTabGroupModelWrapper&) = delete;
  ~SyncBridgeTabGroupModelWrapper();

  // Returns true if the model is initialized.
  bool IsInitialized() const;

  // Returns tab groups corresponding to the data type.
  std::vector<const SavedTabGroup*> GetTabGroups() const;

  // Returns a group with the given `group_id`, null if the group does not exist
  // or does not correspond to the data type.
  const SavedTabGroup* GetGroup(const base::Uuid& group_id) const;

  // Returns the group which contains the tab with `tab_id` if exists and
  // corresponds to the data type, otherwise returns null.
  const SavedTabGroup* GetGroupContainingTab(const base::Uuid& tab_id) const;

  // Removes the tab from the `group_id`. `removed_by` is the user who removed
  // the tab (used for shared tab groups only, may be empty).
  void RemoveTabFromGroup(const base::Uuid& group_id,
                          const base::Uuid& tab_id,
                          GaiaId removed_by = GaiaId());

  // Removes the whole group and all its tabs.
  void RemoveGroup(const base::Uuid& group_id);

  // Updates group's metadata based on the remote update.
  const SavedTabGroup* MergeRemoteGroupMetadata(
      const base::Uuid& group_id,
      const std::u16string& title,
      TabGroupColorId color,
      std::optional<size_t> position,
      std::optional<std::string> creator_cache_guid,
      std::optional<std::string> last_updater_cache_guid,
      base::Time update_time,
      GaiaId updated_by);

  // Updates the tab with the given remote data.
  const SavedTabGroupTab* MergeRemoteTab(const SavedTabGroupTab& remote_tab);

  // Adds a group created remotely.
  void AddGroup(SavedTabGroup group);

  // Adds a new tab to the group created remotely.
  void AddTabToGroup(const base::Uuid& group_id, SavedTabGroupTab tab);

  // Updates the creator cache guid for all saved groups that have
  // `old_cache_guid`, to `new_cache_guid`. Returns a pair of group IDs and tab
  // IDs which were updated.
  std::pair<std::set<base::Uuid>, std::set<base::Uuid>> UpdateLocalCacheGuid(
      std::optional<std::string> old_cache_guid,
      std::optional<std::string> new_cache_guid);

  // Marks the tab group as transitioned to shared, used for shared tab groups
  // only.
  void MarkTransitionedToShared(const base::Uuid& group_id);

  // Initializes with the data loaded from the disk.
  void Initialize(std::vector<SavedTabGroup> groups,
                  std::vector<SavedTabGroupTab> tabs);

  // Called to notify of the sync bridge state changes, e.g. whether initial
  // merge or disable sync are in progress. Invoked only for shared tab group
  // bridge.
  void OnSyncBridgeUpdateTypeChanged(
      SyncBridgeUpdateType sync_bridge_update_type);

 private:
  // Returns whether the current wrapper is used for the shared tab group data.
  bool IsSharedTabGroupData() const;

  const syncer::DataType sync_data_type_;
  const raw_ptr<SavedTabGroupModel> model_;

  LoadCallback on_load_callback_;
};

}  // namespace tab_groups

#endif  // COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_SYNC_BRIDGE_TAB_GROUP_MODEL_WRAPPER_H_