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, | 33 std::unique_ptr<syncer::EntityData> CreateEntityData( |
30 const std::string value) { | 34 const autofill::AutofillEntry& entry) { |
31 return net::EscapePath(name) + "|" + net::EscapePath(value); | 35 auto entity_data = base::MakeUnique<syncer::EntityData>(); |
| 36 entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name()); |
| 37 sync_pb::AutofillSpecifics* autofill = |
| 38 entity_data->specifics.mutable_autofill(); |
| 39 autofill->set_name(base::UTF16ToUTF8(entry.key().name())); |
| 40 autofill->set_value(base::UTF16ToUTF8(entry.key().value())); |
| 41 autofill->add_usage_timestamp(entry.date_created().ToInternalValue()); |
| 42 if (entry.date_created() != entry.date_last_used()) |
| 43 autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue()); |
| 44 return entity_data; |
32 } | 45 } |
33 | 46 |
34 } // namespace | 47 } // namespace |
35 | 48 |
36 namespace autofill { | 49 namespace autofill { |
37 | 50 |
38 // static | 51 // static |
39 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( | 52 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( |
40 AutofillWebDataService* web_data_service, | 53 AutofillWebDataService* web_data_service, |
41 AutofillWebDataBackend* web_data_backend) { | 54 AutofillWebDataBackend* web_data_backend) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 syncer::EntityChangeList entity_changes) { | 101 syncer::EntityChangeList entity_changes) { |
89 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
90 NOTIMPLEMENTED(); | 103 NOTIMPLEMENTED(); |
91 return syncer::SyncError(); | 104 return syncer::SyncError(); |
92 } | 105 } |
93 | 106 |
94 void AutocompleteSyncBridge::AutocompleteSyncBridge::GetData( | 107 void AutocompleteSyncBridge::AutocompleteSyncBridge::GetData( |
95 StorageKeyList storage_keys, | 108 StorageKeyList storage_keys, |
96 DataCallback callback) { | 109 DataCallback callback) { |
97 DCHECK(thread_checker_.CalledOnValidThread()); | 110 DCHECK(thread_checker_.CalledOnValidThread()); |
98 NOTIMPLEMENTED(); | 111 std::unordered_set<std::string> keys_set; |
| 112 for (const auto& key : storage_keys) { |
| 113 keys_set.insert(key); |
| 114 } |
| 115 |
| 116 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
| 117 std::vector<AutofillEntry> entries; |
| 118 GetAutofillTable()->GetAllAutofillEntries(&entries); |
| 119 for (const AutofillEntry& entry : entries) { |
| 120 std::string key = GetStorageKeyFromAutofillEntry(entry); |
| 121 if (keys_set.find(key) != keys_set.end()) { |
| 122 batch->Put(key, CreateEntityData(entry)); |
| 123 } |
| 124 } |
| 125 callback.Run(syncer::SyncError(), std::move(batch)); |
99 } | 126 } |
100 | 127 |
101 void AutocompleteSyncBridge::GetAllData(DataCallback callback) { | 128 void AutocompleteSyncBridge::GetAllData(DataCallback callback) { |
102 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
103 NOTIMPLEMENTED(); | 130 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
| 131 std::vector<AutofillEntry> entries; |
| 132 GetAutofillTable()->GetAllAutofillEntries(&entries); |
| 133 for (const AutofillEntry& entry : entries) { |
| 134 batch->Put(GetStorageKeyFromAutofillEntry(entry), CreateEntityData(entry)); |
| 135 } |
| 136 callback.Run(syncer::SyncError(), std::move(batch)); |
104 } | 137 } |
105 | 138 |
106 std::string AutocompleteSyncBridge::GetClientTag( | 139 std::string AutocompleteSyncBridge::GetClientTag( |
107 const syncer::EntityData& entity_data) { | 140 const syncer::EntityData& entity_data) { |
108 DCHECK(entity_data.specifics.has_autofill()); | 141 DCHECK(entity_data.specifics.has_autofill()); |
109 | 142 |
110 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); | 143 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); |
111 std::string storage_key = | 144 std::string storage_key = |
112 FormatStorageKey(specifics.name(), specifics.value()); | 145 FormatStorageKey(specifics.name(), specifics.value()); |
113 std::string prefix(kAutocompleteEntryNamespaceTag); | 146 std::string prefix(kAutocompleteEntryNamespaceTag); |
114 return prefix + storage_key; | 147 return prefix + storage_key; |
115 } | 148 } |
116 | 149 |
117 std::string AutocompleteSyncBridge::GetStorageKey( | 150 std::string AutocompleteSyncBridge::GetStorageKey( |
118 const syncer::EntityData& entity_data) { | 151 const syncer::EntityData& entity_data) { |
119 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); | 152 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); |
120 return FormatStorageKey(specifics.name(), specifics.value()); | 153 return FormatStorageKey(specifics.name(), specifics.value()); |
121 } | 154 } |
122 | 155 |
123 // AutofillWebDataServiceObserverOnDBThread implementation. | 156 // AutofillWebDataServiceObserverOnDBThread implementation. |
124 void AutocompleteSyncBridge::AutofillEntriesChanged( | 157 void AutocompleteSyncBridge::AutofillEntriesChanged( |
125 const AutofillChangeList& changes) { | 158 const AutofillChangeList& changes) { |
126 DCHECK(thread_checker_.CalledOnValidThread()); | 159 DCHECK(thread_checker_.CalledOnValidThread()); |
127 } | 160 } |
128 | 161 |
| 162 // static |
| 163 AutofillEntry AutocompleteSyncBridge::CreateAutofillEntry( |
| 164 const sync_pb::AutofillSpecifics& autofill_specifics) { |
| 165 AutofillKey key(base::UTF8ToUTF16(autofill_specifics.name()), |
| 166 base::UTF8ToUTF16(autofill_specifics.value())); |
| 167 base::Time date_created, date_last_used; |
| 168 const google::protobuf::RepeatedField<int64_t>& timestamps = |
| 169 autofill_specifics.usage_timestamp(); |
| 170 if (!timestamps.empty()) { |
| 171 date_created = base::Time::FromInternalValue(*timestamps.begin()); |
| 172 date_last_used = base::Time::FromInternalValue(*timestamps.rbegin()); |
| 173 } |
| 174 return AutofillEntry(key, date_created, date_last_used); |
| 175 } |
| 176 |
129 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { | 177 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { |
130 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); | 178 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); |
131 } | 179 } |
132 | 180 |
| 181 std::string AutocompleteSyncBridge::GetStorageKeyFromAutofillEntry( |
| 182 const autofill::AutofillEntry& entry) { |
| 183 return FormatStorageKey(base::UTF16ToUTF8(entry.key().name()), |
| 184 base::UTF16ToUTF8(entry.key().value())); |
| 185 } |
| 186 |
| 187 // static |
| 188 std::string AutocompleteSyncBridge::FormatStorageKey(const std::string& name, |
| 189 const std::string& value) { |
| 190 return net::EscapePath(name) + "|" + net::EscapePath(value); |
| 191 } |
| 192 |
133 } // namespace autofill | 193 } // namespace autofill |
OLD | NEW |