Home | History | Annotate | Download | only in webdata
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop/message_loop_proxy.h"
     10 #include "base/stl_util.h"
     11 #include "components/autofill/core/browser/autofill_country.h"
     12 #include "components/autofill/core/browser/autofill_profile.h"
     13 #include "components/autofill/core/browser/credit_card.h"
     14 #include "components/autofill/core/browser/webdata/autofill_change.h"
     15 #include "components/autofill/core/browser/webdata/autofill_entry.h"
     16 #include "components/autofill/core/browser/webdata/autofill_table.h"
     17 #include "components/autofill/core/browser/webdata/autofill_webdata_backend_impl.h"
     18 #include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h"
     19 #include "components/autofill/core/common/form_field_data.h"
     20 #include "components/webdata/common/web_data_service_backend.h"
     21 #include "components/webdata/common/web_database_service.h"
     22 
     23 using base::Bind;
     24 using base::Time;
     25 
     26 namespace autofill {
     27 
     28 AutofillWebDataService::AutofillWebDataService(
     29     scoped_refptr<WebDatabaseService> wdbs,
     30     scoped_refptr<base::MessageLoopProxy> ui_thread,
     31     scoped_refptr<base::MessageLoopProxy> db_thread,
     32     const ProfileErrorCallback& callback)
     33     : WebDataServiceBase(wdbs, callback, ui_thread),
     34       ui_thread_(ui_thread),
     35       db_thread_(db_thread),
     36       autofill_backend_(NULL),
     37       weak_ptr_factory_(this) {
     38 
     39   base::Closure on_changed_callback = Bind(
     40       &AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread,
     41       weak_ptr_factory_.GetWeakPtr());
     42 
     43   autofill_backend_ = new AutofillWebDataBackendImpl(
     44       wdbs_->GetBackend(), ui_thread_, db_thread_, on_changed_callback);
     45 }
     46 
     47 AutofillWebDataService::AutofillWebDataService(
     48     scoped_refptr<base::MessageLoopProxy> ui_thread,
     49     scoped_refptr<base::MessageLoopProxy> db_thread)
     50     : WebDataServiceBase(NULL, WebDataServiceBase::ProfileErrorCallback(),
     51           ui_thread),
     52       ui_thread_(ui_thread),
     53       db_thread_(db_thread),
     54       autofill_backend_(new AutofillWebDataBackendImpl(NULL,
     55           ui_thread_,
     56           db_thread_,
     57           base::Closure())),
     58       weak_ptr_factory_(this) {
     59 }
     60 
     61 void AutofillWebDataService::ShutdownOnUIThread() {
     62   weak_ptr_factory_.InvalidateWeakPtrs();
     63   db_thread_->PostTask(FROM_HERE,
     64       Bind(&AutofillWebDataBackendImpl::ResetUserData, autofill_backend_));
     65   WebDataServiceBase::ShutdownOnUIThread();
     66 }
     67 
     68 void AutofillWebDataService::AddFormFields(
     69     const std::vector<FormFieldData>& fields) {
     70   wdbs_->ScheduleDBTask(FROM_HERE,
     71       Bind(&AutofillWebDataBackendImpl::AddFormElements,
     72            autofill_backend_, fields));
     73 }
     74 
     75 WebDataServiceBase::Handle AutofillWebDataService::GetFormValuesForElementName(
     76     const base::string16& name, const base::string16& prefix, int limit,
     77     WebDataServiceConsumer* consumer) {
     78   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
     79       Bind(&AutofillWebDataBackendImpl::GetFormValuesForElementName,
     80            autofill_backend_, name, prefix, limit), consumer);
     81 }
     82 
     83 WebDataServiceBase::Handle AutofillWebDataService::HasFormElements(
     84     WebDataServiceConsumer* consumer) {
     85   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
     86       Bind(&AutofillWebDataBackendImpl::HasFormElements, autofill_backend_),
     87       consumer);
     88 }
     89 
     90 void AutofillWebDataService::RemoveFormElementsAddedBetween(
     91     const Time& delete_begin, const Time& delete_end) {
     92   wdbs_->ScheduleDBTask(FROM_HERE,
     93       Bind(&AutofillWebDataBackendImpl::RemoveFormElementsAddedBetween,
     94            autofill_backend_, delete_begin, delete_end));
     95 }
     96 
     97 void AutofillWebDataService::RemoveFormValueForElementName(
     98     const base::string16& name, const base::string16& value) {
     99   wdbs_->ScheduleDBTask(FROM_HERE,
    100       Bind(&AutofillWebDataBackendImpl::RemoveFormValueForElementName,
    101            autofill_backend_, name, value));
    102 }
    103 
    104 void AutofillWebDataService::AddAutofillProfile(
    105     const AutofillProfile& profile) {
    106   wdbs_->ScheduleDBTask(FROM_HERE,
    107       Bind(&AutofillWebDataBackendImpl::AddAutofillProfile,
    108            autofill_backend_, profile));
    109 }
    110 
    111 void AutofillWebDataService::UpdateAutofillProfile(
    112     const AutofillProfile& profile) {
    113   wdbs_->ScheduleDBTask(FROM_HERE,
    114       Bind(&AutofillWebDataBackendImpl::UpdateAutofillProfile,
    115            autofill_backend_, profile));
    116 }
    117 
    118 void AutofillWebDataService::RemoveAutofillProfile(
    119     const std::string& guid) {
    120   wdbs_->ScheduleDBTask(FROM_HERE,
    121       Bind(&AutofillWebDataBackendImpl::RemoveAutofillProfile,
    122            autofill_backend_, guid));
    123 }
    124 
    125 WebDataServiceBase::Handle AutofillWebDataService::GetAutofillProfiles(
    126     WebDataServiceConsumer* consumer) {
    127   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
    128       Bind(&AutofillWebDataBackendImpl::GetAutofillProfiles, autofill_backend_),
    129       consumer);
    130 }
    131 
    132 void AutofillWebDataService::UpdateAutofillEntries(
    133     const std::vector<autofill::AutofillEntry>& autofill_entries) {
    134   wdbs_->ScheduleDBTask(FROM_HERE,
    135                         Bind(&AutofillWebDataBackendImpl::UpdateAutofillEntries,
    136                              autofill_backend_,
    137                              autofill_entries));
    138 }
    139 
    140 void AutofillWebDataService::AddCreditCard(const CreditCard& credit_card) {
    141   wdbs_->ScheduleDBTask(
    142       FROM_HERE,
    143       Bind(&AutofillWebDataBackendImpl::AddCreditCard,
    144            autofill_backend_, credit_card));
    145 }
    146 
    147 void AutofillWebDataService::UpdateCreditCard(
    148     const CreditCard& credit_card) {
    149   wdbs_->ScheduleDBTask(
    150       FROM_HERE,
    151       Bind(&AutofillWebDataBackendImpl::UpdateCreditCard,
    152            autofill_backend_, credit_card));
    153 }
    154 
    155 void AutofillWebDataService::RemoveCreditCard(const std::string& guid) {
    156   wdbs_->ScheduleDBTask(
    157       FROM_HERE,
    158       Bind(&AutofillWebDataBackendImpl::RemoveCreditCard,
    159            autofill_backend_, guid));
    160 }
    161 
    162 WebDataServiceBase::Handle AutofillWebDataService::GetCreditCards(
    163     WebDataServiceConsumer* consumer) {
    164   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
    165       Bind(&AutofillWebDataBackendImpl::GetCreditCards, autofill_backend_),
    166       consumer);
    167 }
    168 
    169 void AutofillWebDataService::RemoveAutofillDataModifiedBetween(
    170     const Time& delete_begin,
    171     const Time& delete_end) {
    172   wdbs_->ScheduleDBTask(
    173       FROM_HERE,
    174       Bind(&AutofillWebDataBackendImpl::RemoveAutofillDataModifiedBetween,
    175            autofill_backend_, delete_begin, delete_end));
    176 }
    177 
    178 void AutofillWebDataService::RemoveOriginURLsModifiedBetween(
    179     const Time& delete_begin, const Time& delete_end) {
    180   wdbs_->ScheduleDBTask(
    181       FROM_HERE,
    182       Bind(&AutofillWebDataBackendImpl::RemoveOriginURLsModifiedBetween,
    183            autofill_backend_, delete_begin, delete_end));
    184 }
    185 
    186 void AutofillWebDataService::AddObserver(
    187     AutofillWebDataServiceObserverOnDBThread* observer) {
    188   DCHECK(db_thread_->BelongsToCurrentThread());
    189   if (autofill_backend_.get())
    190     autofill_backend_->AddObserver(observer);
    191 }
    192 
    193 void AutofillWebDataService::RemoveObserver(
    194     AutofillWebDataServiceObserverOnDBThread* observer) {
    195   DCHECK(db_thread_->BelongsToCurrentThread());
    196   if (autofill_backend_.get())
    197     autofill_backend_->RemoveObserver(observer);
    198 }
    199 
    200 void AutofillWebDataService::AddObserver(
    201     AutofillWebDataServiceObserverOnUIThread* observer) {
    202   DCHECK(ui_thread_->BelongsToCurrentThread());
    203   ui_observer_list_.AddObserver(observer);
    204 }
    205 
    206 void AutofillWebDataService::RemoveObserver(
    207     AutofillWebDataServiceObserverOnUIThread* observer) {
    208   DCHECK(ui_thread_->BelongsToCurrentThread());
    209   ui_observer_list_.RemoveObserver(observer);
    210 }
    211 
    212 base::SupportsUserData* AutofillWebDataService::GetDBUserData() {
    213   DCHECK(db_thread_->BelongsToCurrentThread());
    214   return autofill_backend_->GetDBUserData();
    215 }
    216 
    217 void AutofillWebDataService::GetAutofillBackend(
    218     const base::Callback<void(AutofillWebDataBackend*)>& callback) {
    219   db_thread_->PostTask(FROM_HERE, base::Bind(callback, autofill_backend_));
    220 }
    221 
    222 AutofillWebDataService::~AutofillWebDataService() {
    223 }
    224 
    225 void AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread() {
    226   DCHECK(ui_thread_->BelongsToCurrentThread());
    227   FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnUIThread,
    228                     ui_observer_list_,
    229                     AutofillMultipleChanged());
    230 }
    231 
    232 }  // namespace autofill
    233