autofill_profile_syncable_service.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2014 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#include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
6
7#include "base/guid.h"
8#include "base/location.h"
9#include "base/logging.h"
10#include "base/strings/utf_string_conversions.h"
11#include "components/autofill/core/browser/autofill_country.h"
12#include "components/autofill/core/browser/autofill_profile.h"
13#include "components/autofill/core/browser/form_group.h"
14#include "components/autofill/core/browser/webdata/autofill_table.h"
15#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
16#include "components/webdata/common/web_database.h"
17#include "sync/api/sync_error.h"
18#include "sync/api/sync_error_factory.h"
19#include "sync/protocol/sync.pb.h"
20
21using base::ASCIIToUTF16;
22using base::UTF8ToUTF16;
23using base::UTF16ToUTF8;
24
25namespace autofill {
26
27namespace {
28
29std::string LimitData(const std::string& data) {
30  std::string sanitized_value(data);
31  if (sanitized_value.length() > AutofillTable::kMaxDataLength)
32    sanitized_value.resize(AutofillTable::kMaxDataLength);
33  return sanitized_value;
34}
35
36void* UserDataKey() {
37  // Use the address of a static that COMDAT folding won't ever fold
38  // with something else.
39  static int user_data_key = 0;
40  return reinterpret_cast<void*>(&user_data_key);
41}
42
43}  // namespace
44
45const char kAutofillProfileTag[] = "google_chrome_autofill_profiles";
46
47AutofillProfileSyncableService::AutofillProfileSyncableService(
48    AutofillWebDataBackend* webdata_backend,
49    const std::string& app_locale)
50    : webdata_backend_(webdata_backend),
51      app_locale_(app_locale),
52      scoped_observer_(this) {
53  DCHECK(webdata_backend_);
54
55  scoped_observer_.Add(webdata_backend_);
56}
57
58AutofillProfileSyncableService::~AutofillProfileSyncableService() {
59  DCHECK(CalledOnValidThread());
60}
61
62// static
63void AutofillProfileSyncableService::CreateForWebDataServiceAndBackend(
64    AutofillWebDataService* web_data_service,
65    AutofillWebDataBackend* webdata_backend,
66    const std::string& app_locale) {
67  web_data_service->GetDBUserData()->SetUserData(
68      UserDataKey(),
69      new AutofillProfileSyncableService(webdata_backend, app_locale));
70}
71
72// static
73AutofillProfileSyncableService*
74AutofillProfileSyncableService::FromWebDataService(
75    AutofillWebDataService* web_data_service) {
76  return static_cast<AutofillProfileSyncableService*>(
77      web_data_service->GetDBUserData()->GetUserData(UserDataKey()));
78}
79
80AutofillProfileSyncableService::AutofillProfileSyncableService()
81    : webdata_backend_(NULL),
82      scoped_observer_(this) {
83}
84
85syncer::SyncMergeResult
86AutofillProfileSyncableService::MergeDataAndStartSyncing(
87    syncer::ModelType type,
88    const syncer::SyncDataList& initial_sync_data,
89    scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
90    scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
91  DCHECK(CalledOnValidThread());
92  DCHECK(!sync_processor_.get());
93  DCHECK(sync_processor.get());
94  DCHECK(sync_error_factory.get());
95  DVLOG(1) << "Associating Autofill: MergeDataAndStartSyncing";
96
97  syncer::SyncMergeResult merge_result(type);
98  sync_error_factory_ = sync_error_factory.Pass();
99  if (!LoadAutofillData(&profiles_.get())) {
100    merge_result.set_error(sync_error_factory_->CreateAndUploadError(
101        FROM_HERE, "Could not get the autofill data from WebDatabase."));
102    return merge_result;
103  }
104
105  if (DLOG_IS_ON(INFO)) {
106    DVLOG(2) << "[AUTOFILL MIGRATION]"
107             << "Printing profiles from web db";
108
109    for (ScopedVector<AutofillProfile>::const_iterator ix =
110         profiles_.begin(); ix != profiles_.end(); ++ix) {
111      AutofillProfile* p = *ix;
112      DVLOG(2) << "[AUTOFILL MIGRATION]  "
113               << UTF16ToUTF8(p->GetRawInfo(NAME_FIRST))
114               << UTF16ToUTF8(p->GetRawInfo(NAME_LAST))
115               << p->guid();
116    }
117  }
118
119  sync_processor_ = sync_processor.Pass();
120
121  GUIDToProfileMap remaining_profiles;
122  CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles);
123
124  DataBundle bundle;
125  // Go through and check for all the profiles that sync already knows about.
126  for (syncer::SyncDataList::const_iterator sync_iter =
127           initial_sync_data.begin();
128       sync_iter != initial_sync_data.end();
129       ++sync_iter) {
130    GUIDToProfileMap::iterator it =
131        CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle);
132    // |it| points to created/updated profile. Add it to the |profiles_map_| and
133    // then remove it from |remaining_profiles|. After this loop is completed
134    // |remaining_profiles| will have only those profiles that are not in the
135    // sync.
136    profiles_map_[it->first] = it->second;
137    remaining_profiles.erase(it);
138  }
139
140  // Check for similar unmatched profiles - they are created independently on
141  // two systems, so merge them.
142  for (GUIDToProfileMap::iterator it = bundle.candidates_to_merge.begin();
143       it != bundle.candidates_to_merge.end(); ++it) {
144    GUIDToProfileMap::iterator profile_to_merge =
145        remaining_profiles.find(it->first);
146    if (profile_to_merge != remaining_profiles.end()) {
147      bundle.profiles_to_delete.push_back(profile_to_merge->second->guid());
148      if (MergeProfile(*(profile_to_merge->second), it->second, app_locale_))
149        bundle.profiles_to_sync_back.push_back(it->second);
150      DVLOG(2) << "[AUTOFILL SYNC]"
151               << "Found similar profile in sync db but with a different guid: "
152               << UTF16ToUTF8(it->second->GetRawInfo(NAME_FIRST))
153               << UTF16ToUTF8(it->second->GetRawInfo(NAME_LAST))
154               << "New guid " << it->second->guid()
155               << ". Profile to be deleted "
156               << profile_to_merge->second->guid();
157      remaining_profiles.erase(profile_to_merge);
158    }
159  }
160
161  if (!SaveChangesToWebData(bundle)) {
162    merge_result.set_error(sync_error_factory_->CreateAndUploadError(
163        FROM_HERE,
164        "Failed to update webdata."));
165    return merge_result;
166  }
167
168  syncer::SyncChangeList new_changes;
169  for (GUIDToProfileMap::iterator i = remaining_profiles.begin();
170       i != remaining_profiles.end(); ++i) {
171    new_changes.push_back(
172        syncer::SyncChange(FROM_HERE,
173                           syncer::SyncChange::ACTION_ADD,
174                           CreateData(*(i->second))));
175    profiles_map_[i->first] = i->second;
176  }
177
178  for (size_t i = 0; i < bundle.profiles_to_sync_back.size(); ++i) {
179    new_changes.push_back(
180        syncer::SyncChange(FROM_HERE,
181                           syncer::SyncChange::ACTION_UPDATE,
182                           CreateData(*(bundle.profiles_to_sync_back[i]))));
183  }
184
185  if (!new_changes.empty()) {
186    merge_result.set_error(
187        sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes));
188  }
189
190  if (webdata_backend_)
191    webdata_backend_->NotifyOfMultipleAutofillChanges();
192
193  return merge_result;
194}
195
196void AutofillProfileSyncableService::StopSyncing(syncer::ModelType type) {
197  DCHECK(CalledOnValidThread());
198  DCHECK_EQ(type, syncer::AUTOFILL_PROFILE);
199
200  sync_processor_.reset();
201  sync_error_factory_.reset();
202  profiles_.clear();
203  profiles_map_.clear();
204}
205
206syncer::SyncDataList AutofillProfileSyncableService::GetAllSyncData(
207    syncer::ModelType type) const {
208  DCHECK(CalledOnValidThread());
209  DCHECK(sync_processor_.get());
210  DCHECK_EQ(type, syncer::AUTOFILL_PROFILE);
211
212  syncer::SyncDataList current_data;
213
214  for (GUIDToProfileMap::const_iterator i = profiles_map_.begin();
215       i != profiles_map_.end(); ++i) {
216    current_data.push_back(CreateData(*(i->second)));
217  }
218  return current_data;
219}
220
221syncer::SyncError AutofillProfileSyncableService::ProcessSyncChanges(
222    const tracked_objects::Location& from_here,
223    const syncer::SyncChangeList& change_list) {
224  DCHECK(CalledOnValidThread());
225  if (!sync_processor_.get()) {
226    syncer::SyncError error(FROM_HERE,
227                            syncer::SyncError::DATATYPE_ERROR,
228                            "Models not yet associated.",
229                            syncer::AUTOFILL_PROFILE);
230    return error;
231  }
232
233  DataBundle bundle;
234
235  for (syncer::SyncChangeList::const_iterator i = change_list.begin();
236       i != change_list.end(); ++i) {
237    DCHECK(i->IsValid());
238    switch (i->change_type()) {
239      case syncer::SyncChange::ACTION_ADD:
240      case syncer::SyncChange::ACTION_UPDATE:
241        CreateOrUpdateProfile(i->sync_data(), &profiles_map_, &bundle);
242        break;
243      case syncer::SyncChange::ACTION_DELETE: {
244        std::string guid = i->sync_data().GetSpecifics().
245             autofill_profile().guid();
246        bundle.profiles_to_delete.push_back(guid);
247        profiles_map_.erase(guid);
248      } break;
249      default:
250        NOTREACHED() << "Unexpected sync change state.";
251        return sync_error_factory_->CreateAndUploadError(
252              FROM_HERE,
253              "ProcessSyncChanges failed on ChangeType " +
254                  syncer::SyncChange::ChangeTypeToString(i->change_type()));
255    }
256  }
257
258  if (!SaveChangesToWebData(bundle)) {
259    return sync_error_factory_->CreateAndUploadError(
260        FROM_HERE,
261        "Failed to update webdata.");
262  }
263
264  if (webdata_backend_)
265    webdata_backend_->NotifyOfMultipleAutofillChanges();
266
267  return syncer::SyncError();
268}
269
270void AutofillProfileSyncableService::AutofillProfileChanged(
271    const AutofillProfileChange& change) {
272  // Check if sync is on. If we receive notification prior to the sync being set
273  // up we are going to process all when MergeData..() is called. If we receive
274  // notification after the sync exited, it will be sinced next time Chrome
275  // starts.
276  if (sync_processor_.get()) {
277    ActOnChange(change);
278  } else if (!flare_.is_null()) {
279    flare_.Run(syncer::AUTOFILL_PROFILE);
280    flare_.Reset();
281  }
282}
283
284bool AutofillProfileSyncableService::LoadAutofillData(
285    std::vector<AutofillProfile*>* profiles) {
286  return GetAutofillTable()->GetAutofillProfiles(profiles);
287}
288
289bool AutofillProfileSyncableService::SaveChangesToWebData(
290    const DataBundle& bundle) {
291  DCHECK(CalledOnValidThread());
292
293  AutofillTable* autofill_table = GetAutofillTable();
294
295  bool success = true;
296  for (size_t i = 0; i< bundle.profiles_to_delete.size(); ++i) {
297    if (!autofill_table->RemoveAutofillProfile(bundle.profiles_to_delete[i]))
298      success = false;
299  }
300
301  for (size_t i = 0; i < bundle.profiles_to_add.size(); i++) {
302    if (!autofill_table->AddAutofillProfile(*bundle.profiles_to_add[i]))
303      success = false;
304  }
305
306  for (size_t i = 0; i < bundle.profiles_to_update.size(); i++) {
307    if (!autofill_table->UpdateAutofillProfile(*bundle.profiles_to_update[i]))
308      success = false;
309  }
310  return success;
311}
312
313// static
314bool AutofillProfileSyncableService::OverwriteProfileWithServerData(
315    const sync_pb::AutofillProfileSpecifics& specifics,
316    AutofillProfile* profile,
317    const std::string& app_locale) {
318  bool diff = false;
319  if (profile->origin() != specifics.origin()) {
320    bool was_verified = profile->IsVerified();
321    profile->set_origin(specifics.origin());
322    diff = true;
323
324    // Verified profiles should never be overwritten by unverified ones.
325    DCHECK(!was_verified || profile->IsVerified());
326  }
327
328  // Update all multivalued fields: names, emails, and phones.
329  diff = UpdateMultivaluedField(NAME_FIRST,
330                                specifics.name_first(), profile) || diff;
331  diff = UpdateMultivaluedField(NAME_MIDDLE,
332                                specifics.name_middle(), profile) || diff;
333  diff = UpdateMultivaluedField(NAME_LAST,
334                                specifics.name_last(), profile) || diff;
335  diff = UpdateMultivaluedField(EMAIL_ADDRESS,
336                                specifics.email_address(), profile) || diff;
337  diff = UpdateMultivaluedField(PHONE_HOME_WHOLE_NUMBER,
338                                specifics.phone_home_whole_number(),
339                                profile) || diff;
340
341  // Update all simple single-valued address fields.
342  diff = UpdateField(COMPANY_NAME, specifics.company_name(), profile) || diff;
343  diff = UpdateField(ADDRESS_HOME_CITY,
344                     specifics.address_home_city(), profile) || diff;
345  diff = UpdateField(ADDRESS_HOME_STATE,
346                     specifics.address_home_state(), profile) || diff;
347  diff = UpdateField(ADDRESS_HOME_ZIP,
348                     specifics.address_home_zip(), profile) || diff;
349  diff = UpdateField(ADDRESS_HOME_SORTING_CODE,
350                     specifics.address_home_sorting_code(), profile) || diff;
351  diff = UpdateField(ADDRESS_HOME_DEPENDENT_LOCALITY,
352                     specifics.address_home_dependent_locality(),
353                     profile) || diff;
354
355  // Update the country field, which can contain either a country code (if set
356  // by a newer version of Chrome), or a country name (if set by an older
357  // version of Chrome).
358  base::string16 country_name_or_code =
359      ASCIIToUTF16(specifics.address_home_country());
360  std::string country_code =
361      AutofillCountry::GetCountryCode(country_name_or_code, app_locale);
362  diff = UpdateField(ADDRESS_HOME_COUNTRY, country_code, profile) || diff;
363
364  // Update the street address.  In newer versions of Chrome (M34+), this data
365  // is stored in the |address_home_street_address| field.  In older versions,
366  // this data is stored separated out by address line.
367  if (specifics.has_address_home_street_address()) {
368    diff = UpdateField(ADDRESS_HOME_STREET_ADDRESS,
369                       specifics.address_home_street_address(),
370                       profile) || diff;
371  } else {
372    diff = UpdateField(ADDRESS_HOME_LINE1,
373                       specifics.address_home_line1(), profile) || diff;
374    diff = UpdateField(ADDRESS_HOME_LINE2,
375                       specifics.address_home_line2(), profile) || diff;
376  }
377  return diff;
378}
379
380// static
381void AutofillProfileSyncableService::WriteAutofillProfile(
382    const AutofillProfile& profile,
383    sync_pb::EntitySpecifics* profile_specifics) {
384  sync_pb::AutofillProfileSpecifics* specifics =
385      profile_specifics->mutable_autofill_profile();
386
387  DCHECK(base::IsValidGUID(profile.guid()));
388
389  // Reset all multi-valued fields in the protobuf.
390  specifics->clear_name_first();
391  specifics->clear_name_middle();
392  specifics->clear_name_last();
393  specifics->clear_email_address();
394  specifics->clear_phone_home_whole_number();
395
396  specifics->set_guid(profile.guid());
397  specifics->set_origin(profile.origin());
398
399  std::vector<base::string16> values;
400  profile.GetRawMultiInfo(NAME_FIRST, &values);
401  for (size_t i = 0; i < values.size(); ++i) {
402    specifics->add_name_first(LimitData(UTF16ToUTF8(values[i])));
403  }
404
405  profile.GetRawMultiInfo(NAME_MIDDLE, &values);
406  for (size_t i = 0; i < values.size(); ++i) {
407    specifics->add_name_middle(LimitData(UTF16ToUTF8(values[i])));
408  }
409
410  profile.GetRawMultiInfo(NAME_LAST, &values);
411  for (size_t i = 0; i < values.size(); ++i) {
412    specifics->add_name_last(LimitData(UTF16ToUTF8(values[i])));
413  }
414
415  specifics->set_address_home_line1(
416      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_LINE1))));
417  specifics->set_address_home_line2(
418      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_LINE2))));
419  specifics->set_address_home_city(
420      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_CITY))));
421  specifics->set_address_home_state(
422      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STATE))));
423  specifics->set_address_home_zip(
424      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_ZIP))));
425  specifics->set_address_home_country(
426      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY))));
427  specifics->set_address_home_street_address(
428      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS))));
429  specifics->set_address_home_sorting_code(
430      LimitData(UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE))));
431  specifics->set_address_home_dependent_locality(
432      LimitData(
433          UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY))));
434
435  profile.GetRawMultiInfo(EMAIL_ADDRESS, &values);
436  for (size_t i = 0; i < values.size(); ++i) {
437    specifics->add_email_address(LimitData(UTF16ToUTF8(values[i])));
438  }
439
440  specifics->set_company_name(
441      LimitData(UTF16ToUTF8(profile.GetRawInfo(COMPANY_NAME))));
442
443  profile.GetRawMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values);
444  for (size_t i = 0; i < values.size(); ++i) {
445    specifics->add_phone_home_whole_number(LimitData(UTF16ToUTF8(values[i])));
446  }
447}
448
449void AutofillProfileSyncableService::CreateGUIDToProfileMap(
450    const std::vector<AutofillProfile*>& profiles,
451    GUIDToProfileMap* profile_map) {
452  DCHECK(profile_map);
453  profile_map->clear();
454  for (size_t i = 0; i < profiles.size(); ++i)
455    (*profile_map)[profiles[i]->guid()] = profiles[i];
456}
457
458AutofillProfileSyncableService::GUIDToProfileMap::iterator
459AutofillProfileSyncableService::CreateOrUpdateProfile(
460    const syncer::SyncData& data,
461    GUIDToProfileMap* profile_map,
462    DataBundle* bundle) {
463  DCHECK(profile_map);
464  DCHECK(bundle);
465
466  DCHECK_EQ(syncer::AUTOFILL_PROFILE, data.GetDataType());
467
468  const sync_pb::EntitySpecifics& specifics = data.GetSpecifics();
469  const sync_pb::AutofillProfileSpecifics& autofill_specifics(
470      specifics.autofill_profile());
471
472  GUIDToProfileMap::iterator existing_profile = profile_map->find(
473        autofill_specifics.guid());
474  if (existing_profile != profile_map->end()) {
475    // The synced profile already exists locally.  It might need to be updated.
476    if (OverwriteProfileWithServerData(
477            autofill_specifics, existing_profile->second, app_locale_)) {
478      bundle->profiles_to_update.push_back(existing_profile->second);
479    }
480    return existing_profile;
481  }
482
483
484  // New profile synced.
485  AutofillProfile* new_profile = new AutofillProfile(
486      autofill_specifics.guid(), autofill_specifics.origin());
487  OverwriteProfileWithServerData(autofill_specifics, new_profile, app_locale_);
488
489  // Check if profile appears under a different guid.
490  // Unverified profiles should never overwrite verified ones.
491  for (GUIDToProfileMap::iterator it = profile_map->begin();
492       it != profile_map->end(); ++it) {
493    AutofillProfile* local_profile = it->second;
494    if (local_profile->Compare(*new_profile) == 0) {
495      // Ensure that a verified profile can never revert back to an unverified
496      // one.
497      if (local_profile->IsVerified() && !new_profile->IsVerified()) {
498        new_profile->set_origin(local_profile->origin());
499        bundle->profiles_to_sync_back.push_back(new_profile);
500      }
501
502      bundle->profiles_to_delete.push_back(local_profile->guid());
503      DVLOG(2) << "[AUTOFILL SYNC]"
504               << "Found in sync db but with a different guid: "
505               << UTF16ToUTF8(local_profile->GetRawInfo(NAME_FIRST))
506               << UTF16ToUTF8(local_profile->GetRawInfo(NAME_LAST))
507               << "New guid " << new_profile->guid()
508               << ". Profile to be deleted " << local_profile->guid();
509      profile_map->erase(it);
510      break;
511    } else if (!local_profile->IsVerified() &&
512               !new_profile->IsVerified() &&
513               !local_profile->PrimaryValue().empty() &&
514               local_profile->PrimaryValue() == new_profile->PrimaryValue()) {
515      // Add it to candidates for merge - if there is no profile with this
516      // guid we will merge them.
517      bundle->candidates_to_merge.insert(
518          std::make_pair(local_profile->guid(), new_profile));
519    }
520  }
521  profiles_.push_back(new_profile);
522  bundle->profiles_to_add.push_back(new_profile);
523  return profile_map->insert(std::make_pair(new_profile->guid(),
524                                            new_profile)).first;
525}
526
527void AutofillProfileSyncableService::ActOnChange(
528     const AutofillProfileChange& change) {
529  DCHECK((change.type() == AutofillProfileChange::REMOVE &&
530          !change.profile()) ||
531         (change.type() != AutofillProfileChange::REMOVE && change.profile()));
532  DCHECK(sync_processor_.get());
533  syncer::SyncChangeList new_changes;
534  DataBundle bundle;
535  switch (change.type()) {
536    case AutofillProfileChange::ADD:
537      new_changes.push_back(
538          syncer::SyncChange(FROM_HERE,
539                             syncer::SyncChange::ACTION_ADD,
540                             CreateData(*(change.profile()))));
541      DCHECK(profiles_map_.find(change.profile()->guid()) ==
542             profiles_map_.end());
543      profiles_.push_back(new AutofillProfile(*(change.profile())));
544      profiles_map_[change.profile()->guid()] = profiles_.get().back();
545      break;
546    case AutofillProfileChange::UPDATE: {
547      GUIDToProfileMap::iterator it = profiles_map_.find(
548          change.profile()->guid());
549      DCHECK(it != profiles_map_.end());
550      *(it->second) = *(change.profile());
551      new_changes.push_back(
552          syncer::SyncChange(FROM_HERE,
553                             syncer::SyncChange::ACTION_UPDATE,
554                             CreateData(*(change.profile()))));
555      break;
556    }
557    case AutofillProfileChange::REMOVE: {
558      AutofillProfile empty_profile(change.key(), std::string());
559      new_changes.push_back(
560          syncer::SyncChange(FROM_HERE,
561                             syncer::SyncChange::ACTION_DELETE,
562                             CreateData(empty_profile)));
563      profiles_map_.erase(change.key());
564      break;
565    }
566    default:
567      NOTREACHED();
568  }
569  syncer::SyncError error =
570      sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes);
571  if (error.IsSet()) {
572    // TODO(isherman): Investigating http://crbug.com/121592
573    VLOG(1) << "[AUTOFILL SYNC] "
574            << "Failed processing change:\n"
575            << "  Error: " << error.message() << "\n"
576            << "  Guid: " << change.key();
577  }
578}
579
580syncer::SyncData AutofillProfileSyncableService::CreateData(
581    const AutofillProfile& profile) {
582  sync_pb::EntitySpecifics specifics;
583  WriteAutofillProfile(profile, &specifics);
584  return
585      syncer::SyncData::CreateLocalData(
586          profile.guid(), profile.guid(), specifics);
587}
588
589bool AutofillProfileSyncableService::UpdateField(
590    ServerFieldType field_type,
591    const std::string& new_value,
592    AutofillProfile* autofill_profile) {
593  if (UTF16ToUTF8(autofill_profile->GetRawInfo(field_type)) == new_value)
594    return false;
595  autofill_profile->SetRawInfo(field_type, UTF8ToUTF16(new_value));
596  return true;
597}
598
599bool AutofillProfileSyncableService::UpdateMultivaluedField(
600    ServerFieldType field_type,
601    const ::google::protobuf::RepeatedPtrField<std::string>& new_values,
602    AutofillProfile* autofill_profile) {
603  std::vector<base::string16> values;
604  autofill_profile->GetRawMultiInfo(field_type, &values);
605  bool changed = false;
606  if (static_cast<size_t>(new_values.size()) != values.size()) {
607    values.clear();
608    values.resize(static_cast<size_t>(new_values.size()));
609    changed = true;
610  }
611  for (size_t i = 0; i < values.size(); ++i) {
612    base::string16 synced_value(
613        UTF8ToUTF16(new_values.Get(static_cast<int>(i))));
614    if (values[i] != synced_value) {
615      values[i] = synced_value;
616      changed = true;
617    }
618  }
619  if (changed)
620    autofill_profile->SetRawMultiInfo(field_type, values);
621  return changed;
622}
623
624bool AutofillProfileSyncableService::MergeProfile(
625    const AutofillProfile& merge_from,
626    AutofillProfile* merge_into,
627    const std::string& app_locale) {
628  merge_into->OverwriteWithOrAddTo(merge_from, app_locale);
629  return (merge_into->Compare(merge_from) != 0 ||
630          merge_into->origin() != merge_from.origin());
631}
632
633AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const {
634  return AutofillTable::FromWebDatabase(webdata_backend_->GetDatabase());
635}
636
637void AutofillProfileSyncableService::InjectStartSyncFlare(
638    const syncer::SyncableService::StartSyncFlare& flare) {
639  flare_ = flare;
640}
641
642AutofillProfileSyncableService::DataBundle::DataBundle() {}
643
644AutofillProfileSyncableService::DataBundle::~DataBundle() {}
645
646}  // namespace autofill
647