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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/supports_user_data.h"
14 #include "components/autofill/core/browser/webdata/autofill_webdata.h"
15 #include "components/autofill/core/common/form_field_data.h"
16 #include "components/webdata/common/web_data_results.h"
17 #include "components/webdata/common/web_data_service_base.h"
18 #include "components/webdata/common/web_data_service_consumer.h"
19 #include "components/webdata/common/web_database.h"
21 class WebDatabaseService
;
24 class SingleThreadTaskRunner
;
31 class AutofillProfile
;
32 class AutofillWebDataBackend
;
33 class AutofillWebDataBackendImpl
;
34 class AutofillWebDataServiceObserverOnDBThread
;
35 class AutofillWebDataServiceObserverOnUIThread
;
38 // API for Autofill web data.
39 class AutofillWebDataService
: public AutofillWebData
,
40 public WebDataServiceBase
{
42 AutofillWebDataService(scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread
,
43 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread
);
44 AutofillWebDataService(scoped_refptr
<WebDatabaseService
> wdbs
,
45 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread
,
46 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread
,
47 const ProfileErrorCallback
& callback
);
49 // WebDataServiceBase implementation.
50 void ShutdownOnUIThread() override
;
52 // AutofillWebData implementation.
53 void AddFormFields(const std::vector
<FormFieldData
>& fields
) override
;
54 WebDataServiceBase::Handle
GetFormValuesForElementName(
55 const base::string16
& name
,
56 const base::string16
& prefix
,
58 WebDataServiceConsumer
* consumer
) override
;
60 WebDataServiceBase::Handle
HasFormElements(
61 WebDataServiceConsumer
* consumer
) override
;
62 void RemoveFormElementsAddedBetween(const base::Time
& delete_begin
,
63 const base::Time
& delete_end
) override
;
64 void RemoveFormValueForElementName(const base::string16
& name
,
65 const base::string16
& value
) override
;
68 void AddAutofillProfile(const AutofillProfile
& profile
) override
;
69 void UpdateAutofillProfile(const AutofillProfile
& profile
) override
;
70 void RemoveAutofillProfile(const std::string
& guid
) override
;
71 WebDataServiceBase::Handle
GetAutofillProfiles(
72 WebDataServiceConsumer
* consumer
) override
;
75 WebDataServiceBase::Handle
GetServerProfiles(
76 WebDataServiceConsumer
* consumer
) override
;
78 void UpdateAutofillEntries(
79 const std::vector
<AutofillEntry
>& autofill_entries
) override
;
82 void AddCreditCard(const CreditCard
& credit_card
) override
;
83 void UpdateCreditCard(const CreditCard
& credit_card
) override
;
84 void RemoveCreditCard(const std::string
& guid
) override
;
85 WebDataServiceBase::Handle
GetCreditCards(
86 WebDataServiceConsumer
* consumer
) override
;
89 WebDataServiceBase::Handle
GetServerCreditCards(
90 WebDataServiceConsumer
* consumer
) override
;
91 void UnmaskServerCreditCard(const CreditCard
& card
,
92 const base::string16
& full_number
) override
;
93 void MaskServerCreditCard(const std::string
& id
) override
;
95 void ClearAllServerData();
97 void UpdateServerCardUsageStats(const CreditCard
& credit_card
) override
;
98 void UpdateServerAddressUsageStats(const AutofillProfile
& profile
) override
;
100 void RemoveAutofillDataModifiedBetween(const base::Time
& delete_begin
,
101 const base::Time
& delete_end
) override
;
102 void RemoveOriginURLsModifiedBetween(const base::Time
& delete_begin
,
103 const base::Time
& delete_end
) override
;
105 void AddObserver(AutofillWebDataServiceObserverOnDBThread
* observer
);
106 void RemoveObserver(AutofillWebDataServiceObserverOnDBThread
* observer
);
108 void AddObserver(AutofillWebDataServiceObserverOnUIThread
* observer
);
109 void RemoveObserver(AutofillWebDataServiceObserverOnUIThread
* observer
);
111 // Returns a SupportsUserData objects that may be used to store data
112 // owned by the DB thread on this object. Should be called only from
113 // the DB thread, and will be destroyed on the DB thread soon after
114 // |ShutdownOnUIThread()| is called.
115 base::SupportsUserData
* GetDBUserData();
117 // Takes a callback which will be called on the DB thread with a pointer to an
118 // |AutofillWebdataBackend|. This backend can be used to access or update the
119 // WebDatabase directly on the DB thread.
120 void GetAutofillBackend(
121 const base::Callback
<void(AutofillWebDataBackend
*)>& callback
);
124 ~AutofillWebDataService() override
;
126 virtual void NotifyAutofillMultipleChangedOnUIThread();
128 base::WeakPtr
<AutofillWebDataService
> AsWeakPtr() {
129 return weak_ptr_factory_
.GetWeakPtr();
133 base::ObserverList
<AutofillWebDataServiceObserverOnUIThread
>
136 // The task runner that this class uses as its UI thread.
137 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread_
;
139 // The task runner that this class uses as its DB thread.
140 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_
;
142 scoped_refptr
<AutofillWebDataBackendImpl
> autofill_backend_
;
144 // This factory is used on the UI thread. All vended weak pointers are
145 // invalidated in ShutdownOnUIThread().
146 base::WeakPtrFactory
<AutofillWebDataService
> weak_ptr_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(AutofillWebDataService
);
151 } // namespace autofill
153 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_