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
|
// Copyright 2020 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/segmentation_platform/internal/android/segmentation_platform_service_android.h"
#include <memory>
#include "base/android/callback_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_base.h"
#include "components/segmentation_platform/public/android/input_context_android.h"
#include "components/segmentation_platform/public/android/prediction_options_android.h"
#include "components/segmentation_platform/public/android/segmentation_platform_conversion_bridge.h"
#include "components/segmentation_platform/public/android/training_labels_android.h"
#include "components/segmentation_platform/public/input_context.h"
#include "components/segmentation_platform/public/prediction_options.h"
#include "components/segmentation_platform/public/proto/segmentation_platform.pb.h"
#include "components/segmentation_platform/public/result.h"
#include "components/segmentation_platform/public/segment_selection_result.h"
#include "components/segmentation_platform/public/segmentation_platform_service.h"
#include "components/segmentation_platform/public/trigger.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "components/segmentation_platform/internal/jni_headers/SegmentationPlatformServiceImpl_jni.h"
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::JavaParamRef;
namespace segmentation_platform {
namespace {
const char kSegmentationPlatformServiceBridgeKey[] =
"segmentation_platform_service_bridge";
void RunGetSelectedSegmentCallback(const JavaRef<jobject>& j_callback,
const SegmentSelectionResult& result) {
JNIEnv* env = AttachCurrentThread();
base::android::RunObjectCallbackAndroid(
j_callback,
SegmentationPlatformConversionBridge::CreateJavaSegmentSelectionResult(
env, result));
}
void RunGetClassificationResultCallback(const JavaRef<jobject>& j_callback,
const ClassificationResult& result) {
JNIEnv* env = AttachCurrentThread();
base::android::RunObjectCallbackAndroid(
j_callback,
SegmentationPlatformConversionBridge::CreateJavaClassificationResult(
env, result));
}
void RunInputKeysForModelCallback(const JavaRef<jobject>& j_callback,
std::set<std::string> input_keys) {
JNIEnv* env = AttachCurrentThread();
std::vector<std::string> inputs_vector(input_keys.begin(), input_keys.end());
base::android::RunObjectCallbackAndroid(
j_callback, base::android::ToJavaArrayOfStrings(env, inputs_vector));
}
} // namespace
// This function is declared in segmentation_platform_service.h and
// should be linked in to any binary using
// SegmentationPlatformService::GetJavaObject. static
ScopedJavaLocalRef<jobject> SegmentationPlatformService::GetJavaObject(
SegmentationPlatformService* service) {
if (!service->GetUserData(kSegmentationPlatformServiceBridgeKey)) {
service->SetUserData(
kSegmentationPlatformServiceBridgeKey,
std::make_unique<SegmentationPlatformServiceAndroid>(service));
}
SegmentationPlatformServiceAndroid* bridge =
static_cast<SegmentationPlatformServiceAndroid*>(
service->GetUserData(kSegmentationPlatformServiceBridgeKey));
return bridge->GetJavaObject();
}
SegmentationPlatformServiceAndroid::SegmentationPlatformServiceAndroid(
SegmentationPlatformService* segmentation_platform_service)
: segmentation_platform_service_(segmentation_platform_service) {
DCHECK(segmentation_platform_service_);
JNIEnv* env = base::android::AttachCurrentThread();
java_obj_.Reset(env, Java_SegmentationPlatformServiceImpl_create(
env, reinterpret_cast<int64_t>(this))
.obj());
}
SegmentationPlatformServiceAndroid::~SegmentationPlatformServiceAndroid() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_SegmentationPlatformServiceImpl_clearNativePtr(env, java_obj_);
}
void SegmentationPlatformServiceAndroid::GetSelectedSegment(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
const JavaParamRef<jstring>& j_segmentation_key,
const JavaParamRef<jobject>& jcallback) {
segmentation_platform_service_->GetSelectedSegment(
ConvertJavaStringToUTF8(env, j_segmentation_key),
base::BindOnce(&RunGetSelectedSegmentCallback,
ScopedJavaGlobalRef<jobject>(jcallback)));
}
void SegmentationPlatformServiceAndroid::GetClassificationResult(
JNIEnv* env,
const JavaParamRef<jobject>& j_caller,
const JavaParamRef<jstring>& j_segmentation_key,
const JavaParamRef<jobject>& j_prediction_options,
const JavaParamRef<jobject>& j_input_context,
const JavaParamRef<jobject>& j_callback) {
scoped_refptr<InputContext> native_input_context =
InputContextAndroid::ToNativeInputContext(env, j_input_context);
PredictionOptions native_prediction_options =
PredictionOptionsAndroid::ToNativePredictionOptions(env,
j_prediction_options);
segmentation_platform_service_->GetClassificationResult(
ConvertJavaStringToUTF8(env, j_segmentation_key),
native_prediction_options, native_input_context,
base::BindOnce(&RunGetClassificationResultCallback,
ScopedJavaGlobalRef<jobject>(j_callback)));
}
ScopedJavaLocalRef<jobject>
SegmentationPlatformServiceAndroid::GetCachedSegmentResult(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
const JavaParamRef<jstring>& j_segmentation_key) {
return SegmentationPlatformConversionBridge::CreateJavaSegmentSelectionResult(
env, segmentation_platform_service_->GetCachedSegmentResult(
ConvertJavaStringToUTF8(env, j_segmentation_key)));
}
void SegmentationPlatformServiceAndroid::GetInputKeysForModel(
JNIEnv* env,
const JavaParamRef<jstring>& j_segmentation_key,
const JavaParamRef<jobject>& j_callback) {
segmentation_platform_service_->GetInputKeysForModel(
ConvertJavaStringToUTF8(env, j_segmentation_key),
base::BindOnce(&RunInputKeysForModelCallback,
ScopedJavaGlobalRef<jobject>(j_callback)));
}
void SegmentationPlatformServiceAndroid::CollectTrainingData(
JNIEnv* env,
jint j_segment_id,
jlong j_request_id,
jlong j_ukm_source_id,
const JavaParamRef<jobject>& j_param,
const JavaParamRef<jobject>& j_callback) {
segmentation_platform::TrainingLabels training_labels =
TrainingLabelsAndroid::ToNativeTrainingLabels(env, j_param);
segmentation_platform_service_->CollectTrainingData(
static_cast<proto::SegmentId>(j_segment_id),
segmentation_platform::TrainingRequestId::FromUnsafeValue(j_request_id),
j_ukm_source_id, std::move(training_labels),
base::BindOnce(&base::android::RunBooleanCallbackAndroid,
ScopedJavaGlobalRef<jobject>(j_callback)));
}
ScopedJavaLocalRef<jobject>
SegmentationPlatformServiceAndroid::GetJavaObject() {
return ScopedJavaLocalRef<jobject>(java_obj_);
}
} // namespace segmentation_platform
|