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 #ifndef CHROME_BROWSER_AUTOFILL_PERSONAL_DATA_MANAGER_H_ | |
6 #define CHROME_BROWSER_AUTOFILL_PERSONAL_DATA_MANAGER_H_ | |
7 | |
8 #include <set> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/observer_list.h" | |
15 #include "base/string16.h" | |
16 #include "chrome/browser/api/webdata/web_data_service_consumer.h" | |
17 #include "chrome/browser/autofill/autofill_profile.h" | |
18 #include "chrome/browser/autofill/credit_card.h" | |
19 #include "chrome/browser/autofill/field_types.h" | |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
22 | |
23 class AutofillMetrics; | |
24 class FormStructure; | |
25 class PersonalDataManagerObserver; | |
26 | |
27 namespace autofill_helper { | |
28 void SetProfiles(int, std::vector<AutofillProfile>*); | |
29 void SetCreditCards(int, std::vector<CreditCard>*); | |
30 } | |
31 | |
32 namespace content { | |
33 class BrowserContext; | |
34 } | |
35 | |
36 // Handles loading and saving Autofill profile information to the web database. | |
37 // This class also stores the profiles loaded from the database for use during | |
38 // Autofill. | |
39 class PersonalDataManager : public WebDataServiceConsumer, | |
40 public content::NotificationObserver { | |
41 public: | |
42 // A pair of GUID and variant index. Represents a single FormGroup and a | |
43 // specific data variant. | |
44 typedef std::pair<std::string, size_t> GUIDPair; | |
45 | |
46 PersonalDataManager(); | |
47 virtual ~PersonalDataManager(); | |
48 | |
49 // Kicks off asynchronous loading of profiles and credit cards. | |
50 void Init(content::BrowserContext* context); | |
51 | |
52 // WebDataServiceConsumer: | |
53 virtual void OnWebDataServiceRequestDone( | |
54 WebDataServiceBase::Handle h, | |
55 const WDTypedResult* result) OVERRIDE; | |
56 | |
57 // Adds a listener to be notified of PersonalDataManager events. | |
58 virtual void AddObserver(PersonalDataManagerObserver* observer); | |
59 | |
60 // Removes |observer| as an observer of this PersonalDataManager. | |
61 virtual void RemoveObserver(PersonalDataManagerObserver* observer); | |
62 | |
63 // content::NotificationObserver: | |
64 // Observes "batch" changes made by Sync and refreshes data from the | |
65 // WebDataServiceBase in response. | |
66 virtual void Observe(int type, | |
67 const content::NotificationSource& source, | |
68 const content::NotificationDetails& details) OVERRIDE; | |
69 | |
70 // Scans the given |form| for importable Autofill data. If the form includes | |
71 // sufficient address data, it is immediately imported. If the form includes | |
72 // sufficient credit card data, it is stored into |credit_card|, so that we | |
73 // can prompt the user whether to save this data. | |
74 // Returns |true| if sufficient address or credit card data was found. | |
75 bool ImportFormData(const FormStructure& form, | |
76 const CreditCard** credit_card); | |
77 | |
78 // Saves |imported_profile| to the WebDB if it exists. | |
79 virtual void SaveImportedProfile(const AutofillProfile& imported_profile); | |
80 | |
81 // Saves a credit card value detected in |ImportedFormData|. | |
82 virtual void SaveImportedCreditCard(const CreditCard& imported_credit_card); | |
83 | |
84 // Adds |profile| to the web database. | |
85 void AddProfile(const AutofillProfile& profile); | |
86 | |
87 // Updates |profile| which already exists in the web database. | |
88 void UpdateProfile(const AutofillProfile& profile); | |
89 | |
90 // Removes the profile or credit card represented by |guid|. | |
91 virtual void RemoveByGUID(const std::string& guid); | |
92 | |
93 // Returns the profile with the specified |guid|, or NULL if there is no | |
94 // profile with the specified |guid|. Both web and auxiliary profiles may | |
95 // be returned. | |
96 AutofillProfile* GetProfileByGUID(const std::string& guid); | |
97 | |
98 // Adds |credit_card| to the web database. | |
99 void AddCreditCard(const CreditCard& credit_card); | |
100 | |
101 // Updates |credit_card| which already exists in the web database. | |
102 void UpdateCreditCard(const CreditCard& credit_card); | |
103 | |
104 // Returns the credit card with the specified |guid|, or NULL if there is | |
105 // no credit card with the specified |guid|. | |
106 CreditCard* GetCreditCardByGUID(const std::string& guid); | |
107 | |
108 // Gets the field types availabe in the stored address and credit card data. | |
109 void GetNonEmptyTypes(FieldTypeSet* non_empty_types); | |
110 | |
111 // Returns true if the credit card information is stored with a password. | |
112 bool HasPassword(); | |
113 | |
114 // Returns whether the personal data has been loaded from the web database. | |
115 virtual bool IsDataLoaded() const; | |
116 | |
117 // This PersonalDataManager owns these profiles and credit cards. Their | |
118 // lifetime is until the web database is updated with new profile and credit | |
119 // card information, respectively. |profiles()| returns both web and | |
120 // auxiliary profiles. |web_profiles()| returns only web profiles. | |
121 virtual const std::vector<AutofillProfile*>& GetProfiles(); | |
122 virtual const std::vector<AutofillProfile*>& web_profiles() const; | |
123 virtual const std::vector<CreditCard*>& credit_cards() const; | |
124 | |
125 // Loads profiles that can suggest data for |type|. |field_contents| is the | |
126 // part the user has already typed. |field_is_autofilled| is true if the field | |
127 // has already been autofilled. |other_field_types| represents the rest of | |
128 // form. Identifying info is loaded into the last four outparams. | |
129 void GetProfileSuggestions( | |
130 AutofillFieldType type, | |
131 const string16& field_contents, | |
132 bool field_is_autofilled, | |
133 std::vector<AutofillFieldType> other_field_types, | |
134 std::vector<string16>* values, | |
135 std::vector<string16>* labels, | |
136 std::vector<string16>* icons, | |
137 std::vector<GUIDPair>* guid_pairs); | |
138 | |
139 // Gets credit cards that can suggest data for |type|. See | |
140 // GetProfileSuggestions for argument descriptions. The variant in each | |
141 // GUID pair should be ignored. | |
142 void GetCreditCardSuggestions( | |
143 AutofillFieldType type, | |
144 const string16& field_contents, | |
145 std::vector<string16>* values, | |
146 std::vector<string16>* labels, | |
147 std::vector<string16>* icons, | |
148 std::vector<GUIDPair>* guid_pairs); | |
149 | |
150 // Re-loads profiles and credit cards from the WebDatabase asynchronously. | |
151 // In the general case, this is a no-op and will re-create the same | |
152 // in-memory model as existed prior to the call. If any change occurred to | |
153 // profiles in the WebDatabase directly, as is the case if the browser sync | |
154 // engine processed a change from the cloud, we will learn of these as a | |
155 // result of this call. | |
156 // | |
157 // Also see SetProfile for more details. | |
158 virtual void Refresh(); | |
159 | |
160 // Checks suitability of |profile| for adding to the user's set of profiles. | |
161 static bool IsValidLearnableProfile(const AutofillProfile& profile); | |
162 | |
163 // Merges |profile| into one of the |existing_profiles| if possible; otherwise | |
164 // appends |profile| to the end of that list. Fills |merged_profiles| with the | |
165 // result. | |
166 static bool MergeProfile( | |
167 const AutofillProfile& profile, | |
168 const std::vector<AutofillProfile*>& existing_profiles, | |
169 std::vector<AutofillProfile>* merged_profiles); | |
170 | |
171 protected: | |
172 // Only PersonalDataManagerFactory and certain tests can create instances of | |
173 // PersonalDataManager. | |
174 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, FirstMiddleLast); | |
175 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AutofillIsEnabledAtStartup); | |
176 FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest, | |
177 AggregateExistingAuxiliaryProfile); | |
178 friend class AutofillTest; | |
179 friend class PersonalDataManagerFactory; | |
180 friend class PersonalDataManagerTest; | |
181 friend class ProfileSyncServiceAutofillTest; | |
182 friend class RemoveAutofillTester; | |
183 friend class TestingAutomationProvider; | |
184 friend struct base::DefaultDeleter<PersonalDataManager>; | |
185 friend void autofill_helper::SetProfiles(int, std::vector<AutofillProfile>*); | |
186 friend void autofill_helper::SetCreditCards(int, std::vector<CreditCard>*); | |
187 | |
188 // Sets |web_profiles_| to the contents of |profiles| and updates the web | |
189 // database by adding, updating and removing profiles. | |
190 // The relationship between this and Refresh is subtle. | |
191 // A call to |SetProfiles| could include out-of-date data that may conflict | |
192 // if we didn't refresh-to-latest before an Autofill window was opened for | |
193 // editing. |SetProfiles| is implemented to make a "best effort" to apply the | |
194 // changes, but in extremely rare edge cases it is possible not all of the | |
195 // updates in |profiles| make it to the DB. This is why SetProfiles will | |
196 // invoke Refresh after finishing, to ensure we get into a | |
197 // consistent state. See Refresh for details. | |
198 void SetProfiles(std::vector<AutofillProfile>* profiles); | |
199 | |
200 // Sets |credit_cards_| to the contents of |credit_cards| and updates the web | |
201 // database by adding, updating and removing credit cards. | |
202 void SetCreditCards(std::vector<CreditCard>* credit_cards); | |
203 | |
204 // Loads the saved profiles from the web database. | |
205 virtual void LoadProfiles(); | |
206 | |
207 // Loads the auxiliary profiles. Currently Mac only. | |
208 virtual void LoadAuxiliaryProfiles(); | |
209 | |
210 // Loads the saved credit cards from the web database. | |
211 virtual void LoadCreditCards(); | |
212 | |
213 // Receives the loaded profiles from the web data service and stores them in | |
214 // |credit_cards_|. | |
215 void ReceiveLoadedProfiles(WebDataServiceBase::Handle h, | |
216 const WDTypedResult* result); | |
217 | |
218 // Receives the loaded credit cards from the web data service and stores them | |
219 // in |credit_cards_|. | |
220 void ReceiveLoadedCreditCards(WebDataServiceBase::Handle h, | |
221 const WDTypedResult* result); | |
222 | |
223 // Cancels a pending query to the web database. |handle| is a pointer to the | |
224 // query handle. | |
225 void CancelPendingQuery(WebDataServiceBase::Handle* handle); | |
226 | |
227 // The first time this is called, logs an UMA metrics for the number of | |
228 // profiles the user has. On subsequent calls, does nothing. | |
229 void LogProfileCount() const; | |
230 | |
231 // Returns the value of the AutofillEnabled pref. | |
232 virtual bool IsAutofillEnabled() const; | |
233 | |
234 // For tests. | |
235 const AutofillMetrics* metric_logger() const; | |
236 void set_metric_logger(const AutofillMetrics* metric_logger); | |
237 void set_browser_context(content::BrowserContext* context); | |
238 | |
239 // The browser context this PersonalDataManager is in. | |
240 content::BrowserContext* browser_context_; | |
241 | |
242 // True if personal data has been loaded from the web database. | |
243 bool is_data_loaded_; | |
244 | |
245 // The loaded web profiles. | |
246 ScopedVector<AutofillProfile> web_profiles_; | |
247 | |
248 // Auxiliary profiles. | |
249 mutable ScopedVector<AutofillProfile> auxiliary_profiles_; | |
250 | |
251 // Storage for combined web and auxiliary profiles. Contents are weak | |
252 // references. Lifetime managed by |web_profiles_| and |auxiliary_profiles_|. | |
253 mutable std::vector<AutofillProfile*> profiles_; | |
254 | |
255 // The loaded credit cards. | |
256 ScopedVector<CreditCard> credit_cards_; | |
257 | |
258 // When the manager makes a request from WebDataServiceBase, the database | |
259 // is queried on another thread, we record the query handle until we | |
260 // get called back. We store handles for both profile and credit card queries | |
261 // so they can be loaded at the same time. | |
262 WebDataServiceBase::Handle pending_profiles_query_; | |
263 WebDataServiceBase::Handle pending_creditcards_query_; | |
264 | |
265 // The observers. | |
266 ObserverList<PersonalDataManagerObserver> observers_; | |
267 | |
268 private: | |
269 | |
270 // For logging UMA metrics. Overridden by metrics tests. | |
271 scoped_ptr<const AutofillMetrics> metric_logger_; | |
272 | |
273 // Whether we have already logged the number of profiles this session. | |
274 mutable bool has_logged_profile_count_; | |
275 | |
276 // Manages registration lifetime for NotificationObserver implementation. | |
277 content::NotificationRegistrar notification_registrar_; | |
278 | |
279 DISALLOW_COPY_AND_ASSIGN(PersonalDataManager); | |
280 }; | |
281 | |
282 #endif // CHROME_BROWSER_AUTOFILL_PERSONAL_DATA_MANAGER_H_ | |
OLD | NEW |