Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h" | 5 #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h" |
| 6 | 6 |
| 7 #include <unordered_set> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "components/autofill/core/browser/webdata/autofill_metadata_change_list .h" | 12 #include "components/autofill/core/browser/webdata/autofill_metadata_change_list .h" |
| 10 #include "components/autofill/core/browser/webdata/autofill_table.h" | 13 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 11 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" | 14 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" |
| 12 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 15 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 13 #include "components/sync/model/entity_data.h" | 16 #include "components/sync/model/entity_data.h" |
| 14 #include "components/sync/model/model_type_change_processor.h" | 17 #include "components/sync/model/model_type_change_processor.h" |
| 18 #include "components/sync/model/mutable_data_batch.h" | |
| 15 #include "components/sync/model/sync_error.h" | 19 #include "components/sync/model/sync_error.h" |
| 16 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 const char kAutocompleteEntryNamespaceTag[] = "autofill_entry|"; | 24 const char kAutocompleteEntryNamespaceTag[] = "autofill_entry|"; |
| 21 | 25 |
| 22 void* UserDataKey() { | 26 void* UserDataKey() { |
| 23 // Use the address of a static that COMDAT folding won't ever collide | 27 // Use the address of a static that COMDAT folding won't ever collide |
| 24 // with something else. | 28 // with something else. |
| 25 static int user_data_key = 0; | 29 static int user_data_key = 0; |
| 26 return reinterpret_cast<void*>(&user_data_key); | 30 return reinterpret_cast<void*>(&user_data_key); |
| 27 } | 31 } |
| 28 | 32 |
| 29 const std::string FormatStorageKey(const std::string name, | |
| 30 const std::string value) { | |
| 31 return net::EscapePath(name) + "|" + net::EscapePath(value); | |
| 32 } | |
| 33 | |
| 34 } // namespace | 33 } // namespace |
| 35 | 34 |
| 36 namespace autofill { | 35 namespace autofill { |
| 37 | 36 |
| 38 // static | 37 // static |
| 39 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( | 38 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( |
| 40 AutofillWebDataService* web_data_service, | 39 AutofillWebDataService* web_data_service, |
| 41 AutofillWebDataBackend* web_data_backend) { | 40 AutofillWebDataBackend* web_data_backend) { |
| 42 web_data_service->GetDBUserData()->SetUserData( | 41 web_data_service->GetDBUserData()->SetUserData( |
| 43 UserDataKey(), | 42 UserDataKey(), |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 syncer::EntityChangeList entity_changes) { | 87 syncer::EntityChangeList entity_changes) { |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
| 91 return syncer::SyncError(); | 90 return syncer::SyncError(); |
| 92 } | 91 } |
| 93 | 92 |
| 94 void AutocompleteSyncBridge::AutocompleteSyncBridge::GetData( | 93 void AutocompleteSyncBridge::AutocompleteSyncBridge::GetData( |
| 95 StorageKeyList storage_keys, | 94 StorageKeyList storage_keys, |
| 96 DataCallback callback) { | 95 DataCallback callback) { |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); | 96 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 NOTIMPLEMENTED(); | 97 std::unordered_set<std::string> keys_set; |
| 98 for (const auto& key : storage_keys) { | |
| 99 keys_set.insert(key); | |
| 100 } | |
| 101 | |
| 102 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | |
| 103 std::vector<AutofillEntry> entries; | |
| 104 GetAutofillTable()->GetAllAutofillEntries(&entries); | |
| 105 for (const AutofillEntry& it : entries) { | |
| 106 std::string key = GetStorageKeyFromAutofillEntry(it); | |
| 107 if (keys_set.find(key) != keys_set.end()) { | |
| 108 batch->Put(key, CreateEntityData(it)); | |
| 109 } | |
| 110 } | |
| 111 callback.Run(syncer::SyncError(), std::move(batch)); | |
| 99 } | 112 } |
| 100 | 113 |
| 101 void AutocompleteSyncBridge::GetAllData(DataCallback callback) { | 114 void AutocompleteSyncBridge::GetAllData(DataCallback callback) { |
| 102 DCHECK(thread_checker_.CalledOnValidThread()); | 115 DCHECK(thread_checker_.CalledOnValidThread()); |
| 103 NOTIMPLEMENTED(); | 116 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
| 117 std::vector<AutofillEntry> entries; | |
| 118 GetAutofillTable()->GetAllAutofillEntries(&entries); | |
| 119 for (const AutofillEntry& it : entries) { | |
| 120 batch->Put(GetStorageKeyFromAutofillEntry(it), CreateEntityData(it)); | |
| 121 } | |
| 122 callback.Run(syncer::SyncError(), std::move(batch)); | |
| 104 } | 123 } |
| 105 | 124 |
| 106 std::string AutocompleteSyncBridge::GetClientTag( | 125 std::string AutocompleteSyncBridge::GetClientTag( |
| 107 const syncer::EntityData& entity_data) { | 126 const syncer::EntityData& entity_data) { |
| 108 DCHECK(entity_data.specifics.has_autofill()); | 127 DCHECK(entity_data.specifics.has_autofill()); |
| 109 | 128 |
| 110 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); | 129 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); |
| 111 std::string storage_key = | 130 std::string storage_key = |
| 112 FormatStorageKey(specifics.name(), specifics.value()); | 131 FormatStorageKey(specifics.name(), specifics.value()); |
| 113 std::string prefix(kAutocompleteEntryNamespaceTag); | 132 std::string prefix(kAutocompleteEntryNamespaceTag); |
| 114 return prefix + storage_key; | 133 return prefix + storage_key; |
| 115 } | 134 } |
| 116 | 135 |
| 117 std::string AutocompleteSyncBridge::GetStorageKey( | 136 std::string AutocompleteSyncBridge::GetStorageKey( |
| 118 const syncer::EntityData& entity_data) { | 137 const syncer::EntityData& entity_data) { |
| 119 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); | 138 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); |
| 120 return FormatStorageKey(specifics.name(), specifics.value()); | 139 return FormatStorageKey(specifics.name(), specifics.value()); |
| 121 } | 140 } |
| 122 | 141 |
| 123 // AutofillWebDataServiceObserverOnDBThread implementation. | 142 // AutofillWebDataServiceObserverOnDBThread implementation. |
| 124 void AutocompleteSyncBridge::AutofillEntriesChanged( | 143 void AutocompleteSyncBridge::AutofillEntriesChanged( |
| 125 const AutofillChangeList& changes) { | 144 const AutofillChangeList& changes) { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | 145 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 } | 146 } |
| 128 | 147 |
| 129 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { | 148 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { |
| 130 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); | 149 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); |
| 131 } | 150 } |
| 132 | 151 |
| 152 const std::string AutocompleteSyncBridge::GetStorageKeyFromAutofillEntry( | |
| 153 const autofill::AutofillEntry& entry) { | |
| 154 return FormatStorageKey(base::UTF16ToUTF8(entry.key().name()), | |
| 155 base::UTF16ToUTF8(entry.key().value())); | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 const std::string AutocompleteSyncBridge::FormatStorageKey( | |
| 160 const std::string& name, | |
| 161 const std::string& value) { | |
| 162 return net::EscapePath(name) + "|" + net::EscapePath(value); | |
| 163 } | |
| 164 | |
| 165 // static | |
| 166 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.
| |
| 167 const AutofillEntry& entry) { | |
| 168 auto entity_data = base::MakeUnique<syncer::EntityData>(); | |
| 169 entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name()); | |
| 170 sync_pb::AutofillSpecifics* autofill = | |
| 171 entity_data->specifics.mutable_autofill(); | |
| 172 autofill->set_name(base::UTF16ToUTF8(entry.key().name())); | |
| 173 autofill->set_value(base::UTF16ToUTF8(entry.key().value())); | |
| 174 autofill->add_usage_timestamp(entry.date_created().ToInternalValue()); | |
| 175 if (entry.date_created() != entry.date_last_used()) | |
| 176 autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue()); | |
| 177 return entity_data; | |
| 178 } | |
| 179 | |
| 180 // static | |
| 181 AutofillEntry AutocompleteSyncBridge::CreateAutofillEntry( | |
| 182 const sync_pb::AutofillSpecifics* autofill_specifics) { | |
| 183 AutofillKey key(autofill_specifics->name().c_str(), | |
| 184 autofill_specifics->value().c_str()); | |
| 185 base::Time date_created, date_last_used; | |
| 186 const google::protobuf::RepeatedField<int64_t>& timestamps = | |
| 187 autofill_specifics->usage_timestamp(); | |
| 188 if (timestamps.size() > 0) { | |
| 189 date_created = base::Time::FromInternalValue(*timestamps.begin()); | |
| 190 date_last_used = base::Time::FromInternalValue(*timestamps.rbegin()); | |
| 191 } | |
| 192 return AutofillEntry(key, date_created, date_last_used); | |
| 193 } | |
| 194 | |
| 133 } // namespace autofill | 195 } // namespace autofill |
| OLD | NEW |