Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / webdata / autofill_webdata_backend_impl.h
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 AutofillProfile;
30 class AutofillWebDataServiceObserverOnDBThread;
31 class CreditCard;
32
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
37 // WebDataService.
38 // This class is destroyed on the DB thread.
39 class AutofillWebDataBackendImpl
40     : public base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>,
41       public AutofillWebDataBackend {
42  public:
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
48   // times).
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);
54
55   // AutofillWebDataBackend implementation.
56   void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer) override;
57   void RemoveObserver(
58       AutofillWebDataServiceObserverOnDBThread* observer) override;
59   WebDatabase* GetDatabase() override;
60   void RemoveExpiredFormElements() override;
61   void NotifyOfMultipleAutofillChanges() override;
62
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();
68
69   void ResetUserData();
70
71   // Adds form fields to the web database.
72   WebDatabase::State AddFormElements(const std::vector<FormFieldData>& fields,
73                                      WebDatabase* db);
74
75   // Returns a vector of values which have been entered in form input fields
76   // named |name|.
77   scoped_ptr<WDTypedResult> GetFormValuesForElementName(
78       const base::string16& name,
79       const base::string16& prefix,
80       int limit,
81       WebDatabase* db);
82
83   // Returns true if there are any elements in the form.
84   scoped_ptr<WDTypedResult> HasFormElements(WebDatabase* db);
85
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,
90       WebDatabase* db);
91
92
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,
97                                                    WebDatabase* db);
98
99   // Adds an Autofill profile to the web database.
100   WebDatabase::State AddAutofillProfile(const AutofillProfile& profile,
101                                         WebDatabase* db);
102
103   // Updates an Autofill profile in the web database.
104   WebDatabase::State UpdateAutofillProfile(const AutofillProfile& profile,
105                                            WebDatabase* db);
106
107   // Removes an Autofill profile from the web database.
108   WebDatabase::State RemoveAutofillProfile(const std::string& guid,
109                                            WebDatabase* db);
110
111   // Returns all Autofill profiles from the web database.
112   scoped_ptr<WDTypedResult> GetAutofillProfiles(WebDatabase* db);
113
114   // Updates Autofill entries in the web database.
115   WebDatabase::State UpdateAutofillEntries(
116       const std::vector<autofill::AutofillEntry>& autofill_entries,
117       WebDatabase* db);
118
119   // Adds a credit card to the web database.
120   WebDatabase::State AddCreditCard(const CreditCard& credit_card,
121                                    WebDatabase* db);
122
123   // Updates a credit card in the web database.
124   WebDatabase::State UpdateCreditCard(const CreditCard& credit_card,
125                                       WebDatabase* db);
126
127   // Removes a credit card from the web database.
128   WebDatabase::State RemoveCreditCard(const std::string& guid,
129                                       WebDatabase* db);
130
131   // Returns a vector of all credit cards from the web database.
132   scoped_ptr<WDTypedResult> GetCreditCards(WebDatabase* db);
133
134   // Removes Autofill records from the database.
135   WebDatabase::State RemoveAutofillDataModifiedBetween(
136       const base::Time& delete_begin,
137       const base::Time& delete_end,
138       WebDatabase* db);
139
140   // Removes origin URLs associated with Autofill profiles and credit cards from
141   // the database.
142   WebDatabase::State RemoveOriginURLsModifiedBetween(
143       const base::Time& delete_begin,
144       const base::Time& delete_end,
145       WebDatabase* db);
146
147  protected:
148   ~AutofillWebDataBackendImpl() override;
149
150  private:
151   friend class base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>;
152   friend class base::DeleteHelper<AutofillWebDataBackendImpl>;
153
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 {
159    public:
160     SupportsUserDataAggregatable() {}
161     ~SupportsUserDataAggregatable() override {}
162
163    private:
164     DISALLOW_COPY_AND_ASSIGN(SupportsUserDataAggregatable);
165   };
166
167   // The MessageLoopProxy that this class uses as its UI thread.
168   scoped_refptr<base::MessageLoopProxy> ui_thread_;
169
170   // The MessageLoopProxy that this class uses as its DB thread.
171   scoped_refptr<base::MessageLoopProxy> db_thread_;
172
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_;
177
178   WebDatabase::State RemoveExpiredFormElementsImpl(WebDatabase* db);
179
180   // Callbacks to ensure that sensitive info is destroyed if request is
181   // cancelled.
182   void DestroyAutofillProfileResult(const WDTypedResult* result);
183   void DestroyAutofillCreditCardResult(const WDTypedResult* result);
184
185   ObserverList<AutofillWebDataServiceObserverOnDBThread> db_observer_list_;
186
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_;
190
191   base::Closure on_changed_callback_;
192
193   DISALLOW_COPY_AND_ASSIGN(AutofillWebDataBackendImpl);
194 };
195
196 }  // namespace autofill
197
198 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_