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