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;
30 class AutofillProfile;
31 class AutofillWebDataServiceObserverOnDBThread;
34 // Backend implentation for the AutofillWebDataService. This class runs on the
35 // DB thread, as it handles reads and writes to the WebDatabase, and functions
36 // in it should only be called from that thread. Most functions here are just
37 // the implementations of the corresponding functions in the Autofill
39 // This class is destroyed on the DB thread.
40 class AutofillWebDataBackendImpl
41 : public base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>,
42 public AutofillWebDataBackend {
44 // |web_database_backend| is used to access the WebDatabase directly for
45 // Sync-related operations. |ui_thread| and |db_thread| are the threads that
46 // this class uses as its UI and DB threads respectively.
47 // |on_changed_callback| is a closure which can be used to notify the UI
48 // thread of changes initiated by Sync (this callback may be called multiple
50 AutofillWebDataBackendImpl(
51 scoped_refptr<WebDataServiceBackend> web_database_backend,
52 scoped_refptr<base::MessageLoopProxy> ui_thread,
53 scoped_refptr<base::MessageLoopProxy> db_thread,
54 const base::Closure& on_changed_callback);
56 // AutofillWebDataBackend implementation.
57 virtual void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer)
59 virtual void RemoveObserver(
60 AutofillWebDataServiceObserverOnDBThread* observer) OVERRIDE;
61 virtual WebDatabase* GetDatabase() OVERRIDE;
62 virtual void RemoveExpiredFormElements() OVERRIDE;
63 virtual void NotifyOfMultipleAutofillChanges() OVERRIDE;
65 // Returns a SupportsUserData objects that may be used to store data
66 // owned by the DB thread on this object. Should be called only from
67 // the DB thread, and will be destroyed on the DB thread soon after
68 // |ShutdownOnUIThread()| is called.
69 base::SupportsUserData* GetDBUserData();
73 // Adds form fields to the web database.
74 WebDatabase::State AddFormElements(const std::vector<FormFieldData>& fields,
77 // Returns a vector of values which have been entered in form input fields
79 scoped_ptr<WDTypedResult> GetFormValuesForElementName(
80 const base::string16& name,
81 const base::string16& prefix,
85 // Returns true if there are any elements in the form.
86 scoped_ptr<WDTypedResult> HasFormElements(WebDatabase* db);
88 // Removes form elements recorded for Autocomplete from the database.
89 WebDatabase::State RemoveFormElementsAddedBetween(
90 const base::Time& delete_begin,
91 const base::Time& delete_end,
95 // Removes the Form-value |value| which has been entered in form input fields
96 // named |name| from the database.
97 WebDatabase::State RemoveFormValueForElementName(const base::string16& name,
98 const base::string16& value,
101 // Adds an Autofill profile to the web database.
102 WebDatabase::State AddAutofillProfile(const AutofillProfile& profile,
105 // Updates an Autofill profile in the web database.
106 WebDatabase::State UpdateAutofillProfile(const AutofillProfile& profile,
109 // Removes an Autofill profile from the web database.
110 WebDatabase::State RemoveAutofillProfile(const std::string& guid,
113 // Returns all Autofill profiles from the web database.
114 scoped_ptr<WDTypedResult> GetAutofillProfiles(WebDatabase* db);
116 // Updates Autofill entries in the web database.
117 WebDatabase::State UpdateAutofillEntries(
118 const std::vector<autofill::AutofillEntry>& autofill_entries,
121 // Adds a credit card to the web database.
122 WebDatabase::State AddCreditCard(const CreditCard& credit_card,
125 // Updates a credit card in the web database.
126 WebDatabase::State UpdateCreditCard(const CreditCard& credit_card,
129 // Removes a credit card from the web database.
130 WebDatabase::State RemoveCreditCard(const std::string& guid,
133 // Returns a vector of all credit cards from the web database.
134 scoped_ptr<WDTypedResult> GetCreditCards(WebDatabase* db);
136 // Removes Autofill records from the database.
137 WebDatabase::State RemoveAutofillDataModifiedBetween(
138 const base::Time& delete_begin,
139 const base::Time& delete_end,
142 // Removes origin URLs associated with Autofill profiles and credit cards from
144 WebDatabase::State RemoveOriginURLsModifiedBetween(
145 const base::Time& delete_begin,
146 const base::Time& delete_end,
150 virtual ~AutofillWebDataBackendImpl();
153 friend class base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>;
154 friend class base::DeleteHelper<AutofillWebDataBackendImpl>;
156 // This makes the destructor public, and thus allows us to aggregate
157 // SupportsUserData. It is private by default to prevent incorrect
158 // usage in class hierarchies where it is inherited by
159 // reference-counted objects.
160 class SupportsUserDataAggregatable : public base::SupportsUserData {
162 SupportsUserDataAggregatable() {}
163 virtual ~SupportsUserDataAggregatable() {}
165 DISALLOW_COPY_AND_ASSIGN(SupportsUserDataAggregatable);
168 // The MessageLoopProxy that this class uses as its UI thread.
169 scoped_refptr<base::MessageLoopProxy> ui_thread_;
171 // The MessageLoopProxy that this class uses as its DB thread.
172 scoped_refptr<base::MessageLoopProxy> db_thread_;
174 // Storage for user data to be accessed only on the DB thread. May
175 // be used e.g. for SyncableService subclasses that need to be owned
176 // by this object. Is created on first call to |GetDBUserData()|.
177 scoped_ptr<SupportsUserDataAggregatable> user_data_;
179 WebDatabase::State RemoveExpiredFormElementsImpl(WebDatabase* db);
181 // Callbacks to ensure that sensitive info is destroyed if request is
183 void DestroyAutofillProfileResult(const WDTypedResult* result);
184 void DestroyAutofillCreditCardResult(const WDTypedResult* result);
186 ObserverList<AutofillWebDataServiceObserverOnDBThread> db_observer_list_;
188 // WebDataServiceBackend allows direct access to DB.
189 // TODO(caitkp): Make it so nobody but us needs direct DB access anymore.
190 scoped_refptr<WebDataServiceBackend> web_database_backend_;
192 base::Closure on_changed_callback_;
194 DISALLOW_COPY_AND_ASSIGN(AutofillWebDataBackendImpl);
197 } // namespace autofill
199 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_