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
|
// 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.
#include "components/collaboration/internal/android/messaging/messaging_backend_service_bridge.h"
#include <memory>
#include <optional>
#include <set>
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/uuid.h"
#include "components/collaboration/internal/android/messaging/conversion_utils.h"
#include "components/collaboration/public/messaging/activity_log.h"
#include "components/collaboration/public/messaging/message.h"
#include "components/saved_tab_groups/public/android/tab_group_sync_conversions_bridge.h"
#include "components/saved_tab_groups/public/android/tab_group_sync_conversions_utils.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/collaboration/internal/messaging_jni_headers/MessagingBackendServiceBridge_jni.h"
using base::android::ConvertJavaStringToUTF16;
namespace collaboration::messaging::android {
namespace {
const char kMessagingBackendServiceBridgeUserDataKey[] =
"messaging_backend_service";
constexpr int32_t kInvalidTabId = -1;
} // namespace
// static
base::android::ScopedJavaLocalRef<jobject>
MessagingBackendServiceBridge::GetBridgeForMessagingBackendService(
MessagingBackendService* service) {
if (!service->GetUserData(kMessagingBackendServiceBridgeUserDataKey)) {
service->SetUserData(
kMessagingBackendServiceBridgeUserDataKey,
base::WrapUnique(new MessagingBackendServiceBridge(service)));
}
MessagingBackendServiceBridge* bridge =
static_cast<MessagingBackendServiceBridge*>(
service->GetUserData(kMessagingBackendServiceBridgeUserDataKey));
return bridge->GetJavaObject();
}
// static
std::unique_ptr<MessagingBackendServiceBridge>
MessagingBackendServiceBridge::CreateForTest(MessagingBackendService* service) {
MessagingBackendServiceBridge* bridge =
new MessagingBackendServiceBridge(service);
return base::WrapUnique(bridge);
}
MessagingBackendServiceBridge::MessagingBackendServiceBridge(
MessagingBackendService* service)
: service_(service) {
java_ref_.Reset(Java_MessagingBackendServiceBridge_create(
base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this)));
service_->AddPersistentMessageObserver(this);
service_->SetInstantMessageDelegate(this);
}
MessagingBackendServiceBridge::~MessagingBackendServiceBridge() {
service_->SetInstantMessageDelegate(nullptr);
service_->RemovePersistentMessageObserver(this);
Java_MessagingBackendServiceBridge_onNativeDestroyed(
base::android::AttachCurrentThread(), java_ref_);
}
base::android::ScopedJavaLocalRef<jobject>
MessagingBackendServiceBridge::GetJavaObject() {
return base::android::ScopedJavaLocalRef<jobject>(java_ref_);
}
bool MessagingBackendServiceBridge::IsInitialized(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller) {
return service_->IsInitialized();
}
base::android::ScopedJavaLocalRef<jobject>
MessagingBackendServiceBridge::GetMessagesForTab(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
jint j_local_tab_id,
const base::android::JavaParamRef<jstring>& j_sync_tab_id,
jint j_type) {
auto type = static_cast<PersistentNotificationType>(j_type);
std::optional<PersistentNotificationType> type_opt = std::make_optional(type);
if (type == PersistentNotificationType::UNDEFINED) {
type_opt = std::nullopt;
}
if (j_local_tab_id != kInvalidTabId) {
CHECK(!j_sync_tab_id);
tab_groups::LocalTabID tab_id = tab_groups::FromJavaTabId(j_local_tab_id);
auto messages = service_->GetMessagesForTab(tab_id, type_opt);
return PersistentMessagesToJava(env, messages);
}
if (j_sync_tab_id) {
CHECK(j_local_tab_id == kInvalidTabId);
std::string sync_tab_id_str =
base::android::ConvertJavaStringToUTF8(env, j_sync_tab_id);
auto tab_id = base::Uuid::ParseLowercase(sync_tab_id_str);
auto messages = service_->GetMessagesForTab(tab_id, type_opt);
return PersistentMessagesToJava(env, messages);
}
NOTREACHED();
}
base::android::ScopedJavaLocalRef<jobject>
MessagingBackendServiceBridge::GetMessagesForGroup(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
const base::android::JavaParamRef<jobject>& j_local_group_id,
const base::android::JavaParamRef<jstring>& j_sync_group_id,
jint j_type) {
auto type = static_cast<PersistentNotificationType>(j_type);
std::optional<PersistentNotificationType> type_opt = std::make_optional(type);
if (type == PersistentNotificationType::UNDEFINED) {
type_opt = std::nullopt;
}
if (j_local_group_id) {
CHECK(!j_sync_group_id);
auto group_id =
tab_groups::TabGroupSyncConversionsBridge::FromJavaTabGroupId(
env, j_local_group_id);
auto messages = service_->GetMessagesForGroup(group_id, type_opt);
return PersistentMessagesToJava(env, messages);
}
if (j_sync_group_id) {
CHECK(!j_local_group_id);
std::string sync_group_id_str =
base::android::ConvertJavaStringToUTF8(env, j_sync_group_id);
auto group_id = base::Uuid::ParseLowercase(sync_group_id_str);
auto messages = service_->GetMessagesForGroup(group_id, type_opt);
return PersistentMessagesToJava(env, messages);
}
NOTREACHED();
}
base::android::ScopedJavaLocalRef<jobject>
MessagingBackendServiceBridge::GetMessages(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
jint j_type) {
auto type = static_cast<PersistentNotificationType>(j_type);
std::optional<PersistentNotificationType> type_opt = std::make_optional(type);
if (type == PersistentNotificationType::UNDEFINED) {
type_opt = std::nullopt;
}
auto messages = service_->GetMessages(type_opt);
return PersistentMessagesToJava(env, messages);
}
base::android::ScopedJavaLocalRef<jobject>
MessagingBackendServiceBridge::GetActivityLog(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
jstring j_collaboration_id) {
ActivityLogQueryParams query_params;
query_params.collaboration_id = data_sharing::GroupId(
base::android::ConvertJavaStringToUTF8(env, j_collaboration_id));
auto activity_log_items = service_->GetActivityLog(query_params);
return ActivityLogItemsToJava(env, activity_log_items);
}
void MessagingBackendServiceBridge::ClearDirtyTabMessagesForGroup(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
const base::android::JavaParamRef<jstring>& j_collaboration_id) {
auto collaboration_id = data_sharing::GroupId(
base::android::ConvertJavaStringToUTF8(env, j_collaboration_id));
service_->ClearDirtyTabMessagesForGroup(collaboration_id);
}
void MessagingBackendServiceBridge::ClearPersistentMessage(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
const base::android::JavaParamRef<jstring>& j_message_id,
jint j_type) {
CHECK(j_message_id);
auto message_id = base::Uuid::ParseLowercase(
base::android::ConvertJavaStringToUTF8(env, j_message_id));
auto type = static_cast<PersistentNotificationType>(j_type);
std::optional<PersistentNotificationType> type_opt = std::make_optional(type);
if (type == PersistentNotificationType::UNDEFINED) {
type_opt = std::nullopt;
}
service_->ClearPersistentMessage(message_id, type_opt);
}
void MessagingBackendServiceBridge::RunInstantaneousMessageSuccessCallback(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_caller,
jlong j_callback,
jboolean j_result) {
CHECK(j_callback);
std::unique_ptr<
MessagingBackendService::InstantMessageDelegate::SuccessCallback>
callback_ptr(
reinterpret_cast<MessagingBackendService::InstantMessageDelegate::
SuccessCallback*>(j_callback));
std::move(*callback_ptr).Run(j_result);
}
void MessagingBackendServiceBridge::OnMessagingBackendServiceInitialized() {
if (java_ref_.is_null()) {
return;
}
Java_MessagingBackendServiceBridge_onMessagingBackendServiceInitialized(
base::android::AttachCurrentThread(), java_ref_);
}
void MessagingBackendServiceBridge::DisplayPersistentMessage(
PersistentMessage message) {
if (java_ref_.is_null()) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_MessagingBackendServiceBridge_displayPersistentMessage(
env, java_ref_, PersistentMessageToJava(env, message));
}
void MessagingBackendServiceBridge::HidePersistentMessage(
PersistentMessage message) {
if (java_ref_.is_null()) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_MessagingBackendServiceBridge_hidePersistentMessage(
env, java_ref_, PersistentMessageToJava(env, message));
}
void MessagingBackendServiceBridge::DisplayInstantaneousMessage(
InstantMessage message,
InstantMessageDelegate::SuccessCallback success_callback) {
if (java_ref_.is_null()) {
// We definitely failed to display the message.
std::move(success_callback).Run(false);
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
// We expect Java to always call us back through
// MessagingBackendServiceBridge::RunInstantaneousMessageSuccessCallback,
// Copy |callback| on the heap to pass the pointer through JNI. This callback
// will be deleted when it's run.
CHECK(!success_callback.is_null());
jlong j_native_ptr = reinterpret_cast<jlong>(
new MessagingBackendService::InstantMessageDelegate::SuccessCallback(
std::move(success_callback)));
Java_MessagingBackendServiceBridge_displayInstantaneousMessage(
env, java_ref_, InstantMessageToJava(env, message), j_native_ptr);
}
void MessagingBackendServiceBridge::HideInstantaneousMessage(
const std::set<base::Uuid>& message_ids) {
if (java_ref_.is_null()) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_MessagingBackendServiceBridge_hideInstantaneousMessage(
env, java_ref_, UuidSetToJavaStringSet(env, message_ids));
}
} // namespace collaboration::messaging::android
|