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
|
// 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_ANDROID_TAB_GROUP_SYNC_SERVICE_ANDROID_H_
#define COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_ANDROID_TAB_GROUP_SYNC_SERVICE_ANDROID_H_
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "components/saved_tab_groups/internal/android/versioning_message_controller_android.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace tab_groups {
// Helper class responsible for bridging the TabGroupSyncService between
// C++ and Java.
class TabGroupSyncServiceAndroid : public base::SupportsUserData::Data,
public TabGroupSyncService::Observer {
public:
explicit TabGroupSyncServiceAndroid(TabGroupSyncService* service);
~TabGroupSyncServiceAndroid() override;
ScopedJavaLocalRef<jobject> GetJavaObject();
// TabGroupSyncService::Observer overrides.
void OnInitialized() override;
void OnTabGroupAdded(const SavedTabGroup& group,
TriggerSource source) override;
void OnTabGroupUpdated(const SavedTabGroup& group,
TriggerSource source) override;
void OnTabGroupRemoved(const LocalTabGroupID& local_id,
TriggerSource source) override;
void OnTabGroupRemoved(const base::Uuid& sync_id,
TriggerSource source) override;
void OnTabGroupLocalIdChanged(
const base::Uuid& sync_id,
const std::optional<LocalTabGroupID>& local_id) override;
// Mutation methods (Java -> native).
// Mutator methods that result in group metadata mutation.
void AddGroup(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_saved_tab_group);
void RemoveGroupByLocalId(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_local_group_id);
void RemoveGroupBySyncId(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_sync_group_id);
void UpdateVisualData(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
const JavaParamRef<jstring>& j_title,
jint j_color);
void MakeTabGroupShared(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
const JavaParamRef<jstring>& j_collaboration_id,
const JavaParamRef<jobject>& j_boolean_callback);
void AboutToUnShareTabGroup(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
const JavaParamRef<jobject>& j_callback);
void OnTabGroupUnShareComplete(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
const jboolean j_success);
// Mutator methods that result in tab metadata mutation.
void AddTab(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
jint j_tab_id,
const JavaParamRef<jstring>& j_title,
const JavaParamRef<jobject>& j_url,
jint j_position);
void UpdateTab(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
jint j_tab_id,
const JavaParamRef<jstring>& j_title,
const JavaParamRef<jobject>& j_url,
jint j_position);
void RemoveTab(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
jint j_tab_id);
void MoveTab(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
jint j_tab_id,
int j_new_index_in_group);
void SetTabSelected(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
jint j_tab_id,
const JavaParamRef<jstring>& j_tab_title);
// Accessor methods.
ScopedJavaLocalRef<jobjectArray> GetAllGroupIds(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller);
ScopedJavaLocalRef<jobject> GetGroupBySyncGroupId(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_sync_group_id);
ScopedJavaLocalRef<jobject> GetGroupByLocalGroupId(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id);
ScopedJavaLocalRef<jobjectArray> GetDeletedGroupIds(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller);
// Book-keeping methods to maintain in-memory mapping of sync and local IDs.
void UpdateLocalTabGroupMapping(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_sync_id,
const JavaParamRef<jobject>& j_local_id,
jint j_opening_source);
void RemoveLocalTabGroupMapping(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_local_id,
jint j_closing_source);
void UpdateLocalTabId(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jobject>& j_group_id,
const JavaParamRef<jstring>& j_sync_tab_id,
jint j_local_tab_id);
bool WasTabGroupClosedLocally(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_sync_tab_group_id);
// Helper methods for attributions.
bool IsRemoteDevice(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_sync_cache_guid);
void RecordTabGroupEvent(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
jint j_event_type,
const JavaParamRef<jobject>& j_local_group_id,
jint j_local_tab_id,
jint j_opening_source,
jint j_closing_source);
void UpdateArchivalStatus(JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_sync_group_id,
const jboolean j_archival_status);
ScopedJavaLocalRef<jobject> GetVersioningMessageController(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller);
void SetCollaborationAvailableInFinderForTesting(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_collaboration_id);
private:
// A reference to the Java counterpart of this class. See
// TabGroupSyncServiceImpl.java.
ScopedJavaGlobalRef<jobject> java_obj_;
// Owns the JNI bridge for VersioningMessageController.
std::unique_ptr<VersioningMessageControllerAndroid>
versioning_messaging_controller_android_;
// Not owned. This is safe because the JNI bridge is destroyed and the
// native pointer in Java is cleared whenever TabGroupSyncService is
// destroyed. Hence there will be no subsequent unsafe calls to native.
raw_ptr<TabGroupSyncService> tab_group_sync_service_;
};
} // namespace tab_groups
#endif // COMPONENTS_SAVED_TAB_GROUPS_INTERNAL_ANDROID_TAB_GROUP_SYNC_SERVICE_ANDROID_H_
|