Chromium Code Reviews| 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 "components/autofill/browser/webdata/autofill_webdata_backend.h" | 5 #include "components/autofill/browser/webdata/autofill_webdata_backend.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "components/autofill/browser/autofill_country.h" | 9 #include "components/autofill/browser/autofill_country.h" |
| 10 #include "components/autofill/browser/autofill_profile.h" | 10 #include "components/autofill/browser/autofill_profile.h" |
| 11 #include "components/autofill/browser/credit_card.h" | 11 #include "components/autofill/browser/credit_card.h" |
| 12 #include "components/autofill/browser/webdata/autofill_change.h" | 12 #include "components/autofill/browser/webdata/autofill_change.h" |
| 13 #include "components/autofill/browser/webdata/autofill_entry.h" | 13 #include "components/autofill/browser/webdata/autofill_entry.h" |
| 14 #include "components/autofill/browser/webdata/autofill_table.h" | 14 #include "components/autofill/browser/webdata/autofill_table.h" |
| 15 #include "components/autofill/browser/webdata/autofill_webdata_service.h" | |
| 15 #include "components/autofill/browser/webdata/autofill_webdata_service_observer. h" | 16 #include "components/autofill/browser/webdata/autofill_webdata_service_observer. h" |
| 17 #include "components/autofill/browser/webdata/syncable_service_delegate.h" | |
| 16 #include "components/autofill/common/form_field_data.h" | 18 #include "components/autofill/common/form_field_data.h" |
| 17 | 19 |
| 18 using base::Bind; | 20 using base::Bind; |
| 19 using base::Time; | 21 using base::Time; |
| 20 using content::BrowserThread; | 22 using content::BrowserThread; |
| 21 | 23 |
| 22 namespace autofill { | 24 namespace autofill { |
| 23 | 25 |
| 26 class SyncableAutofillDelegate : public SyncableServiceDelegate { | |
|
Jói
2013/05/06 23:08:39
Do you expect that there might be multiple impleme
| |
| 27 public: | |
| 28 SyncableAutofillDelegate( | |
| 29 WebDatabase* db, | |
| 30 AutofillWebDataBackend* autofill_backend, | |
| 31 const base::Closure& on_changed) | |
| 32 : web_database_(db), | |
| 33 autofill_backend_(autofill_backend), | |
| 34 on_changed_(new base::Closure(on_changed)), | |
| 35 weak_ptr_factory_(this) { | |
| 36 } | |
| 37 | |
| 38 virtual ~SyncableAutofillDelegate() { | |
| 39 } | |
| 40 | |
| 41 base::WeakPtr<SyncableAutofillDelegate> AsWeakPtr() { | |
| 42 return weak_ptr_factory_.GetWeakPtr(); | |
| 43 } | |
| 44 | |
| 45 virtual WebDatabase* GetDatabase() OVERRIDE { | |
| 46 return web_database_; | |
| 47 } | |
| 48 | |
| 49 virtual void AddObserver( | |
| 50 AutofillWebDataServiceObserverOnDBThread* observer) OVERRIDE { | |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 52 if (autofill_backend_) | |
| 53 autofill_backend_->AddObserver(observer); | |
| 54 } | |
| 55 | |
| 56 virtual void RemoveObserver( | |
| 57 AutofillWebDataServiceObserverOnDBThread* observer) OVERRIDE { | |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 59 if (autofill_backend_) | |
| 60 autofill_backend_->RemoveObserver(observer); | |
| 61 } | |
| 62 | |
| 63 virtual void RemoveExpiredFormElements() OVERRIDE { | |
| 64 if (!autofill_backend_) | |
| 65 return; | |
| 66 | |
| 67 WebDatabase::State state = | |
| 68 autofill_backend_->RemoveExpiredFormElements(web_database_); | |
| 69 | |
| 70 if (state == WebDatabase::COMMIT_NEEDED) { | |
| 71 web_database_->CommitTransaction(); | |
| 72 web_database_->BeginTransaction(); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 virtual void NotifyOfMultipleAutofillChanges() OVERRIDE { | |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 78 | |
| 79 if (!autofill_backend_ || !on_changed_ || on_changed_->is_null()) | |
| 80 return; | |
| 81 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, *on_changed_); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 WebDatabase* web_database_; | |
| 86 AutofillWebDataBackend* autofill_backend_; | |
| 87 base::Closure* on_changed_; | |
| 88 base::WeakPtrFactory<SyncableAutofillDelegate> weak_ptr_factory_; | |
| 89 }; | |
|
Ilya Sherman
2013/05/07 00:02:43
nit: Please tuck this into an anonymous namespace.
| |
| 90 | |
| 24 AutofillWebDataBackend::AutofillWebDataBackend() { | 91 AutofillWebDataBackend::AutofillWebDataBackend() { |
| 25 } | 92 } |
| 26 | 93 |
| 27 void AutofillWebDataBackend::AddObserver( | 94 void AutofillWebDataBackend::AddObserver( |
| 28 AutofillWebDataServiceObserverOnDBThread* observer) { | 95 AutofillWebDataServiceObserverOnDBThread* observer) { |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 30 db_observer_list_.AddObserver(observer); | 97 db_observer_list_.AddObserver(observer); |
| 31 } | 98 } |
| 32 | 99 |
| 33 void AutofillWebDataBackend::RemoveObserver( | 100 void AutofillWebDataBackend::RemoveObserver( |
| 34 AutofillWebDataServiceObserverOnDBThread* observer) { | 101 AutofillWebDataServiceObserverOnDBThread* observer) { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 36 db_observer_list_.RemoveObserver(observer); | 103 db_observer_list_.RemoveObserver(observer); |
| 37 } | 104 } |
| 38 | 105 |
| 106 void AutofillWebDataBackend::GetDelegate( | |
| 107 const AutofillWebDataService::DelegateOnDBCallback& callback, | |
| 108 const base::Closure& on_changed_callback, | |
| 109 WebDatabase* db) { | |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 111 if (!delegate_.get()) | |
| 112 delegate_.reset(new SyncableAutofillDelegate( | |
| 113 db, this, on_changed_callback)); | |
| 114 callback.Run(delegate_->AsWeakPtr()); | |
| 115 } | |
| 116 | |
| 39 AutofillWebDataBackend::~AutofillWebDataBackend() { | 117 AutofillWebDataBackend::~AutofillWebDataBackend() { |
| 118 delegate_.reset(); | |
| 40 } | 119 } |
| 41 | 120 |
| 42 WebDatabase::State AutofillWebDataBackend::AddFormElements( | 121 WebDatabase::State AutofillWebDataBackend::AddFormElements( |
| 43 const std::vector<FormFieldData>& fields, WebDatabase* db) { | 122 const std::vector<FormFieldData>& fields, WebDatabase* db) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 45 AutofillChangeList changes; | 124 AutofillChangeList changes; |
| 46 if (!AutofillTable::FromWebDatabase(db)->AddFormFieldValues( | 125 if (!AutofillTable::FromWebDatabase(db)->AddFormFieldValues( |
| 47 fields, &changes)) { | 126 fields, &changes)) { |
| 48 NOTREACHED(); | 127 NOTREACHED(); |
| 49 return WebDatabase::COMMIT_NOT_NEEDED; | 128 return WebDatabase::COMMIT_NOT_NEEDED; |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 const WDTypedResult* result) { | 387 const WDTypedResult* result) { |
| 309 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); | 388 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); |
| 310 const WDResult<std::vector<CreditCard*> >* r = | 389 const WDResult<std::vector<CreditCard*> >* r = |
| 311 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); | 390 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); |
| 312 | 391 |
| 313 std::vector<CreditCard*> credit_cards = r->GetValue(); | 392 std::vector<CreditCard*> credit_cards = r->GetValue(); |
| 314 STLDeleteElements(&credit_cards); | 393 STLDeleteElements(&credit_cards); |
| 315 } | 394 } |
| 316 | 395 |
| 317 } // namespace autofill | 396 } // namespace autofill |
| OLD | NEW |