summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/autofill_profile_sync_util.cc
blob: f81dc6f031531646fad6e8ca91d54ea5308cde7d (plain)
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
// Copyright 2018 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.

#include "components/autofill/core/browser/autofill_profile_sync_util.h"

#include "base/guid.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/country_names.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/proto/autofill_sync.pb.h"
#include "components/autofill/core/browser/webdata/autofill_table.h"
#include "components/sync/model/entity_data.h"

using base::UTF16ToUTF8;
using base::UTF8ToUTF16;
using sync_pb::AutofillProfileSpecifics;
using syncer::EntityData;

namespace autofill {
namespace {

std::string TruncateUTF8(const std::string& data) {
  std::string trimmed_value;
  base::TruncateUTF8ToByteSize(data, AutofillTable::kMaxDataLength,
                               &trimmed_value);
  return trimmed_value;
}

bool IsAutofillProfileSpecificsValid(
    const AutofillProfileSpecifics& specifics) {
  return base::IsValidGUID(specifics.guid());
}

}  // namespace

std::unique_ptr<EntityData> CreateEntityDataFromAutofillProfile(
    const AutofillProfile& entry) {
  // Validity of the guid is guaranteed by the database layer.
  DCHECK(base::IsValidGUID(entry.guid()));

  auto entity_data = std::make_unique<EntityData>();
  entity_data->non_unique_name = entry.guid();
  AutofillProfileSpecifics* specifics =
      entity_data->specifics.mutable_autofill_profile();

  specifics->set_guid(entry.guid());
  specifics->set_origin(entry.origin());

  specifics->set_use_count(entry.use_count());
  specifics->set_use_date(entry.use_date().ToTimeT());
  specifics->set_address_home_language_code(
      TruncateUTF8(entry.language_code()));
  specifics->set_validity_state_bitfield(entry.GetValidityBitfieldValue());

  // Set repeated fields.
  if (entry.HasRawInfo(NAME_FIRST)) {
    specifics->add_name_first(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(NAME_FIRST))));
  }
  if (entry.HasRawInfo(NAME_MIDDLE)) {
    specifics->add_name_middle(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(NAME_MIDDLE))));
  }
  if (entry.HasRawInfo(NAME_LAST)) {
    specifics->add_name_last(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(NAME_LAST))));
  }
  if (entry.HasRawInfo(NAME_FULL)) {
    specifics->add_name_full(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(NAME_FULL))));
  }
  if (entry.HasRawInfo(EMAIL_ADDRESS)) {
    specifics->add_email_address(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(EMAIL_ADDRESS))));
  }
  if (entry.HasRawInfo(PHONE_HOME_WHOLE_NUMBER)) {
    specifics->add_phone_home_whole_number(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(PHONE_HOME_WHOLE_NUMBER))));
  }

  // Set simple single-valued fields.
  if (entry.HasRawInfo(COMPANY_NAME)) {
    specifics->set_company_name(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(COMPANY_NAME))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_CITY)) {
    specifics->set_address_home_city(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_CITY))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_STATE)) {
    specifics->set_address_home_state(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_STATE))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_ZIP)) {
    specifics->set_address_home_zip(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_ZIP))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_SORTING_CODE)) {
    specifics->set_address_home_sorting_code(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_SORTING_CODE))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)) {
    specifics->set_address_home_dependent_locality(TruncateUTF8(
        UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_COUNTRY)) {
    specifics->set_address_home_country(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_COUNTRY))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_STREET_ADDRESS)) {
    specifics->set_address_home_street_address(TruncateUTF8(
        UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_LINE1)) {
    specifics->set_address_home_line1(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_LINE1))));
  }
  if (entry.HasRawInfo(ADDRESS_HOME_LINE2)) {
    specifics->set_address_home_line2(
        TruncateUTF8(UTF16ToUTF8(entry.GetRawInfo(ADDRESS_HOME_LINE2))));
  }

  return entity_data;
}

