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> | 7 #include <unordered_set> |
| 8 #include <utility> |
| 9 #include <vector> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/browser/proto/autofill_sync.pb.h" |
12 #include "components/autofill/core/browser/webdata/autofill_metadata_change_list
.h" | 15 #include "components/autofill/core/browser/webdata/autofill_metadata_change_list
.h" |
13 #include "components/autofill/core/browser/webdata/autofill_table.h" | 16 #include "components/autofill/core/browser/webdata/autofill_table.h" |
14 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" | 17 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h" |
15 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 18 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
16 #include "components/sync/model/entity_data.h" | 19 #include "components/sync/model/entity_data.h" |
17 #include "components/sync/model/model_type_change_processor.h" | 20 #include "components/sync/model/model_type_change_processor.h" |
18 #include "components/sync/model/mutable_data_batch.h" | 21 #include "components/sync/model/mutable_data_batch.h" |
19 #include "components/sync/model/sync_error.h" | 22 #include "components/sync/model/sync_error.h" |
20 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
21 | 24 |
| 25 namespace autofill { |
| 26 |
22 namespace { | 27 namespace { |
23 | 28 |
24 const char kAutocompleteEntryNamespaceTag[] = "autofill_entry|"; | 29 const char kAutocompleteEntryNamespaceTag[] = "autofill_entry|"; |
| 30 const char kAutocompleteTagDelimiter[] = "|"; |
25 | 31 |
26 void* UserDataKey() { | 32 void* UserDataKey() { |
27 // Use the address of a static that COMDAT folding won't ever collide | 33 // Use the address of a static that COMDAT folding won't ever collide |
28 // with something else. | 34 // with something else. |
29 static int user_data_key = 0; | 35 static int user_data_key = 0; |
30 return reinterpret_cast<void*>(&user_data_key); | 36 return reinterpret_cast<void*>(&user_data_key); |
31 } | 37 } |
32 | 38 |
33 std::unique_ptr<syncer::EntityData> CreateEntityData( | 39 std::unique_ptr<syncer::EntityData> CreateEntityData( |
34 const autofill::AutofillEntry& entry) { | 40 const AutofillEntry& entry) { |
35 auto entity_data = base::MakeUnique<syncer::EntityData>(); | 41 auto entity_data = base::MakeUnique<syncer::EntityData>(); |
36 entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name()); | 42 entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name()); |
37 sync_pb::AutofillSpecifics* autofill = | 43 sync_pb::AutofillSpecifics* autofill = |
38 entity_data->specifics.mutable_autofill(); | 44 entity_data->specifics.mutable_autofill(); |
39 autofill->set_name(base::UTF16ToUTF8(entry.key().name())); | 45 autofill->set_name(base::UTF16ToUTF8(entry.key().name())); |
40 autofill->set_value(base::UTF16ToUTF8(entry.key().value())); | 46 autofill->set_value(base::UTF16ToUTF8(entry.key().value())); |
41 autofill->add_usage_timestamp(entry.date_created().ToInternalValue()); | 47 autofill->add_usage_timestamp(entry.date_created().ToInternalValue()); |
42 if (entry.date_created() != entry.date_last_used()) | 48 if (entry.date_created() != entry.date_last_used()) |
43 autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue()); | 49 autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue()); |
44 return entity_data; | 50 return entity_data; |
45 } | 51 } |
46 | 52 |
| 53 std::string BuildSerializedStorageKey(const std::string& name, |
| 54 const std::string& value) { |
| 55 AutofillSyncStorageKey proto; |
| 56 proto.set_name(name); |
| 57 proto.set_value(value); |
| 58 return proto.SerializeAsString(); |
| 59 } |
| 60 |
| 61 std::string GetStorageKeyFromModel(const AutofillKey& key) { |
| 62 return BuildSerializedStorageKey(base::UTF16ToUTF8(key.name()), |
| 63 base::UTF16ToUTF8(key.value())); |
| 64 } |
| 65 |
47 } // namespace | 66 } // namespace |
48 | 67 |
49 namespace autofill { | |
50 | |
51 // static | 68 // static |
52 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( | 69 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( |
53 AutofillWebDataService* web_data_service, | 70 AutofillWebDataService* web_data_service, |
54 AutofillWebDataBackend* web_data_backend) { | 71 AutofillWebDataBackend* web_data_backend) { |
55 web_data_service->GetDBUserData()->SetUserData( | 72 web_data_service->GetDBUserData()->SetUserData( |
56 UserDataKey(), | 73 UserDataKey(), |
57 new AutocompleteSyncBridge( | 74 new AutocompleteSyncBridge( |
58 web_data_backend, | 75 web_data_backend, |
59 base::Bind(&syncer::ModelTypeChangeProcessor::Create))); | 76 base::Bind(&syncer::ModelTypeChangeProcessor::Create))); |
60 } | 77 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 DCHECK(thread_checker_.CalledOnValidThread()); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
111 std::unordered_set<std::string> keys_set; | 128 std::unordered_set<std::string> keys_set; |
112 for (const auto& key : storage_keys) { | 129 for (const auto& key : storage_keys) { |
113 keys_set.insert(key); | 130 keys_set.insert(key); |
114 } | 131 } |
115 | 132 |
116 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 133 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
117 std::vector<AutofillEntry> entries; | 134 std::vector<AutofillEntry> entries; |
118 GetAutofillTable()->GetAllAutofillEntries(&entries); | 135 GetAutofillTable()->GetAllAutofillEntries(&entries); |
119 for (const AutofillEntry& entry : entries) { | 136 for (const AutofillEntry& entry : entries) { |
120 std::string key = GetStorageKeyFromAutofillEntry(entry); | 137 std::string key = GetStorageKeyFromModel(entry.key()); |
121 if (keys_set.find(key) != keys_set.end()) { | 138 if (keys_set.find(key) != keys_set.end()) { |
122 batch->Put(key, CreateEntityData(entry)); | 139 batch->Put(key, CreateEntityData(entry)); |
123 } | 140 } |
124 } | 141 } |
125 callback.Run(syncer::SyncError(), std::move(batch)); | 142 callback.Run(syncer::SyncError(), std::move(batch)); |
126 } | 143 } |
127 | 144 |
128 void AutocompleteSyncBridge::GetAllData(DataCallback callback) { | 145 void AutocompleteSyncBridge::GetAllData(DataCallback callback) { |
129 DCHECK(thread_checker_.CalledOnValidThread()); | 146 DCHECK(thread_checker_.CalledOnValidThread()); |
130 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); | 147 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); |
131 std::vector<AutofillEntry> entries; | 148 std::vector<AutofillEntry> entries; |
132 GetAutofillTable()->GetAllAutofillEntries(&entries); | 149 GetAutofillTable()->GetAllAutofillEntries(&entries); |
133 for (const AutofillEntry& entry : entries) { | 150 for (const AutofillEntry& entry : entries) { |
134 batch->Put(GetStorageKeyFromAutofillEntry(entry), CreateEntityData(entry)); | 151 batch->Put(GetStorageKeyFromModel(entry.key()), CreateEntityData(entry)); |
135 } | 152 } |
136 callback.Run(syncer::SyncError(), std::move(batch)); | 153 callback.Run(syncer::SyncError(), std::move(batch)); |
137 } | 154 } |
138 | 155 |
139 std::string AutocompleteSyncBridge::GetClientTag( | 156 std::string AutocompleteSyncBridge::GetClientTag( |
140 const syncer::EntityData& entity_data) { | 157 const syncer::EntityData& entity_data) { |
141 DCHECK(entity_data.specifics.has_autofill()); | 158 DCHECK(entity_data.specifics.has_autofill()); |
142 | |
143 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); | 159 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); |
144 std::string storage_key = | 160 return std::string(kAutocompleteEntryNamespaceTag) + |
145 FormatStorageKey(specifics.name(), specifics.value()); | 161 net::EscapePath(specifics.name()) + |
146 std::string prefix(kAutocompleteEntryNamespaceTag); | 162 std::string(kAutocompleteTagDelimiter) + |
147 return prefix + storage_key; | 163 net::EscapePath(specifics.value()); |
148 } | 164 } |
149 | 165 |
150 std::string AutocompleteSyncBridge::GetStorageKey( | 166 std::string AutocompleteSyncBridge::GetStorageKey( |
151 const syncer::EntityData& entity_data) { | 167 const syncer::EntityData& entity_data) { |
| 168 DCHECK(entity_data.specifics.has_autofill()); |
152 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); | 169 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); |
153 return FormatStorageKey(specifics.name(), specifics.value()); | 170 return BuildSerializedStorageKey(specifics.name(), specifics.value()); |
154 } | 171 } |
155 | 172 |
156 // AutofillWebDataServiceObserverOnDBThread implementation. | 173 // AutofillWebDataServiceObserverOnDBThread implementation. |
157 void AutocompleteSyncBridge::AutofillEntriesChanged( | 174 void AutocompleteSyncBridge::AutofillEntriesChanged( |
158 const AutofillChangeList& changes) { | 175 const AutofillChangeList& changes) { |
159 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
160 } | 177 } |
161 | 178 |
162 // static | 179 // static |
163 AutofillEntry AutocompleteSyncBridge::CreateAutofillEntry( | 180 AutofillEntry AutocompleteSyncBridge::CreateAutofillEntry( |
164 const sync_pb::AutofillSpecifics& autofill_specifics) { | 181 const sync_pb::AutofillSpecifics& autofill_specifics) { |
165 AutofillKey key(base::UTF8ToUTF16(autofill_specifics.name()), | 182 AutofillKey key(base::UTF8ToUTF16(autofill_specifics.name()), |
166 base::UTF8ToUTF16(autofill_specifics.value())); | 183 base::UTF8ToUTF16(autofill_specifics.value())); |
167 base::Time date_created, date_last_used; | 184 base::Time date_created, date_last_used; |
168 const google::protobuf::RepeatedField<int64_t>& timestamps = | 185 const google::protobuf::RepeatedField<int64_t>& timestamps = |
169 autofill_specifics.usage_timestamp(); | 186 autofill_specifics.usage_timestamp(); |
170 if (!timestamps.empty()) { | 187 if (!timestamps.empty()) { |
171 date_created = base::Time::FromInternalValue(*timestamps.begin()); | 188 date_created = base::Time::FromInternalValue(*timestamps.begin()); |
172 date_last_used = base::Time::FromInternalValue(*timestamps.rbegin()); | 189 date_last_used = base::Time::FromInternalValue(*timestamps.rbegin()); |
173 } | 190 } |
174 return AutofillEntry(key, date_created, date_last_used); | 191 return AutofillEntry(key, date_created, date_last_used); |
175 } | 192 } |
176 | 193 |
177 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { | 194 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { |
178 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); | 195 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); |
179 } | 196 } |
180 | 197 |
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 | |
193 } // namespace autofill | 198 } // namespace autofill |
OLD | NEW |