OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include "components/autofill/browser/webdata/autofill_webdata_backend.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/stl_util.h" | |
9 #include "components/autofill/browser/autofill_country.h" | |
10 #include "components/autofill/browser/autofill_profile.h" | |
11 #include "components/autofill/browser/credit_card.h" | |
12 #include "components/autofill/browser/webdata/autofill_change.h" | |
13 #include "components/autofill/browser/webdata/autofill_entry.h" | |
14 #include "components/autofill/browser/webdata/autofill_table.h" | |
15 #include "components/autofill/browser/webdata/autofill_webdata_service_observer.
h" | |
16 #include "components/autofill/common/form_field_data.h" | |
17 | |
18 using base::Bind; | |
19 using base::Time; | |
20 using content::BrowserThread; | |
21 | |
22 namespace autofill { | |
23 | |
24 AutofillWebDataBackend::AutofillWebDataBackend() { | |
25 } | |
26 | |
27 void AutofillWebDataBackend::AddObserver( | |
28 AutofillWebDataServiceObserverOnDBThread* observer) { | |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
30 db_observer_list_.AddObserver(observer); | |
31 } | |
32 | |
33 void AutofillWebDataBackend::RemoveObserver( | |
34 AutofillWebDataServiceObserverOnDBThread* observer) { | |
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
36 db_observer_list_.RemoveObserver(observer); | |
37 } | |
38 | |
39 AutofillWebDataBackend::~AutofillWebDataBackend() { | |
40 } | |
41 | |
42 WebDatabase::State AutofillWebDataBackend::AddFormElements( | |
43 const std::vector<FormFieldData>& fields, WebDatabase* db) { | |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
45 AutofillChangeList changes; | |
46 if (!AutofillTable::FromWebDatabase(db)->AddFormFieldValues( | |
47 fields, &changes)) { | |
48 NOTREACHED(); | |
49 return WebDatabase::COMMIT_NOT_NEEDED; | |
50 } | |
51 | |
52 // Post the notifications including the list of affected keys. | |
53 // This is sent here so that work resulting from this notification will be | |
54 // done on the DB thread, and not the UI thread. | |
55 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
56 db_observer_list_, | |
57 AutofillEntriesChanged(changes)); | |
58 | |
59 return WebDatabase::COMMIT_NEEDED; | |
60 } | |
61 | |
62 scoped_ptr<WDTypedResult> | |
63 AutofillWebDataBackend::GetFormValuesForElementName( | |
64 const base::string16& name, const base::string16& prefix, int limit, | |
65 WebDatabase* db) { | |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
67 std::vector<base::string16> values; | |
68 AutofillTable::FromWebDatabase(db)->GetFormValuesForElementName( | |
69 name, prefix, &values, limit); | |
70 return scoped_ptr<WDTypedResult>( | |
71 new WDResult<std::vector<base::string16> >(AUTOFILL_VALUE_RESULT, | |
72 values)); | |
73 } | |
74 | |
75 WebDatabase::State AutofillWebDataBackend::RemoveFormElementsAddedBetween( | |
76 const base::Time& delete_begin, const base::Time& delete_end, | |
77 WebDatabase* db) { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
79 AutofillChangeList changes; | |
80 | |
81 if (AutofillTable::FromWebDatabase(db)->RemoveFormElementsAddedBetween( | |
82 delete_begin, delete_end, &changes)) { | |
83 if (!changes.empty()) { | |
84 // Post the notifications including the list of affected keys. | |
85 // This is sent here so that work resulting from this notification | |
86 // will be done on the DB thread, and not the UI thread. | |
87 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
88 db_observer_list_, | |
89 AutofillEntriesChanged(changes)); | |
90 } | |
91 return WebDatabase::COMMIT_NEEDED; | |
92 } | |
93 return WebDatabase::COMMIT_NOT_NEEDED; | |
94 } | |
95 | |
96 WebDatabase::State AutofillWebDataBackend::RemoveExpiredFormElements( | |
97 WebDatabase* db) { | |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
99 AutofillChangeList changes; | |
100 | |
101 if (AutofillTable::FromWebDatabase(db)->RemoveExpiredFormElements(&changes)) { | |
102 if (!changes.empty()) { | |
103 // Post the notifications including the list of affected keys. | |
104 // This is sent here so that work resulting from this notification | |
105 // will be done on the DB thread, and not the UI thread. | |
106 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
107 db_observer_list_, | |
108 AutofillEntriesChanged(changes)); | |
109 } | |
110 return WebDatabase::COMMIT_NEEDED; | |
111 } | |
112 return WebDatabase::COMMIT_NOT_NEEDED; | |
113 } | |
114 | |
115 WebDatabase::State AutofillWebDataBackend::RemoveFormValueForElementName( | |
116 const base::string16& name, const base::string16& value, WebDatabase* db) { | |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
118 | |
119 if (AutofillTable::FromWebDatabase(db)->RemoveFormElement(name, value)) { | |
120 AutofillChangeList changes; | |
121 changes.push_back( | |
122 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value))); | |
123 | |
124 // Post the notifications including the list of affected keys. | |
125 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
126 db_observer_list_, | |
127 AutofillEntriesChanged(changes)); | |
128 | |
129 return WebDatabase::COMMIT_NEEDED; | |
130 } | |
131 return WebDatabase::COMMIT_NOT_NEEDED; | |
132 } | |
133 | |
134 WebDatabase::State AutofillWebDataBackend::AddAutofillProfile( | |
135 const AutofillProfile& profile, WebDatabase* db) { | |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
137 if (!AutofillTable::FromWebDatabase(db)->AddAutofillProfile(profile)) { | |
138 NOTREACHED(); | |
139 return WebDatabase::COMMIT_NOT_NEEDED; | |
140 } | |
141 | |
142 // Send GUID-based notification. | |
143 AutofillProfileChange change( | |
144 AutofillProfileChange::ADD, profile.guid(), &profile); | |
145 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
146 db_observer_list_, | |
147 AutofillProfileChanged(change)); | |
148 | |
149 return WebDatabase::COMMIT_NEEDED; | |
150 } | |
151 | |
152 WebDatabase::State AutofillWebDataBackend::UpdateAutofillProfile( | |
153 const AutofillProfile& profile, WebDatabase* db) { | |
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
155 // Only perform the update if the profile exists. It is currently | |
156 // valid to try to update a missing profile. We simply drop the write and | |
157 // the caller will detect this on the next refresh. | |
158 AutofillProfile* original_profile = NULL; | |
159 if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(profile.guid(), | |
160 &original_profile)) { | |
161 return WebDatabase::COMMIT_NOT_NEEDED; | |
162 } | |
163 scoped_ptr<AutofillProfile> scoped_profile(original_profile); | |
164 | |
165 if (!AutofillTable::FromWebDatabase(db)->UpdateAutofillProfileMulti( | |
166 profile)) { | |
167 NOTREACHED(); | |
168 return WebDatabase::COMMIT_NEEDED; | |
169 } | |
170 | |
171 // Send GUID-based notification. | |
172 AutofillProfileChange change( | |
173 AutofillProfileChange::UPDATE, profile.guid(), &profile); | |
174 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
175 db_observer_list_, | |
176 AutofillProfileChanged(change)); | |
177 | |
178 return WebDatabase::COMMIT_NEEDED; | |
179 } | |
180 | |
181 WebDatabase::State AutofillWebDataBackend::RemoveAutofillProfile( | |
182 const std::string& guid, WebDatabase* db) { | |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
184 AutofillProfile* profile = NULL; | |
185 if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(guid, &profile)) { | |
186 NOTREACHED(); | |
187 return WebDatabase::COMMIT_NOT_NEEDED; | |
188 } | |
189 scoped_ptr<AutofillProfile> scoped_profile(profile); | |
190 | |
191 if (!AutofillTable::FromWebDatabase(db)->RemoveAutofillProfile(guid)) { | |
192 NOTREACHED(); | |
193 return WebDatabase::COMMIT_NOT_NEEDED; | |
194 } | |
195 | |
196 // Send GUID-based notification. | |
197 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); | |
198 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
199 db_observer_list_, | |
200 AutofillProfileChanged(change)); | |
201 | |
202 return WebDatabase::COMMIT_NEEDED; | |
203 } | |
204 | |
205 scoped_ptr<WDTypedResult> AutofillWebDataBackend::GetAutofillProfiles( | |
206 WebDatabase* db) { | |
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
208 std::vector<AutofillProfile*> profiles; | |
209 AutofillTable::FromWebDatabase(db)->GetAutofillProfiles(&profiles); | |
210 return scoped_ptr<WDTypedResult>( | |
211 new WDDestroyableResult<std::vector<AutofillProfile*> >( | |
212 AUTOFILL_PROFILES_RESULT, | |
213 profiles, | |
214 base::Bind(&AutofillWebDataBackend::DestroyAutofillProfileResult, | |
215 base::Unretained(this)))); | |
216 } | |
217 | |
218 WebDatabase::State AutofillWebDataBackend::AddCreditCard( | |
219 const CreditCard& credit_card, WebDatabase* db) { | |
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
221 if (!AutofillTable::FromWebDatabase(db)->AddCreditCard(credit_card)) { | |
222 NOTREACHED(); | |
223 return WebDatabase::COMMIT_NOT_NEEDED; | |
224 } | |
225 | |
226 return WebDatabase::COMMIT_NEEDED; | |
227 } | |
228 | |
229 WebDatabase::State AutofillWebDataBackend::UpdateCreditCard( | |
230 const CreditCard& credit_card, WebDatabase* db) { | |
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
232 // It is currently valid to try to update a missing profile. We simply drop | |
233 // the write and the caller will detect this on the next refresh. | |
234 CreditCard* original_credit_card = NULL; | |
235 if (!AutofillTable::FromWebDatabase(db)->GetCreditCard(credit_card.guid(), | |
236 &original_credit_card)) { | |
237 return WebDatabase::COMMIT_NOT_NEEDED; | |
238 } | |
239 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); | |
240 | |
241 if (!AutofillTable::FromWebDatabase(db)->UpdateCreditCard(credit_card)) { | |
242 NOTREACHED(); | |
243 return WebDatabase::COMMIT_NOT_NEEDED; | |
244 } | |
245 return WebDatabase::COMMIT_NEEDED; | |
246 } | |
247 | |
248 WebDatabase::State AutofillWebDataBackend::RemoveCreditCard( | |
249 const std::string& guid, WebDatabase* db) { | |
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
251 if (!AutofillTable::FromWebDatabase(db)->RemoveCreditCard(guid)) { | |
252 NOTREACHED(); | |
253 return WebDatabase::COMMIT_NOT_NEEDED; | |
254 } | |
255 return WebDatabase::COMMIT_NEEDED; | |
256 } | |
257 | |
258 scoped_ptr<WDTypedResult> AutofillWebDataBackend::GetCreditCards( | |
259 WebDatabase* db) { | |
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
261 std::vector<CreditCard*> credit_cards; | |
262 AutofillTable::FromWebDatabase(db)->GetCreditCards(&credit_cards); | |
263 return scoped_ptr<WDTypedResult>( | |
264 new WDDestroyableResult<std::vector<CreditCard*> >( | |
265 AUTOFILL_CREDITCARDS_RESULT, | |
266 credit_cards, | |
267 base::Bind(&AutofillWebDataBackend::DestroyAutofillCreditCardResult, | |
268 base::Unretained(this)))); | |
269 } | |
270 | |
271 WebDatabase::State | |
272 AutofillWebDataBackend::RemoveAutofillDataModifiedBetween( | |
273 const base::Time& delete_begin, | |
274 const base::Time& delete_end, | |
275 WebDatabase* db) { | |
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
277 std::vector<std::string> profile_guids; | |
278 std::vector<std::string> credit_card_guids; | |
279 if (AutofillTable::FromWebDatabase(db)->RemoveAutofillDataModifiedBetween( | |
280 delete_begin, | |
281 delete_end, | |
282 &profile_guids, | |
283 &credit_card_guids)) { | |
284 for (std::vector<std::string>::iterator iter = profile_guids.begin(); | |
285 iter != profile_guids.end(); ++iter) { | |
286 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter, NULL); | |
287 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
288 db_observer_list_, | |
289 AutofillProfileChanged(change)); | |
290 } | |
291 // Note: It is the caller's responsibility to post notifications for any | |
292 // changes, e.g. by calling the Refresh() method of PersonalDataManager. | |
293 return WebDatabase::COMMIT_NEEDED; | |
294 } | |
295 return WebDatabase::COMMIT_NOT_NEEDED; | |
296 } | |
297 | |
298 void AutofillWebDataBackend::DestroyAutofillProfileResult( | |
299 const WDTypedResult* result) { | |
300 DCHECK(result->GetType() == AUTOFILL_PROFILES_RESULT); | |
301 const WDResult<std::vector<AutofillProfile*> >* r = | |
302 static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result); | |
303 std::vector<AutofillProfile*> profiles = r->GetValue(); | |
304 STLDeleteElements(&profiles); | |
305 } | |
306 | |
307 void AutofillWebDataBackend::DestroyAutofillCreditCardResult( | |
308 const WDTypedResult* result) { | |
309 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); | |
310 const WDResult<std::vector<CreditCard*> >* r = | |
311 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); | |
312 | |
313 std::vector<CreditCard*> credit_cards = r->GetValue(); | |
314 STLDeleteElements(&credit_cards); | |
315 } | |
316 | |
317 } // namespace autofill | |
OLD | NEW |