Chromium Code Reviews| Index: chrome/browser/autofill/android/auxiliary_profiles_android.cc |
| diff --git a/chrome/browser/autofill/android/auxiliary_profiles_android.cc b/chrome/browser/autofill/android/auxiliary_profiles_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..81ec02b6d5cea2c64e0ceaf392b347394d3e11ea |
| --- /dev/null |
| +++ b/chrome/browser/autofill/android/auxiliary_profiles_android.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2013 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. |
| + |
| +// Populates default autofill profile from user's own Android contact. |
| +#include "chrome/browser/autofill/android/auxiliary_profiles_android.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/guid.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/string16.h" |
| +#include "base/string_util.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/autofill/android/auxiliary_profile_loader_android.h" |
| +#include "chrome/browser/autofill/autofill_profile.h" |
| +#include "chrome/browser/autofill/phone_number.h" |
| + |
| +// Generates the autofill profile by accessing the Android |
| +// ContactsContract.Profile API through PersonalAutofillPopulator via JNI. |
| +// The generated profile corresponds to the user's "ME" contact in the |
| +// "People" app. The caller passes a vector of profiles into the constructor |
| +// then initiates a fetch using |GetContactsProfile()| method. This clears |
| +// any existing addresses. |
| + |
| +// Randomly generated guid. The Autofillprofile class requires a consistent |
| +// unique guid or else things break. |
| +const char kAndroidMeContactA[] = "9A9E1C06-7A3B-48FA-AA4F-135CA6FC25D9"; |
| + |
| +namespace { |
| + |
| +// Takes misc. address information strings from Android API and collapses |
| +// into single string for "address line 2" |
| +string16 CollapseAddress(const string16& post_office_box, |
| + const string16& neighborhood) { |
| + std::vector<string16> accumulator; |
| + if (!post_office_box.empty()) |
| + accumulator.push_back(post_office_box); |
| + if (!neighborhood.empty()) |
| + accumulator.push_back(neighborhood); |
| + |
| + return JoinString(accumulator, ASCIIToUTF16(", ")); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace autofill { |
| + |
| +AuxiliaryProfilesAndroid::AuxiliaryProfilesAndroid( |
| + const AuxiliaryProfileLoaderAndroid& profileLoader) |
| + : profile_loader_(profileLoader) {} |
| + |
| +AuxiliaryProfilesAndroid::~AuxiliaryProfilesAndroid() { |
| +} |
| + |
| +scoped_ptr<AutofillProfile> AuxiliaryProfilesAndroid::LoadContactsProfile() { |
| + scoped_ptr<AutofillProfile> profile(new AutofillProfile(kAndroidMeContactA)); |
| + LoadName(profile.get()); |
| + LoadEmailAddress(profile.get()); |
| + LoadPhoneNumbers(profile.get()); |
| + |
| + // Android user's profile contact does not parse its address |
| + // into constituent parts. Instead we just get a long string blob. |
| + // Disable address population until we implement a way to parse the |
| + // data. |
| + // http://crbug.com/178838 |
| + // LoadAddress(profile.get()); |
| + |
| + return profile.Pass(); |
| +} |
| + |
| +void AuxiliaryProfilesAndroid::LoadAddress(AutofillProfile* profile) { |
| + string16 street = profile_loader_.GetStreet(); |
| + string16 post_office_box = profile_loader_.GetPostOfficeBox(); |
| + string16 neighborhood = profile_loader_.GetNeighborhood(); |
| + string16 city = profile_loader_.GetCity(); |
| + string16 postal_code = profile_loader_.GetPostalCode(); |
| + string16 region = profile_loader_.GetRegion(); |
| + string16 country = profile_loader_.GetCountry(); |
| + |
| + string16 street2 = |
| + CollapseAddress(post_office_box, neighborhood); |
|
Ilya Sherman
2013/03/11 22:39:44
nit: Looks like this fits on the previous line.
apiccion
2013/03/13 20:17:28
Done.
|
| + |
| + profile->SetRawInfo(ADDRESS_HOME_LINE1, street); |
| + profile->SetRawInfo(ADDRESS_HOME_LINE2, street2); |
| + profile->SetRawInfo(ADDRESS_HOME_CITY, city); |
| + profile->SetRawInfo(ADDRESS_HOME_STATE, region); |
| + profile->SetRawInfo(ADDRESS_HOME_ZIP, postal_code); |
| + profile->SetRawInfo(ADDRESS_HOME_COUNTRY, country); |
| +} |
| + |
| +void AuxiliaryProfilesAndroid::LoadName(AutofillProfile* profile) { |
| + string16 first_name = profile_loader_.GetFirstName(); |
| + string16 middle_name = profile_loader_.GetMiddleName(); |
| + string16 last_name = profile_loader_.GetLastName(); |
| + |
| + profile->SetRawInfo(NAME_FIRST, first_name); |
| + profile->SetRawInfo(NAME_MIDDLE, middle_name); |
| + profile->SetRawInfo(NAME_LAST, last_name); |
| +} |
| + |
| +void AuxiliaryProfilesAndroid::LoadEmailAddress(AutofillProfile* profile) { |
| + std::vector<string16> emails; |
| + profile_loader_.GetEmailAddresses(&emails); |
| + profile->SetRawMultiInfo(EMAIL_ADDRESS, emails); |
| +} |
| + |
| +void AuxiliaryProfilesAndroid::LoadPhoneNumbers(AutofillProfile* profile) { |
| + std::vector<string16> phone_numbers; |
| + profile_loader_.GetPhoneNumbers(&phone_numbers); |
| + profile->SetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, phone_numbers); |
| +} |
| + |
| +} // namespace |