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
|
// 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_TAB_GROUP_SYNC_BRIDGE_MEDIATOR_H_
#define COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_TAB_GROUP_SYNC_BRIDGE_MEDIATOR_H_
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/scoped_observation_traits.h"
#include "base/uuid.h"
#include "components/saved_tab_groups/internal/saved_tab_group_model_observer.h"
#include "components/saved_tab_groups/internal/sync_bridge_tab_group_model_wrapper.h"
#include "components/saved_tab_groups/public/saved_tab_group.h"
#include "components/saved_tab_groups/public/saved_tab_group_tab.h"
#include "google_apis/gaia/gaia_id.h"
class PrefService;
namespace data_sharing {
class Logger;
} // namespace data_sharing
namespace syncer {
class DataTypeControllerDelegate;
} // namespace syncer
namespace tab_groups {
class SavedTabGroupModel;
class SavedTabGroupSyncBridge;
class SharedTabGroupDataSyncBridge;
struct SyncDataTypeConfiguration;
// Used to control sync bridges for Saved and Shared tab groups and dispatch
// changes in the model to a corresponding bridge.
class TabGroupSyncBridgeMediator : public SavedTabGroupModelObserver {
public:
// `model` and `pref_service` must never be null and must outlive the current
// object.
TabGroupSyncBridgeMediator(
SavedTabGroupModel* model,
PrefService* pref_service,
data_sharing::Logger* logger,
std::unique_ptr<SyncDataTypeConfiguration> saved_tab_group_configuration,
std::unique_ptr<SyncDataTypeConfiguration>
shared_tab_group_configuration);
TabGroupSyncBridgeMediator(const TabGroupSyncBridgeMediator&) = delete;
TabGroupSyncBridgeMediator& operator=(const TabGroupSyncBridgeMediator&) =
delete;
~TabGroupSyncBridgeMediator() override;
// Delegates for sync initialization.
base::WeakPtr<syncer::DataTypeControllerDelegate>
GetSavedTabGroupControllerDelegate();
base::WeakPtr<syncer::DataTypeControllerDelegate>
GetSharedTabGroupControllerDelegate();
// SavedTabGroupSyncBridge specific getters.
bool IsSavedBridgeSyncing() const;
std::optional<std::string> GetLocalCacheGuidForSavedBridge() const;
std::optional<GaiaId> GetGaiaIdForSavedBridge() const;
// SharedTabGroupDataSyncBridge specific getters.
// Returns the obfuscated Gaia ID of the account that is currently syncing
// shared tab groups. Returns nullopt if the bridge is not syncing (including
// if the bridge has not downloaded any data yet).
std::optional<GaiaId> GetTrackingGaiaIdForSharedBridge() const;
// SavedTabGroupModelObserver overrides.
void SavedTabGroupAddedLocally(const base::Uuid& guid) override;
void SavedTabGroupRemovedLocally(const SavedTabGroup& removed_group) override;
void SavedTabGroupUpdatedLocally(
const base::Uuid& group_guid,
const std::optional<base::Uuid>& tab_guid) override;
void SavedTabGroupTabMovedLocally(const base::Uuid& group_guid,
const base::Uuid& tab_guid) override;
void SavedTabGroupReorderedLocally() override;
void SavedTabGroupLocalIdChanged(const base::Uuid& group_guid) override;
void SavedTabGroupLastUserInteractionTimeUpdated(
const base::Uuid& group_guid) override;
// Called to untrack entities for a given collaboration from sync server.
void UntrackEntitiesForCollaboration(
const syncer::CollaborationId& collaboration_id);
private:
// Populates loaded entries to the model when all data is loaded.
void InitializeModelIfReady();
void OnSavedGroupsWithTabsLoaded(std::vector<SavedTabGroup> groups,
std::vector<SavedTabGroupTab> tabs);
void OnSharedGroupsWithTabsLoaded(std::vector<SavedTabGroup> groups,
std::vector<SavedTabGroupTab> tabs);
raw_ptr<SavedTabGroupModel> model_;
// Observes the SavedTabGroupModel.
base::ScopedObservation<SavedTabGroupModel, SavedTabGroupModelObserver>
observation_{this};
SyncBridgeTabGroupModelWrapper saved_bridge_model_wrapper_;
std::unique_ptr<SavedTabGroupSyncBridge> saved_bridge_;
SyncBridgeTabGroupModelWrapper shared_bridge_model_wrapper_;
std::unique_ptr<SharedTabGroupDataSyncBridge> shared_bridge_;
// Temporary storage of groups and tabs loaded from the disk for both saved
// and shared tab groups. Empty once the model is initialized.
std::vector<SavedTabGroup> loaded_saved_groups_;
std::vector<SavedTabGroupTab> loaded_saved_tabs_;
std::vector<SavedTabGroup> loaded_shared_groups_;
std::vector<SavedTabGroupTab> loaded_shared_tabs_;
bool saved_tab_groups_loaded_ = false;
bool shared_tab_groups_loaded_ = false;
};
} // namespace tab_groups
#endif // COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_TAB_GROUP_SYNC_BRIDGE_MEDIATOR_H_
|