1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_PERSONAL_DATA_MANAGER_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_PERSONAL_DATA_MANAGER_H_
#include <list>
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/strings/string16.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/suggestion.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_member.h"
#include "components/webdata/common/web_data_service_consumer.h"
#if defined(OS_ANDROID)
#include "net/url_request/url_request_context_getter.h"
#endif
class AccountTrackerService;
class Browser;
class PrefService;
class RemoveAutofillTester;
class SigninManagerBase;
namespace autofill {
class AutofillInteractiveTest;
class AutofillTest;
class FormStructure;
class PersonalDataManagerObserver;
class PersonalDataManagerFactory;
} // namespace autofill
namespace autofill_helper {
void SetProfiles(int, std::vector<autofill::AutofillProfile>*);
void SetCreditCards(int, std::vector<autofill::CreditCard>*);
} // namespace autofill_helper
namespace syncer {
class SyncService;
} // namespace syncer
namespace autofill {
extern const char kFrecencyFieldTrialName[];
extern const char kFrecencyFieldTrialStateEnabled[];
extern const char kFrecencyFieldTrialLimitParam[];
// Handles loading and saving Autofill profile information to the web database.
// This class also stores the profiles loaded from the database for use during
// Autofill.
class PersonalDataManager : public KeyedService,
public WebDataServiceConsumer,
public AutofillWebDataServiceObserverOnUIThread {
public:
explicit PersonalDataManager(const std::string& app_locale);
~PersonalDataManager() override;
// Kicks off asynchronous loading of profiles and credit cards.
// |pref_service| must outlive this instance. |is_off_the_record| informs
// this instance whether the user is currently operating in an off-the-record
// context.
void Init(scoped_refptr<AutofillWebDataService> database,
PrefService* pref_service,
AccountTrackerService* account_tracker,
SigninManagerBase* signin_manager,
bool is_off_the_record);
// Called once the sync service is known to be instantiated. Note that it may
// not be started, but it's preferences can be queried.
void OnSyncServiceInitialized(syncer::SyncService* sync_service);
// WebDataServiceConsumer:
void OnWebDataServiceRequestDone(
WebDataServiceBase::Handle h,
std::unique_ptr<WDTypedResult> result) override;
// AutofillWebDataServiceObserverOnUIThread:
void AutofillMultipleChanged() override;
void SyncStarted(syncer::ModelType model_type) override;
// Adds a listener to be notified of PersonalDataManager events.
virtual void AddObserver(PersonalDataManagerObserver* observer);
// Removes |observer| as an observer of this PersonalDataManager.
virtual void RemoveObserver(PersonalDataManagerObserver* observer);
// Scans the given |form| for importable Autofill data. If the form includes
// sufficient address data for a new profile, it is immediately imported. If
// the form includes sufficient credit card data for a new credit card, it is
// stored into |imported_credit_card| so that we can prompt the user whether
// to save this data. If the form contains credit card data already present in
// a local credit card entry *and* |should_return_local_card| is true, the
// data is stored into |imported_credit_card| so that we can prompt the user
// whether to upload it.
// Returns |true| if sufficient address or credit card data was found.
bool ImportFormData(const FormStructure& form,
bool should_return_local_card,
std::unique_ptr<CreditCard>* imported_credit_card);
// Called to indicate |data_model| was used (to fill in a form). Updates
// the database accordingly. Can invalidate |data_model|, particularly if
// it's a Mac address book entry.
virtual void RecordUseOf(const AutofillDataModel& data_model);
// Saves |imported_profile| to the WebDB if it exists. Returns the guid of
// the new or updated profile, or the empty string if no profile was saved.
virtual std::string SaveImportedProfile(
const AutofillProfile& imported_profile);
// Saves |imported_credit_card| to the WebDB if it exists. Returns the guid of
// of the new or updated card, or the empty string if no card was saved.
virtual std::string SaveImportedCreditCard(
const CreditCard& imported_credit_card);
// Adds |profile| to the web database.
void AddProfile(const AutofillProfile& profile);
// Updates |profile| which already exists in the web database.
void UpdateProfile(const AutofillProfile& profile);
// Removes the profile or credit card represented by |guid|.
virtual void RemoveByGUID(const std::string& guid);
// Returns the profile with the specified |guid|, or NULL if there is no
// profile with the specified |guid|. Both web and auxiliary profiles may
// be returned.
AutofillProfile* GetProfileByGUID(const std::string& guid);
// Adds |credit_card| to the web database.
void AddCreditCard(const CreditCard& credit_card);
// Updates |credit_card| which already exists in the web database. This
// can only be used on local credit cards.
virtual void UpdateCreditCard(const CreditCard& credit_card);
// Update a server card. Only the full number and masked/unmasked
// status can be changed. Looks up the card by server ID.
virtual void UpdateServerCreditCard(const CreditCard& credit_card);
// Updates the use stats and billing address id for the server |credit_card|.
// Looks up the card by server_id.
void UpdateServerCardMetadata(const CreditCard& credit_card);
// Resets the card for |guid| to the masked state.
void ResetFullServerCard(const std::string& guid);
// Resets all unmasked cards to the masked state.
void ResetFullServerCards();
// Deletes all server profiles and cards (both masked and unmasked).
void ClearAllServerData();
// Sets a server credit card for test.
void AddServerCreditCardForTest(std::unique_ptr<CreditCard> credit_card);
// Returns the credit card with the specified |guid|, or NULL if there is
// no credit card with the specified |guid|.
virtual CreditCard* GetCreditCardByGUID(const std::string& guid);
// Gets the field types availabe in the stored address and credit card data.
void GetNonEmptyTypes(ServerFieldTypeSet* non_empty_types);
// Returns true if the credit card information is stored with a password.
bool HasPassword();
// Returns whether the personal data has been loaded from the web database.
virtual bool IsDataLoaded() const;
// This PersonalDataManager owns these profiles and credit cards. Their
// lifetime is until the web database is updated with new profile and credit
// card information, respectively. |GetProfiles()| returns both web and
// auxiliary profiles. |web_profiles()| returns only web profiles.
virtual const std::vector<AutofillProfile*>& GetProfiles() const;
virtual std::vector<AutofillProfile*> web_profiles() const;
// Returns just LOCAL_CARD cards.
virtual std::vector<CreditCard*> GetLocalCreditCards() const;
// Returns all credit cards, server and local.
virtual const std::vector<CreditCard*>& GetCreditCards() const;
// Returns true if there is some data synced from Wallet.
bool HasServerData() const;
// Returns the profiles to suggest to the user, ordered by frecency.
const std::vector<AutofillProfile*> GetProfilesToSuggest() const;
// Loads profiles that can suggest data for |type|. |field_contents| is the
// part the user has already typed. |field_is_autofilled| is true if the field
// has already been autofilled. |other_field_types| represents the rest of
// form.
std::vector<Suggestion> GetProfileSuggestions(
const AutofillType& type,
const base::string16& field_contents,
bool field_is_autofilled,
const std::vector<ServerFieldType>& other_field_types);
// Returns the credit cards to suggest to the user. Those have been deduped
// and ordered by frecency with the expired cards put at the end of the
// vector.
const std::vector<CreditCard*> GetCreditCardsToSuggest() const;
// Gets credit cards that can suggest data for |type|. See
// GetProfileSuggestions for argument descriptions. The variant in each
// GUID pair should be ignored.
std::vector<Suggestion> GetCreditCardSuggestions(
const AutofillType& type,
const base::string16& field_contents);
// Re-loads profiles and credit cards from the WebDatabase asynchronously.
// In the general case, this is a no-op and will re-create the same
// in-memory model as existed prior to the call. If any change occurred to
// profiles in the WebDatabase directly, as is the case if the browser sync
// engine processed a change from the cloud, we will learn of these as a
// result of this call.
//
// Also see SetProfile for more details.
virtual void Refresh();
const std::string& app_locale() const { return app_locale_; }
// Checks suitability of |profile| for adding to the user's set of profiles.
static bool IsValidLearnableProfile(const AutofillProfile& profile,
const std::string& app_locale);
// Merges |new_profile| into one of the |existing_profiles| if possible;
// otherwise appends |new_profile| to the end of that list. Fills
// |merged_profiles| with the result. Returns the |guid| of the new or updated
// profile.
std::string MergeProfile(
const AutofillProfile& new_profile,
std::vector<std::unique_ptr<AutofillProfile>>* existing_profiles,
const std::string& app_locale,
std::vector<AutofillProfile>* merged_profiles);
// Returns true if |country_code| is a country that the user is likely to
// be associated with the user. More concretely, it checks if there are any
// addresses with this country or if the user's system timezone is in the
// given country.
virtual bool IsCountryOfInterest(const std::string& country_code) const;
// Returns our best guess for the country a user is likely to use when
// inputting a new address. The value is calculated once and cached, so it
// will only update when Chrome is restarted.
virtual const std::string& GetDefaultCountryCodeForNewAddress() const;
// De-dupe credit card to suggest. Full server cards are preferred over their
// local duplicates, and local cards are preferred over their masked server
// card duplicate.
static void DedupeCreditCardToSuggest(
std::list<CreditCard*>* cards_to_suggest);
// Notifies test observers that personal data has changed.
void NotifyPersonalDataChangedForTest() {
NotifyPersonalDataChanged();
}
#if defined(OS_ANDROID)
// Sets the URL request context getter to be used when normalizing addresses
// with libaddressinput's address validator.
void SetURLRequestContextGetter(
net::URLRequestContextGetter* context_getter) {
context_getter_ = context_getter;
}
// Returns the class used to fetch the address validation rules.
net::URLRequestContextGetter* GetURLRequestContextGetter() const {
return context_getter_.get();
}
#endif
protected:
// Only PersonalDataManagerFactory and certain tests can create instances of
// PersonalDataManager.
FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, FirstMiddleLast);
FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AutofillIsEnabledAtStartup);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
DedupeProfiles_ProfilesToDelete);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
DedupeProfiles_GuidsMergeMap);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
UpdateCardsBillingAddressReference);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest, ApplyProfileUseDatesFix);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyProfileUseDatesFix_NotAppliedTwice);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_CardsBillingAddressIdUpdated);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_MergedProfileValues);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_VerifiedProfileFirst);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_VerifiedProfileLast);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_MultipleVerifiedProfiles);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_FeatureDisabled);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_NopIfZeroProfiles);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_NopIfOneProfile);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_OncePerVersion);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
ApplyDedupingRoutine_MultipleDedupes);
friend class autofill::AutofillInteractiveTest;
friend class autofill::AutofillTest;
friend class autofill::PersonalDataManagerFactory;
friend class PersonalDataManagerTest;
friend class ProfileSyncServiceAutofillTest;
friend class ::RemoveAutofillTester;
friend std::default_delete<PersonalDataManager>;
friend void autofill_helper::SetProfiles(
int, std::vector<autofill::AutofillProfile>*);
friend void autofill_helper::SetCreditCards(
int, std::vector<autofill::CreditCard>*);
friend void SetTestProfiles(
Browser* browser, std::vector<AutofillProfile>* profiles);
// Sets |web_profiles_| to the contents of |profiles| and updates the web
// database by adding, updating and removing profiles.
// The relationship between this and Refresh is subtle.
// A call to |SetProfiles| could include out-of-date data that may conflict
// if we didn't refresh-to-latest before an Autofill window was opened for
// editing. |SetProfiles| is implemented to make a "best effort" to apply the
// changes, but in extremely rare edge cases it is possible not all of the
// updates in |profiles| make it to the DB. This is why SetProfiles will
// invoke Refresh after finishing, to ensure we get into a
// consistent state. See Refresh for details.
virtual void SetProfiles(std::vector<AutofillProfile>* profiles);
// Sets |credit_cards_| to the contents of |credit_cards| and updates the web
// database by adding, updating and removing credit cards.
void SetCreditCards(std::vector<CreditCard>* credit_cards);
// Loads the saved profiles from the web database.
virtual void LoadProfiles();
// Loads the saved credit cards from the web database.
virtual void LoadCreditCards();
// Cancels a pending query to the web database. |handle| is a pointer to the
// query handle.
void CancelPendingQuery(WebDataServiceBase::Handle* handle);
// Notifies observers that personal data has changed.
void NotifyPersonalDataChanged();
// The first time this is called, logs an UMA metric for the number of
// profiles the user has. On subsequent calls, does nothing.
void LogProfileCount() const;
// The first time this is called, logs an UMA metric for the number of local
// credit cards the user has. On subsequent calls, does nothing.
void LogLocalCreditCardCount() const;
// The first time this is called, logs an UMA metric for the number of server
// credit cards the user has (both masked and unmasked). On subsequent calls,
// does nothing.
void LogServerCreditCardCounts() const;
// Returns the value of the AutofillEnabled pref.
virtual bool IsAutofillEnabled() const;
// Overrideable for testing.
virtual std::string CountryCodeForCurrentTimezone() const;
// Sets which PrefService to use and observe. |pref_service| is not owned by
// this class and must outlive |this|.
void SetPrefService(PrefService* pref_service);
void set_database(scoped_refptr<AutofillWebDataService> database) {
database_ = database;
}
void set_account_tracker(AccountTrackerService* account_tracker) {
account_tracker_ = account_tracker;
}
void set_signin_manager(SigninManagerBase* signin_manager) {
signin_manager_ = signin_manager;
}
// The backing database that this PersonalDataManager uses.
scoped_refptr<AutofillWebDataService> database_;
// True if personal data has been loaded from the web database.
bool is_data_loaded_;
// The loaded web profiles. These are constructed from entries on web pages
// and from manually editing in the settings.
std::vector<std::unique_ptr<AutofillProfile>> web_profiles_;
// Profiles read from the user's account stored on the server.
mutable std::vector<std::unique_ptr<AutofillProfile>> server_profiles_;
// Storage for web profiles. Contents are weak references. Lifetime managed
// by |web_profiles_|.
mutable std::vector<AutofillProfile*> profiles_;
// Cached versions of the local and server credit cards.
std::vector<std::unique_ptr<CreditCard>> local_credit_cards_;
std::vector<std::unique_ptr<CreditCard>> server_credit_cards_;
// A combination of local and server credit cards. The pointers are owned
// by the local/sverver_credit_cards_ vectors.
mutable std::vector<CreditCard*> credit_cards_;
// When the manager makes a request from WebDataServiceBase, the database
// is queried on another thread, we record the query handle until we
// get called back. We store handles for both profile and credit card queries
// so they can be loaded at the same time.
WebDataServiceBase::Handle pending_profiles_query_;
WebDataServiceBase::Handle pending_server_profiles_query_;
WebDataServiceBase::Handle pending_creditcards_query_;
WebDataServiceBase::Handle pending_server_creditcards_query_;
// The observers.
base::ObserverList<PersonalDataManagerObserver> observers_;
private:
// Finds the country code that occurs most frequently among all profiles.
// Prefers verified profiles over unverified ones.
std::string MostCommonCountryCodeFromProfiles() const;
// Called when the value of prefs::kAutofillEnabled changes.
void EnabledPrefChanged();
// Go through the |form| fields and attempt to extract and import valid
// address profiles. Returns true on extraction success of at least one
// profile. There are many reasons that extraction may fail (see
// implementation).
bool ImportAddressProfiles(const FormStructure& form);
// Helper method for ImportAddressProfiles which only considers the fields for
// a specified |section|.
bool ImportAddressProfileForSection(const FormStructure& form,
const std::string& section);
// Go through the |form| fields and attempt to extract a new credit card in
// |imported_credit_card|, or update an existing card.
// |should_return_local_card| will indicate whether |imported_credit_card| is
// filled even if an existing card was updated. Success is defined as having a
// new card to import, or having merged with an existing card.
bool ImportCreditCard(const FormStructure& form,
bool should_return_local_card,
std::unique_ptr<CreditCard>* imported_credit_card);
// Functionally equivalent to GetProfiles(), but also records metrics if
// |record_metrics| is true. Metrics should be recorded when the returned
// profiles will be used to populate the fields shown in an Autofill popup.
const std::vector<AutofillProfile*>& GetProfiles(
bool record_metrics) const;
// Returns credit card suggestions based on the |cards_to_suggest| and the
// |type| and |field_contents| of the credit card field.
std::vector<Suggestion> GetSuggestionsForCards(
const AutofillType& type,
const base::string16& field_contents,
const std::vector<CreditCard*>& cards_to_suggest) const;
// Runs the Autofill use date fix routine if it's never been done. Returns
// whether the routine was run.
void ApplyProfileUseDatesFix();
// Applies the deduping routine once per major version if the feature is
// enabled. Calls DedupeProfiles with the content of |web_profiles_| as a
// parameter. Removes the profiles to delete from the database and updates the
// others. Also updates the credit cards' billing address references. Returns
// true if the routine was run.
bool ApplyDedupingRoutine();
// Goes through all the |existing_profiles| and merges all similar unverified
// profiles together. Also discards unverified profiles that are similar to a
// verified profile. All the profiles except the results of the merges will be
// added to |profile_guids_to_delete|. This routine should be run once per
// major version. Records all the merges into the |guids_merge_map|.
//
// This method should only be called by ApplyDedupingRoutine. It is split for
// testing purposes.
void DedupeProfiles(
std::vector<std::unique_ptr<AutofillProfile>>* existing_profiles,
std::unordered_set<AutofillProfile*>* profile_guids_to_delete,
std::unordered_map<std::string, std::string>* guids_merge_map);
// Updates the credit cards' billing address reference based on the merges
// that happened during the dedupe, as defined in |guids_merge_map|. Also
// updates the cards entries in the database.
void UpdateCardsBillingAddressReference(
const std::unordered_map<std::string, std::string>& guids_merge_map);
const std::string app_locale_;
// The default country code for new addresses.
mutable std::string default_country_code_;
// The PrefService that this instance uses. Must outlive this instance.
PrefService* pref_service_;
// The AccountTrackerService that this instance uses. Must outlive this
// instance.
AccountTrackerService* account_tracker_;
// The signin manager that this instance uses. Must outlive this instance.
SigninManagerBase* signin_manager_;
// Whether the user is currently operating in an off-the-record context.
// Default value is false.
bool is_off_the_record_;
// Whether we have already logged the number of profiles this session.
mutable bool has_logged_profile_count_;
// Whether we have already logged the number of local credit cards this
// session.
mutable bool has_logged_local_credit_card_count_;
// Whether we have already logged the number of server credit cards this
// session.
mutable bool has_logged_server_credit_card_counts_;
// An observer to listen for changes to prefs::kAutofillEnabled.
std::unique_ptr<BooleanPrefMember> enabled_pref_;
// An observer to listen for changes to prefs::kAutofillWalletImportEnabled.
std::unique_ptr<BooleanPrefMember> wallet_enabled_pref_;
// True if autofill profile cleanup needs to be performed.
bool is_autofill_profile_cleanup_pending_ = false;
#if defined(OS_ANDROID)
// The context for the request to be used to fetch libaddressinput's address
// validation rules.
scoped_refptr<net::URLRequestContextGetter> context_getter_;
#endif
DISALLOW_COPY_AND_ASSIGN(PersonalDataManager);
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_PERSONAL_DATA_MANAGER_H_
|