1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Populates default autofill profile from user's own Android contact. 6 #include "components/autofill/core/browser/android/auxiliary_profiles_android.h" 7 8 #include <vector> 9 10 #include "base/guid.h" 11 #include "base/logging.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_vector.h" 14 #include "base/strings/string16.h" 15 #include "base/strings/string_util.h" 16 #include "base/strings/utf_string_conversions.h" 17 #include "components/autofill/core/browser/android/auxiliary_profile_loader_android.h" 18 #include "components/autofill/core/browser/autofill_profile.h" 19 #include "components/autofill/core/browser/autofill_type.h" 20 #include "components/autofill/core/browser/phone_number.h" 21 22 // Generates the autofill profile by accessing the Android 23 // ContactsContract.Profile API through PersonalAutofillPopulator via JNI. 24 // The generated profile corresponds to the user's "ME" contact in the 25 // "People" app. The caller passes a vector of profiles into the constructor 26 // then initiates a fetch using |GetContactsProfile()| method. This clears 27 // any existing addresses. 28 29 // Randomly generated guid. The Autofillprofile class requires a consistent 30 // unique guid or else things break. 31 const char kAndroidMeContactA[] = "9A9E1C06-7A3B-48FA-AA4F-135CA6FC25D9"; 32 33 // The origin string for all profiles loaded from the Android contacts list. 34 const char kAndroidContactsOrigin[] = "Android Contacts"; 35 36 namespace { 37 38 // Takes misc. address information strings from Android API and collapses 39 // into single string for "address line 2" 40 base::string16 CollapseAddress(const base::string16& post_office_box, 41 const base::string16& neighborhood) { 42 std::vector<base::string16> accumulator; 43 if (!post_office_box.empty()) 44 accumulator.push_back(post_office_box); 45 if (!neighborhood.empty()) 46 accumulator.push_back(neighborhood); 47 48 return JoinString(accumulator, base::ASCIIToUTF16(", ")); 49 } 50 51 } // namespace 52 53 namespace autofill { 54 55 AuxiliaryProfilesAndroid::AuxiliaryProfilesAndroid( 56 const AuxiliaryProfileLoaderAndroid& profileLoader, 57 const std::string& app_locale) 58 : profile_loader_(profileLoader), 59 app_locale_(app_locale) {} 60 61 AuxiliaryProfilesAndroid::~AuxiliaryProfilesAndroid() { 62 } 63 64 scoped_ptr<AutofillProfile> AuxiliaryProfilesAndroid::LoadContactsProfile() { 65 scoped_ptr<AutofillProfile> profile( 66 new AutofillProfile(kAndroidMeContactA, kAndroidContactsOrigin)); 67 LoadName(profile.get()); 68 LoadEmailAddress(profile.get()); 69 LoadPhoneNumbers(profile.get()); 70 71 // Android user's profile contact does not parse its address 72 // into constituent parts. Instead we just get a long string blob. 73 // Disable address population until we implement a way to parse the 74 // data. 75 // http://crbug.com/178838 76 // LoadAddress(profile.get()); 77 78 return profile.Pass(); 79 } 80 81 void AuxiliaryProfilesAndroid::LoadAddress(AutofillProfile* profile) { 82 base::string16 street = profile_loader_.GetStreet(); 83 base::string16 post_office_box = profile_loader_.GetPostOfficeBox(); 84 base::string16 neighborhood = profile_loader_.GetNeighborhood(); 85 base::string16 city = profile_loader_.GetCity(); 86 base::string16 postal_code = profile_loader_.GetPostalCode(); 87 base::string16 region = profile_loader_.GetRegion(); 88 base::string16 country = profile_loader_.GetCountry(); 89 90 base::string16 street2 = CollapseAddress(post_office_box, neighborhood); 91 92 profile->SetRawInfo(ADDRESS_HOME_LINE1, street); 93 profile->SetRawInfo(ADDRESS_HOME_LINE2, street2); 94 profile->SetRawInfo(ADDRESS_HOME_CITY, city); 95 profile->SetRawInfo(ADDRESS_HOME_STATE, region); 96 profile->SetRawInfo(ADDRESS_HOME_ZIP, postal_code); 97 profile->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY), country, app_locale_); 98 } 99 100 void AuxiliaryProfilesAndroid::LoadName(AutofillProfile* profile) { 101 base::string16 first_name = profile_loader_.GetFirstName(); 102 base::string16 middle_name = profile_loader_.GetMiddleName(); 103 base::string16 last_name = profile_loader_.GetLastName(); 104 105 profile->SetRawInfo(NAME_FIRST, first_name); 106 profile->SetRawInfo(NAME_MIDDLE, middle_name); 107 profile->SetRawInfo(NAME_LAST, last_name); 108 } 109 110 void AuxiliaryProfilesAndroid::LoadEmailAddress(AutofillProfile* profile) { 111 std::vector<base::string16> emails; 112 profile_loader_.GetEmailAddresses(&emails); 113 profile->SetRawMultiInfo(EMAIL_ADDRESS, emails); 114 } 115 116 void AuxiliaryProfilesAndroid::LoadPhoneNumbers(AutofillProfile* profile) { 117 std::vector<base::string16> phone_numbers; 118 profile_loader_.GetPhoneNumbers(&phone_numbers); 119 profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, phone_numbers); 120 } 121 122 } // namespace autofill 123