1 | // Copyright (c) 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 | package org.chromium.chrome.browser.autofill; |
6 | |
7 | import org.chromium.base.CalledByNative; |
8 | import org.chromium.base.JNINamespace; |
9 | import org.chromium.base.ThreadUtils; |
10 | |
11 | import java.util.ArrayList; |
12 | import java.util.List; |
13 | |
14 | /** |
15 | * Android wrapper of the PersonalDataManager which provides access from the Java |
16 | * layer. |
17 | * |
18 | * Only usable from the UI thread as it's primary purpose is for supporting the Android |
19 | * preferences UI. |
20 | * |
21 | * See chrome/browser/autofill/personal_data_manager.h for more details. |
22 | */ |
23 | @JNINamespace("autofill") |
24 | public class PersonalDataManager { |
25 | |
26 | public interface PersonalDataManagerObserver { |
27 | public abstract void onPersonalDataChanged(); |
28 | } |
29 | |
30 | public static class AutofillProfile { |
31 | private String mGUID; |
32 | private String mOrigin; |
33 | private String mFullName; |
34 | private String mCompanyName; |
35 | private String mAddressLine1; |
36 | private String mAddressLine2; |
37 | private String mCity; |
38 | private String mState; |
39 | private String mZip; |
40 | private String mCountry; |
41 | private String mPhoneNumber; |
42 | private String mEmailAddress; |
43 | |
44 | @CalledByNative("AutofillProfile") |
45 | public static AutofillProfile create(String guid, String origin, String fullName, |
46 | String companyName, String addressLine1, String addressLine2, String city, |
47 | String state, String zip, String country, String phoneNumber, String emailAddress) { |
48 | return new AutofillProfile(guid, origin, fullName, companyName, addressLine1, |
49 | addressLine2, city, state, zip, country, phoneNumber, emailAddress); |
50 | } |
51 | |
52 | public AutofillProfile(String guid, String origin, String fullName, String companyName, |
53 | String addressLine1, String addressLine2, String city, String state, |
54 | String zip, String country, String phoneNumber, String emailAddress) { |
55 | mGUID = guid; |
56 | mOrigin = origin; |
57 | mFullName = fullName; |
58 | mCompanyName = companyName; |
59 | mAddressLine1 = addressLine1; |
60 | mAddressLine2 = addressLine2; |
61 | mCity = city; |
62 | mState = state; |
63 | mZip = zip; |
64 | mCountry = country; |
65 | mPhoneNumber = phoneNumber; |
66 | mEmailAddress = emailAddress; |
67 | } |
68 | |
69 | @CalledByNative("AutofillProfile") |
70 | public String getGUID() { |
71 | return mGUID; |
72 | } |
73 | |
74 | @CalledByNative("AutofillProfile") |
75 | public String getOrigin() { |
76 | return mOrigin; |
77 | } |
78 | |
79 | @CalledByNative("AutofillProfile") |
80 | public String getFullName() { |
81 | return mFullName; |
82 | } |
83 | |
84 | @CalledByNative("AutofillProfile") |
85 | public String getCompanyName() { |
86 | return mCompanyName; |
87 | } |
88 | |
89 | @CalledByNative("AutofillProfile") |
90 | public String getAddressLine1() { |
91 | return mAddressLine1; |
92 | } |
93 | |
94 | @CalledByNative("AutofillProfile") |
95 | public String getAddressLine2() { |
96 | return mAddressLine2; |
97 | } |
98 | |
99 | @CalledByNative("AutofillProfile") |
100 | public String getCity() { |
101 | return mCity; |
102 | } |
103 | |
104 | @CalledByNative("AutofillProfile") |
105 | public String getState() { |
106 | return mState; |
107 | } |
108 | |
109 | @CalledByNative("AutofillProfile") |
110 | public String getZip() { |
111 | return mZip; |
112 | } |
113 | |
114 | @CalledByNative("AutofillProfile") |
115 | public String getCountry() { |
116 | return mCountry; |
117 | } |
118 | |
119 | public String getCountryCode() { |
120 | return nativeToCountryCode(mCountry); |
121 | } |
122 | |
123 | @CalledByNative("AutofillProfile") |
124 | public String getPhoneNumber() { |
125 | return mPhoneNumber; |
126 | } |
127 | |
128 | @CalledByNative("AutofillProfile") |
129 | public String getEmailAddress() { |
130 | return mEmailAddress; |
131 | } |
132 | |
133 | public void setGUID(String guid) { |
134 | mGUID = guid; |
135 | } |
136 | |
137 | public void setOrigin(String origin) { |
138 | mOrigin = origin; |
139 | } |
140 | |
141 | public void setFullName(String fullName) { |
142 | mFullName = fullName; |
143 | } |
144 | |
145 | public void setCompanyName(String companyName) { |
146 | mCompanyName = companyName; |
147 | } |
148 | |
149 | public void setAddressLine1(String addressLine1) { |
150 | mAddressLine1 = addressLine1; |
151 | } |
152 | |
153 | public void setAddressLine2(String addressLine2) { |
154 | mAddressLine2 = addressLine2; |
155 | } |
156 | |
157 | public void setCity(String city) { |
158 | mCity = city; |
159 | } |
160 | |
161 | public void setState(String state) { |
162 | mState = state; |
163 | } |
164 | |
165 | public void setZip(String zip) { |
166 | mZip = zip; |
167 | } |
168 | |
169 | public void setCountry(String country) { |
170 | mCountry = country; |
171 | } |
172 | |
173 | public void setPhoneNumber(String phoneNumber) { |
174 | mPhoneNumber = phoneNumber; |
175 | } |
176 | |
177 | public void setEmailAddress(String emailAddress) { |
178 | mEmailAddress = emailAddress; |
179 | } |
180 | } |
181 | |
182 | public static class CreditCard { |
183 | // Note that while some of these fields are numbers, they're predominantly read, |
184 | // marshaled and compared as strings. To save conversions, we use strings everywhere. |
185 | private String mGUID; |
186 | private String mOrigin; |
187 | private String mName; |
188 | private String mNumber; |
189 | private String mObfuscatedNumber; |
190 | private String mMonth; |
191 | private String mYear; |
192 | |
193 | @CalledByNative("CreditCard") |
194 | public static CreditCard create(String guid, String origin, String name, String number, |
195 | String obfuscatedNumber, String month, String year) { |
196 | return new CreditCard(guid, origin, name, number, obfuscatedNumber, month, year); |
197 | } |
198 | |
199 | public CreditCard(String guid, String origin, String name, String number, |
200 | String obfuscatedNumber, String month, String year) { |
201 | mGUID = guid; |
202 | mOrigin = origin; |
203 | mName = name; |
204 | mNumber = number; |
205 | mObfuscatedNumber = obfuscatedNumber; |
206 | mMonth = month; |
207 | mYear = year; |
208 | } |
209 | |
210 | @CalledByNative("CreditCard") |
211 | public String getGUID() { |
212 | return mGUID; |
213 | } |
214 | |
215 | @CalledByNative("CreditCard") |
216 | public String getOrigin() { |
217 | return mOrigin; |
218 | } |
219 | |
220 | @CalledByNative("CreditCard") |
221 | public String getName() { |
222 | return mName; |
223 | } |
224 | |
225 | @CalledByNative("CreditCard") |
226 | public String getNumber() { |
227 | return mNumber; |
228 | } |
229 | |
230 | public String getObfuscatedNumber() { |
231 | return mObfuscatedNumber; |
232 | } |
233 | |
234 | @CalledByNative("CreditCard") |
235 | public String getMonth() { |
236 | return mMonth; |
237 | } |
238 | |
239 | @CalledByNative("CreditCard") |
240 | public String getYear() { |
241 | return mYear; |
242 | } |
243 | |
244 | public void setGUID(String guid) { |
245 | mGUID = guid; |
246 | } |
247 | |
248 | public void setOrigin(String origin) { |
249 | mOrigin = origin; |
250 | } |
251 | |
252 | public void setName(String name) { |
253 | mName = name; |
254 | } |
255 | |
256 | public void setNumber(String number) { |
257 | mNumber = number; |
258 | } |
259 | |
260 | public void setObfuscatedNumber(String obfuscatedNumber) { |
261 | mObfuscatedNumber = obfuscatedNumber; |
262 | } |
263 | |
264 | public void setMonth(String month) { |
265 | mMonth = month; |
266 | } |
267 | |
268 | public void setYear(String year) { |
269 | mYear = year; |
270 | } |
271 | } |
272 | |
273 | private static PersonalDataManager sManager; |
274 | |
275 | public static PersonalDataManager getInstance() { |
276 | ThreadUtils.assertOnUiThread(); |
277 | if (sManager == null) { |
278 | sManager = new PersonalDataManager(); |
279 | } |
280 | return sManager; |
281 | } |
282 | |
283 | private final int mPersonalDataManagerAndroid; |
284 | private final List<PersonalDataManagerObserver> mDataObservers = |
285 | new ArrayList<PersonalDataManagerObserver>(); |
286 | |
287 | private PersonalDataManager() { |
288 | // Note that this technically leaks the native object, however, PersonalDataManager |
289 | // is a singleton that lives forever and there's no clean shutdown of Chrome on Android |
290 | mPersonalDataManagerAndroid = nativeInit(); |
291 | } |
292 | |
293 | /** |
294 | * Called from native when template URL service is done loading. |
295 | */ |
296 | @CalledByNative |
297 | private void personalDataChanged() { |
298 | ThreadUtils.assertOnUiThread(); |
299 | for (PersonalDataManagerObserver observer : mDataObservers) { |
300 | observer.onPersonalDataChanged(); |
301 | } |
302 | } |
303 | |
304 | /** |
305 | * Registers a PersonalDataManagerObserver on the native side. |
306 | */ |
307 | public void registerDataObserver(PersonalDataManagerObserver observer) { |
308 | ThreadUtils.assertOnUiThread(); |
309 | assert !mDataObservers.contains(observer); |
310 | mDataObservers.add(observer); |
311 | } |
312 | |
313 | /** |
314 | * Unregisters the provided observer. |
315 | */ |
316 | public void unregisterDataObserver(PersonalDataManagerObserver observer) { |
317 | ThreadUtils.assertOnUiThread(); |
318 | assert (mDataObservers.size() > 0); |
319 | assert (mDataObservers.contains(observer)); |
320 | mDataObservers.remove(observer); |
321 | } |
322 | |
323 | public List<AutofillProfile> getProfiles() { |
324 | ThreadUtils.assertOnUiThread(); |
325 | int profileCount = nativeGetProfileCount(mPersonalDataManagerAndroid); |
326 | List<AutofillProfile> profiles = new ArrayList<AutofillProfile>(profileCount); |
327 | for (int i = 0; i < profileCount; i++) { |
328 | profiles.add(nativeGetProfileByIndex(mPersonalDataManagerAndroid, i)); |
329 | } |
330 | return profiles; |
331 | } |
332 | |
333 | public AutofillProfile getProfile(String guid) { |
334 | ThreadUtils.assertOnUiThread(); |
335 | return nativeGetProfileByGUID(mPersonalDataManagerAndroid, guid); |
336 | } |
337 | |
338 | public void deleteProfile(String guid) { |
339 | ThreadUtils.assertOnUiThread(); |
340 | nativeRemoveByGUID(mPersonalDataManagerAndroid, guid); |
341 | } |
342 | |
343 | public String setProfile(AutofillProfile profile) { |
344 | ThreadUtils.assertOnUiThread(); |
345 | return nativeSetProfile(mPersonalDataManagerAndroid, profile); |
346 | } |
347 | |
348 | public List<CreditCard> getCreditCards() { |
349 | ThreadUtils.assertOnUiThread(); |
350 | int count = nativeGetCreditCardCount(mPersonalDataManagerAndroid); |
351 | List<CreditCard> cards = new ArrayList<CreditCard>(count); |
352 | for (int i = 0; i < count; i++) { |
353 | cards.add(nativeGetCreditCardByIndex(mPersonalDataManagerAndroid, i)); |
354 | } |
355 | return cards; |
356 | } |
357 | |
358 | public CreditCard getCreditCard(String guid) { |
359 | ThreadUtils.assertOnUiThread(); |
360 | return nativeGetCreditCardByGUID(mPersonalDataManagerAndroid, guid); |
361 | } |
362 | |
363 | public String setCreditCard(CreditCard card) { |
364 | ThreadUtils.assertOnUiThread(); |
365 | return nativeSetCreditCard(mPersonalDataManagerAndroid, card); |
366 | } |
367 | |
368 | public void deleteCreditCard(String guid) { |
369 | ThreadUtils.assertOnUiThread(); |
370 | nativeRemoveByGUID(mPersonalDataManagerAndroid, guid); |
371 | } |
372 | |
373 | private native int nativeInit(); |
374 | private native int nativeGetProfileCount(int nativePersonalDataManagerAndroid); |
375 | private native AutofillProfile nativeGetProfileByIndex(int nativePersonalDataManagerAndroid, |
376 | int index); |
377 | private native AutofillProfile nativeGetProfileByGUID(int nativePersonalDataManagerAndroid, |
378 | String guid); |
379 | private native String nativeSetProfile(int nativePersonalDataManagerAndroid, |
380 | AutofillProfile profile); |
381 | private native int nativeGetCreditCardCount(int nativePersonalDataManagerAndroid); |
382 | private native CreditCard nativeGetCreditCardByIndex(int nativePersonalDataManagerAndroid, |
383 | int index); |
384 | private native CreditCard nativeGetCreditCardByGUID(int nativePersonalDataManagerAndroid, |
385 | String guid); |
386 | private native String nativeSetCreditCard(int nativePersonalDataManagerAndroid, |
387 | CreditCard card); |
388 | private native void nativeRemoveByGUID(int nativePersonalDataManagerAndroid, String guid); |
389 | private static native String nativeToCountryCode(String countryName); |
390 | } |