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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/collaboration/internal/messaging/instant_message_processor_impl.h"
#include <optional>
#include <set>
#include <unordered_map>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/hash.h"
#include "base/strings/to_string.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "components/collaboration/public/messaging/message.h"
#include "components/collaboration/public/messaging/messaging_backend_service.h"
#include "components/data_sharing/public/group_data.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace collaboration::messaging {
struct MessageAggregationKey {
InstantNotificationLevel level;
CollaborationEvent event;
std::optional<data_sharing::GroupId> collaboration_id;
bool operator==(const MessageAggregationKey& other) const {
return level == other.level && event == other.event &&
collaboration_id == other.collaboration_id;
}
};
struct MessageAggregationKeyHash {
MessageAggregationKeyHash() = default;
~MessageAggregationKeyHash() = default;
size_t operator()(const MessageAggregationKey& k) const {
size_t hash = 0;
return base::HashCombine(hash, k.level, k.event, k.collaboration_id);
}
};
} // namespace collaboration::messaging
namespace collaboration::messaging {
namespace {
constexpr base::TimeDelta kQueueInterval = base::Seconds(1);
bool ShouldAggregate(const InstantMessage& message) {
switch (message.collaboration_event) {
case CollaborationEvent::TAB_UPDATED:
case CollaborationEvent::TAB_REMOVED:
return false;
case CollaborationEvent::TAB_GROUP_REMOVED:
case CollaborationEvent::COLLABORATION_MEMBER_ADDED:
return true;
case CollaborationEvent::UNDEFINED:
case CollaborationEvent::TAB_ADDED:
case CollaborationEvent::TAB_GROUP_ADDED:
case CollaborationEvent::TAB_GROUP_NAME_UPDATED:
case CollaborationEvent::TAB_GROUP_COLOR_UPDATED:
case CollaborationEvent::COLLABORATION_ADDED:
case CollaborationEvent::COLLABORATION_REMOVED:
case CollaborationEvent::COLLABORATION_MEMBER_REMOVED:
CHECK(false) << "Unexpected event in instant message "
<< static_cast<int>(message.collaboration_event);
}
}
// All key constructions should happen via this method.
MessageAggregationKey CreateMessageAggregationKey(
const InstantMessage& message) {
CHECK(ShouldAggregate(message));
MessageAggregationKey key;
key.level = message.level;
key.event = message.collaboration_event;
if (message.collaboration_event ==
CollaborationEvent::COLLABORATION_MEMBER_ADDED) {
key.collaboration_id = message.attributions[0].collaboration_id;
}
return key;
}
} // namespace
InstantMessageProcessorImpl::InstantMessageProcessorImpl() = default;
InstantMessageProcessorImpl::~InstantMessageProcessorImpl() = default;
void InstantMessageProcessorImpl::SetMessagingBackendService(
MessagingBackendService* messaging_backend_service) {
CHECK(!messaging_backend_service_);
CHECK(messaging_backend_service);
messaging_backend_service_ = messaging_backend_service;
}
void InstantMessageProcessorImpl::SetInstantMessageDelegate(
InstantMessageDelegate* instant_message_delegate) {
// We must be either setting a delegate where there was none before or
// we should be resetting a non-null delegate.
CHECK((instant_message_delegate_ == nullptr &&
instant_message_delegate != nullptr) ||
(instant_message_delegate_ != nullptr &&
instant_message_delegate == nullptr));
instant_message_delegate_ = instant_message_delegate;
}
bool InstantMessageProcessorImpl::IsEnabled() const {
return !!instant_message_delegate_;
}
void InstantMessageProcessorImpl::DisplayInstantMessage(
const InstantMessage& message) {
message_queue_.push_back(message);
ScheduleProcessing();
}
void InstantMessageProcessorImpl::HideInstantMessage(
const std::set<base::Uuid>& message_ids) {
// Remove the messages from the queue that have MessageAttribution IDs that
// match.
message_queue_.erase(
std::remove_if(message_queue_.begin(), message_queue_.end(),
[&message_ids](const InstantMessage& message) {
CHECK(IsSingleMessage(message));
std::optional<base::Uuid> current_message_id =
message.attributions[0].id;
if (!current_message_id.has_value()) {
return false;
}
return base::Contains(message_ids, *current_message_id);
}),
message_queue_.end());
// In case the message was displayed previously, we still tell the UI about
// every message ID, even if it might have been removed from the queue.
instant_message_delegate_->HideInstantaneousMessage(message_ids);
}
void InstantMessageProcessorImpl::ScheduleProcessing() {
if (processing_scheduled_) {
return;
}
processing_scheduled_ = true;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&InstantMessageProcessorImpl::ProcessQueue,
weak_ptr_factory_.GetWeakPtr()),
kQueueInterval);
}
void InstantMessageProcessorImpl::ProcessQueue() {
processing_scheduled_ = false;
if (message_queue_.empty()) {
// The queue might have been cleared by HideInstantMessage.
return;
}
std::vector<InstantMessage> aggregated_messages =
AggregateMessages(message_queue_);
message_queue_.clear();
for (const auto& message : aggregated_messages) {
std::vector<base::Uuid> message_ids;
for (const auto& attribution : message.attributions) {
CHECK(attribution.id.has_value());
message_ids.emplace_back(attribution.id.value());
}
instant_message_delegate_->DisplayInstantaneousMessage(
message,
base::BindOnce(&InstantMessageProcessorImpl::OnMessageDisplayed,
weak_ptr_factory_.GetWeakPtr(), message_ids));
}
}
std::vector<InstantMessage> InstantMessageProcessorImpl::AggregateMessages(
const std::vector<InstantMessage>& messages) {
std::unordered_map<MessageAggregationKey, std::vector<const InstantMessage*>,
MessageAggregationKeyHash>
aggregation_map;
std::vector<InstantMessage> aggregated_messages;
std::vector<InstantMessage> non_aggregated_messages;
// Populate the aggregation map.
for (const auto& message : messages) {
CHECK(IsSingleMessage(message));
if (!ShouldAggregate(message)) {
non_aggregated_messages.emplace_back(message);
continue;
}
MessageAggregationKey key = CreateMessageAggregationKey(message);
aggregation_map[key].emplace_back(&message);
}
// Process the aggregation map.
for (const auto& [key, message_ptrs] : aggregation_map) {
if (message_ptrs.size() == 1) {
non_aggregated_messages.push_back(*message_ptrs[0]);
} else {
// Aggregated message case.
std::vector<InstantMessage> aggregated_msgs_temp;
for (const auto* message_ptr : message_ptrs) {
aggregated_msgs_temp.push_back(*message_ptr);
}
aggregated_messages.push_back(
CreateAggregatedMessage(aggregated_msgs_temp));
}
}
// Combine with non-aggregated messages as a single list and return.
aggregated_messages.insert(aggregated_messages.end(),
non_aggregated_messages.begin(),
non_aggregated_messages.end());
return aggregated_messages;
}
InstantMessage InstantMessageProcessorImpl::CreateAggregatedMessage(
const std::vector<InstantMessage>& messages) {
CHECK(messages.size() > 1u);
InstantMessage aggregated_message;
aggregated_message.collaboration_event = messages[0].collaboration_event;
aggregated_message.level = messages[0].level;
aggregated_message.type = messages[0].type;
// Populate message attributions for the aggregated message.
for (const auto& message : messages) {
CHECK(IsSingleMessage(message));
aggregated_message.attributions.emplace_back(message.attributions[0]);
}
// Create message string for aggregation based on message type.
switch (messages[0].collaboration_event) {
case CollaborationEvent::COLLABORATION_MEMBER_ADDED: {
std::string group_name =
messages[0]
.attributions[0]
.tab_group_metadata->last_known_title.value();
aggregated_message.localized_message = l10n_util::GetStringFUTF16(
IDS_DATA_SHARING_TOAST_NEW_MEMBER_MULTIPLE_MEMBERS,
base::UTF8ToUTF16(base::ToString(messages.size())),
base::UTF8ToUTF16(group_name));
break;
}
case CollaborationEvent::TAB_GROUP_REMOVED: {
aggregated_message.localized_message = l10n_util::GetStringFUTF16(
IDS_DATA_SHARING_TOAST_BLOCK_LEAVE_MULTIPLE_GROUPS,
base::UTF8ToUTF16(base::ToString(messages.size())));
break;
}
default:
CHECK(false);
}
return aggregated_message;
}
void InstantMessageProcessorImpl::OnMessageDisplayed(
const std::vector<base::Uuid>& message_ids,
bool success) {
if (!success) {
return;
}
for (const base::Uuid& message_id : message_ids) {
messaging_backend_service_->ClearPersistentMessage(
message_id, PersistentNotificationType::INSTANT_MESSAGE);
}
}
} // namespace collaboration::messaging
|