Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Side by Side Diff: components/autofill/core/browser/webdata/autocomplete_sync_bridge.cc

Issue 2620783002: [sync] Handle local changes in AutocompleteSyncBridge (Closed)
Patch Set: self-review cleanups Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 22 matching lines...) Expand all
33 // with something else. 33 // with something else.
34 static int user_data_key = 0; 34 static int user_data_key = 0;
35 return reinterpret_cast<void*>(&user_data_key); 35 return reinterpret_cast<void*>(&user_data_key);
36 } 36 }
37 37
38 std::unique_ptr<syncer::EntityData> CreateEntityData( 38 std::unique_ptr<syncer::EntityData> CreateEntityData(
39 const AutofillEntry& entry) { 39 const AutofillEntry& entry) {
40 auto entity_data = base::MakeUnique<syncer::EntityData>(); 40 auto entity_data = base::MakeUnique<syncer::EntityData>();
41 entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name()); 41 entity_data->non_unique_name = base::UTF16ToUTF8(entry.key().name());
42 sync_pb::AutofillSpecifics* autofill = 42 sync_pb::AutofillSpecifics* autofill =
43 entity_data->specifics.mutable_autofill(); 43 entity_data->specifics.mutable_autofill();
maxbogue 2017/01/12 01:33:35 PopulateAutofillSpecifics(entry, entity_data->spec
44 autofill->set_name(base::UTF16ToUTF8(entry.key().name())); 44 autofill->set_name(base::UTF16ToUTF8(entry.key().name()));
45 autofill->set_value(base::UTF16ToUTF8(entry.key().value())); 45 autofill->set_value(base::UTF16ToUTF8(entry.key().value()));
46 autofill->add_usage_timestamp(entry.date_created().ToInternalValue()); 46 autofill->add_usage_timestamp(entry.date_created().ToInternalValue());
47 if (entry.date_created() != entry.date_last_used()) 47 if (entry.date_created() != entry.date_last_used())
48 autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue()); 48 autofill->add_usage_timestamp(entry.date_last_used().ToInternalValue());
49 return entity_data; 49 return entity_data;
50 } 50 }
51 51
52 std::string BuildSerializedStorageKey(const std::string& name, 52 std::string BuildSerializedStorageKey(const std::string& name,
53 const std::string& value) { 53 const std::string& value) {
54 AutofillSyncStorageKey proto; 54 AutofillSyncStorageKey proto;
55 proto.set_name(name); 55 proto.set_name(name);
56 proto.set_value(value); 56 proto.set_value(value);
57 return proto.SerializeAsString(); 57 return proto.SerializeAsString();
58 } 58 }
59 59
60 std::string GetStorageKeyFromModel(const AutofillKey& key) { 60 void WriteAutofillEntry(const AutofillEntry& entry,
maxbogue 2017/01/12 01:33:35 A more idiomatic name for this function would be P
Patrick Noland 2017/01/13 23:31:52 Actually I don't think this is needed at all
61 return BuildSerializedStorageKey(base::UTF16ToUTF8(key.name()), 61 sync_pb::AutofillSpecifics* autofill_specifics) {
62 base::UTF16ToUTF8(key.value())); 62 autofill_specifics->set_name(base::UTF16ToUTF8(entry.key().name()));
63 autofill_specifics->set_value(base::UTF16ToUTF8(entry.key().value()));
64 autofill_specifics->add_usage_timestamp(
65 entry.date_created().ToInternalValue());
66 if (entry.date_created() != entry.date_last_used()) {
67 autofill_specifics->add_usage_timestamp(
68 entry.date_last_used().ToInternalValue());
69 }
63 } 70 }
64 71
65 } // namespace 72 } // namespace
66 73
67 // static 74 // static
68 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( 75 void AutocompleteSyncBridge::CreateForWebDataServiceAndBackend(
69 AutofillWebDataService* web_data_service, 76 AutofillWebDataService* web_data_service,
70 AutofillWebDataBackend* web_data_backend) { 77 AutofillWebDataBackend* web_data_backend) {
71 web_data_service->GetDBUserData()->SetUserData( 78 web_data_service->GetDBUserData()->SetUserData(
72 UserDataKey(), 79 UserDataKey(),
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 return; 163 return;
157 } 164 }
158 165
159 auto batch = base::MakeUnique<syncer::MutableDataBatch>(); 166 auto batch = base::MakeUnique<syncer::MutableDataBatch>();
160 for (const AutofillEntry& entry : entries) { 167 for (const AutofillEntry& entry : entries) {
161 batch->Put(GetStorageKeyFromModel(entry.key()), CreateEntityData(entry)); 168 batch->Put(GetStorageKeyFromModel(entry.key()), CreateEntityData(entry));
162 } 169 }
163 callback.Run(std::move(batch)); 170 callback.Run(std::move(batch));
164 } 171 }
165 172
173 void AutocompleteSyncBridge::ActOnChanges(const AutofillChangeList& changes) {
174 DCHECK(change_processor());
skym 2017/01/12 00:18:32 I'd remove this. The bridge should always have a p
Patrick Noland 2017/01/13 23:31:51 Done.
175 std::unique_ptr<syncer::MetadataChangeList> change_list =
skym 2017/01/12 00:18:32 Need to call TakeError() on this thing.
Patrick Noland 2017/01/13 23:31:52 Done.
176 CreateMetadataChangeList();
177 for (const auto& change : changes) {
178 std::string storage_key = GetStorageKeyFromModel(change.key());
179 switch (change.type()) {
180 case AutofillChange::ADD:
181 case AutofillChange::UPDATE: {
182 base::Time date_created, date_last_used;
183 bool success = GetAutofillTable()->GetAutofillTimestamps(
184 change.key().name(), change.key().value(), &date_created,
185 &date_last_used);
186 DCHECK(success);
skym 2017/01/12 00:18:32 Is this really impossible? How about something lik
maxbogue 2017/01/12 01:33:35 +1, please handle your errors :)
Patrick Noland 2017/01/13 23:31:51 Done.
187
188 AutofillEntry entry(change.key(), date_created, date_last_used);
189 sync_pb::AutofillSpecifics specifics;
maxbogue 2017/01/12 01:33:35 You do not use this variable.
Patrick Noland 2017/01/13 23:31:51 Done.
190
191 WriteAutofillEntry(entry, &specifics);
skym 2017/01/12 00:18:32 This feels more awkward than it needed to be.
maxbogue 2017/01/12 01:33:35 This line is populating a specifics variable that
Patrick Noland 2017/01/13 23:31:51 Done.
192 std::unique_ptr<syncer::EntityData> entity_data =
193 CreateEntityData(entry);
194 change_processor()->Put(storage_key, std::move(entity_data),
195 change_list.get());
196 break;
197 }
198 case AutofillChange::REMOVE: {
199 change_processor()->Delete(storage_key, change_list.get());
200 break;
201 }
202 }
203 }
204 }
205
166 std::string AutocompleteSyncBridge::GetClientTag( 206 std::string AutocompleteSyncBridge::GetClientTag(
167 const syncer::EntityData& entity_data) { 207 const syncer::EntityData& entity_data) {
168 DCHECK(entity_data.specifics.has_autofill()); 208 DCHECK(entity_data.specifics.has_autofill());
169 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); 209 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill();
170 return std::string(kAutocompleteEntryNamespaceTag) + 210 return std::string(kAutocompleteEntryNamespaceTag) +
171 net::EscapePath(specifics.name()) + 211 net::EscapePath(specifics.name()) +
172 std::string(kAutocompleteTagDelimiter) + 212 std::string(kAutocompleteTagDelimiter) +
173 net::EscapePath(specifics.value()); 213 net::EscapePath(specifics.value());
174 } 214 }
175 215
176 std::string AutocompleteSyncBridge::GetStorageKey( 216 std::string AutocompleteSyncBridge::GetStorageKey(
177 const syncer::EntityData& entity_data) { 217 const syncer::EntityData& entity_data) {
178 DCHECK(entity_data.specifics.has_autofill()); 218 DCHECK(entity_data.specifics.has_autofill());
179 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill(); 219 const sync_pb::AutofillSpecifics specifics = entity_data.specifics.autofill();
180 return BuildSerializedStorageKey(specifics.name(), specifics.value()); 220 return BuildSerializedStorageKey(specifics.name(), specifics.value());
181 } 221 }
182 222
223 void AutocompleteSyncBridge::InjectStartSyncFlare(
224 const syncer::ModelTypeSyncBridge::StartSyncFlare& flare) {
maxbogue 2017/01/12 01:33:35 omit "syncer::ModelTypeSyncBridge::"
Patrick Noland 2017/01/13 23:31:52 n/a now that flare is gone
225 flare_ = flare;
226 }
227
183 // AutofillWebDataServiceObserverOnDBThread implementation. 228 // AutofillWebDataServiceObserverOnDBThread implementation.
184 void AutocompleteSyncBridge::AutofillEntriesChanged( 229 void AutocompleteSyncBridge::AutofillEntriesChanged(
185 const AutofillChangeList& changes) { 230 const AutofillChangeList& changes) {
186 DCHECK(thread_checker_.CalledOnValidThread()); 231 DCHECK(thread_checker_.CalledOnValidThread());
187 } 232 if (change_processor()->IsTrackingMetadata()) {
maxbogue 2017/01/12 01:33:35 I would prefer this just be at the top of ActOnLoc
skym 2017/01/12 18:49:16 So, this only works if we're not tracking because
Patrick Noland 2017/01/13 23:31:51 Done.
188 233 ActOnChanges(changes);
189 // static 234 } else if (!flare_.is_null()) {
maxbogue 2017/01/12 01:33:35 The flare is simply an optimization right? We're a
skym 2017/01/12 18:49:16 Agreed, lets remove the flare.
190 AutofillEntry AutocompleteSyncBridge::CreateAutofillEntry( 235 flare_.Run(syncer::AUTOFILL);
skym 2017/01/12 00:18:32 It looks like we end up dropping the changes here,
maxbogue 2017/01/12 01:33:35 I don't think this comment is accurate; maybe Sky
skym 2017/01/12 18:49:16 So I think in the Directory world, we were more co
Patrick Noland 2017/01/13 23:31:51 I've gone ahead and added metadata loading
191 const sync_pb::AutofillSpecifics& autofill_specifics) { 236 flare_.Reset();
192 AutofillKey key(base::UTF8ToUTF16(autofill_specifics.name()),
193 base::UTF8ToUTF16(autofill_specifics.value()));
194 base::Time date_created, date_last_used;
195 const google::protobuf::RepeatedField<int64_t>& timestamps =
196 autofill_specifics.usage_timestamp();
197 if (!timestamps.empty()) {
198 date_created = base::Time::FromInternalValue(*timestamps.begin());
199 date_last_used = base::Time::FromInternalValue(*timestamps.rbegin());
200 } 237 }
201 return AutofillEntry(key, date_created, date_last_used);
202 } 238 }
203 239
204 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const { 240 AutofillTable* AutocompleteSyncBridge::GetAutofillTable() const {
205 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase()); 241 return AutofillTable::FromWebDatabase(web_data_backend_->GetDatabase());
206 } 242 }
207 243
244 std::string AutocompleteSyncBridge::GetStorageKeyFromModel(
maxbogue 2017/01/12 01:33:35 "// static" on a line above
Patrick Noland 2017/01/13 23:31:52 n/a now that unit tests don't use this
245 const AutofillKey& key) {
246 return BuildSerializedStorageKey(base::UTF16ToUTF8(key.name()),
247 base::UTF16ToUTF8(key.value()));
248 }
249
208 } // namespace autofill 250 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698