chromium/components/autofill/core/browser/geo/subkey_requester.cc

// Copyright 2017 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/autofill/core/browser/geo/subkey_requester.h"

#include <memory>
#include <utility>

#include "base/cancelable_callback.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "components/autofill/android/main_autofill_jni_headers/SubKeyRequester_jni.h"
#endif  // BUILDFLAG(IS_ANDROID)

namespace autofill {

namespace {

Source;
Storage;

class SubKeyRequest : public SubKeyRequester::Request {};

#if BUILDFLAG(IS_ANDROID)
void OnSubKeysReceived(base::android::ScopedJavaGlobalRef<jobject> jdelegate,
                       const std::vector<std::string>& subkeys_codes,
                       const std::vector<std::string>& subkeys_names) {
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_GetSubKeysRequestDelegate_onSubKeysReceived(
      env, jdelegate, base::android::ToJavaArrayOfStrings(env, subkeys_codes),
      base::android::ToJavaArrayOfStrings(env, subkeys_names));
}
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace

SubKeyRequester::SubKeyRequester(std::unique_ptr<Source> source,
                                 std::unique_ptr<Storage> storage,
                                 const std::string& language)
    :{}

SubKeyRequester::~SubKeyRequester() = default;

void SubKeyRequester::StartRegionSubKeysRequest(const std::string& region_code,
                                                int timeout_seconds,
                                                SubKeyReceiverCallback cb) {}

bool SubKeyRequester::AreRulesLoadedForRegion(const std::string& region_code) {}

void SubKeyRequester::LoadRulesForRegion(const std::string& region_code) {}

void SubKeyRequester::OnAddressValidationRulesLoaded(
    const std::string& region_code,
    bool success) {}

void SubKeyRequester::CancelPendingGetSubKeys() {}

#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject> SubKeyRequester::GetJavaObject() {
  if (!java_ref_) {
    java_ref_.Reset(
        Java_SubKeyRequester_Constructor(base::android::AttachCurrentThread(),
                                         reinterpret_cast<intptr_t>(this)));
  }
  return base::android::ScopedJavaLocalRef<jobject>(java_ref_);
}

void SubKeyRequester::LoadRulesForSubKeys(
    JNIEnv* env,
    const base::android::JavaParamRef<jstring>& jregion_code) {
  LoadRulesForRegion(base::android::ConvertJavaStringToUTF8(env, jregion_code));
}

void SubKeyRequester::StartRegionSubKeysRequest(
    JNIEnv* env,
    const base::android::JavaParamRef<jstring>& jregion_code,
    jint jtimeout_seconds,
    const base::android::JavaParamRef<jobject>& jdelegate) {
  const std::string region_code =
      base::android::ConvertJavaStringToUTF8(env, jregion_code);

  base::android::ScopedJavaGlobalRef<jobject> my_jdelegate;
  my_jdelegate.Reset(env, jdelegate);

  SubKeyReceiverCallback cb =
      base::BindOnce(&OnSubKeysReceived,
                     base::android::ScopedJavaGlobalRef<jobject>(my_jdelegate));

  StartRegionSubKeysRequest(region_code, jtimeout_seconds, std::move(cb));
}

void SubKeyRequester::CancelPendingGetSubKeys(JNIEnv* env) {
  CancelPendingGetSubKeys();
}
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace autofill