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.
5 #include "components/autofill/core/browser/webdata/autofill_webdata_service.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"
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) {
39 base::Closure on_changed_callback
= Bind(
40 &AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread
,
41 weak_ptr_factory_
.GetWeakPtr());
43 autofill_backend_
= new AutofillWebDataBackendImpl(
44 wdbs_
->GetBackend(), ui_thread_
, db_thread_
, on_changed_callback
);
47 AutofillWebDataService::AutofillWebDataService(
48 scoped_refptr
<base::MessageLoopProxy
> ui_thread
,
49 scoped_refptr
<base::MessageLoopProxy
> db_thread
)
50 : WebDataServiceBase(NULL
, WebDataServiceBase::ProfileErrorCallback(),
52 ui_thread_(ui_thread
),
53 db_thread_(db_thread
),
54 autofill_backend_(new AutofillWebDataBackendImpl(NULL
,
58 weak_ptr_factory_(this) {
61 void AutofillWebDataService::ShutdownOnUIThread() {
62 weak_ptr_factory_
.InvalidateWeakPtrs();
63 db_thread_
->PostTask(FROM_HERE
,
64 Bind(&AutofillWebDataBackendImpl::ResetUserData
, autofill_backend_
));
65 WebDataServiceBase::ShutdownOnUIThread();
68 void AutofillWebDataService::AddFormFields(
69 const std::vector
<FormFieldData
>& fields
) {
70 wdbs_
->ScheduleDBTask(FROM_HERE
,
71 Bind(&AutofillWebDataBackendImpl::AddFormElements
,
72 autofill_backend_
, fields
));
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
);
83 WebDataServiceBase::Handle
AutofillWebDataService::HasFormElements(
84 WebDataServiceConsumer
* consumer
) {
85 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
86 Bind(&AutofillWebDataBackendImpl::HasFormElements
, autofill_backend_
),
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
));
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
));
104 void AutofillWebDataService::AddAutofillProfile(
105 const AutofillProfile
& profile
) {
106 wdbs_
->ScheduleDBTask(FROM_HERE
,
107 Bind(&AutofillWebDataBackendImpl::AddAutofillProfile
,
108 autofill_backend_
, profile
));
111 void AutofillWebDataService::UpdateAutofillProfile(
112 const AutofillProfile
& profile
) {
113 wdbs_
->ScheduleDBTask(FROM_HERE
,
114 Bind(&AutofillWebDataBackendImpl::UpdateAutofillProfile
,
115 autofill_backend_
, profile
));
118 void AutofillWebDataService::RemoveAutofillProfile(
119 const std::string
& guid
) {
120 wdbs_
->ScheduleDBTask(FROM_HERE
,
121 Bind(&AutofillWebDataBackendImpl::RemoveAutofillProfile
,
122 autofill_backend_
, guid
));
125 WebDataServiceBase::Handle
AutofillWebDataService::GetAutofillProfiles(
126 WebDataServiceConsumer
* consumer
) {
127 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
128 Bind(&AutofillWebDataBackendImpl::GetAutofillProfiles
, autofill_backend_
),
132 WebDataServiceBase::Handle
AutofillWebDataService::GetAutofillServerProfiles(
133 WebDataServiceConsumer
* consumer
) {
134 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
135 Bind(&AutofillWebDataBackendImpl::GetAutofillServerProfiles
,
140 void AutofillWebDataService::UpdateAutofillEntries(
141 const std::vector
<autofill::AutofillEntry
>& autofill_entries
) {
142 wdbs_
->ScheduleDBTask(FROM_HERE
,
143 Bind(&AutofillWebDataBackendImpl::UpdateAutofillEntries
,
148 void AutofillWebDataService::AddCreditCard(const CreditCard
& credit_card
) {
149 wdbs_
->ScheduleDBTask(
151 Bind(&AutofillWebDataBackendImpl::AddCreditCard
,
152 autofill_backend_
, credit_card
));
155 void AutofillWebDataService::UpdateCreditCard(
156 const CreditCard
& credit_card
) {
157 wdbs_
->ScheduleDBTask(
159 Bind(&AutofillWebDataBackendImpl::UpdateCreditCard
,
160 autofill_backend_
, credit_card
));
163 void AutofillWebDataService::RemoveCreditCard(const std::string
& guid
) {
164 wdbs_
->ScheduleDBTask(
166 Bind(&AutofillWebDataBackendImpl::RemoveCreditCard
,
167 autofill_backend_
, guid
));
170 WebDataServiceBase::Handle
AutofillWebDataService::GetCreditCards(
171 WebDataServiceConsumer
* consumer
) {
172 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
173 Bind(&AutofillWebDataBackendImpl::GetCreditCards
, autofill_backend_
),
177 WebDataServiceBase::Handle
AutofillWebDataService::GetServerCreditCards(
178 WebDataServiceConsumer
* consumer
) {
179 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
180 Bind(&AutofillWebDataBackendImpl::GetServerCreditCards
,
185 void AutofillWebDataService::UnmaskServerCreditCard(
186 const std::string
& id
,
187 const base::string16
& full_number
) {
188 wdbs_
->ScheduleDBTask(
190 Bind(&AutofillWebDataBackendImpl::UnmaskServerCreditCard
,
191 autofill_backend_
, id
, full_number
));
194 void AutofillWebDataService::MaskServerCreditCard(const std::string
& id
) {
195 wdbs_
->ScheduleDBTask(
197 Bind(&AutofillWebDataBackendImpl::MaskServerCreditCard
,
198 autofill_backend_
, id
));
201 void AutofillWebDataService::UpdateUnmaskedCardUsageStats(
202 const CreditCard
& credit_card
) {
203 wdbs_
->ScheduleDBTask(
205 Bind(&AutofillWebDataBackendImpl::UpdateUnmaskedCardUsageStats
,
206 autofill_backend_
, credit_card
));
209 void AutofillWebDataService::RemoveAutofillDataModifiedBetween(
210 const Time
& delete_begin
,
211 const Time
& delete_end
) {
212 wdbs_
->ScheduleDBTask(
214 Bind(&AutofillWebDataBackendImpl::RemoveAutofillDataModifiedBetween
,
215 autofill_backend_
, delete_begin
, delete_end
));
218 void AutofillWebDataService::RemoveOriginURLsModifiedBetween(
219 const Time
& delete_begin
, const Time
& delete_end
) {
220 wdbs_
->ScheduleDBTask(
222 Bind(&AutofillWebDataBackendImpl::RemoveOriginURLsModifiedBetween
,
223 autofill_backend_
, delete_begin
, delete_end
));
226 void AutofillWebDataService::AddObserver(
227 AutofillWebDataServiceObserverOnDBThread
* observer
) {
228 DCHECK(db_thread_
->BelongsToCurrentThread());
229 if (autofill_backend_
.get())
230 autofill_backend_
->AddObserver(observer
);
233 void AutofillWebDataService::RemoveObserver(
234 AutofillWebDataServiceObserverOnDBThread
* observer
) {
235 DCHECK(db_thread_
->BelongsToCurrentThread());
236 if (autofill_backend_
.get())
237 autofill_backend_
->RemoveObserver(observer
);
240 void AutofillWebDataService::AddObserver(
241 AutofillWebDataServiceObserverOnUIThread
* observer
) {
242 DCHECK(ui_thread_
->BelongsToCurrentThread());
243 ui_observer_list_
.AddObserver(observer
);
246 void AutofillWebDataService::RemoveObserver(
247 AutofillWebDataServiceObserverOnUIThread
* observer
) {
248 DCHECK(ui_thread_
->BelongsToCurrentThread());
249 ui_observer_list_
.RemoveObserver(observer
);
252 base::SupportsUserData
* AutofillWebDataService::GetDBUserData() {
253 DCHECK(db_thread_
->BelongsToCurrentThread());
254 return autofill_backend_
->GetDBUserData();
257 void AutofillWebDataService::GetAutofillBackend(
258 const base::Callback
<void(AutofillWebDataBackend
*)>& callback
) {
259 db_thread_
->PostTask(FROM_HERE
, base::Bind(callback
, autofill_backend_
));
262 AutofillWebDataService::~AutofillWebDataService() {
265 void AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread() {
266 DCHECK(ui_thread_
->BelongsToCurrentThread());
267 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnUIThread
,
269 AutofillMultipleChanged());
272 } // namespace autofill