OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/webdata/autocomplete_syncable_service.h" | 5 #include "chrome/browser/webdata/autocomplete_syncable_service.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "components/autofill/browser/webdata/autofill_backend_delegate.h" |
10 #include "components/autofill/browser/webdata/autofill_table.h" | 11 #include "components/autofill/browser/webdata/autofill_table.h" |
11 #include "components/autofill/browser/webdata/autofill_webdata_service.h" | 12 #include "components/autofill/browser/webdata/autofill_webdata_service.h" |
12 #include "components/webdata/common/web_database.h" | 13 #include "components/webdata/common/web_database.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
15 #include "sync/api/sync_error.h" | 16 #include "sync/api/sync_error.h" |
16 #include "sync/api/sync_error_factory.h" | 17 #include "sync/api/sync_error_factory.h" |
17 #include "sync/protocol/autofill_specifics.pb.h" | 18 #include "sync/protocol/autofill_specifics.pb.h" |
18 #include "sync/protocol/sync.pb.h" | 19 #include "sync/protocol/sync.pb.h" |
19 | 20 |
20 using autofill::AutofillChange; | 21 using autofill::AutofillChange; |
21 using autofill::AutofillChangeList; | 22 using autofill::AutofillChangeList; |
22 using autofill::AutofillEntry; | 23 using autofill::AutofillEntry; |
23 using autofill::AutofillKey; | 24 using autofill::AutofillKey; |
24 using autofill::AutofillTable; | 25 using autofill::AutofillTable; |
25 using autofill::AutofillWebDataService; | 26 using autofill::AutofillWebDataService; |
| 27 using autofill::AutofillBackendDelegate; |
26 using content::BrowserThread; | 28 using content::BrowserThread; |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 const char kAutofillEntryNamespaceTag[] = "autofill_entry|"; | 32 const char kAutofillEntryNamespaceTag[] = "autofill_entry|"; |
31 | 33 |
32 // Merges timestamps from the |autofill| entry and |timestamps|. Returns | 34 // Merges timestamps from the |autofill| entry and |timestamps|. Returns |
33 // true if they were different, false if they were the same. | 35 // true if they were different, false if they were the same. |
34 // All of the timestamp vectors are assummed to be sorted, resulting vector is | 36 // All of the timestamp vectors are assummed to be sorted, resulting vector is |
35 // sorted as well. Only two timestamps - the earliest and the latest are stored. | 37 // sorted as well. Only two timestamps - the earliest and the latest are stored. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 void* UserDataKey() { | 87 void* UserDataKey() { |
86 // Use the address of a static that COMDAT folding won't ever fold | 88 // Use the address of a static that COMDAT folding won't ever fold |
87 // with something else. | 89 // with something else. |
88 static int user_data_key = 0; | 90 static int user_data_key = 0; |
89 return reinterpret_cast<void*>(&user_data_key); | 91 return reinterpret_cast<void*>(&user_data_key); |
90 } | 92 } |
91 | 93 |
92 } // namespace | 94 } // namespace |
93 | 95 |
94 AutocompleteSyncableService::AutocompleteSyncableService( | 96 AutocompleteSyncableService::AutocompleteSyncableService( |
| 97 AutofillBackendDelegate* web_data_delegate, |
95 AutofillWebDataService* web_data_service) | 98 AutofillWebDataService* web_data_service) |
96 : web_data_service_(web_data_service), | 99 : web_data_delegate_(web_data_delegate), |
| 100 web_data_service_(web_data_service), |
97 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)), | 101 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)), |
98 cull_expired_entries_(false) { | 102 cull_expired_entries_(false) { |
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
100 DCHECK(web_data_service_); | 104 DCHECK(web_data_delegate_); |
101 | 105 |
102 scoped_observer_.Add(web_data_service_); | 106 scoped_observer_.Add(web_data_delegate_); |
103 } | 107 } |
104 | 108 |
105 AutocompleteSyncableService::~AutocompleteSyncableService() { | 109 AutocompleteSyncableService::~AutocompleteSyncableService() { |
106 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
107 } | 111 } |
108 | 112 |
109 // static | 113 // static |
110 void AutocompleteSyncableService::CreateForWebDataService( | 114 void AutocompleteSyncableService::CreateForWebDataService( |
111 AutofillWebDataService* web_data_service) { | 115 AutofillWebDataService* web_data_service, |
| 116 AutofillBackendDelegate* delegate) { |
112 web_data_service->GetDBUserData()->SetUserData( | 117 web_data_service->GetDBUserData()->SetUserData( |
113 UserDataKey(), new AutocompleteSyncableService(web_data_service)); | 118 UserDataKey(), |
| 119 new AutocompleteSyncableService(delegate, web_data_service)); |
114 } | 120 } |
115 | 121 |
116 // static | 122 // static |
117 AutocompleteSyncableService* AutocompleteSyncableService::FromWebDataService( | 123 AutocompleteSyncableService* AutocompleteSyncableService::FromWebDataService( |
118 AutofillWebDataService* web_data_service) { | 124 AutofillWebDataService* web_data_service) { |
119 return static_cast<AutocompleteSyncableService*>( | 125 return static_cast<AutocompleteSyncableService*>( |
120 web_data_service->GetDBUserData()->GetUserData(UserDataKey())); | 126 web_data_service->GetDBUserData()->GetUserData(UserDataKey())); |
121 } | 127 } |
122 | 128 |
123 AutocompleteSyncableService::AutocompleteSyncableService() | 129 AutocompleteSyncableService::AutocompleteSyncableService() |
124 : web_data_service_(NULL), | 130 : web_data_delegate_(NULL), |
125 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)), | 131 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_observer_(this)), |
126 cull_expired_entries_(false) { | 132 cull_expired_entries_(false) { |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
128 } | 134 } |
129 | 135 |
130 syncer::SyncMergeResult AutocompleteSyncableService::MergeDataAndStartSyncing( | 136 syncer::SyncMergeResult AutocompleteSyncableService::MergeDataAndStartSyncing( |
131 syncer::ModelType type, | 137 syncer::ModelType type, |
132 const syncer::SyncDataList& initial_sync_data, | 138 const syncer::SyncDataList& initial_sync_data, |
133 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 139 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
134 scoped_ptr<syncer::SyncErrorFactory> error_handler) { | 140 scoped_ptr<syncer::SyncErrorFactory> error_handler) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 CreateOrUpdateEntry(*sync_iter, &new_db_entries, &new_synced_entries); | 173 CreateOrUpdateEntry(*sync_iter, &new_db_entries, &new_synced_entries); |
168 } | 174 } |
169 | 175 |
170 if (!SaveChangesToWebData(new_synced_entries)) { | 176 if (!SaveChangesToWebData(new_synced_entries)) { |
171 merge_result.set_error(error_handler_->CreateAndUploadError( | 177 merge_result.set_error(error_handler_->CreateAndUploadError( |
172 FROM_HERE, | 178 FROM_HERE, |
173 "Failed to update webdata.")); | 179 "Failed to update webdata.")); |
174 return merge_result; | 180 return merge_result; |
175 } | 181 } |
176 | 182 |
177 AutofillWebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); | 183 if (web_data_delegate_) |
| 184 web_data_delegate_->NotifyOfMultipleAutofillChanges(web_data_service_); |
178 | 185 |
179 syncer::SyncChangeList new_changes; | 186 syncer::SyncChangeList new_changes; |
180 for (AutocompleteEntryMap::iterator i = new_db_entries.begin(); | 187 for (AutocompleteEntryMap::iterator i = new_db_entries.begin(); |
181 i != new_db_entries.end(); ++i) { | 188 i != new_db_entries.end(); ++i) { |
182 new_changes.push_back( | 189 new_changes.push_back( |
183 syncer::SyncChange(FROM_HERE, | 190 syncer::SyncChange(FROM_HERE, |
184 i->second.first, | 191 i->second.first, |
185 CreateSyncData(*(i->second.second)))); | 192 CreateSyncData(*(i->second.second)))); |
186 } | 193 } |
187 | 194 |
188 if (cull_expired_entries_) { | 195 if (cull_expired_entries_) { |
189 // This will schedule a deletion operation on the DB thread, which will | 196 // This will schedule a deletion operation on the DB thread, which will |
190 // trigger a notification to propagate the deletion to Sync. | 197 // trigger a notification to propagate the deletion to Sync. |
191 web_data_service_->RemoveExpiredFormElements(); | 198 web_data_delegate_->RemoveExpiredFormElements(); |
192 } | 199 } |
193 | 200 |
194 merge_result.set_error( | 201 merge_result.set_error( |
195 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); | 202 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes)); |
196 return merge_result; | 203 return merge_result; |
197 } | 204 } |
198 | 205 |
199 void AutocompleteSyncableService::StopSyncing(syncer::ModelType type) { | 206 void AutocompleteSyncableService::StopSyncing(syncer::ModelType type) { |
200 DCHECK(CalledOnValidThread()); | 207 DCHECK(CalledOnValidThread()); |
201 DCHECK_EQ(syncer::AUTOFILL, type); | 208 DCHECK_EQ(syncer::AUTOFILL, type); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 syncer::SyncChange::ChangeTypeToString(i->change_type())); | 291 syncer::SyncChange::ChangeTypeToString(i->change_type())); |
285 } | 292 } |
286 } | 293 } |
287 | 294 |
288 if (!SaveChangesToWebData(new_entries)) { | 295 if (!SaveChangesToWebData(new_entries)) { |
289 return error_handler_->CreateAndUploadError( | 296 return error_handler_->CreateAndUploadError( |
290 FROM_HERE, | 297 FROM_HERE, |
291 "Failed to update webdata."); | 298 "Failed to update webdata."); |
292 } | 299 } |
293 | 300 |
294 AutofillWebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); | 301 if (web_data_delegate_) |
| 302 web_data_delegate_->NotifyOfMultipleAutofillChanges(web_data_service_); |
295 | 303 |
296 if (cull_expired_entries_) { | 304 if (cull_expired_entries_) { |
297 // This will schedule a deletion operation on the DB thread, which will | 305 // This will schedule a deletion operation on the DB thread, which will |
298 // trigger a notification to propagate the deletion to Sync. | 306 // trigger a notification to propagate the deletion to Sync. |
299 web_data_service_->RemoveExpiredFormElements(); | 307 web_data_delegate_->RemoveExpiredFormElements(); |
300 } | 308 } |
301 | 309 |
302 return list_processing_error; | 310 return list_processing_error; |
303 } | 311 } |
304 | 312 |
305 void AutocompleteSyncableService::AutofillEntriesChanged( | 313 void AutocompleteSyncableService::AutofillEntriesChanged( |
306 const AutofillChangeList& changes) { | 314 const AutofillChangeList& changes) { |
307 // Check if sync is on. If we receive notification prior to the sync being set | 315 // Check if sync is on. If we receive notification prior to the sync being set |
308 // up we are going to process all when MergeData..() is called. If we receive | 316 // up we are going to process all when MergeData..() is called. If we receive |
309 // notification after the sync exited, it will be sinced next time Chrome | 317 // notification after the sync exited, it will be sinced next time Chrome |
310 // starts. | 318 // starts. |
311 if (sync_processor_.get()) | 319 if (sync_processor_.get()) |
312 ActOnChanges(changes); | 320 ActOnChanges(changes); |
313 } | 321 } |
314 | 322 |
315 bool AutocompleteSyncableService::LoadAutofillData( | 323 bool AutocompleteSyncableService::LoadAutofillData( |
316 std::vector<AutofillEntry>* entries) const { | 324 std::vector<AutofillEntry>* entries) const { |
317 return AutofillTable::FromWebDatabase( | 325 return AutofillTable::FromWebDatabase( |
318 web_data_service_->GetDatabase())->GetAllAutofillEntries(entries); | 326 web_data_delegate_->GetDatabase())->GetAllAutofillEntries(entries); |
319 } | 327 } |
320 | 328 |
321 bool AutocompleteSyncableService::SaveChangesToWebData( | 329 bool AutocompleteSyncableService::SaveChangesToWebData( |
322 const std::vector<AutofillEntry>& new_entries) { | 330 const std::vector<AutofillEntry>& new_entries) { |
323 DCHECK(CalledOnValidThread()); | 331 DCHECK(CalledOnValidThread()); |
324 | 332 |
325 if (!new_entries.empty() && | 333 if (!new_entries.empty() && |
326 !AutofillTable::FromWebDatabase( | 334 !AutofillTable::FromWebDatabase( |
327 web_data_service_->GetDatabase())->UpdateAutofillEntries( | 335 web_data_delegate_->GetDatabase())->UpdateAutofillEntries( |
328 new_entries)) { | 336 new_entries)) { |
329 return false; | 337 return false; |
330 } | 338 } |
331 return true; | 339 return true; |
332 } | 340 } |
333 | 341 |
334 // Creates or updates an autocomplete entry based on |data|. | 342 // Creates or updates an autocomplete entry based on |data|. |
335 void AutocompleteSyncableService::CreateOrUpdateEntry( | 343 void AutocompleteSyncableService::CreateOrUpdateEntry( |
336 const syncer::SyncData& data, | 344 const syncer::SyncData& data, |
337 AutocompleteEntryMap* loaded_data, | 345 AutocompleteEntryMap* loaded_data, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 const std::vector<base::Time>& ts(entry.timestamps()); | 399 const std::vector<base::Time>& ts(entry.timestamps()); |
392 for (std::vector<base::Time>::const_iterator timestamp = ts.begin(); | 400 for (std::vector<base::Time>::const_iterator timestamp = ts.begin(); |
393 timestamp != ts.end(); ++timestamp) { | 401 timestamp != ts.end(); ++timestamp) { |
394 autofill->add_usage_timestamp(timestamp->ToInternalValue()); | 402 autofill->add_usage_timestamp(timestamp->ToInternalValue()); |
395 } | 403 } |
396 } | 404 } |
397 | 405 |
398 syncer::SyncError AutocompleteSyncableService::AutofillEntryDelete( | 406 syncer::SyncError AutocompleteSyncableService::AutofillEntryDelete( |
399 const sync_pb::AutofillSpecifics& autofill) { | 407 const sync_pb::AutofillSpecifics& autofill) { |
400 if (!AutofillTable::FromWebDatabase( | 408 if (!AutofillTable::FromWebDatabase( |
401 web_data_service_->GetDatabase())->RemoveFormElement( | 409 web_data_delegate_->GetDatabase())->RemoveFormElement( |
402 UTF8ToUTF16(autofill.name()), UTF8ToUTF16(autofill.value()))) { | 410 UTF8ToUTF16(autofill.name()), UTF8ToUTF16(autofill.value()))) { |
403 return error_handler_->CreateAndUploadError( | 411 return error_handler_->CreateAndUploadError( |
404 FROM_HERE, | 412 FROM_HERE, |
405 "Could not remove autocomplete entry from WebDatabase."); | 413 "Could not remove autocomplete entry from WebDatabase."); |
406 } | 414 } |
407 return syncer::SyncError(); | 415 return syncer::SyncError(); |
408 } | 416 } |
409 | 417 |
410 void AutocompleteSyncableService::ActOnChanges( | 418 void AutocompleteSyncableService::ActOnChanges( |
411 const AutofillChangeList& changes) { | 419 const AutofillChangeList& changes) { |
412 DCHECK(sync_processor_.get()); | 420 DCHECK(sync_processor_.get()); |
413 syncer::SyncChangeList new_changes; | 421 syncer::SyncChangeList new_changes; |
414 for (AutofillChangeList::const_iterator change = changes.begin(); | 422 for (AutofillChangeList::const_iterator change = changes.begin(); |
415 change != changes.end(); ++change) { | 423 change != changes.end(); ++change) { |
416 switch (change->type()) { | 424 switch (change->type()) { |
417 case AutofillChange::ADD: | 425 case AutofillChange::ADD: |
418 case AutofillChange::UPDATE: { | 426 case AutofillChange::UPDATE: { |
419 std::vector<base::Time> timestamps; | 427 std::vector<base::Time> timestamps; |
420 if (!AutofillTable::FromWebDatabase( | 428 if (!AutofillTable::FromWebDatabase( |
421 web_data_service_->GetDatabase())->GetAutofillTimestamps( | 429 web_data_delegate_->GetDatabase())->GetAutofillTimestamps( |
422 change->key().name(), | 430 change->key().name(), |
423 change->key().value(), | 431 change->key().value(), |
424 ×tamps)) { | 432 ×tamps)) { |
425 NOTREACHED(); | 433 NOTREACHED(); |
426 return; | 434 return; |
427 } | 435 } |
428 AutofillEntry entry(change->key(), timestamps); | 436 AutofillEntry entry(change->key(), timestamps); |
429 syncer::SyncChange::SyncChangeType change_type = | 437 syncer::SyncChange::SyncChangeType change_type = |
430 (change->type() == AutofillChange::ADD) ? | 438 (change->type() == AutofillChange::ADD) ? |
431 syncer::SyncChange::ACTION_ADD : | 439 syncer::SyncChange::ACTION_ADD : |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 UTF16ToUTF8(entry.key().value()))); | 480 UTF16ToUTF8(entry.key().value()))); |
473 return syncer::SyncData::CreateLocalData(tag, tag, autofill_specifics); | 481 return syncer::SyncData::CreateLocalData(tag, tag, autofill_specifics); |
474 } | 482 } |
475 | 483 |
476 // static | 484 // static |
477 std::string AutocompleteSyncableService::KeyToTag(const std::string& name, | 485 std::string AutocompleteSyncableService::KeyToTag(const std::string& name, |
478 const std::string& value) { | 486 const std::string& value) { |
479 std::string ns(kAutofillEntryNamespaceTag); | 487 std::string ns(kAutofillEntryNamespaceTag); |
480 return ns + net::EscapePath(name) + "|" + net::EscapePath(value); | 488 return ns + net::EscapePath(name) + "|" + net::EscapePath(value); |
481 } | 489 } |
OLD | NEW |