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
|
// 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_COLLABORATION_INTERNAL_COLLABORATION_CONTROLLER_H_
#define COMPONENTS_COLLABORATION_INTERNAL_COLLABORATION_CONTROLLER_H_
#include <array>
#include <map>
#include <memory>
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "components/collaboration/public/collaboration_controller_delegate.h"
#include "components/collaboration/public/collaboration_flow_type.h"
#include "components/data_sharing/public/data_sharing_service.h"
#include "components/data_sharing/public/group_data.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/signin/public/identity_manager/identity_manager.h"
namespace syncer {
class SyncService;
} // namespace syncer
namespace collaboration {
class CollaborationService;
class ControllerState;
// The class for managing a single collaboration group flow.
class CollaborationController
: public tab_groups::TabGroupSyncService::Observer {
public:
// States of a collaboration group flow. All new flows starts PENDNG.
enum class StateId {
// Initial state. The request has been received, awaiting delegate to be
// initialized and authentication status to be verified.
kPending,
// Waiting on more information about a potentially managed account.
kWaitingForPolicyUpdate,
// UI is showing authentication screens (sign-in/sync/access token). Waiting
// for result.
kAuthenticating,
// Waiting for tab group sync service and data sharing service to be ready
// to use.
kWaitingForServicesToInitialize,
// Authentication is completed. Controller will check requirements for each
// specific flows.
kCheckingFlowRequirements,
// Delegate is showing invitation screen to the user.
kAddingUserToGroup,
// Waiting for tab group to be added in sync and people group to be added in
// DataSharing. Loading UI should be shown.
kWaitingForSyncAndDataSharingGroup,
// Delegate is promoting the local tab group.
kOpeningLocalTabGroup,
// Delegate is showing the share sheet.
kShowingShareScreen,
// Delegate requested creating a shared tab group.
kMakingTabGroupShared,
// Delegate is sharing the tab group's url.
kSharingTabGroupUrl,
// Delegate is showing the manage people screen.
kShowingManageScreen,
// Delegate is showing the leave group screen.
kLeavingGroup,
// Delegate is showing the delete group screen.
kDeletingGroup,
// A shared tab group has been deleted, cleaning up.
kCleaningUpSharedTabGroup,
// The flow is cancelled.
kCancel,
// An error occurred and need to be shown to the user.
kError,
};
class Flow {
public:
// Join flow constructor.
Flow(FlowType type, const data_sharing::GroupToken& token);
// Share/manage/leave/delete flow constructor.
Flow(FlowType type, const tab_groups::EitherGroupID& either_id);
~Flow();
Flow(const Flow&);
const FlowType type;
const data_sharing::GroupToken& join_token() const {
DCHECK_EQ(type, FlowType::kJoin);
return join_token_;
}
const tab_groups::EitherGroupID& either_id() const {
return either_id_;
}
const data_sharing::GroupToken& share_token() const {
DCHECK_EQ(type, FlowType::kShareOrManage);
CHECK(share_token_.IsValid());
return share_token_;
}
void set_share_token(const data_sharing::GroupToken& token) {
share_token_ = token;
}
private:
// ID for join flow.
const data_sharing::GroupToken join_token_;
// ID for share/manage/leave/delete flow.
const tab_groups::EitherGroupID either_id_;
data_sharing::GroupToken share_token_;
};
using FinishCallback = base::OnceCallback<void(const void*)>;
explicit CollaborationController(
const Flow& flow,
CollaborationService* collaboration_service,
data_sharing::DataSharingService* data_sharing_service,
tab_groups::TabGroupSyncService* tab_group_sync_service,
syncer::SyncService* sync_service,
signin::IdentityManager* identity_manager,
std::unique_ptr<CollaborationControllerDelegate> delegate,
FinishCallback finish_and_delete);
~CollaborationController() override;
// Disallow copy/assign.
CollaborationController(const CollaborationController&) = delete;
CollaborationController& operator=(const CollaborationController&) = delete;
// Getters.
CollaborationControllerDelegate* delegate() { return delegate_.get(); }
data_sharing::DataSharingService* data_sharing_service() {
return data_sharing_service_.get();
}
tab_groups::TabGroupSyncService* tab_group_sync_service() {
return tab_group_sync_service_.get();
}
syncer::SyncService* sync_service() { return sync_service_.get(); }
signin::IdentityManager* identity_manager() {
return identity_manager_.get();
}
CollaborationService* collaboration_service() {
return collaboration_service_.get();
}
Flow& flow() { return flow_; }
// Called to transition to another state.
void TransitionTo(StateId state,
const CollaborationControllerDelegate::ErrorInfo& error =
CollaborationControllerDelegate::ErrorInfo());
// Called to refocus the current flow.
void PromoteCurrentSession();
// Called when the flow is finished to exit and clean itself up in the
// service.
void Exit();
// Cancels and exits the current flow.
void Cancel();
// Helper functions used in tests.
void SetStateForTesting(StateId state);
StateId GetStateForTesting();
// TabGroupSyncService::Observer implementation.
void OnTabGroupRemoved(const tab_groups::LocalTabGroupID& local_id,
tab_groups::TriggerSource source) override;
void OnTabGroupRemoved(const base::Uuid& sync_id,
tab_groups::TriggerSource source) override;
void OnTabGroupMigrated(const tab_groups::SavedTabGroup& new_group,
const base::Uuid& old_sync_id,
tab_groups::TriggerSource source) override;
private:
static constexpr std::array<std::pair<StateId, StateId>, 41>
kValidTransitions = {{
// kPending transitions to:
//
// kAuthenticating: After all initialization steps complete
// successfully and authentication status is not valid.
// kWaitingForPolicyUpdate: Current account info are not ready.
// kCheckingFlowRequirements: After all initialization steps
// complete successfully and authentication status is valid.
// kError: An error occurred during initialization.
{StateId::kPending, StateId::kAuthenticating},
{StateId::kPending, StateId::kWaitingForPolicyUpdate},
{StateId::kPending, StateId::kWaitingForServicesToInitialize},
{StateId::kPending, StateId::kError},
// kWaitingForPolicyUpdate transitions to:
//
// kAuthenticating: Current account is not managed and sync consent
// is needed.
// kCheckingFlowRequirements: Current account is not managed.
// kError: Current account is managed.
{StateId::kWaitingForPolicyUpdate, StateId::kAuthenticating},
{StateId::kWaitingForPolicyUpdate,
StateId::kCheckingFlowRequirements},
{StateId::kWaitingForPolicyUpdate, StateId::kError},
// kAuthenticating transitions to:
//
// kWaitingForPolicyUpdate: Current account info are not ready.
// kCheckingFlowRequirements: After all authentication steps are
// completed and verified.
// kCancel: After the user cancels the process.
// kError: An error occurred during authentication.
{StateId::kAuthenticating, StateId::kWaitingForPolicyUpdate},
{StateId::kAuthenticating, StateId::kWaitingForServicesToInitialize},
{StateId::kAuthenticating, StateId::kCancel},
{StateId::kAuthenticating, StateId::kError},
// kWaitingForServicesToInitialize transition to:
//
// kCheckingFlowRequirements: After all services finish
// initializing.
// kError: An error occurred while waiting for service
// initialization.
{StateId::kWaitingForServicesToInitialize,
StateId::kCheckingFlowRequirements},
{StateId::kWaitingForServicesToInitialize, StateId::kError},
// kCheckingFlowRequirements transition to:
//
// kAddingUserToGroup: When user is not in current people group.
// kWaitingForSyncAndDataSharingGroup: When user is in current
// people group,
// but tab group not found in sync.
// kOpeningLocalTabGroup: When user is in current people group, and
// tab group found in sync.
// kShowingShareScreen: In share flow, when the tab group is not
// shared.
// kShowingManageScreen: In share flow, when the tab group is a
// shared tab group.
// kError: An error occurred while checking requirements. This could
// be due to version mismatch.
{StateId::kCheckingFlowRequirements, StateId::kAddingUserToGroup},
{StateId::kCheckingFlowRequirements,
StateId::kWaitingForSyncAndDataSharingGroup},
{StateId::kCheckingFlowRequirements, StateId::kOpeningLocalTabGroup},
{StateId::kCheckingFlowRequirements, StateId::kShowingShareScreen},
{StateId::kCheckingFlowRequirements, StateId::kShowingManageScreen},
{StateId::kCheckingFlowRequirements, StateId::kLeavingGroup},
{StateId::kCheckingFlowRequirements, StateId::kDeletingGroup},
{StateId::kCheckingFlowRequirements, StateId::kError},
// kAddingUserToGroup transition to:
//
// kWaitingForSyncAndDataSharingGroup: After the user accept the
// join
// invitation and the tab group is not yet added in sync.
// kOpeningLocalTabGroup: After the user accept the join invitation
// and the tab group is in sync.
// kCancel: After the user cancels the join invitation
// kError: An error occurred during invitation screen.
{StateId::kAddingUserToGroup,
StateId::kWaitingForSyncAndDataSharingGroup},
{StateId::kAddingUserToGroup, StateId::kOpeningLocalTabGroup},
{StateId::kAddingUserToGroup, StateId::kCancel},
{StateId::kAddingUserToGroup, StateId::kError},
// kWaitingForSyncAndDataSharingGroup transition to:
//
// kOpeningLocalTabGroup: After tab group is added in sync.
// kError: An error occurred while waiting for sync tab group.
{StateId::kWaitingForSyncAndDataSharingGroup,
StateId::kOpeningLocalTabGroup},
{StateId::kWaitingForSyncAndDataSharingGroup, StateId::kError},
// kOpeningLocalTabGroup transition to:
//
// kCancel: After the promote is done successfully, cancel the flow
// to clean up.
// kError: An error occurred while opening local tab group.
{StateId::kOpeningLocalTabGroup, StateId::kCancel},
{StateId::kOpeningLocalTabGroup, StateId::kError},
// kShowingShareScreen transition to:
//
// kSharingTabGroupUrl: After share screen request creating a shared
// tab group.
// kCancel: After the user exit the share screen without sharing.
// kError: An error occurred while showing the share screen.
{StateId::kShowingShareScreen, StateId::kMakingTabGroupShared},
{StateId::kShowingShareScreen, StateId::kCancel},
{StateId::kShowingShareScreen, StateId::kError},
// kMakingTabGroupShared transition to:
//
// kSharingTabGroupUrl: After shared tab group is successfully
// created.
// kError: An error occurred while creating the shared tab group.
{StateId::kMakingTabGroupShared, StateId::kSharingTabGroupUrl},
{StateId::kMakingTabGroupShared, StateId::kError},
// kSharingTabGroupUrl transition to:
//
// kError: An error occurred while sharing the url.
{StateId::kSharingTabGroupUrl, StateId::kError},
// kShowingManageScreen transition to:
//
// kCleaningUpSharedTabGroup: When deletion happened on a manage
// screen.
// kError: An error occurred while showing the manage people screen.
{StateId::kShowingManageScreen, StateId::kCleaningUpSharedTabGroup},
{StateId::kShowingManageScreen, StateId::kError},
// kShowingManageScreen transition to:
//
// kCleaningUpSharedTabGroup: After leaving group successfully.
// kError: An error occurred while leaving group.
{StateId::kLeavingGroup, StateId::kCleaningUpSharedTabGroup},
{StateId::kLeavingGroup, StateId::kError},
// kDeletingGroup transition to:
//
// kCleaningUpSharedTabGroup: When deletion has been completed.
// kError: An error occurred while deleting group.
{StateId::kDeletingGroup, StateId::kCleaningUpSharedTabGroup},
{StateId::kDeletingGroup, StateId::kError},
}};
// Note: This would only cancel the flow if the flow was started with the same
// variant of the EitherGroupID. The caller must call this method using both
// sync ID and local ID to be sure that the flow for the tab group is
// cancelled.
void CancelShareOrManageFlow(const tab_groups::EitherGroupID& either_id);
bool IsValidStateTransition(StateId from, StateId to);
std::unique_ptr<ControllerState> CreateStateObject(StateId state);
void Start();
THREAD_CHECKER(thread_checker_);
std::unique_ptr<ControllerState> current_state_;
Flow flow_;
bool is_deleting_{false};
const raw_ptr<CollaborationService> collaboration_service_;
const raw_ptr<data_sharing::DataSharingService> data_sharing_service_;
const raw_ptr<tab_groups::TabGroupSyncService> tab_group_sync_service_;
const raw_ptr<syncer::SyncService> sync_service_;
const raw_ptr<signin::IdentityManager> identity_manager_;
std::unique_ptr<CollaborationControllerDelegate> delegate_;
FinishCallback finish_and_delete_;
base::ScopedObservation<tab_groups::TabGroupSyncService,
tab_groups::TabGroupSyncService::Observer>
tab_group_sync_service_observer_{this};
base::WeakPtrFactory<CollaborationController> weak_ptr_factory_{this};
};
} // namespace collaboration
#endif // COMPONENTS_COLLABORATION_INTERNAL_COLLABORATION_CONTROLLER_H_
|