blob: 69004936f44127df5836fd92d47b0f05d898a331 [file] [log] [blame]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_UI_CONTROLLER_ANDROID_UTILS_H_
#define CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_UI_CONTROLLER_ANDROID_UTILS_H_
#include <map>
#include <string>
#include <vector>
#include "base/android/jni_android.h"
#include "components/autofill_assistant/browser/bottom_sheet_state.h"
#include "components/autofill_assistant/browser/service.pb.h"
#include "components/autofill_assistant/browser/trigger_context.h"
#include "components/autofill_assistant/browser/user_model.h"
#include "components/autofill_assistant/browser/view_layout.pb.h"
#include "content/public/browser/web_contents.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"
namespace autofill_assistant {
class Service;
class ServiceRequestSender;
class ClientAndroid;
namespace ui_controller_android_utils {
// Returns a 32-bit Integer representing |color_string| in Java, or null if
// |color_string| is invalid.
// TODO(806868): Get rid of this overload and always use GetJavaColor(proto).
base::android::ScopedJavaLocalRef<jobject> GetJavaColor(
JNIEnv* env,
const std::string& color_string);
// Returns a 32-bit Integer representing |proto| in Java, or null if
// |proto| is invalid.
base::android::ScopedJavaLocalRef<jobject> GetJavaColor(
JNIEnv* env,
const base::android::ScopedJavaLocalRef<jobject>& jcontext,
const ColorProto& proto);
// Returns the pixelsize of |proto| in |jcontext|, or |nullopt| if |proto| is
// invalid.
absl::optional<int> GetPixelSize(
JNIEnv* env,
const base::android::ScopedJavaLocalRef<jobject>& jcontext,
const ClientDimensionProto& proto);
// Returns the pixelsize of |proto| in |jcontext|, or |default_value| if |proto|
// is invalid.
int GetPixelSizeOrDefault(
JNIEnv* env,
const base::android::ScopedJavaLocalRef<jobject>& jcontext,
const ClientDimensionProto& proto,
int default_value);
// Returns an instance of an |AssistantDrawable| or nullptr if it could not
// be created.
base::android::ScopedJavaLocalRef<jobject> CreateJavaDrawable(
JNIEnv* env,
const base::android::ScopedJavaLocalRef<jobject>& jcontext,
const DrawableProto& proto,
const UserModel* user_model = nullptr);
// Returns the java equivalent of |proto|.
base::android::ScopedJavaLocalRef<jobject> ToJavaValue(JNIEnv* env,
const ValueProto& proto);
// Returns the native equivalent of |jvalue|. Returns an empty ValueProto if
// |jvalue| is null.
ValueProto ToNativeValue(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jvalue);
// Returns an instance of |AssistantInfoPopup| for |proto|.
base::android::ScopedJavaLocalRef<jobject> CreateJavaInfoPopup(
JNIEnv* env,
const InfoPopupProto& proto);
// Shows an instance of |AssistantInfoPopup| on the screen.
void ShowJavaInfoPopup(JNIEnv* env,
base::android::ScopedJavaLocalRef<jobject> jinfo_popup,
base::android::ScopedJavaLocalRef<jobject> jcontext);
// Converts a java string to native. Returns an empty string if input is null.
std::string SafeConvertJavaStringToNative(
JNIEnv* env,
const base::android::JavaRef<jstring>& jstring);
// Creates a BottomSheetState from the Android SheetState enum defined in
// components/browser_ui/bottomsheet/BottomSheetController.java.
BottomSheetState ToNativeBottomSheetState(int state);
// Converts a BottomSheetState to the Android SheetState enum.
int ToJavaBottomSheetState(BottomSheetState state);
// Returns an instance of |AssistantChip| or nullptr if the chip type is
// invalid.
base::android::ScopedJavaLocalRef<jobject> CreateJavaAssistantChip(
JNIEnv* env,
const ChipProto& chip);
// Returns a list of |AssistantChip| instances or nullptr if any of the chips
// in |chips| has an invalid type.
base::android::ScopedJavaLocalRef<jobject> CreateJavaAssistantChipList(
JNIEnv* env,
const std::vector<ChipProto>& chips);
// Creates a std::map from an incoming set of Java string keys and values.
std::map<std::string, std::string> CreateStringMapFromJava(
JNIEnv* env,
const base::android::JavaRef<jobjectArray>& keys,
const base::android::JavaRef<jobjectArray>& values);
// Creates a C++ trigger context for the specified java inputs.
std::unique_ptr<TriggerContext> CreateTriggerContext(
JNIEnv* env,
content::WebContents* web_contents,
const base::android::JavaRef<jstring>& jexperiment_ids,
const base::android::JavaRef<jobjectArray>& jparameter_names,
const base::android::JavaRef<jobjectArray>& jparameter_values,
const base::android::JavaRef<jobjectArray>& jdevice_only_parameter_names,
const base::android::JavaRef<jobjectArray>& jdevice_only_parameter_values,
jboolean onboarding_shown,
jboolean is_direct_action,
const base::android::JavaRef<jstring>& jinitial_url);
// Returns true if |web_contents| is owned by a custom tab. Assumes that
// |web_contents| is valid and currently owned by a tab.
bool IsCustomTab(content::WebContents* web_contents);
// Returns the service to inject, if any, for |client_android|. This is used for
// integration tests, which provide a test service to communicate with.
std::unique_ptr<Service> GetServiceToInject(JNIEnv* env,
ClientAndroid* client_android);
// Returns the service request sender to inject, if any. This is used for
// integration tests which provide a test service request sender to communicate
// with.
std::unique_ptr<ServiceRequestSender> GetServiceRequestSenderToInject(
JNIEnv* env);
} // namespace ui_controller_android_utils
} // namespace autofill_assistant
#endif // CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_UI_CONTROLLER_ANDROID_UTILS_H_