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
|
// 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 "base/android/callback_android.h"
#include "base/android/scoped_java_ref.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/segmentation_platform/segmentation_platform_service_factory.h"
#include "chrome/browser/ui/android/toolbar/adaptive_toolbar_enums.h"
#include "chrome/browser/ui/android/toolbar/jni_headers/AdaptiveToolbarBridge_jni.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"
using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
using segmentation_platform::InputContext;
namespace {
AdaptiveToolbarButtonVariant ActionLabelToAdaptiveToolbarButtonVariant(
const std::string& label) {
AdaptiveToolbarButtonVariant button_variant =
AdaptiveToolbarButtonVariant::kUnknown;
if (label == segmentation_platform::kAdaptiveToolbarModelLabelNewTab) {
button_variant = AdaptiveToolbarButtonVariant::kNewTab;
} else if (label == segmentation_platform::kAdaptiveToolbarModelLabelShare) {
button_variant = AdaptiveToolbarButtonVariant::kShare;
} else if (label == segmentation_platform::kAdaptiveToolbarModelLabelVoice) {
button_variant = AdaptiveToolbarButtonVariant::kVoice;
} else if (label ==
segmentation_platform::kAdaptiveToolbarModelLabelTranslate) {
button_variant = AdaptiveToolbarButtonVariant::kTranslate;
} else if (label ==
segmentation_platform::kAdaptiveToolbarModelLabelAddToBookmarks) {
button_variant = AdaptiveToolbarButtonVariant::kAddToBookmarks;
}
return button_variant;
}
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 RunGetClassificationResultCallback(
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::kNotReady;
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);
}
} // namespace
void JNI_AdaptiveToolbarBridge_GetSessionVariantButton(
JNIEnv* env,
const JavaParamRef<jobject>& j_profile,
const JavaParamRef<jobject>& j_callback) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
if (!profile) {
RunGetClassificationResultCallback(
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) {
RunGetClassificationResultCallback(
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(
&RunGetClassificationResultCallback,
base::android::ScopedJavaGlobalRef<jobject>(j_callback)));
} else {
segmentation_platform_service->GetSelectedSegment(
segmentation_platform::kAdaptiveToolbarSegmentationKey,
base::BindOnce(
&RunGetSelectedSegmentCallback,
base::android::ScopedJavaGlobalRef<jobject>(j_callback)));
}
}
|