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
|
// 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 "chrome/browser/ui/android/tab_model/tab_collection_tab_model_impl.h"
#include <algorithm>
#include <optional>
#include <utility>
#include "base/android/jni_android.h"
#include "base/android/token_android.h"
#include "base/numerics/safe_conversions.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/android/tab_interface_android.h"
#include "chrome/browser/profiles/profile.h"
#include "components/tabs/public/tab_group.h"
#include "components/tabs/public/tab_group_tab_collection.h"
#include "components/tabs/public/tab_interface.h"
#include "components/tabs/public/tab_strip_collection.h"
#include "tab_collection_tab_model_impl.h"
#include "ui/gfx/range/range.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
// This JNI header is generated from TabCollectionTabModelImpl.java.
#include "chrome/android/chrome_jni_headers/TabCollectionTabModelImpl_jni.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
using base::android::TokenAndroid;
using tab_groups::TabGroupId;
namespace tabs {
namespace {
constexpr int kInvalidTabIndex = -1;
// Converts the `tab_android` to a `unique_ptr<TabInterface>`. Under the hood we
// use a wrapper class `TabInterfaceAndroid` which takes a weak ptr to
// `TabAndroid` to avoid memory management issues.
std::unique_ptr<TabInterface> ToTabInterface(TabAndroid* tab_android) {
return std::make_unique<TabInterfaceAndroid>(tab_android);
}
// Converts the wrapper class TabInterfaceAndroid* to a TabAndroid*. This will
// crash if the `tab_interface` has outlived the TabAndroid*.
TabAndroid* ToTabAndroid(TabInterface* tab_interface) {
auto weak_tab_android =
static_cast<TabInterfaceAndroid*>(tab_interface)->GetWeakPtr();
CHECK(weak_tab_android);
return static_cast<TabAndroid*>(weak_tab_android.get());
}
} // namespace
TabCollectionTabModelImpl::TabCollectionTabModelImpl(
JNIEnv* env,
const jni_zero::JavaRef<jobject>& java_object,
Profile* profile)
: java_object_(env, java_object),
profile_(profile),
tab_strip_collection_(std::make_unique<TabStripCollection>()) {}
TabCollectionTabModelImpl::~TabCollectionTabModelImpl() = default;
void TabCollectionTabModelImpl::Destroy(JNIEnv* env) {
delete this;
}
int TabCollectionTabModelImpl::GetTabCountRecursive(JNIEnv* env) const {
return base::checked_cast<int>(tab_strip_collection_->TabCountRecursive());
}
int TabCollectionTabModelImpl::GetIndexOfTabRecursive(
JNIEnv* env,
const JavaParamRef<jobject>& j_tab_android) const {
TabAndroid* target_tab = TabAndroid::GetNativeTab(env, j_tab_android);
if (!target_tab) {
return kInvalidTabIndex;
}
int current_index = 0;
for (TabInterface* tab_in_collection : *tab_strip_collection_) {
if (ToTabAndroid(tab_in_collection) == target_tab) {
return current_index;
}
current_index++;
}
return kInvalidTabIndex;
}
ScopedJavaLocalRef<jobject> TabCollectionTabModelImpl::GetTabAtIndexRecursive(
JNIEnv* env,
size_t index) const {
if (index >= tab_strip_collection_->TabCountRecursive()) {
return ScopedJavaLocalRef<jobject>();
}
TabInterface* tab = tab_strip_collection_->GetTabAtIndexRecursive(index);
TabAndroid* tab_android = ToTabAndroid(tab);
return tab_android->GetJavaObject();
}
void TabCollectionTabModelImpl::AddTabRecursive(
JNIEnv* env,
const JavaParamRef<jobject>& j_tab_android,
size_t index,
const JavaParamRef<jobject>& j_tab_group_id,
bool is_pinned) {
TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab_android);
CHECK(tab_android);
std::optional<TabGroupId> tab_group_id;
if (j_tab_group_id) {
tab_group_id = TabGroupId::FromRawToken(
TokenAndroid::FromJavaToken(env, j_tab_group_id));
}
index = GetSafeIndex(index, tab_group_id, is_pinned);
auto tab_interface_android = ToTabInterface(tab_android);
tab_strip_collection_->AddTabRecursive(std::move(tab_interface_android),
index, tab_group_id, is_pinned);
}
size_t TabCollectionTabModelImpl::GetSafeIndex(
size_t proposed_index,
const std::optional<TabGroupId>& tab_group_id,
bool is_pinned) const {
size_t first_non_pinned_index =
tab_strip_collection_->IndexOfFirstNonPinnedTab();
if (is_pinned) {
return std::min(proposed_index, first_non_pinned_index);
}
size_t tab_count = tab_strip_collection_->TabCountRecursive();
size_t clamped_index =
std::clamp(proposed_index, first_non_pinned_index, tab_count);
if (tab_group_id) {
TabGroupTabCollection* group_collection =
tab_strip_collection_->GetTabGroupCollection(*tab_group_id);
if (group_collection) {
gfx::Range range = group_collection->GetTabGroup()->ListTabs();
if (!range.is_empty()) {
return std::clamp(proposed_index, range.start(), range.end());
}
}
}
// Always safe since these are the edges.
if (clamped_index == first_non_pinned_index || clamped_index == tab_count) {
return clamped_index;
}
std::optional<TabGroupId> group_at_index = GetGroupIdAt(clamped_index);
if (group_at_index && group_at_index == GetGroupIdAt(clamped_index - 1)) {
// Insertion will happen inside a tab group we need to push it out.
TabGroupTabCollection* group_collection =
tab_strip_collection_->GetTabGroupCollection(*group_at_index);
gfx::Range range = group_collection->GetTabGroup()->ListTabs();
// Push to the nearest boundary.
if (clamped_index - range.start() < range.end() - clamped_index) {
return range.start();
} else {
return range.end();
}
}
return clamped_index;
}
std::optional<TabGroupId> TabCollectionTabModelImpl::GetGroupIdAt(
size_t index) const {
if (index < tab_strip_collection_->TabCountRecursive()) {
return tab_strip_collection_->GetTabAtIndexRecursive(index)->GetGroup();
} else {
return std::nullopt;
}
}
static jlong JNI_TabCollectionTabModelImpl_Init(
JNIEnv* env,
const JavaParamRef<jobject>& j_java_object,
const JavaParamRef<jobject>& j_profile) {
Profile* profile = Profile::FromJavaObject(j_profile);
TabCollectionTabModelImpl* tab_collection_tab_model_impl =
new TabCollectionTabModelImpl(env, j_java_object, profile);
return reinterpret_cast<intptr_t>(tab_collection_tab_model_impl);
}
} // namespace tabs
|