std::unique_ptr<AutofillProfile> CreateAutofillProfileFromSpecifics(
    const AutofillProfileSpecifics& specifics) {
  if (!IsAutofillProfileSpecificsValid(specifics)) {
    return nullptr;
  }
  std::unique_ptr<AutofillProfile> profile =
      std::make_unique<AutofillProfile>(specifics.guid(), specifics.origin());

  // Set info that has a default value (and does not distinguish whether it is
  // set or not).
  profile->set_use_count(specifics.use_count());
  profile->set_use_date(base::Time::FromTimeT(specifics.use_date()));
  profile->set_language_code(specifics.address_home_language_code());
  profile->SetValidityFromBitfieldValue(specifics.validity_state_bitfield());

  // Set repeated fields.
  if (specifics.name_first_size() > 0) {
    profile->SetRawInfo(NAME_FIRST, UTF8ToUTF16(specifics.name_first(0)));
  }
  if (specifics.name_middle_size() > 0) {
    profile->SetRawInfo(NAME_MIDDLE, UTF8ToUTF16(specifics.name_middle(0)));
  }
  if (specifics.name_last_size() > 0) {
    profile->SetRawInfo(NAME_LAST, UTF8ToUTF16(specifics.name_last(0)));
  }
  if (specifics.name_full_size() > 0) {
    profile->SetRawInfo(NAME_FULL, UTF8ToUTF16(specifics.name_full(0)));
  }
  if (specifics.email_address_size() > 0) {
    profile->SetRawInfo(EMAIL_ADDRESS, UTF8ToUTF16(specifics.email_address(0)));
  }
  if (specifics.phone_home_whole_number_size() > 0) {
    profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
                        UTF8ToUTF16(specifics.phone_home_whole_number(0)));
  }

  // Set simple single-valued fields.
  if (specifics.has_company_name()) {
    profile->SetRawInfo(COMPANY_NAME, UTF8ToUTF16(specifics.company_name()));
  }
  if (specifics.has_address_home_city()) {
    profile->SetRawInfo(ADDRESS_HOME_CITY,
                        UTF8ToUTF16(specifics.address_home_city()));
  }
  if (specifics.has_address_home_state()) {
    profile->SetRawInfo(ADDRESS_HOME_STATE,
                        UTF8ToUTF16(specifics.address_home_state()));
  }
  if (specifics.has_address_home_zip()) {
    profile->SetRawInfo(ADDRESS_HOME_ZIP,
                        UTF8ToUTF16(specifics.address_home_zip()));
  }
  if (specifics.has_address_home_sorting_code()) {
    profile->SetRawInfo(ADDRESS_HOME_SORTING_CODE,
                        UTF8ToUTF16(specifics.address_home_sorting_code()));
  }
  if (specifics.has_address_home_dependent_locality()) {
    profile->SetRawInfo(
        ADDRESS_HOME_DEPENDENT_LOCALITY,
        UTF8ToUTF16(specifics.address_home_dependent_locality()));
  }
  if (specifics.has_address_home_country()) {
    // Update the country field, which can contain either a country code (if set
    // by a newer version of Chrome), or a country name (if set by an older
    // version of Chrome).
    // TODO(jkrcal): Move this migration logic into Address::SetRawInfo()?
    base::string16 country_name_or_code =
        base::ASCIIToUTF16(specifics.address_home_country());
    std::string country_code =
        CountryNames::GetInstance()->GetCountryCode(country_name_or_code);
    profile->SetRawInfo(ADDRESS_HOME_COUNTRY, UTF8ToUTF16(country_code));
  }
  if (specifics.has_address_home_line1()) {
    profile->SetRawInfo(ADDRESS_HOME_LINE1,
                        UTF8ToUTF16(specifics.address_home_line1()));
  }
  if (specifics.has_address_home_line2()) {
    profile->SetRawInfo(ADDRESS_HOME_LINE2,
                        UTF8ToUTF16(specifics.address_home_line2()));
  }
  // Set first the deprecated subparts (line1 & line2) and only after that the
  // full address (street_address) so that the latter wins in case of conflict.
  // This is needed because all the address fields are backed by the same
  // storage.
  if (specifics.has_address_home_street_address()) {
    profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS,
                        UTF8ToUTF16(specifics.address_home_street_address()));
  }

  return profile;
}

std::string GetStorageKeyFromAutofillProfile(const AutofillProfile& entry) {
  // Validity of the guid is guaranteed by the database layer.
  DCHECK(base::IsValidGUID(entry.guid()));
  return entry.guid();
}

std::string GetStorageKeyFromAutofillProfileSpecifics(
    const AutofillProfileSpecifics& specifics) {
  if (!IsAutofillProfileSpecificsValid(specifics)) {
    return std::string();
  }
  return specifics.guid();
}

}  // namespace autofill