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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_AUTOFILL_ANDROID_PERSONAL_DATA_MANAGER_ANDROID_H_
#define CHROME_BROWSER_AUTOFILL_ANDROID_PERSONAL_DATA_MANAGER_ANDROID_H_
#include <vector>
#include "base/android/jni_weak_ref.h"
#include "base/android/scoped_java_ref.h"
#include "base/memory/raw_ptr.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager_observer.h"
class PrefService;
namespace autofill {
class AddressDataManager;
class PaymentsDataManager;
class PaymentInstrument;
// Android wrapper of the PersonalDataManager which provides access from the
// Java layer. Note that on Android, there's only a single profile, and
// therefore a single instance of this wrapper.
class PersonalDataManagerAndroid : public PersonalDataManagerObserver {
public:
PersonalDataManagerAndroid(JNIEnv* env,
const jni_zero::JavaRef<jobject>& obj,
PersonalDataManager* personal_data_manager,
PrefService* prefs);
PersonalDataManagerAndroid(const PersonalDataManagerAndroid&) = delete;
PersonalDataManagerAndroid& operator=(const PersonalDataManagerAndroid&) =
delete;
// Trigger the destruction of the C++ object from Java.
void Destroy(JNIEnv* env);
static base::android::ScopedJavaLocalRef<jobject>
CreateJavaCreditCardFromNative(JNIEnv* env, const CreditCard& card);
static void PopulateNativeCreditCardFromJava(
const base::android::JavaRef<jobject>& jcard,
JNIEnv* env,
CreditCard* card);
// Returns true if personal data manager has loaded the initial data.
jboolean IsDataLoaded(JNIEnv* env) const;
// These functions act on "web profiles" aka "LOCAL_PROFILE" profiles.
// -------------------------
// Returns the GUIDs of all profiles.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileGUIDsForSettings(
JNIEnv* env);
// Returns the GUIDs of the profiles to suggest to the user. See
// PersonalDataManager::GetProfilesToSuggest for more details.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileGUIDsToSuggest(
JNIEnv* env);
// Returns the profile with the specified `guid`, or NULL if there is no
// profile with the specified `guid`. Both web and auxiliary profiles may
// be returned.
base::android::ScopedJavaLocalRef<jobject> GetProfileByGUID(
JNIEnv* env,
std::string& guid);
// Determines whether the logged in user (if any) is eligible to store
// Autofill address profiles to their account.
jboolean IsEligibleForAddressAccountStorage(JNIEnv* env);
// Determines the country for for the newly created address profile.
std::string GetDefaultCountryCodeForNewAddress(JNIEnv* env) const;
// Adds or modifies a profile. If `guid` is an empty string, we are creating
// a new profile. Else we are updating an existing profile. Always returns
// the GUID for this profile; the GUID it may have just been created.
std::string SetProfile(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jprofile,
std::string& guid);
// Adds or modifies a profile like SetProfile interface if `jprofile` is
// local. Otherwise it creates a local copy of it.
std::string SetProfileToLocal(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jprofile,
std::string& guid);
// Gets the labels for all known profiles. These labels are useful for
// distinguishing the profiles from one another.
//
// The labels never contain the full name and include at least 2 fields.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileLabelsForSettings(
JNIEnv* env);
// Gets the labels for the profiles to suggest to the user. These labels are
// useful for distinguishing the profiles from one another.
//
// The labels never contain the email address, or phone numbers. The
// `include_name_in_label` argument controls whether the name is included.
// All other fields are included in the label.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileLabelsToSuggest(
JNIEnv* env,
jboolean include_name_in_label,
jboolean include_organization_in_label,
jboolean include_country_in_label);
// Returns the shipping label of the given profile for PaymentRequest. This
// label does not contain the full name or the email address but will include
// the country depending on the value of `include_country_in_label`. All other
// fields are included in the label.
std::u16string GetShippingAddressLabelForPaymentRequest(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jprofile,
std::string& guid,
bool include_country_in_label);
// These functions act on local credit cards.
// --------------------
// Returns the GUIDs of all the credit cards.
base::android::ScopedJavaLocalRef<jobjectArray> GetCreditCardGUIDsForSettings(
JNIEnv* env);
// Returns the GUIDs of the credit cards to suggest to the user. See
// GetCreditCardsToSuggest in payments_suggestion_generator.h for more
// details.
base::android::ScopedJavaLocalRef<jobjectArray> GetCreditCardGUIDsToSuggest(
JNIEnv* env);
// Returns the credit card with the specified `guid`, or NULL if there is
// no credit card with the specified `guid`.
base::android::ScopedJavaLocalRef<jobject> GetCreditCardByGUID(
JNIEnv* env,
std::string& guid);
// Returns a credit card with the specified `jcard_number`. This is used for
// determining the card's obfuscated number, issuer icon, and type in one go.
// This function does not interact with the autofill table on disk, so can be
// used for cards that are not saved.
base::android::ScopedJavaLocalRef<jobject> GetCreditCardForNumber(
JNIEnv* env,
std::u16string& jcard_number);
// Adds or modifies a local credit card. If `guid` is an empty string, we
// are creating a new card. Else we are updating an existing card. Always
// returns the GUID for this card; the GUID it may have just been created.
std::string SetCreditCard(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcard);
// Updates the billing address of a server credit card `jcard`.
void UpdateServerCardBillingAddress(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcard);
// Removes the credit card or IBAN represented by `guid`.
void RemoveByGUID(JNIEnv* env, const std::string& guid);
// Removes the profile represented by `guid`.
void RemoveProfile(JNIEnv* env, const std::string& guid);
// Delete all local credit cards.
void DeleteAllLocalCreditCards(JNIEnv* env);
// PersonalDataManagerObserver:
void OnPersonalDataChanged() override;
// These functions act on the usage stats of local profiles and credit cards.
// --------------------
// Records the use and log usage metrics for the profile associated with the
// `guid`. Increments the use count of the profile and sets its use date to
// the current time.
void RecordAndLogProfileUse(JNIEnv* env, std::string& guid);
// Records the use and log usage metrics for the credit card associated with
// the `guid`. Increments the use count of the credit card and sets its use
// date to the current time.
void RecordAndLogCreditCardUse(JNIEnv* env, std::string& guid);
// Checks whether the Autofill PersonalDataManager has profiles.
jboolean HasProfiles(JNIEnv* env);
// Checks whether the Autofill PersonalDataManager has credit cards.
jboolean HasCreditCards(JNIEnv* env);
// Checks whether FIDO authentication is available.
jboolean IsFidoAuthenticationAvailable(JNIEnv* env);
static base::android::ScopedJavaLocalRef<jobject> CreateJavaIbanFromNative(
JNIEnv* env,
const Iban& iban);
static void PopulateNativeIbanFromJava(
const base::android::JavaRef<jobject>& jiban,
JNIEnv* env,
Iban* iban);
// Add a server IBAN. Used only in tests.
void AddServerIbanForTest(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jiban);
// Return IBAN with the specified `guid`, or Null if there is no IBAN with
// the specified `guid`.
base::android::ScopedJavaLocalRef<jobject> GetIbanByGuid(JNIEnv* env,
std::string& guid);
// Returns an array of all stored IBANs.
base::android::ScopedJavaLocalRef<jobjectArray> GetIbansForSettings(
JNIEnv* env);
// Adds or modifies a local IBAN. If `jiban`'s GUID is an empty string we
// create a new IBAN, otherwise we update the existing IBAN. Always returns
// the GUID for this IBAN; the GUID may have just been created.
std::string AddOrUpdateLocalIban(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jiban);
// Checks if `jiban_value` is a valid IBAN.
static jboolean IsValidIban(JNIEnv* env, std::u16string& jiban_value);
// Returns whether the `Add IBAN` button should be shown on the payment
// methods settings page.
jboolean ShouldShowAddIbanButtonOnSettingsPage(JNIEnv* env);
// Returns whether the Autofill feature for profiles is managed.
jboolean IsAutofillProfileManaged(JNIEnv* env);
// Returns whether the Autofill feature for credit cards is managed.
jboolean IsAutofillCreditCardManaged(JNIEnv* env);
// Returns an array of BankAccount objects retrieved from the
// PersonalDataManager.
base::android::ScopedJavaLocalRef<jobjectArray> GetMaskedBankAccounts(
JNIEnv* env);
// Create an object of Java BankAccount from native BankAccount.
static base::android::ScopedJavaLocalRef<jobject>
CreateJavaBankAccountFromNative(JNIEnv* env, const BankAccount& bank_account);
// Create an object of native BankAccount from Java BankAccount.
static BankAccount CreateNativeBankAccountFromJava(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jbank_account);
// Returns an array of Ewallet objects retrieved from the PersonalDataManager.
base::android::ScopedJavaLocalRef<jobjectArray> GetEwallets(JNIEnv* env);
// Create an object of Java Ewallet from native Ewallet.
static base::android::ScopedJavaLocalRef<jobject> CreateJavaEwalletFromNative(
JNIEnv* env,
const Ewallet& ewallet);
// Create an object of native Ewallet from Java Ewallet.
static Ewallet CreateNativeEwalletFromJava(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jewallet);
private:
~PersonalDataManagerAndroid() override;
// Returns the GUIDs of the `profiles` passed as parameter.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileGUIDs(
JNIEnv* env,
const std::vector<const AutofillProfile*>& profiles);
// Returns the GUIDs of the `credit_cards` passed as parameter.
base::android::ScopedJavaLocalRef<jobjectArray> GetCreditCardGUIDs(
JNIEnv* env,
const std::vector<const CreditCard*>& credit_cards);
// Gets the labels for the `profiles` passed as parameters. These labels are
// useful for distinguishing the profiles from one another.
//
// The labels never contain the full name and include at least 2 fields.
//
// If `address_only` is true, then such fields as phone number, and email
// address are also omitted, but all other fields are included in the label.
base::android::ScopedJavaLocalRef<jobjectArray> GetProfileLabels(
JNIEnv* env,
bool address_only,
bool include_name_in_label,
bool include_organization_in_label,
bool include_country_in_label,
std::vector<const AutofillProfile*> profiles);
// Shared method used when creating Java PaymentInstrument.
static std::vector<int> GetPaymentRailsFromPaymentInstrument(
const PaymentInstrument& payment_instrument);
AddressDataManager& address_data_manager() {
return pdm_observation_.GetSource()->address_data_manager();
}
const AddressDataManager& address_data_manager() const {
return const_cast<PersonalDataManagerAndroid*>(this)
->address_data_manager();
}
PaymentsDataManager& payments_data_manager() {
return pdm_observation_.GetSource()->payments_data_manager();
}
// Pointer to the java counterpart.
JavaObjectWeakGlobalRef weak_java_obj_;
base::ScopedObservation<PersonalDataManager, PersonalDataManagerObserver>
pdm_observation_{this};
const raw_ptr<PrefService> prefs_;
};
} // namespace autofill
#endif // CHROME_BROWSER_AUTOFILL_ANDROID_PERSONAL_DATA_MANAGER_ANDROID_H_
|