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
|
// 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_SERVICE_IMPL_H_
#define COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_TAB_GROUP_SYNC_SERVICE_IMPL_H_
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/containers/circular_deque.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "base/timer/timer.h"
#include "base/uuid.h"
#include "components/optimization_guide/core/hints/optimization_guide_decider.h"
#include "components/saved_tab_groups/delegate/tab_group_sync_delegate.h"
#include "components/saved_tab_groups/internal/saved_tab_group_model.h"
#include "components/saved_tab_groups/internal/saved_tab_group_sync_bridge.h"
#include "components/saved_tab_groups/internal/shared_tab_group_account_data_sync_bridge.h"
#include "components/saved_tab_groups/internal/shared_tab_group_data_sync_bridge.h"
#include "components/saved_tab_groups/internal/tab_group_sync_bridge_mediator.h"
#include "components/saved_tab_groups/internal/tab_group_sync_coordinator.h"
#include "components/saved_tab_groups/public/collaboration_finder.h"
#include "components/saved_tab_groups/public/saved_tab_group.h"
#include "components/saved_tab_groups/public/tab_group_sync_metrics_logger.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/saved_tab_groups/public/versioning_message_controller.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync/base/collaboration_id.h"
class PrefService;
namespace data_sharing {
class Logger;
} // namespace data_sharing
namespace tab_groups {
struct SyncDataTypeConfiguration;
// The internal implementation of the TabGroupSyncService.
class TabGroupSyncServiceImpl : public TabGroupSyncService,
public SavedTabGroupModelObserver,
public CollaborationFinder::Client,
public signin::IdentityManager::Observer {
public:
// `saved_tab_group_configuration` must not be `nullptr`.
// `shared_tab_group_configuration` should be provided if feature is enabled.
TabGroupSyncServiceImpl(
std::unique_ptr<SavedTabGroupModel> model,
std::unique_ptr<SyncDataTypeConfiguration> saved_tab_group_configuration,
std::unique_ptr<SyncDataTypeConfiguration> shared_tab_group_configuration,
std::unique_ptr<SyncDataTypeConfiguration>
shared_tab_group_account_configuration,
PrefService* pref_service,
std::unique_ptr<TabGroupSyncMetricsLogger> metrics_logger,
optimization_guide::OptimizationGuideDecider* optimization_guide_decider,
signin::IdentityManager* identity_manager,
std::unique_ptr<CollaborationFinder> collaboration_finder,
data_sharing::Logger* logger);
~TabGroupSyncServiceImpl() override;
// Disallow copy/assign.
TabGroupSyncServiceImpl(const TabGroupSyncServiceImpl&) = delete;
TabGroupSyncServiceImpl& operator=(const TabGroupSyncServiceImpl&) = delete;
// TabGroupSyncService implementation.
void SetTabGroupSyncDelegate(
std::unique_ptr<TabGroupSyncDelegate> delegate) override;
void AddGroup(SavedTabGroup group) override;
void RemoveGroup(const LocalTabGroupID& local_id) override;
void RemoveGroup(const base::Uuid& sync_id) override;
void UpdateVisualData(
const LocalTabGroupID local_group_id,
const tab_groups::TabGroupVisualData* visual_data) override;
void UpdateGroupPosition(const base::Uuid& sync_id,
std::optional<bool> is_pinned,
std::optional<int> new_index) override;
void AddTab(const LocalTabGroupID& group_id,
const LocalTabID& tab_id,
const std::u16string& title,
const GURL& url,
std::optional<size_t> position) override;
void NavigateTab(const LocalTabGroupID& group_id,
const LocalTabID& tab_id,
const GURL& url,
const std::u16string& title) override;
void UpdateTabProperties(const LocalTabGroupID& group_id,
const LocalTabID& tab_id,
const SavedTabGroupTabBuilder& tab_builder) override;
void RemoveTab(const LocalTabGroupID& group_id,
const LocalTabID& tab_id) override;
void MoveTab(const LocalTabGroupID& group_id,
const LocalTabID& tab_id,
int new_group_index) override;
void OnTabSelected(const std::optional<LocalTabGroupID>& group_id,
const LocalTabID& tab_id,
const std::u16string& title) override;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
void SaveGroup(SavedTabGroup group) override;
void UnsaveGroup(const LocalTabGroupID& local_id) override;
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
void MakeTabGroupShared(const LocalTabGroupID& local_group_id,
const syncer::CollaborationId& collaboration_id,
TabGroupSharingCallback callback) override;
void MakeTabGroupSharedForTesting(
const LocalTabGroupID& local_group_id,
const syncer::CollaborationId& collaboration_id) override;
void AboutToUnShareTabGroup(const LocalTabGroupID& local_group_id,
base::OnceClosure on_complete_callback) override;
void OnTabGroupUnShareComplete(const LocalTabGroupID& local_group_id,
bool success) override;
void OnCollaborationRemoved(
const syncer::CollaborationId& collaboration_id) override;
std::vector<const SavedTabGroup*> ReadAllGroups() const override;
std::vector<SavedTabGroup> GetAllGroups() const override;
std::optional<SavedTabGroup> GetGroup(const base::Uuid& guid) const override;
std::optional<SavedTabGroup> GetGroup(
const LocalTabGroupID& local_id) const override;
std::optional<SavedTabGroup> GetGroup(
const EitherGroupID& either_id) const override;
std::vector<LocalTabGroupID> GetDeletedGroupIds() const override;
std::optional<std::u16string> GetTitleForPreviouslyExistingSharedTabGroup(
const CollaborationId& collaboration_id) const override;
std::optional<LocalTabGroupID> OpenTabGroup(
const base::Uuid& sync_group_id,
std::unique_ptr<TabGroupActionContext> context) override;
void UpdateLocalTabGroupMapping(const base::Uuid& sync_id,
const LocalTabGroupID& local_id,
OpeningSource opening_source) override;
void RemoveLocalTabGroupMapping(const LocalTabGroupID& local_id,
ClosingSource closing_source) override;
void UpdateLocalTabId(const LocalTabGroupID& local_group_id,
const base::Uuid& sync_tab_id,
const LocalTabID& local_tab_id) override;
void ConnectLocalTabGroup(const base::Uuid& sync_id,
const LocalTabGroupID& local_id,
OpeningSource opening_source) override;
bool IsRemoteDevice(
const std::optional<std::string>& cache_guid) const override;
bool WasTabGroupClosedLocally(
const base::Uuid& sync_tab_group_id) const override;
std::u16string GetTabTitle(const LocalTabID& local_tab_id) override;
std::set<LocalTabID> GetSelectedTabs() override;
void RecordTabGroupEvent(const EventDetails& event_details) override;
void UpdateArchivalStatus(const base::Uuid& sync_id,
bool archival_status) override;
void UpdateTabLastSeenTime(const base::Uuid& group_id,
const base::Uuid& tab_id,
TriggerSource source) override;
TabGroupSyncMetricsLogger* GetTabGroupSyncMetricsLogger() override;
base::WeakPtr<syncer::DataTypeControllerDelegate>
GetSavedTabGroupControllerDelegate() override;
base::WeakPtr<syncer::DataTypeControllerDelegate>
GetSharedTabGroupControllerDelegate() override;
base::WeakPtr<syncer::DataTypeControllerDelegate>
GetSharedTabGroupAccountControllerDelegate() override;
std::unique_ptr<ScopedLocalObservationPauser>
CreateScopedLocalObserverPauser() override;
void GetURLRestriction(
const GURL& url,
TabGroupSyncService::UrlRestrictionCallback callback) override;
std::unique_ptr<std::vector<SavedTabGroup>>
TakeSharedTabGroupsAvailableAtStartupForMessaging() override;
bool HadSharedTabGroupsLastSession(bool open_shared_tab_groups) override;
VersioningMessageController* GetVersioningMessageController() override;
void OnLastTabClosed(const SavedTabGroup& saved_tab_group) override;
void AddObserver(TabGroupSyncService::Observer* observer) override;
void RemoveObserver(TabGroupSyncService::Observer* observer) override;
// signin::IdentityManager::Observer:
void OnPrimaryAccountChanged(
const signin::PrimaryAccountChangeEvent& event_details) override;
// tab_groups::CollaborationFinder::Client:
void OnCollaborationAvailable(
const syncer::CollaborationId& collaboration_id) override;
// For testing only.
void SetIsInitializedForTesting(bool initialized) override;
CollaborationFinder* GetCollaborationFinderForTesting() override;
// Called to set a coordinator that will manage all interactions with the tab
// model UI layer.
void SetCoordinator(std::unique_ptr<TabGroupSyncCoordinator> coordinator);
SavedTabGroupModel* GetModel() { return model_.get(); }
private:
struct TabGroupSharingTimeoutInfo {
TabGroupSharingTimeoutInfo();
~TabGroupSharingTimeoutInfo();
base::OneShotTimer timer;
TabGroupSharingCallback callback;
};
// KeyedService:
void Shutdown() override;
// SavedTabGroupModelObserver implementation.
void SavedTabGroupReorderedLocally() override;
void SavedTabGroupReorderedFromSync() override;
void SavedTabGroupAddedFromSync(const base::Uuid& guid) override;
void SavedTabGroupAddedLocally(const base::Uuid& guid) override;
void SavedTabGroupUpdatedFromSync(
const base::Uuid& group_guid,
const std::optional<base::Uuid>& tab_guid) override;
void SavedTabGroupUpdatedLocally(
const base::Uuid& group_guid,
const std::optional<base::Uuid>& tab_guid) override;
void SavedTabGroupRemovedFromSync(
const SavedTabGroup& removed_group) override;
void SavedTabGroupRemovedLocally(const SavedTabGroup& removed_group) override;
void SavedTabGroupLocalIdChanged(const base::Uuid& saved_group_id) override;
void SavedTabGroupTabLastSeenTimeUpdated(const base::Uuid& tab_id,
TriggerSource source) override;
void SavedTabGroupModelLoaded() override;
void OnSyncBridgeUpdateTypeChanged(
SyncBridgeUpdateType sync_bridge_update_type) override;
void TabGroupTransitioningToSavedRemovedFromSync(
const base::Uuid& saved_group_id) override;
// Called to notify the observers that service initialization is complete.
void NotifyServiceInitialized();
// Consolidation methods for adapting to observer signals from either
// direction (local -> remote or remote -> local).
// TODO(shaktisahu): Make SavedTabGroupModelObserver consolidate these signals
// directly at some point.
void HandleTabGroupAdded(const base::Uuid& guid, TriggerSource source);
void HandleTabGroupUpdated(const base::Uuid& group_guid,
const std::optional<base::Uuid>& tab_guid,
TriggerSource source);
void NotifyTabGroupAdded(const base::Uuid& guid, TriggerSource source);
void NotifyTabGroupUpdated(const base::Uuid& guid, TriggerSource source);
void NotifyTabGroupMigrated(const base::Uuid& new_group_guid,
TriggerSource source);
void HandleTabGroupRemoved(const SavedTabGroup& removed_group,
TriggerSource source);
void HandleTabGroupsReordered(TriggerSource source);
void NotifyOnSyncBridgeUpdateTypeChanged(
SyncBridgeUpdateType sync_bridge_update_type);
// Read and write deleted local group IDs to disk. We add a local ID in
// response to a group deletion event from sync. We clear that ID only when
// RemoveLocalTabGroupMapping is invoked from the UI.
// On startup, UI invokes GetDeletedGroupIdsFromPref to clean up any deleted
// groups from tab model.
std::vector<LocalTabGroupID> GetDeletedGroupIdsFromPref() const;
void AddDeletedGroupIdToPref(const LocalTabGroupID& local_id,
const base::Uuid& sync_id);
void RemoveDeletedGroupIdFromPref(const LocalTabGroupID& local_id);
void AddLocallyClosedGroupIdToPref(const base::Uuid& sync_id);
void RemoveLocallyClosedGroupIdFromPref(const base::Uuid& sync_id);
// Wrapper function that calls all metric recording functions.
void RecordMetrics();
// Force remove all tab groups that are currently not open in the local tab
// model. This is used on a certain finch group in order to fix an earlier
// bug.
void ForceRemoveClosedTabGroupsOnStartup();
// Called to clean up originating saved tab groups if needed.
void CleanUpOriginatingSavedTabGroupsIfNeeded();
// Helper function to update attributions for a group and optionally a tab.
void UpdateAttributions(
const LocalTabGroupID& group_id,
const std::optional<LocalTabID>& tab_id = std::nullopt);
// Helper method to update shared attributions for a group or tab. When
// `tab_id` is provided, the shared attributions are updated for the tab only.
// This method updates the shared attributions while UpdateAttributions()
// updates attributions for the saved tab groups.
void UpdateSharedAttributions(
const LocalTabGroupID& group_id,
const std::optional<LocalTabID>& tab_id = std::nullopt);
// Helper function to log a tab group event in histograms.
void LogEvent(TabGroupEvent event,
LocalTabGroupID group_id,
const std::optional<LocalTabID>& tab_id = std::nullopt);
// Creates a copy of all shared tab groups from the model and stores them in
// `shared_tab_groups_available_at_startup_for_messaging_` for later
// retrieval.
void StoreSharedTabGroupsAvailableAtStartupForMessaging();
// Transitions the originating saved tab group to the given shared tab group
// if the saved tab group is open in the tab strip. Returns true if the group
// was transitioned.
bool TransitionSavedToSharedTabGroupIfNeeded(
const SavedTabGroup& shared_group);
// Transitions the originating shared tab group to the given saved tab group.
// Returns true if the group is transitioned.
bool TransitionSharedToSavedTabGroupIfNeeded(
const SavedTabGroup& saved_group);
// Transitions a originating tab group to a new tab group. Called when
// either a saved tab group is becoming shared, or when a shared tab group is
// becoming private. This call will find the orignating tab group from the
// `tab_group`'s originating group guid, and replace it with `tab_group`.
// `opening_source` is the reason for adding the new group, and
// `closing_source` is the reason for removing the originating tab group.
// Returns true if the group is transitioned.
bool TransitionOriginatingTabGroupToNewGroupIfNeeded(
const SavedTabGroup& tab_group,
OpeningSource opening_source,
ClosingSource closing_source);
// Helper method called by NavigateTab() when UrlRestriction is retrieved.
void NavigateTabInternal(
const LocalTabGroupID& group_id,
const LocalTabID& tab_id,
const GURL& url,
const std::u16string& title,
const GURL& previous_tab_url,
const std::optional<proto::UrlRestriction>& url_restriction);
void NotifyTabSelected();
void OnTabGroupSharingTimeout(const base::Uuid& group_guid);
void NotifyTabGroupSharingResult(const base::Uuid& group_guid,
TabGroupSharingResult result);
// Whether a given group should show up in TabGroups
bool ShouldExposeSavedTabGroupInList(const SavedTabGroup& group) const;
// Find tab group by collaboration Id.
std::optional<SavedTabGroup> FindGroupWithCollaborationId(
const syncer::CollaborationId& collaboration_id);
// Updates the last seen time for any focused tab. Invoked for remote updates.
// This is because if a tab is updated from remote while being focused, it
// should automatically be marked as seen.
void UpdateLastSeenTimeForAnyFocusedTabForRemoteUpdates(
const SavedTabGroup* group,
TriggerSource source);
// Check if there are any groups that are stuck in the middle of transitioning
// to shared and completes the transition.
void FinishTransitionToSharedIfNotCompleted();
// Register PageEntities optimization type if there is a shared tab group.
void RegisterPageEntityOptimizationTypeIfNeeded();
THREAD_CHECKER(thread_checker_);
// The in-memory model representing the currently present saved tab groups.
std::unique_ptr<SavedTabGroupModel> model_;
// Sync bridges and data storage for both saved and shared tab group data.
std::unique_ptr<TabGroupSyncBridgeMediator> sync_bridge_mediator_;
// Sync bridge for shared tab group account data.
std::unique_ptr<SharedTabGroupAccountDataSyncBridge>
shared_tab_group_account_data_bridge_;
// The UI coordinator to apply changes between local tab groups and the
// TabGroupSyncService.
std::unique_ptr<TabGroupSyncCoordinator> coordinator_;
// Helper class for logging metrics.
std::unique_ptr<TabGroupSyncMetricsLogger> metrics_logger_;
// For finding collaboration availability info from DataSharingService.
std::unique_ptr<CollaborationFinder> collaboration_finder_;
// Logger for logging to debug UI.
raw_ptr<data_sharing::Logger> logger_ = nullptr;
// The pref service for storing migration status.
raw_ptr<PrefService> pref_service_ = nullptr;
// Whether the initialization has been completed, i.e. all the groups and the
// ID mappings have been loaded into memory.
bool is_initialized_ = false;
// Groups with zero tabs are groups that still haven't received their tabs
// from sync. UI can't handle these groups, hence the service needs to wait
// before notifying the observers.
std::set<base::Uuid> empty_groups_;
// Keeps track of shared tab groups that are waiting for their respective
// people groups to be available in DataSharingService backend. UI can't
// handle these groups, hence the service needs to wait before notifying the
// observers. Once the group becomes available, OnTabGroupAdded() will be
// invoked for the shared tab group.
std::vector<std::tuple<syncer::CollaborationId, base::Uuid, TriggerSource>>
shared_tab_groups_waiting_for_collaboration_;
// Obsevers of the model.
base::ObserverList<TabGroupSyncService::Observer> observers_;
// Temporary storage for shared tab groups that were available at startup,
// before applying local changes. This is retrieved by the
// MessagingBackendService when it starts up to safely know what changes are
// new, and what were there from before, without needing to persist any data
// on its own, but still be able to calculate deltas of all network triggered
// updates.
std::unique_ptr<std::vector<SavedTabGroup>>
shared_tab_groups_available_at_startup_for_messaging_;
// Whether shared tab groups existed during startup.
bool had_shared_tab_groups_on_startup_ = false;
// Whether open shared tab groups existed during startup.
bool had_open_shared_tab_groups_on_startup_ = false;
// Temporary in-memory mapping from collaboration ID to title for tab groups
// that we/ have previously known about. This is to facilitate displaying of
// tab group titles in the UI when a user is removed from a tab group.
std::unordered_map<CollaborationId, std::u16string>
titles_for_previously_existing_shared_tab_groups_;
// Keeps track of API calls received before the service is initialized.
// Once the initialization is complete, these callbacks are run in the order
// they were received. External observers are notified of init completion only
// after this step. Currently used only for UpdateLocalTabGroupMapping()
// calls.
base::circular_deque<base::OnceClosure> pending_actions_;
// Keeps track of API calls received when not signed in. Unlike most other
// methods of this class, these API calls cannot proceed without a signed in
// account (e.g. they need user attribution). Currently required
// for MakeTabGroupShared() only.
// Once the sign-in and initial sync is complete for the account, these
// callbacks will be run in the order they were received.
base::circular_deque<base::OnceClosure> pending_actions_waiting_sign_in_;
// A handle to optimization guide for information about synced URLs.
raw_ptr<optimization_guide::OptimizationGuideDecider> opt_guide_ = nullptr;
// Keeps track of the tab group sharing progress. There can be multiple
// sharing requests for different tab groups (e.g. from different windows).
std::map<base::Uuid, TabGroupSharingTimeoutInfo>
tab_group_sharing_timeout_info_;
// The versioning message controller which is responsible for business logic
// related to shared tab groups versioning related messages.
std::unique_ptr<VersioningMessageController> versioning_message_controller_;
base::ScopedObservation<signin::IdentityManager,
signin::IdentityManager::Observer>
identity_manager_observation_{this};
bool page_entity_optimization_type_registered_ = false;
base::WeakPtrFactory<TabGroupSyncServiceImpl> weak_ptr_factory_{this};
};
} // namespace tab_groups
#endif // COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_TAB_GROUP_SYNC_SERVICE_IMPL_H_
|