• 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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
7 
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"
20 
21 namespace base {
22 class MessageLoopProxy;
23 }
24 
25 class WebDataServiceBackend;
26 
27 namespace autofill {
28 
29 class AutofillChange;
30 class AutofillProfile;
31 class AutofillWebDataServiceObserverOnDBThread;
32 class CreditCard;
33 
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
38 // WebDataService.
39 // This class is destroyed on the DB thread.
40 class AutofillWebDataBackendImpl
41     : public base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>,
42       public AutofillWebDataBackend {
43  public:
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
49   // times).
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);
55 
56   // AutofillWebDataBackend implementation.
57   virtual void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer)
58       OVERRIDE;
59   virtual void RemoveObserver(
60       AutofillWebDataServiceObserverOnDBThread* observer) OVERRIDE;
61   virtual WebDatabase* GetDatabase() OVERRIDE;
62   virtual void RemoveExpiredFormElements() OVERRIDE;
63   virtual void NotifyOfMultipleAutofillChanges() OVERRIDE;
64 
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();
70 
71   void ResetUserData();
72 
73   // Adds form fields to the web database.
74   WebDatabase::State AddFormElements(const std::vector<FormFieldData>& fields,
75                                      WebDatabase* db);
76 
77   // Returns a vector of values which have been entered in form input fields
78   // named |name|.
79   scoped_ptr<WDTypedResult> GetFormValuesForElementName(
80       const base::string16& name,
81       const base::string16& prefix,
82       int limit,
83       WebDatabase* db);
84 
85   // Returns true if there are any elements in the form.
86   scoped_ptr<WDTypedResult> HasFormElements(WebDatabase* db);
87 
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,
92       WebDatabase* db);
93 
94 
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,
99                                                    WebDatabase* db);
100 
101   // Adds an Autofill profile to the web database.
102   WebDatabase::State AddAutofillProfile(const AutofillProfile& profile,
103                                         WebDatabase* db);
104 
105   // Updates an Autofill profile in the web database.
106   WebDatabase::State UpdateAutofillProfile(const AutofillProfile& profile,
107                                            WebDatabase* db);
108 
109   // Removes an Autofill profile from the web database.
110   WebDatabase::State RemoveAutofillProfile(const std::string& guid,
111                                            WebDatabase* db);
112 
113   // Returns all Autofill profiles from the web database.
114   scoped_ptr<WDTypedResult> GetAutofillProfiles(WebDatabase* db);
115 
116   // Adds a credit card to the web database.
117   WebDatabase::State AddCreditCard(const CreditCard& credit_card,
118                                    WebDatabase* db);
119 
120   // Updates a credit card in the web database.
121   WebDatabase::State UpdateCreditCard(const CreditCard& credit_card,
122                                       WebDatabase* db);
123 
124   // Removes a credit card from the web database.
125   WebDatabase::State RemoveCreditCard(const std::string& guid,
126                                       WebDatabase* db);
127 
128   // Returns a vector of all credit cards from the web database.
129   scoped_ptr<WDTypedResult> GetCreditCards(WebDatabase* db);
130 
131   // Removes Autofill records from the database.
132   WebDatabase::State RemoveAutofillDataModifiedBetween(
133       const base::Time& delete_begin,
134       const base::Time& delete_end,
135       WebDatabase* db);
136 
137   // Removes origin URLs associated with Autofill profiles and credit cards from
138   // the database.
139   WebDatabase::State RemoveOriginURLsModifiedBetween(
140       const base::Time& delete_begin,
141       const base::Time& delete_end,
142       WebDatabase* db);
143 
144  protected:
145   virtual ~AutofillWebDataBackendImpl();
146 
147  private:
148   friend class base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>;
149   friend class base::DeleteHelper<AutofillWebDataBackendImpl>;
150 
151   // This makes the destructor public, and thus allows us to aggregate
152   // SupportsUserData. It is private by default to prevent incorrect
153   // usage in class hierarchies where it is inherited by
154   // reference-counted objects.
155   class SupportsUserDataAggregatable : public base::SupportsUserData {
156    public:
SupportsUserDataAggregatable()157     SupportsUserDataAggregatable() {}
~SupportsUserDataAggregatable()158     virtual ~SupportsUserDataAggregatable() {}
159    private:
160     DISALLOW_COPY_AND_ASSIGN(SupportsUserDataAggregatable);
161   };
162 
163   // The MessageLoopProxy that this class uses as its UI thread.
164   scoped_refptr<base::MessageLoopProxy> ui_thread_;
165 
166   // The MessageLoopProxy that this class uses as its DB thread.
167   scoped_refptr<base::MessageLoopProxy> db_thread_;
168 
169   // Storage for user data to be accessed only on the DB thread. May
170   // be used e.g. for SyncableService subclasses that need to be owned
171   // by this object. Is created on first call to |GetDBUserData()|.
172   scoped_ptr<SupportsUserDataAggregatable> user_data_;
173 
174   WebDatabase::State RemoveExpiredFormElementsImpl(WebDatabase* db);
175 
176   // Callbacks to ensure that sensitive info is destroyed if request is
177   // cancelled.
178   void DestroyAutofillProfileResult(const WDTypedResult* result);
179   void DestroyAutofillCreditCardResult(const WDTypedResult* result);
180 
181   ObserverList<AutofillWebDataServiceObserverOnDBThread> db_observer_list_;
182 
183   // WebDataServiceBackend allows direct access to DB.
184   // TODO(caitkp): Make it so nobody but us needs direct DB access anymore.
185   scoped_refptr<WebDataServiceBackend> web_database_backend_;
186 
187   base::Closure on_changed_callback_;
188 
189   DISALLOW_COPY_AND_ASSIGN(AutofillWebDataBackendImpl);
190 };
191 
192 }  // namespace autofill
193 
194 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
195