• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AutofillWebDataService(scoped_refptr<WebDatabaseService> wdbs,scoped_refptr<base::MessageLoopProxy> ui_thread,scoped_refptr<base::MessageLoopProxy> db_thread,const ProfileErrorCallback & callback)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       weak_ptr_factory_(this),
37       autofill_backend_(NULL) {
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 
AutofillWebDataService(scoped_refptr<base::MessageLoopProxy> ui_thread,scoped_refptr<base::MessageLoopProxy> db_thread)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       weak_ptr_factory_(this),
55       autofill_backend_(new AutofillWebDataBackendImpl(NULL,
56           ui_thread_,
57           db_thread_,
58           base::Closure())) {
59 }
60 
ShutdownOnUIThread()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 
AddFormFields(const std::vector<FormFieldData> & fields)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 
GetFormValuesForElementName(const base::string16 & name,const base::string16 & prefix,int limit,WebDataServiceConsumer * consumer)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 
HasFormElements(WebDataServiceConsumer * consumer)83 WebDataServiceBase::Handle AutofillWebDataService::HasFormElements(
84     WebDataServiceConsumer* consumer) {
85   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
86       Bind(&AutofillWebDataBackendImpl::HasFormElements, autofill_backend_),
87       consumer);
88 }
89 
RemoveFormElementsAddedBetween(const Time & delete_begin,const Time & delete_end)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 
RemoveFormValueForElementName(const base::string16 & name,const base::string16 & value)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 
AddAutofillProfile(const AutofillProfile & profile)104 void AutofillWebDataService::AddAutofillProfile(
105     const AutofillProfile& profile) {
106   wdbs_->ScheduleDBTask(FROM_HERE,
107       Bind(&AutofillWebDataBackendImpl::AddAutofillProfile,
108            autofill_backend_, profile));
109 }
110 
UpdateAutofillProfile(const AutofillProfile & profile)111 void AutofillWebDataService::UpdateAutofillProfile(
112     const AutofillProfile& profile) {
113   wdbs_->ScheduleDBTask(FROM_HERE,
114       Bind(&AutofillWebDataBackendImpl::UpdateAutofillProfile,
115            autofill_backend_, profile));
116 }
117 
RemoveAutofillProfile(const std::string & guid)118 void AutofillWebDataService::RemoveAutofillProfile(
119     const std::string& guid) {
120   wdbs_->ScheduleDBTask(FROM_HERE,
121       Bind(&AutofillWebDataBackendImpl::RemoveAutofillProfile,
122            autofill_backend_, guid));
123 }
124 
GetAutofillProfiles(WebDataServiceConsumer * consumer)125 WebDataServiceBase::Handle AutofillWebDataService::GetAutofillProfiles(
126     WebDataServiceConsumer* consumer) {
127   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
128       Bind(&AutofillWebDataBackendImpl::GetAutofillProfiles, autofill_backend_),
129       consumer);
130 }
131 
AddCreditCard(const CreditCard & credit_card)132 void AutofillWebDataService::AddCreditCard(const CreditCard& credit_card) {
133   wdbs_->ScheduleDBTask(
134       FROM_HERE,
135       Bind(&AutofillWebDataBackendImpl::AddCreditCard,
136            autofill_backend_, credit_card));
137 }
138 
UpdateCreditCard(const CreditCard & credit_card)139 void AutofillWebDataService::UpdateCreditCard(
140     const CreditCard& credit_card) {
141   wdbs_->ScheduleDBTask(
142       FROM_HERE,
143       Bind(&AutofillWebDataBackendImpl::UpdateCreditCard,
144            autofill_backend_, credit_card));
145 }
146 
RemoveCreditCard(const std::string & guid)147 void AutofillWebDataService::RemoveCreditCard(const std::string& guid) {
148   wdbs_->ScheduleDBTask(
149       FROM_HERE,
150       Bind(&AutofillWebDataBackendImpl::RemoveCreditCard,
151            autofill_backend_, guid));
152 }
153 
GetCreditCards(WebDataServiceConsumer * consumer)154 WebDataServiceBase::Handle AutofillWebDataService::GetCreditCards(
155     WebDataServiceConsumer* consumer) {
156   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
157       Bind(&AutofillWebDataBackendImpl::GetCreditCards, autofill_backend_),
158       consumer);
159 }
160 
RemoveAutofillDataModifiedBetween(const Time & delete_begin,const Time & delete_end)161 void AutofillWebDataService::RemoveAutofillDataModifiedBetween(
162     const Time& delete_begin,
163     const Time& delete_end) {
164   wdbs_->ScheduleDBTask(
165       FROM_HERE,
166       Bind(&AutofillWebDataBackendImpl::RemoveAutofillDataModifiedBetween,
167            autofill_backend_, delete_begin, delete_end));
168 }
169 
RemoveOriginURLsModifiedBetween(const Time & delete_begin,const Time & delete_end)170 void AutofillWebDataService::RemoveOriginURLsModifiedBetween(
171     const Time& delete_begin, const Time& delete_end) {
172   wdbs_->ScheduleDBTask(
173       FROM_HERE,
174       Bind(&AutofillWebDataBackendImpl::RemoveOriginURLsModifiedBetween,
175            autofill_backend_, delete_begin, delete_end));
176 }
177 
AddObserver(AutofillWebDataServiceObserverOnDBThread * observer)178 void AutofillWebDataService::AddObserver(
179     AutofillWebDataServiceObserverOnDBThread* observer) {
180   DCHECK(db_thread_->BelongsToCurrentThread());
181   if (autofill_backend_.get())
182     autofill_backend_->AddObserver(observer);
183 }
184 
RemoveObserver(AutofillWebDataServiceObserverOnDBThread * observer)185 void AutofillWebDataService::RemoveObserver(
186     AutofillWebDataServiceObserverOnDBThread* observer) {
187   DCHECK(db_thread_->BelongsToCurrentThread());
188   if (autofill_backend_.get())
189     autofill_backend_->RemoveObserver(observer);
190 }
191 
AddObserver(AutofillWebDataServiceObserverOnUIThread * observer)192 void AutofillWebDataService::AddObserver(
193     AutofillWebDataServiceObserverOnUIThread* observer) {
194   DCHECK(ui_thread_->BelongsToCurrentThread());
195   ui_observer_list_.AddObserver(observer);
196 }
197 
RemoveObserver(AutofillWebDataServiceObserverOnUIThread * observer)198 void AutofillWebDataService::RemoveObserver(
199     AutofillWebDataServiceObserverOnUIThread* observer) {
200   DCHECK(ui_thread_->BelongsToCurrentThread());
201   ui_observer_list_.RemoveObserver(observer);
202 }
203 
GetDBUserData()204 base::SupportsUserData* AutofillWebDataService::GetDBUserData() {
205   DCHECK(db_thread_->BelongsToCurrentThread());
206   return autofill_backend_->GetDBUserData();
207 }
208 
GetAutofillBackend(const base::Callback<void (AutofillWebDataBackend *)> & callback)209 void AutofillWebDataService::GetAutofillBackend(
210     const base::Callback<void(AutofillWebDataBackend*)>& callback) {
211   db_thread_->PostTask(FROM_HERE, base::Bind(callback, autofill_backend_));
212 }
213 
~AutofillWebDataService()214 AutofillWebDataService::~AutofillWebDataService() {
215 }
216 
NotifyAutofillMultipleChangedOnUIThread()217 void AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread() {
218   DCHECK(ui_thread_->BelongsToCurrentThread());
219   FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnUIThread,
220                     ui_observer_list_,
221                     AutofillMultipleChanged());
222 }
223 
224 }  // namespace autofill
225