personal_data_manager.h revision 116680a4aac90f2aa7413d9095a592090648e557
1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_PERSONAL_DATA_MANAGER_H_
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#define COMPONENTS_AUTOFILL_CORE_BROWSER_PERSONAL_DATA_MANAGER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_vector.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/observer_list.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/prefs/pref_member.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/autofill/core/browser/autofill_metrics.h"
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_profile.h"
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/credit_card.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/field_types.h"
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h"
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "components/webdata/common/web_data_service_consumer.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class PrefService;
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class RemoveAutofillTester;
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace autofill {
30ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochclass AutofillInteractiveTest;
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class AutofillTest;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FormStructure;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PersonalDataManagerObserver;
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class PersonalDataManagerFactory;
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace autofill
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace autofill_helper {
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void SetProfiles(int, std::vector<autofill::AutofillProfile>*);
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void SetCreditCards(int, std::vector<autofill::CreditCard>*);
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace autofill_helper
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace autofill {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Handles loading and saving Autofill profile information to the web database.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class also stores the profiles loaded from the database for use during
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Autofill.
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class PersonalDataManager : public KeyedService,
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                            public WebDataServiceConsumer,
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            public AutofillWebDataServiceObserverOnUIThread {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A pair of GUID and variant index. Represents a single FormGroup and a
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // specific data variant.
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::pair<std::string, size_t> GUIDPair;
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  explicit PersonalDataManager(const std::string& app_locale);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~PersonalDataManager();
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Kicks off asynchronous loading of profiles and credit cards.
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |pref_service| must outlive this instance.  |is_off_the_record| informs
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // this instance whether the user is currently operating in an off-the-record
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // context.
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Init(scoped_refptr<AutofillWebDataService> database,
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            PrefService* pref_service,
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            bool is_off_the_record);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // WebDataServiceConsumer:
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnWebDataServiceRequestDone(
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WebDataServiceBase::Handle h,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const WDTypedResult* result) OVERRIDE;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // AutofillWebDataServiceObserverOnUIThread:
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void AutofillMultipleChanged() OVERRIDE;
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Adds a listener to be notified of PersonalDataManager events.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void AddObserver(PersonalDataManagerObserver* observer);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes |observer| as an observer of this PersonalDataManager.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RemoveObserver(PersonalDataManagerObserver* observer);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Scans the given |form| for importable Autofill data. If the form includes
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sufficient address data, it is immediately imported. If the form includes
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sufficient credit card data, it is stored into |credit_card|, so that we
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // can prompt the user whether to save this data.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns |true| if sufficient address or credit card data was found.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ImportFormData(const FormStructure& form,
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                      scoped_ptr<CreditCard>* credit_card);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Saves |imported_profile| to the WebDB if it exists. Returns the guid of
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // the new or updated profile, or the empty string if no profile was saved.
90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual std::string SaveImportedProfile(
91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const AutofillProfile& imported_profile);
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Saves a credit card value detected in |ImportedFormData|. Returns the guid
94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // of the new or updated card, or the empty string if no card was saved.
95d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  virtual std::string SaveImportedCreditCard(
96d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const CreditCard& imported_credit_card);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds |profile| to the web database.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddProfile(const AutofillProfile& profile);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates |profile| which already exists in the web database.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateProfile(const AutofillProfile& profile);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes the profile or credit card represented by |guid|.
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void RemoveByGUID(const std::string& guid);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the profile with the specified |guid|, or NULL if there is no
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // profile with the specified |guid|. Both web and auxiliary profiles may
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // be returned.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutofillProfile* GetProfileByGUID(const std::string& guid);
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds |credit_card| to the web database.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddCreditCard(const CreditCard& credit_card);
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates |credit_card| which already exists in the web database.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateCreditCard(const CreditCard& credit_card);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the credit card with the specified |guid|, or NULL if there is
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // no credit card with the specified |guid|.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CreditCard* GetCreditCardByGUID(const std::string& guid);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the field types availabe in the stored address and credit card data.
1233240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void GetNonEmptyTypes(ServerFieldTypeSet* non_empty_types);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the credit card information is stored with a password.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasPassword();
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns whether the personal data has been loaded from the web database.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsDataLoaded() const;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This PersonalDataManager owns these profiles and credit cards.  Their
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // lifetime is until the web database is updated with new profile and credit
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // card information, respectively.  |GetProfiles()| returns both web and
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // auxiliary profiles.  |web_profiles()| returns only web profiles.
1350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual const std::vector<AutofillProfile*>& GetProfiles() const;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::vector<AutofillProfile*>& web_profiles() const;
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual const std::vector<CreditCard*>& GetCreditCards() const;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Loads profiles that can suggest data for |type|. |field_contents| is the
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // part the user has already typed. |field_is_autofilled| is true if the field
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // has already been autofilled. |other_field_types| represents the rest of
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // form. |filter| is run on each potential suggestion. If |filter| returns
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // true, the profile added to the last four outparams (else it's omitted).
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void GetProfileSuggestions(
1453240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch      const AutofillType& type,
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const base::string16& field_contents,
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool field_is_autofilled,
148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const std::vector<ServerFieldType>& other_field_types,
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const base::Callback<bool(const AutofillProfile&)>& filter,
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::vector<base::string16>* values,
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::vector<base::string16>* labels,
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::vector<base::string16>* icons,
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<GUIDPair>* guid_pairs);
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Gets credit cards that can suggest data for |type|. See
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // GetProfileSuggestions for argument descriptions. The variant in each
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // GUID pair should be ignored.
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void GetCreditCardSuggestions(
1593240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch      const AutofillType& type,
160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const base::string16& field_contents,
161c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::vector<base::string16>* values,
162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::vector<base::string16>* labels,
163c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::vector<base::string16>* icons,
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<GUIDPair>* guid_pairs);
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Re-loads profiles and credit cards from the WebDatabase asynchronously.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In the general case, this is a no-op and will re-create the same
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in-memory model as existed prior to the call.  If any change occurred to
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // profiles in the WebDatabase directly, as is the case if the browser sync
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // engine processed a change from the cloud, we will learn of these as a
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // result of this call.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Also see SetProfile for more details.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Refresh();
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::string& app_locale() const { return app_locale_; }
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Checks suitability of |profile| for adding to the user's set of profiles.
179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static bool IsValidLearnableProfile(const AutofillProfile& profile,
180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                      const std::string& app_locale);
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Merges |new_profile| into one of the |existing_profiles| if possible;
1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // otherwise appends |new_profile| to the end of that list. Fills
184d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |merged_profiles| with the result. Returns the |guid| of the new or updated
185d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // profile.
186d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  static std::string MergeProfile(
1877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const AutofillProfile& new_profile,
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<AutofillProfile*>& existing_profiles,
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::string& app_locale,
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<AutofillProfile>* merged_profiles);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns true if |country_code| is a country that the user is likely to
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // be associated with the user. More concretely, it checks if there are any
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // addresses with this country or if the user's system timezone is in the
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // given country.
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsCountryOfInterest(const std::string& country_code) const;
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Returns our best guess for the country a user is likely to use when
1990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // inputting a new address. The value is calculated once and cached, so it
2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // will only update when Chrome is restarted.
201a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual const std::string& GetDefaultCountryCodeForNewAddress() const;
2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
20346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if defined(OS_MACOSX) && !defined(OS_IOS)
20446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // If Chrome has not prompted for access to the user's address book, the
20546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // method prompts the user for permission and blocks the process. Otherwise,
20646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // this method has no effect. The return value reflects whether the user was
20746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // prompted with a modal dialog.
20846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool AccessAddressBook();
20946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
21046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Whether an autofill suggestion should be displayed to prompt the user to
21146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // grant Chrome access to the user's address book.
21246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool ShouldShowAccessAddressBookSuggestion(AutofillType type);
2136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
2146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // The Chrome binary is in the process of being changed, or has been changed.
2156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Future attempts to access the Address Book might incorrectly present a
2166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // blocking dialog.
2176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void BinaryChanging();
21846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
21946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Only PersonalDataManagerFactory and certain tests can create instances of
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PersonalDataManager.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, FirstMiddleLast);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AutofillIsEnabledAtStartup);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           AggregateExistingAuxiliaryProfile);
227ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  friend class autofill::AutofillInteractiveTest;
228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend class autofill::AutofillTest;
229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend class autofill::PersonalDataManagerFactory;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class PersonalDataManagerTest;
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class ProfileSyncServiceAutofillTest;
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend class ::RemoveAutofillTester;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TestingAutomationProvider;
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend struct base::DefaultDeleter<PersonalDataManager>;
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend void autofill_helper::SetProfiles(
236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int, std::vector<autofill::AutofillProfile>*);
237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend void autofill_helper::SetCreditCards(
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      int, std::vector<autofill::CreditCard>*);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets |web_profiles_| to the contents of |profiles| and updates the web
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // database by adding, updating and removing profiles.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The relationship between this and Refresh is subtle.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A call to |SetProfiles| could include out-of-date data that may conflict
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if we didn't refresh-to-latest before an Autofill window was opened for
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // editing. |SetProfiles| is implemented to make a "best effort" to apply the
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // changes, but in extremely rare edge cases it is possible not all of the
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // updates in |profiles| make it to the DB.  This is why SetProfiles will
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // invoke Refresh after finishing, to ensure we get into a
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // consistent state.  See Refresh for details.
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetProfiles(std::vector<AutofillProfile>* profiles);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets |credit_cards_| to the contents of |credit_cards| and updates the web
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // database by adding, updating and removing credit cards.
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetCreditCards(std::vector<CreditCard>* credit_cards);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loads the saved profiles from the web database.
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void LoadProfiles();
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Loads the auxiliary profiles.  Currently Mac and Android only.
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void LoadAuxiliaryProfiles(bool record_metrics) const;
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Loads the saved credit cards from the web database.
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void LoadCreditCards();
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Receives the loaded profiles from the web data service and stores them in
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |credit_cards_|.
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ReceiveLoadedProfiles(WebDataServiceBase::Handle h,
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const WDTypedResult* result);
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Receives the loaded credit cards from the web data service and stores them
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in |credit_cards_|.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ReceiveLoadedCreditCards(WebDataServiceBase::Handle h,
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const WDTypedResult* result);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Cancels a pending query to the web database.  |handle| is a pointer to the
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // query handle.
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CancelPendingQuery(WebDataServiceBase::Handle* handle);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
279a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Notifies observers that personal data has changed.
280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void NotifyPersonalDataChanged();
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The first time this is called, logs an UMA metrics for the number of
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // profiles the user has. On subsequent calls, does nothing.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void LogProfileCount() const;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the value of the AutofillEnabled pref.
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsAutofillEnabled() const;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Overrideable for testing.
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual std::string CountryCodeForCurrentTimezone() const;
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Sets which PrefService to use and observe. |pref_service| is not owned by
293a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // this class and must outlive |this|.
294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void SetPrefService(PrefService* pref_service);
295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For tests.
297f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const AutofillMetrics* metric_logger() const { return metric_logger_.get(); }
298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void set_database(scoped_refptr<AutofillWebDataService> database) {
300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    database_ = database;
301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
302f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
303f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void set_metric_logger(const AutofillMetrics* metric_logger) {
304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    metric_logger_.reset(metric_logger);
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
307f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The backing database that this PersonalDataManager uses.
308f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<AutofillWebDataService> database_;
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if personal data has been loaded from the web database.
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_data_loaded_;
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The loaded web profiles.
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ScopedVector<AutofillProfile> web_profiles_;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Auxiliary profiles.
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable ScopedVector<AutofillProfile> auxiliary_profiles_;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Storage for combined web and auxiliary profiles.  Contents are weak
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // references.  Lifetime managed by |web_profiles_| and |auxiliary_profiles_|.
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable std::vector<AutofillProfile*> profiles_;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The loaded credit cards.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ScopedVector<CreditCard> credit_cards_;
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When the manager makes a request from WebDataServiceBase, the database
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is queried on another thread, we record the query handle until we
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // get called back.  We store handles for both profile and credit card queries
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so they can be loaded at the same time.
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebDataServiceBase::Handle pending_profiles_query_;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebDataServiceBase::Handle pending_creditcards_query_;
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The observers.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObserverList<PersonalDataManagerObserver> observers_;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Finds the country code that occurs most frequently among all profiles.
3380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Prefers verified profiles over unverified ones.
3390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  std::string MostCommonCountryCodeFromProfiles() const;
3400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
341a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Called when the value of prefs::kAutofillEnabled changes.
342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void EnabledPrefChanged();
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
344116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Functionally equivalent to GetProfiles(), but also records metrics if
345116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |record_metrics| is true. Metrics should be recorded when the returned
346116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // profiles will be used to populate the fields shown in an Autofill popup.
347116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const std::vector<AutofillProfile*>& GetProfiles(
348116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bool record_metrics) const;
349116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const std::string app_locale_;
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The default country code for new addresses.
3530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  mutable std::string default_country_code_;
3540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For logging UMA metrics. Overridden by metrics tests.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<const AutofillMetrics> metric_logger_;
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The PrefService that this instance uses. Must outlive this instance.
3590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  PrefService* pref_service_;
3600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
361f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Whether the user is currently operating in an off-the-record context.
362f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Default value is false.
363f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool is_off_the_record_;
364f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether we have already logged the number of profiles this session.
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable bool has_logged_profile_count_;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // An observer to listen for changes to prefs::kAutofillEnabled.
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<BooleanPrefMember> enabled_pref_;
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PersonalDataManager);
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
374c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace autofill
375c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
376eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_PERSONAL_DATA_MANAGER_H_
377