12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/android/personal_data_manager_android.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/android/jni_array.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/android/jni_string.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/format_macros.h"
104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/prefs/pref_service.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/autofill/personal_data_manager_factory.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/pref_names.h"
17a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "components/autofill/core/browser/autofill_country.h"
183240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch#include "components/autofill/core/browser/autofill_type.h"
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/field_types.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/personal_data_manager.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "jni/PersonalDataManager_jni.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::android::ConvertJavaStringToUTF8;
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::android::ConvertUTF16ToJavaString;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::android::ConvertUTF8ToJavaString;
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::android::ScopedJavaLocalRef;
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace autofill {
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)PrefService* GetPrefs() {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ProfileManager::GetActiveUserProfile()->GetOriginalProfile()->GetPrefs();
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jobject> CreateJavaProfileFromNative(
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const AutofillProfile& profile) {
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return Java_AutofillProfile_create(
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      env,
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ConvertUTF8ToJavaString(env, profile.guid()).obj(),
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ConvertUTF8ToJavaString(env, profile.origin()).obj(),
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(NAME_FULL)).obj(),
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(COMPANY_NAME)).obj(),
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          env, profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)).obj(),
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_STATE))
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .obj(),
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_CITY))
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .obj(),
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          env, profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)).obj(),
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_ZIP)).obj(),
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          env, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)).obj(),
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_COUNTRY))
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .obj(),
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER))
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .obj(),
60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      ConvertUTF16ToJavaString(env, profile.GetRawInfo(EMAIL_ADDRESS)).obj(),
61010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      ConvertUTF8ToJavaString(env, profile.language_code()).obj());
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PopulateNativeProfileFromJava(
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const jobject& jprofile,
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AutofillProfile* profile) {
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  profile->set_origin(
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ConvertJavaStringToUTF8(
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          Java_AutofillProfile_getOrigin(env, jprofile)));
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(NAME_FULL),
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getFullName(env, jprofile)),
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(COMPANY_NAME),
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getCompanyName(env, jprofile)),
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(ADDRESS_HOME_STREET_ADDRESS),
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getStreetAddress(env, jprofile)),
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(ADDRESS_HOME_STATE),
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getRegion(env, jprofile)),
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(ADDRESS_HOME_CITY),
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getLocality(env, jprofile)),
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY),
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getDependentLocality(env, jprofile)),
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(ADDRESS_HOME_ZIP),
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getPostalCode(env, jprofile)),
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(ADDRESS_HOME_SORTING_CODE),
108010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      ConvertJavaStringToUTF16(
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getSortingCode(env, jprofile)),
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
1116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  profile->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
1126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   ConvertJavaStringToUTF16(
1136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                       Java_AutofillProfile_getCountryCode(env, jprofile)),
1146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                   g_browser_process->GetApplicationLocale());
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(PHONE_HOME_WHOLE_NUMBER),
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getPhoneNumber(env, jprofile)),
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile->SetInfo(
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      AutofillType(EMAIL_ADDRESS),
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          Java_AutofillProfile_getEmailAddress(env, jprofile)),
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      g_browser_process->GetApplicationLocale());
125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  profile->set_language_code(
126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      ConvertJavaStringToUTF8(
127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          Java_AutofillProfile_getLanguageCode(env, jprofile)));
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jobject> CreateJavaCreditCardFromNative(
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const CreditCard& card) {
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return Java_CreditCard_create(
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      env,
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ConvertUTF8ToJavaString(env, card.guid()).obj(),
13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ConvertUTF8ToJavaString(env, card.origin()).obj(),
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NAME)).obj(),
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NUMBER)).obj(),
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(env, card.ObfuscatedNumber()).obj(),
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          env,
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          card.GetRawInfo(CREDIT_CARD_EXP_MONTH)).obj(),
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertUTF16ToJavaString(
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          env,
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PopulateNativeCreditCardFromJava(
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const jobject& jcard,
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CreditCard* card) {
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  card->set_origin(
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  card->SetRawInfo(
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CREDIT_CARD_NAME,
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  card->SetRawInfo(
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CREDIT_CARD_NUMBER,
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  card->SetRawInfo(
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CREDIT_CARD_EXP_MONTH,
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  card->SetRawInfo(
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CREDIT_CARD_EXP_4_DIGIT_YEAR,
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                       jobject obj)
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : weak_java_obj_(env, obj),
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ProfileManager::GetActiveUserProfile())) {
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  personal_data_manager_->AddObserver(this);
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  personal_data_manager_->RemoveObserver(this);
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)jint PersonalDataManagerAndroid::GetProfileCount(JNIEnv* unused_env,
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                 jobject unused_obj) {
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return personal_data_manager_->GetProfiles().size();
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByIndex(
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject unused_obj,
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jint index) {
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::vector<AutofillProfile*>& profiles =
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      personal_data_manager_->GetProfiles();
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t index_size_t = static_cast<size_t>(index);
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_LT(index_size_t, profiles.size());
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CreateJavaProfileFromNative(env, *profiles[index_size_t]);
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByGUID(
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject unused_obj,
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jstring jguid) {
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ConvertJavaStringToUTF8(env, jguid));
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!profile)
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return ScopedJavaLocalRef<jobject>();
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CreateJavaProfileFromNative(env, *profile);
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject unused_obj,
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject jprofile) {
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string guid = ConvertJavaStringToUTF8(
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      env,
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Java_AutofillProfile_getGUID(env, jprofile).obj());
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillProfile profile;
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PopulateNativeProfileFromJava(jprofile, env, &profile);
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (guid.empty()) {
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    personal_data_manager_->AddProfile(profile);
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    profile.set_guid(guid);
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    personal_data_manager_->UpdateProfile(profile);
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ConvertUTF8ToJavaString(env, profile.guid());
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)jint PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv* unused_env,
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                    jobject unused_obj) {
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return personal_data_manager_->GetCreditCards().size();
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByIndex(
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject unused_obj,
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jint index) {
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::vector<CreditCard*>& credit_cards =
241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      personal_data_manager_->GetCreditCards();
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t index_size_t = static_cast<size_t>(index);
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_LT(index_size_t, credit_cards.size());
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CreateJavaCreditCardFromNative(env, *credit_cards[index_size_t]);
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByGUID(
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject unused_obj,
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jstring jguid) {
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          ConvertJavaStringToUTF8(env, jguid));
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!card)
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return ScopedJavaLocalRef<jobject>();
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CreateJavaCreditCardFromNative(env, *card);
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    JNIEnv* env,
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject unused_obj,
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    jobject jcard) {
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string guid = ConvertJavaStringToUTF8(
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       env,
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       Java_CreditCard_getGUID(env, jcard).obj());
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CreditCard card;
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PopulateNativeCreditCardFromJava(jcard, env, &card);
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (guid.empty()) {
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    personal_data_manager_->AddCreditCard(card);
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    card.set_guid(guid);
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    personal_data_manager_->UpdateCreditCard(card);
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return ConvertUTF8ToJavaString(env, card.guid());
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
2805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    JNIEnv* env,
2815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    jobject unused_obj) {
2825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::vector<base::string16> labels;
2835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  AutofillProfile::CreateInferredLabels(
2845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      personal_data_manager_->GetProfiles(),
2855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      NULL,
2865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      NAME_FULL,
2875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      2,
2885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      g_browser_process->GetApplicationLocale(),
2895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      &labels);
2905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return base::android::ToJavaArrayOfStrings(env, labels);
2925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                              jobject unused_obj,
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                              jstring jguid) {
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  personal_data_manager_->RemoveByGUID(ConvertJavaStringToUTF8(env, jguid));
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PersonalDataManagerAndroid::OnPersonalDataChanged() {
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (weak_java_obj_.get(env).is_null())
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Java_PersonalDataManager_personalDataChanged(env,
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                               weak_java_obj_.get(env).obj());
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool PersonalDataManagerAndroid::Register(JNIEnv* env) {
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return RegisterNativesImpl(env);
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Returns whether the Autofill feature is enabled.
3154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static jboolean IsAutofillEnabled(JNIEnv* env, jclass clazz) {
3164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled);
3174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Enables or disables the Autofill feature.
3204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static void SetAutofillEnabled(JNIEnv* env, jclass clazz, jboolean enable) {
3214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled, enable);
3224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Returns whether Autofill feature is managed.
3254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static jboolean IsAutofillManaged(JNIEnv* env, jclass clazz) {
3264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled);
3274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
329a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
330a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// the application locale, or an empty string.
331a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)static jstring ToCountryCode(JNIEnv* env, jclass clazz, jstring jcountry_name) {
332a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return ConvertUTF8ToJavaString(
333a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      env,
334a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      AutofillCountry::GetCountryCode(
335a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          base::android::ConvertJavaStringToUTF16(env, jcountry_name),
336a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          g_browser_process->GetApplicationLocale())).Release();
337a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
338a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)static jlong Init(JNIEnv* env, jobject obj) {
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PersonalDataManagerAndroid* personal_data_manager_android =
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new PersonalDataManagerAndroid(env, obj);
342f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return reinterpret_cast<intptr_t>(personal_data_manager_android);
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace autofill
346