#ifndef COMPONENTS_COLLABORATION_INTERNAL_MESSAGING_MESSAGING_BACKEND_SERVICE_IMPL_H_
#define COMPONENTS_COLLABORATION_INTERNAL_MESSAGING_MESSAGING_BACKEND_SERVICE_IMPL_H_
#include <memory>
#include <unordered_map>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "components/collaboration/internal/messaging/configuration.h"
#include "components/collaboration/internal/messaging/data_sharing_change_notifier.h"
#include "components/collaboration/internal/messaging/instant_message_processor.h"
#include "components/collaboration/internal/messaging/storage/messaging_backend_store.h"
#include "components/collaboration/internal/messaging/tab_group_change_notifier.h"
#include "components/collaboration/public/messaging/message.h"
#include "components/collaboration/public/messaging/messaging_backend_service.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
namespace collaboration_pb {
class Message;
}
namespace data_sharing {
class DataSharingService;
}
namespace tab_groups {
class TabGroupSyncService;
}
namespace collaboration::messaging {
class DataSharingChangeNotifier;
class MessagingBackendStore;
class MessagingBackendServiceImpl : public MessagingBackendService,
public TabGroupChangeNotifier::Observer,
public DataSharingChangeNotifier::Observer {
public:
MessagingBackendServiceImpl(
const MessagingBackendConfiguration& configuration,
std::unique_ptr<TabGroupChangeNotifier> tab_group_change_notifier,
std::unique_ptr<DataSharingChangeNotifier> data_sharing_change_notifier,
std::unique_ptr<MessagingBackendStore> messaging_backend_store,
std::unique_ptr<InstantMessageProcessor> instant_message_processor,
tab_groups::TabGroupSyncService* tab_group_sync_service,
data_sharing::DataSharingService* data_sharing_service,
signin::IdentityManager* identity_manager);
~MessagingBackendServiceImpl() override;
void SetInstantMessageDelegate(
InstantMessageDelegate* instant_message_delegate) override;
void AddPersistentMessageObserver(
PersistentMessageObserver* observer) override;
void RemovePersistentMessageObserver(
PersistentMessageObserver* observer) override;
bool IsInitialized() override;
std::vector<PersistentMessage> GetMessagesForTab(
tab_groups::EitherTabID tab_id,
PersistentNotificationType type) override;
std::vector<PersistentMessage> GetMessagesForGroup(
tab_groups::EitherGroupID group_id,
PersistentNotificationType type) override;
std::vector<PersistentMessage> GetMessages(
PersistentNotificationType type) override;
std::vector<ActivityLogItem> GetActivityLog(
const ActivityLogQueryParams& params) override;
void ClearDirtyTabMessagesForGroup(
const data_sharing::GroupId& collaboration_group_id) override;
void ClearPersistentMessage(const base::Uuid& message_id,
PersistentNotificationType type) override;
void RemoveMessages(const std::vector<base::Uuid>& message_ids) override;
void AddActivityLogForTesting(
data_sharing::GroupId collaboration_id,
const std::vector<ActivityLogItem>& activity_log) override;
void OnTabGroupChangeNotifierInitialized() override;
void OnSyncDisabled() override;
void OnTabGroupAdded(const tab_groups::SavedTabGroup& added_group,
tab_groups::TriggerSource source) override;
void OnTabGroupRemoved(tab_groups::SavedTabGroup removed_group,
tab_groups::TriggerSource source) override;
void OnTabGroupNameUpdated(const tab_groups::SavedTabGroup& updated_group,
tab_groups::TriggerSource source) override;
void OnTabGroupColorUpdated(const tab_groups::SavedTabGroup& updated_group,
tab_groups::TriggerSource source) override;
void OnTabAdded(const tab_groups::SavedTabGroupTab& added_tab,
tab_groups::TriggerSource source) override;
void OnTabRemoved(tab_groups::SavedTabGroupTab removed_tab,
tab_groups::TriggerSource source,
bool is_selected) override;
void OnTabUpdated(const tab_groups::SavedTabGroupTab& before,
const tab_groups::SavedTabGroupTab& after,
tab_groups::TriggerSource source,
bool is_selected) override;
void OnTabSelectionChanged(const tab_groups::LocalTabID& tab_id,
bool is_selected) override;
void OnTabLastSeenTimeChanged(const base::Uuid& tab_id,
tab_groups::TriggerSource source) override;
void OnTabGroupOpened(const tab_groups::SavedTabGroup& tab_group) override;
void OnTabGroupClosed(const tab_groups::SavedTabGroup& tab_group) override;
void OnDataSharingChangeNotifierInitialized() override;
void OnGroupMemberAdded(const data_sharing::GroupData& group_data,
const GaiaId& member_gaia_id,
const base::Time& event_time) override;
void OnGroupMemberRemoved(const data_sharing::GroupData& group_data,
const GaiaId& member_gaia_id,
const base::Time& event_time) override;
static std::u16string GetTruncatedTabTitleForTesting(
const std::u16string& original_title);
private:
void OnStoreInitialized(bool success);
void ClearDirtyTabMessagesForGroup(
const data_sharing::GroupId& collaboration_group_id,
const std::optional<tab_groups::SavedTabGroup>& tab_group);
std::optional<std::string> GetDisplayNameForUserInGroup(
const data_sharing::GroupId& group_id,
const GaiaId& gaia_id);
std::optional<ActivityLogItem> ConvertMessageToActivityLogItem(
const collaboration_pb::Message& message,
bool is_tab_activity);
std::optional<data_sharing::GroupId> GetCollaborationGroupIdForTab(
const tab_groups::SavedTabGroupTab& tab);
TabGroupMessageMetadata CreateTabGroupMessageMetadataFromCollaborationId(
const collaboration_pb::Message& message,
std::optional<tab_groups::SavedTabGroup> tab_group,
std::optional<data_sharing::GroupId> collaboration_group_id);
TabGroupMessageMetadata CreateTabGroupMessageMetadataFromMessageOrTabGroup(
const collaboration_pb::Message& message,
const std::optional<tab_groups::SavedTabGroup>& tab_group);
std::optional<tab_groups::SavedTabGroup> GetTabGroupFromCollaborationId(
const std::string& collaboration_id);
std::optional<data_sharing::GroupMember> GetGroupMemberFromGaiaId(
const data_sharing::GroupId& collaboration_group_id,
std::optional<GaiaId> gaia_id);
std::optional<data_sharing::GroupId> GetCollaborationGroupId(
tab_groups::EitherGroupID group_id);
std::optional<tab_groups::SavedTabGroupTab> GetTabFromTabId(
tab_groups::EitherTabID tab_id);
std::vector<PersistentMessage> ConvertMessagesToPersistentMessages(
const std::vector<collaboration_pb::Message>& messages,
DirtyType lookup_dirty_type,
PersistentNotificationType type);
std::vector<PersistentMessage> ConvertMessageToPersistentMessages(
const collaboration_pb::Message& message,
DirtyType lookup_dirty_type,
PersistentNotificationType type,
bool allow_dirty_tab_group_message);
PersistentMessage CreatePersistentMessage(
const collaboration_pb::Message& message,
const std::optional<tab_groups::SavedTabGroup>& tab_group,
const std::optional<tab_groups::SavedTabGroupTab>& tab,
PersistentNotificationType type);
InstantMessage CreateInstantMessage(
const collaboration_pb::Message& message,
const std::optional<tab_groups::SavedTabGroup>& tab_group,
const std::optional<tab_groups::SavedTabGroupTab>& tab);
void NotifyDisplayPersistentMessagesForTypes(
const PersistentMessage& base_message,
const std::vector<PersistentNotificationType>& types);
void NotifyHidePersistentMessagesForTypes(
const PersistentMessage& base_message,
const std::vector<PersistentNotificationType>& types);
void DisplayOrHideTabGroupDirtyDotForTabGroup(
const data_sharing::GroupId& collaboration_group_id,
base::Uuid shared_tab_group_id);
MessageAttribution CreateMessageAttributionForTabUpdates(
const collaboration_pb::Message& message,
const std::optional<tab_groups::SavedTabGroup>& tab_group,
const std::optional<tab_groups::SavedTabGroupTab>& tab);
void DisplayInstantMessage(
const base::Uuid& db_message_uuid,
const InstantMessage& base_message,
const std::vector<InstantNotificationLevel>& levels);
void ClearMessageDirtyBit(base::Uuid db_message_id, bool success);
PersistentMessage CreatePersistentMessageFromTabGroupAndTab(
const data_sharing::GroupId& collaboration_group_id,
const tab_groups::SavedTabGroupTab tab,
CollaborationEvent collaboration_event);
MessagingBackendConfiguration configuration_;
std::unique_ptr<TabGroupChangeNotifier> tab_group_change_notifier_;
std::unique_ptr<DataSharingChangeNotifier> data_sharing_change_notifier_;
std::unique_ptr<MessagingBackendStore> store_;
base::ScopedObservation<TabGroupChangeNotifier,
TabGroupChangeNotifier::Observer>
tab_group_change_notifier_observer_{this};
base::ScopedObservation<DataSharingChangeNotifier,
DataSharingChangeNotifier::Observer>
data_sharing_change_notifier_observer_{this};
bool initialized_ = false;
DataSharingChangeNotifier::FlushCallback data_sharing_flush_callback_;
std::unique_ptr<InstantMessageProcessor> instant_message_processor_;
raw_ptr<tab_groups::TabGroupSyncService> tab_group_sync_service_;
raw_ptr<data_sharing::DataSharingService> data_sharing_service_;
raw_ptr<signin::IdentityManager> identity_manager_;
base::ObserverList<PersistentMessageObserver> persistent_message_observers_;
std::unordered_map<data_sharing::GroupId, const std::vector<ActivityLogItem>&>
activity_log_for_testing_;
base::WeakPtrFactory<MessagingBackendServiceImpl> weak_ptr_factory_{this};
};
}
#endif