 Chromium Code Reviews
 Chromium Code Reviews Issue 2582713003:
  [USS] Impelementation for GetData and GetAllData  (Closed)
    
  
    Issue 2582713003:
  [USS] Impelementation for GetData and GetAllData  (Closed) 
  | Index: components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc | 
| diff --git a/components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc b/components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc | 
| index 3a76db0b5d0a5be7b9ded4b4062b4ee87db1befb..1767500648951982b01e09dee9beb0976ae13fab 100644 | 
| --- a/components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc | 
| +++ b/components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc | 
| @@ -4,14 +4,18 @@ | 
| #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h" | 
| +#include <unordered_set> | 
| + | 
| #include "base/bind.h" | 
| #include "base/memory/ptr_util.h" | 
| +#include "base/strings/utf_string_conversions.h" | 
| #include "components/autofill/core/browser/webdata/autofill_metadata_change_list.h" | 
| #include "components/autofill/core/browser/webdata/autofill_table.h" | 
| #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" | 
| #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 
| #include "components/sync/model/entity_data.h" | 
| #include "components/sync/model/model_type_change_processor.h" | 
| +#include "components/sync/model/mutable_data_batch.h" | 
| #include "components/sync/model/sync_error.h" | 
| #include "net/base/escape.h" | 
| @@ -26,11 +30,6 @@ void* UserDataKey() { | 
| return reinterpret_cast<void*>(&user_data_key); | 
| } | 
| -const std::string FormatStorageKey(const std::string name, | 
| - const std::string value) { | 
| - return net::EscapePath(name) + "|" + net::EscapePath(value); | 
| -} | 
| - | 
| } // namespace | 
| namespace autofill { | 
| @@ -95,12 +94,32 @@ void AutocompleteSyncBridge::AutocompleteSyncBridge::GetData( | 
| StorageKeyList storage_keys, | 
| DataCallback callback) { | 
| DCHECK(thread_checker_.CalledOnValidThread()); | 
| - NOTIMPLEMENTED(); | 
| + std::unordered_set<std::string> keys_set; | 
| + for (const auto& key : storage_keys) { | 
| + keys_set.insert(key); | 
| + } | 
| + | 
| + auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 
| + std::vector<AutofillEntry> entries; | 
| + GetAutofillTable()->GetAllAutofillEntries(&entries); | 
| + for (const AutofillEntry& it : entries) { | 
| + std::string key = GetStorageKeyFromAutofillEntry(it); | 
| + if (keys_set.find(key) != keys_set.end()) { | 
| + batch->Put(key, CreateEntityData(it)); | 
| + } | 
| + } | 
| + callback.Run(syncer::SyncError(), std::move(batch)); | 
| } | 
| void AutocompleteSyncBridge::GetAllData(DataCallback callback) { | 
| DCHECK(thread_checker_.CalledOnValidThread()); | 
| - NOTIMPLEMENTED(); | 
| + auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 
| + std::vector<AutofillEntry> entries; | 
| + GetAutofillTable()->GetAllAutofillEntries(&entries); | 
| + for (const AutofillEntry& it : entries) { | 
| + batch->Put(GetStorageKeyFromAutofillEntry(it), CreateEntityData(it)); | 
| + } | 
| + callback.Run(syncer::SyncError(), std::move(batch)); | 
| } | 
| std::string AutocompleteSyncBridge::GetClientTag( | 
| @@ -130,4 +149,47 @@ AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { | 
| return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); | 
| } | 
| +const std::string AutocompleteSyncBridge::GetStorageKeyFromAutofillEntry( | 
| + const autofill::AutofillEntry& entry) { | 
| + return FormatStorageKey(base::UTF16ToUTF8(entry.key().name()), | 
| + base::UTF16ToUTF8(entry.key().value())); | 
| +} | 
| + | 
| +// static | 
| +const std::string AutocompleteSyncBridge::FormatStorageKey( | 
| + const std::string& name, | 
| + const std::string& value) { | 
| + return net::EscapePath(name) + "|" + net::EscapePath(value); | 
| +} | 
| + | 
| +// static | 
| +std::unique_ptr<syncer::EntityData> AutocompleteSyncBridge::CreateEntityData( | 
| 
skym
2016/12/19 17:40:08
Looks like you could get away with this being an a
 
Gang Wu
2016/12/19 23:28:09
Done.
 | 
| + const AutofillEntry& entry) { | 
| + auto entity_data = base::MakeUnique<syncer::EntityData>(); | 
| + entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name()); | 
| + sync_pb::AutofillSpecifics* autofill = | 
| + entity_data->specifics.mutable_autofill(); | 
| + autofill->set_name(base::UTF16ToUTF8(entry.key().name())); | 
| + autofill->set_value(base::UTF16ToUTF8(entry.key().value())); | 
| + autofill->add_usage_timestamp(entry.date_created().ToInternalValue()); | 
| + if (entry.date_created() != entry.date_last_used()) | 
| + autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue()); | 
| + return entity_data; | 
| +} | 
| + | 
| +// static | 
| +AutofillEntry AutocompleteSyncBridge::CreateAutofillEntry( | 
| + const sync_pb::AutofillSpecifics* autofill_specifics) { | 
| + AutofillKey key(autofill_specifics->name().c_str(), | 
| + autofill_specifics->value().c_str()); | 
| + base::Time date_created, date_last_used; | 
| + const google::protobuf::RepeatedField<int64_t>& timestamps = | 
| + autofill_specifics->usage_timestamp(); | 
| + if (timestamps.size() > 0) { | 
| + date_created = base::Time::FromInternalValue(*timestamps.begin()); | 
| + date_last_used = base::Time::FromInternalValue(*timestamps.rbegin()); | 
| + } | 
| + return AutofillEntry(key, date_created, date_last_used); | 
| +} | 
| + | 
| } // namespace autofill |