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_BACKEND_IMPL_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/ref_counted_delete_on_message_loop.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "base/supports_user_data.h"
13 #include "components/autofill/core/browser/webdata/autofill_webdata.h"
14 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.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"
22 class MessageLoopProxy;
25 class WebDataServiceBackend;
29 class AutofillProfile;
30 class AutofillWebDataServiceObserverOnDBThread;
33 // Backend implentation for the AutofillWebDataService. This class runs on the
34 // DB thread, as it handles reads and writes to the WebDatabase, and functions
35 // in it should only be called from that thread. Most functions here are just
36 // the implementations of the corresponding functions in the Autofill
38 // This class is destroyed on the DB thread.
39 class AutofillWebDataBackendImpl
40 : public base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>,
41 public AutofillWebDataBackend {
43 // |web_database_backend| is used to access the WebDatabase directly for
44 // Sync-related operations. |ui_thread| and |db_thread| are the threads that
45 // this class uses as its UI and DB threads respectively.
46 // |on_changed_callback| is a closure which can be used to notify the UI
47 // thread of changes initiated by Sync (this callback may be called multiple
49 AutofillWebDataBackendImpl(
50 scoped_refptr<WebDataServiceBackend> web_database_backend,
51 scoped_refptr<base::MessageLoopProxy> ui_thread,
52 scoped_refptr<base::MessageLoopProxy> db_thread,
53 const base::Closure& on_changed_callback);
55 // AutofillWebDataBackend implementation.
56 void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer) override;
58 AutofillWebDataServiceObserverOnDBThread* observer) override;
59 WebDatabase* GetDatabase() override;
60 void RemoveExpiredFormElements() override;
61 void NotifyOfMultipleAutofillChanges() override;
63 // Returns a SupportsUserData objects that may be used to store data
64 // owned by the DB thread on this object. Should be called only from
65 // the DB thread, and will be destroyed on the DB thread soon after
66 // |ShutdownOnUIThread()| is called.
67 base::SupportsUserData* GetDBUserData();
71 // Adds form fields to the web database.
72 WebDatabase::State AddFormElements(const std::vector<FormFieldData>& fields,
75 // Returns a vector of values which have been entered in form input fields
77 scoped_ptr<WDTypedResult> GetFormValuesForElementName(
78 const base::string16& name,
79 const base::string16& prefix,
83 // Returns true if there are any elements in the form.
84 scoped_ptr<WDTypedResult> HasFormElements(WebDatabase* db);
86 // Removes form elements recorded for Autocomplete from the database.
87 WebDatabase::State RemoveFormElementsAddedBetween(
88 const base::Time& delete_begin,
89 const base::Time& delete_end,
93 // Removes the Form-value |value| which has been entered in form input fields
94 // named |name| from the database.
95 WebDatabase::State RemoveFormValueForElementName(const base::string16& name,
96 const base::string16& value,
99 // Adds an Autofill profile to the web database.
100 WebDatabase::State AddAutofillProfile(const AutofillProfile& profile,
103 // Updates an Autofill profile in the web database.
104 WebDatabase::State UpdateAutofillProfile(const AutofillProfile& profile,
107 // Removes an Autofill profile from the web database.
108 WebDatabase::State RemoveAutofillProfile(const std::string& guid,
111 // Returns all Autofill profiles from the web database.
112 scoped_ptr<WDTypedResult> GetAutofillProfiles(WebDatabase* db);
114 // Updates Autofill entries in the web database.
115 WebDatabase::State UpdateAutofillEntries(
116 const std::vector<autofill::AutofillEntry>& autofill_entries,
119 // Adds a credit card to the web database.
120 WebDatabase::State AddCreditCard(const CreditCard& credit_card,
123 // Updates a credit card in the web database.
124 WebDatabase::State UpdateCreditCard(const CreditCard& credit_card,
127 // Removes a credit card from the web database.
128 WebDatabase::State RemoveCreditCard(const std::string& guid,
131 // Returns a vector of all credit cards from the web database.
132 scoped_ptr<WDTypedResult> GetCreditCards(WebDatabase* db);
134 // Removes Autofill records from the database.
135 WebDatabase::State RemoveAutofillDataModifiedBetween(
136 const base::Time& delete_begin,
137 const base::Time& delete_end,
140 // Removes origin URLs associated with Autofill profiles and credit cards from
142 WebDatabase::State RemoveOriginURLsModifiedBetween(
143 const base::Time& delete_begin,
144 const base::Time& delete_end,
148 ~AutofillWebDataBackendImpl() override;
151 friend class base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>;
152 friend class base::DeleteHelper<AutofillWebDataBackendImpl>;
154 // This makes the destructor public, and thus allows us to aggregate
155 // SupportsUserData. It is private by default to prevent incorrect
156 // usage in class hierarchies where it is inherited by
157 // reference-counted objects.
158 class SupportsUserDataAggregatable : public base::SupportsUserData {
160 SupportsUserDataAggregatable() {}
161 ~SupportsUserDataAggregatable() override {}
164 DISALLOW_COPY_AND_ASSIGN(SupportsUserDataAggregatable);
167 // The MessageLoopProxy that this class uses as its UI thread.
168 scoped_refptr<base::MessageLoopProxy> ui_thread_;
170 // The MessageLoopProxy that this class uses as its DB thread.
171 scoped_refptr<base::MessageLoopProxy> db_thread_;
173 // Storage for user data to be accessed only on the DB thread. May
174 // be used e.g. for SyncableService subclasses that need to be owned
175 // by this object. Is created on first call to |GetDBUserData()|.
176 scoped_ptr<SupportsUserDataAggregatable> user_data_;
178 WebDatabase::State RemoveExpiredFormElementsImpl(WebDatabase* db);
180 // Callbacks to ensure that sensitive info is destroyed if request is
182 void DestroyAutofillProfileResult(const WDTypedResult* result);
183 void DestroyAutofillCreditCardResult(const WDTypedResult* result);
185 ObserverList<AutofillWebDataServiceObserverOnDBThread> db_observer_list_;
187 // WebDataServiceBackend allows direct access to DB.
188 // TODO(caitkp): Make it so nobody but us needs direct DB access anymore.
189 scoped_refptr<WebDataServiceBackend> web_database_backend_;
191 base::Closure on_changed_callback_;
193 DISALLOW_COPY_AND_ASSIGN(AutofillWebDataBackendImpl);
196 } // namespace autofill
198 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_