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 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
// Copyright 2022 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/toolbar/adaptive_toolbar_bridge.h"
#include "base/android/callback_android.h"
#include "base/android/jni_array.h"
#include "base/android/scoped_java_ref.h"
#include "base/no_destructor.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/segmentation_platform/segmentation_platform_service_factory.h"
#include "chrome/browser/ui/android/toolbar/adaptive_toolbar_enums.h"
#include "components/segmentation_platform/public/android/segmentation_platform_conversion_bridge.h"
#include "components/segmentation_platform/public/constants.h"
#include "components/segmentation_platform/public/features.h"
#include "components/segmentation_platform/public/input_context.h"
#include "components/segmentation_platform/public/segmentation_platform_service.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/ui/android/toolbar/jni_headers/AdaptiveToolbarBridge_jni.h"
using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
using segmentation_platform::InputContext;
namespace {
std::map<std::string, AdaptiveToolbarButtonVariant> GetEnumLabelMapping() {
static base::NoDestructor<std::map<std::string, AdaptiveToolbarButtonVariant>>
enum_label_mapping(
{{
segmentation_platform::kAdaptiveToolbarModelLabelNewTab,
AdaptiveToolbarButtonVariant::kNewTab,
},
{
segmentation_platform::kAdaptiveToolbarModelLabelShare,
AdaptiveToolbarButtonVariant::kShare,
},
{
segmentation_platform::kAdaptiveToolbarModelLabelVoice,
AdaptiveToolbarButtonVariant::kVoice,
},
{
segmentation_platform::kAdaptiveToolbarModelLabelTranslate,
AdaptiveToolbarButtonVariant::kTranslate,
},
{
segmentation_platform::kAdaptiveToolbarModelLabelAddToBookmarks,
AdaptiveToolbarButtonVariant::kAddToBookmarks,
},
{
segmentation_platform::kAdaptiveToolbarModelLabelReadAloud,
AdaptiveToolbarButtonVariant::kReadAloud,
}});
return *enum_label_mapping;
}
AdaptiveToolbarButtonVariant ActionLabelToAdaptiveToolbarButtonVariant(
const std::string& label) {
std::map<std::string, AdaptiveToolbarButtonVariant> label_enum_mapping =
GetEnumLabelMapping();
if (label_enum_mapping.contains(label)) {
return label_enum_mapping.at(label);
}
return AdaptiveToolbarButtonVariant::kUnknown;
}
void RunGetSelectedSegmentCallback(
const JavaRef<jobject>& j_callback,
const segmentation_platform::SegmentSelectionResult& result) {
AdaptiveToolbarButtonVariant button_variant =
AdaptiveToolbarButtonVariant::kUnknown;
if (result.segment.has_value()) {
switch (result.segment.value()) {
case segmentation_platform::proto::SegmentId::
OPTIMIZATION_TARGET_SEGMENTATION_NEW_TAB:
button_variant = AdaptiveToolbarButtonVariant::kNewTab;
break;
case segmentation_platform::proto::SegmentId::
OPTIMIZATION_TARGET_SEGMENTATION_SHARE:
button_variant = AdaptiveToolbarButtonVariant::kShare;
break;
case segmentation_platform::proto::SegmentId::
OPTIMIZATION_TARGET_SEGMENTATION_VOICE:
button_variant = AdaptiveToolbarButtonVariant::kVoice;
break;
case segmentation_platform::proto::SegmentId::OPTIMIZATION_TARGET_UNKNOWN:
button_variant = AdaptiveToolbarButtonVariant::kUnknown;
break;
default:
NOTREACHED();
}
}
ScopedJavaLocalRef<jobject> j_result =
Java_AdaptiveToolbarBridge_createResult(
base::android::AttachCurrentThread(), result.is_ready,
static_cast<int32_t>(button_variant));
base::android::RunObjectCallbackAndroid(j_callback, j_result);
}
void RunGetClassificationSingleResultCallback(
const base::android::JavaRef<jobject>& j_callback,
const segmentation_platform::ClassificationResult& result) {
std::string button_to_show =
result.ordered_labels.empty() ? "" : result.ordered_labels[0];
bool is_ready =
result.status == segmentation_platform::PredictionStatus::kSucceeded;
int button_variant = static_cast<int32_t>(
ActionLabelToAdaptiveToolbarButtonVariant(button_to_show));
ScopedJavaLocalRef<jobject> j_result =
Java_AdaptiveToolbarBridge_createResult(
base::android::AttachCurrentThread(), is_ready, button_variant);
base::android::RunObjectCallbackAndroid(j_callback, j_result);
}
void RunGetClassificationMultipleResultCallback(
base::OnceCallback<void(bool, std::vector<int>)> callback,
const segmentation_platform::ClassificationResult& result) {
std::vector<int> ranked_buttons;
bool is_ready =
result.status == segmentation_platform::PredictionStatus::kSucceeded;
for (std::string label : result.ordered_labels) {
ranked_buttons.emplace_back(
static_cast<int32_t>(ActionLabelToAdaptiveToolbarButtonVariant(label)));
}
if (ranked_buttons.empty()) {
ranked_buttons.emplace_back(
static_cast<int32_t>(AdaptiveToolbarButtonVariant::kUnknown));
}
std::move(callback).Run(is_ready, ranked_buttons);
}
void RunGetAnnotatedNumericResultCallback(
base::OnceCallback<void(bool, std::vector<int>)> callback,
const segmentation_platform::AnnotatedNumericResult& result) {
bool is_ready =
result.status == segmentation_platform::PredictionStatus::kSucceeded;
std::map<std::string, AdaptiveToolbarButtonVariant> enum_label_mapping =
GetEnumLabelMapping();
// Map that sorts elements with largest first.
std::multimap<float, AdaptiveToolbarButtonVariant, std::greater<>>
sorted_button_scores;
for (std::pair<std::string, AdaptiveToolbarButtonVariant> button :
enum_label_mapping) {
std::optional<float> score_for_button =
result.GetResultForLabel(button.first);
if (score_for_button.has_value()) {
sorted_button_scores.emplace(score_for_button.value(), button.second);
}
}
std::vector<int> sorted_buttons;
for (std::pair<float, AdaptiveToolbarButtonVariant> score_button :
sorted_button_scores) {
sorted_buttons.emplace_back(static_cast<int32_t>(score_button.second));
}
if (sorted_buttons.empty()) {
sorted_buttons.emplace_back(
static_cast<int32_t>(AdaptiveToolbarButtonVariant::kUnknown));
}
std::move(callback).Run(is_ready, sorted_buttons);
}
void RunJavaCallbackWithRankedButtons(
const base::android::JavaRef<jobject>& j_callback,
bool is_ready,
std::vector<int> ranked_buttons) {
ScopedJavaLocalRef<jintArray> java_ranked_buttons =
base::android::ToJavaIntArray(base::android::AttachCurrentThread(),
ranked_buttons);
ScopedJavaLocalRef<jobject> j_result =
Java_AdaptiveToolbarBridge_createResultList(
base::android::AttachCurrentThread(), is_ready, java_ranked_buttons);
base::android::RunObjectCallbackAndroid(j_callback, j_result);
}
} // namespace
void JNI_AdaptiveToolbarBridge_GetRankedSessionVariantButtons(
JNIEnv* env,
Profile* profile,
jboolean j_use_raw_results,
const JavaParamRef<jobject>& j_callback) {
bool use_raw_results = static_cast<bool>(j_use_raw_results);
base::OnceCallback<void(bool, std::vector<int>)> wrapped_callback =
base::BindOnce(&RunJavaCallbackWithRankedButtons,
base::android::ScopedJavaGlobalRef<jobject>(j_callback));
adaptive_toolbar::GetRankedSessionVariantButtons(profile, use_raw_results,
std::move(wrapped_callback));
}
void JNI_AdaptiveToolbarBridge_GetSessionVariantButton(
JNIEnv* env,
Profile* profile,
const JavaParamRef<jobject>& j_callback) {
if (!profile) {
RunGetClassificationSingleResultCallback(
j_callback, segmentation_platform::ClassificationResult(
segmentation_platform::PredictionStatus::kFailed));
return;
}
segmentation_platform::SegmentationPlatformService*
segmentation_platform_service = segmentation_platform::
SegmentationPlatformServiceFactory::GetForProfile(profile);
if (!segmentation_platform_service) {
RunGetClassificationSingleResultCallback(
j_callback, segmentation_platform::ClassificationResult(
segmentation_platform::PredictionStatus::kFailed));
return;
}
bool use_multi_output = base::FeatureList::IsEnabled(
segmentation_platform::features::
kSegmentationPlatformAdaptiveToolbarV2Feature);
if (use_multi_output) {
segmentation_platform_service->GetClassificationResult(
segmentation_platform::kAdaptiveToolbarSegmentationKey,
segmentation_platform::PredictionOptions(),
base::MakeRefCounted<segmentation_platform::InputContext>(),
base::BindOnce(
&RunGetClassificationSingleResultCallback,
base::android::ScopedJavaGlobalRef<jobject>(j_callback)));
} else {
segmentation_platform_service->GetSelectedSegment(
segmentation_platform::kAdaptiveToolbarSegmentationKey,
base::BindOnce(
&RunGetSelectedSegmentCallback,
base::android::ScopedJavaGlobalRef<jobject>(j_callback)));
}
}
namespace adaptive_toolbar {
// This method retrieves a list of toolbar buttons ranked by priority, only one
// button can be shown, but we return a list so we can try other options in case
// the top one is not available in the current UI (e.g. tablets already have a
// bookmark button, so we don't show it here).
// This list is retrieved from segmentation platform, which:
// 1) Runs an ML model which returns a score for each button.
// 2) Applies thresholds to filter out buttons with low scores.
// 3) Returns a list sorted by score.
// The current model's low score thresholds are set to use new tab as the
// default option, so the other ones get often filtered out, this is a problem
// in tablets because that button is not supported. The |use_raw_results| option
// uses a segmentation platform API that skips step 2, so we can use the
// unfiltered scores on tablets.
void GetRankedSessionVariantButtons(
Profile* profile,
bool use_raw_results,
base::OnceCallback<void(bool, std::vector<int>)> callback) {
if (!profile) {
std::move(callback).Run(false, std::vector<int>());
return;
}
segmentation_platform::SegmentationPlatformService*
segmentation_platform_service = segmentation_platform::
SegmentationPlatformServiceFactory::GetForProfile(profile);
if (!segmentation_platform_service) {
std::move(callback).Run(false, std::vector<int>());
return;
}
if (use_raw_results) {
segmentation_platform_service->GetAnnotatedNumericResult(
segmentation_platform::kAdaptiveToolbarSegmentationKey,
segmentation_platform::PredictionOptions(),
base::MakeRefCounted<segmentation_platform::InputContext>(),
base::BindOnce(&RunGetAnnotatedNumericResultCallback,
std::move(callback)));
} else {
segmentation_platform_service->GetClassificationResult(
segmentation_platform::kAdaptiveToolbarSegmentationKey,
segmentation_platform::PredictionOptions(),
base::MakeRefCounted<segmentation_platform::InputContext>(),
base::BindOnce(&RunGetClassificationMultipleResultCallback,
std::move(callback)));
}
}
} // namespace adaptive_toolbar
|