Home | History | Annotate | Download | only in autofill
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <set>
     10 #include <vector>
     11 
     12 #ifdef ANDROID
     13 #include "base/base_api.h"
     14 #endif
     15 #include "base/memory/ref_counted.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/memory/scoped_vector.h"
     18 #include "base/observer_list.h"
     19 #include "base/string16.h"
     20 #include "chrome/browser/autofill/autofill_profile.h"
     21 #include "chrome/browser/autofill/credit_card.h"
     22 #include "chrome/browser/autofill/field_types.h"
     23 #include "chrome/browser/sync/profile_sync_service_observer.h"
     24 #include "chrome/browser/webdata/web_data_service.h"
     25 
     26 class AutofillManager;
     27 class AutofillMetrics;
     28 class FormStructure;
     29 class Profile;
     30 
     31 // Handles loading and saving Autofill profile information to the web database.
     32 // This class also stores the profiles loaded from the database for use during
     33 // Autofill.
     34 class
     35 #ifdef ANDROID
     36 BASE_API
     37 #endif
     38 PersonalDataManager
     39     : public WebDataServiceConsumer,
     40       public ProfileSyncServiceObserver,
     41       public base::RefCountedThreadSafe<PersonalDataManager> {
     42  public:
     43   // An interface the PersonalDataManager uses to notify its clients (observers)
     44   // when it has finished loading personal data from the web database.  Register
     45   // the observer via PersonalDataManager::SetObserver.
     46   class
     47 #ifdef ANDROID
     48 BASE_API
     49 #endif
     50   Observer {
     51    public:
     52     // Notifies the observer that the PersonalDataManager has finished loading.
     53     // TODO: OnPersonalDataLoaded should be nuked in favor of only
     54     // OnPersonalDataChanged.
     55     virtual void OnPersonalDataLoaded() = 0;
     56 
     57     // Notifies the observer that the PersonalDataManager changed in some way.
     58     virtual void OnPersonalDataChanged() {}
     59 
     60    protected:
     61     virtual ~Observer() {}
     62   };
     63 
     64   // WebDataServiceConsumer implementation:
     65   virtual void OnWebDataServiceRequestDone(WebDataService::Handle h,
     66                                            const WDTypedResult* result);
     67 
     68   // Sets the listener to be notified of PersonalDataManager events.
     69   virtual void SetObserver(PersonalDataManager::Observer* observer);
     70 
     71   // Removes |observer| as the observer of this PersonalDataManager.
     72   virtual void RemoveObserver(PersonalDataManager::Observer* observer);
     73 
     74   // ProfileSyncServiceObserver:
     75   virtual void OnStateChanged();
     76 
     77         // TODO(isherman): Update this comment
     78   // If Autofill is able to determine the field types of a significant number of
     79   // field types that contain information in the FormStructures a profile will
     80   // be created with all of the information from recognized fields. Returns
     81   // whether a profile was created.
     82   bool ImportFormData(const std::vector<const FormStructure*>& form_structures,
     83                       const CreditCard** credit_card);
     84 
     85   // Saves a credit card value detected in |ImportedFormData|.
     86   virtual void SaveImportedCreditCard(const CreditCard& imported_credit_card);
     87 
     88   // Sets |web_profiles_| to the contents of |profiles| and updates the web
     89   // database by adding, updating and removing profiles.
     90   //
     91   // The relationship between this and Refresh is subtle.
     92   // A call to |SetProfiles| could include out-of-date data that may conflict
     93   // if we didn't refresh-to-latest before an Autofill window was opened for
     94   // editing. |SetProfiles| is implemented to make a "best effort" to apply the
     95   // changes, but in extremely rare edge cases it is possible not all of the
     96   // updates in |profiles| make it to the DB.  This is why SetProfiles will
     97   // invoke Refresh after finishing, to ensure we get into a
     98   // consistent state.  See Refresh for details.
     99   void SetProfiles(std::vector<AutofillProfile>* profiles);
    100 
    101   // Sets |credit_cards_| to the contents of |credit_cards| and updates the web
    102   // database by adding, updating and removing credit cards.
    103   void SetCreditCards(std::vector<CreditCard>* credit_cards);
    104 
    105   // Adds |profile| to the web database.
    106   void AddProfile(const AutofillProfile& profile);
    107 
    108   // Updates |profile| which already exists in the web database.
    109   void UpdateProfile(const AutofillProfile& profile);
    110 
    111   // Removes the profile represented by |guid|.
    112   void RemoveProfile(const std::string& guid);
    113 
    114   // Returns the profile with the specified |guid|, or NULL if there is no
    115   // profile with the specified |guid|.
    116   AutofillProfile* GetProfileByGUID(const std::string& guid);
    117 
    118   // Adds |credit_card| to the web database.
    119   void AddCreditCard(const CreditCard& credit_card);
    120 
    121   // Updates |credit_card| which already exists in the web database.
    122   void UpdateCreditCard(const CreditCard& credit_card);
    123 
    124   // Removes the credit card represented by |guid|.
    125   void RemoveCreditCard(const std::string& guid);
    126 
    127   // Returns the credit card with the specified |guid|, or NULL if there is
    128   // no credit card with the specified |guid|.
    129   CreditCard* GetCreditCardByGUID(const std::string& guid);
    130 
    131   // Gets the possible field types for the given text, determined by matching
    132   // the text with all known personal information and returning matching types.
    133   void GetPossibleFieldTypes(const string16& text,
    134                              FieldTypeSet* possible_types);
    135 
    136   // Returns true if the credit card information is stored with a password.
    137   bool HasPassword();
    138 
    139   // Returns whether the personal data has been loaded from the web database.
    140   virtual bool IsDataLoaded() const;
    141 
    142   // This PersonalDataManager owns these profiles and credit cards.  Their
    143   // lifetime is until the web database is updated with new profile and credit
    144   // card information, respectively.  |profiles()| returns both web and
    145   // auxiliary profiles.  |web_profiles()| returns only web profiles.
    146   const std::vector<AutofillProfile*>& profiles();
    147   virtual const std::vector<AutofillProfile*>& web_profiles();
    148   virtual const std::vector<CreditCard*>& credit_cards();
    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   // Kicks off asynchronous loading of profiles and credit cards.
    161   void Init(Profile* profile);
    162 
    163   // Checks suitability of |profile| for adding to the user's set of profiles.
    164   static bool IsValidLearnableProfile(const AutofillProfile& profile);
    165 
    166   // Merges |profile| into one of the |existing_profiles| if possible; otherwise
    167   // appends |profile| to the end of that list. Fills |merged_profiles| with the
    168   // result.
    169   static bool MergeProfile(
    170       const AutofillProfile& profile,
    171       const std::vector<AutofillProfile*>& existing_profiles,
    172       std::vector<AutofillProfile>* merged_profiles);
    173 
    174  protected:
    175   // Make sure that only Profile and certain tests can create an instance of
    176   // PersonalDataManager.
    177   friend class base::RefCountedThreadSafe<PersonalDataManager>;
    178   friend class AutofillMergeTest;
    179   friend class PersonalDataManagerTest;
    180   friend class ProfileImpl;
    181   friend class ProfileSyncServiceAutofillTest;
    182 #ifdef ANDROID
    183   friend class ProfileImplAndroid;
    184 #endif
    185 
    186   PersonalDataManager();
    187   virtual ~PersonalDataManager();
    188 
    189   // Loads the saved profiles from the web database.
    190   virtual void LoadProfiles();
    191 
    192   // Loads the auxiliary profiles.  Currently Mac only.
    193   void LoadAuxiliaryProfiles();
    194 
    195   // Loads the saved credit cards from the web database.
    196   virtual void LoadCreditCards();
    197 
    198   // Receives the loaded profiles from the web data service and stores them in
    199   // |credit_cards_|.
    200   void ReceiveLoadedProfiles(WebDataService::Handle h,
    201                              const WDTypedResult* result);
    202 
    203   // Receives the loaded credit cards from the web data service and stores them
    204   // in |credit_cards_|.
    205   void ReceiveLoadedCreditCards(WebDataService::Handle h,
    206                                 const WDTypedResult* result);
    207 
    208   // Cancels a pending query to the web database.  |handle| is a pointer to the
    209   // query handle.
    210   void CancelPendingQuery(WebDataService::Handle* handle);
    211 
    212   // Saves |imported_profile| to the WebDB if it exists.
    213   virtual void SaveImportedProfile(const AutofillProfile& imported_profile);
    214 
    215   // Notifies Sync about data migration if necessary.
    216   void EmptyMigrationTrash();
    217 
    218   // The first time this is called, logs an UMA metrics for the number of
    219   // profiles the user has. On subsequent calls, does nothing.
    220   void LogProfileCount() const;
    221 
    222   // Returns the value of the AutofillEnabled pref.
    223   virtual bool IsAutofillEnabled() const;
    224 
    225   // For tests.
    226   const AutofillMetrics* metric_logger() const;
    227   void set_metric_logger(const AutofillMetrics* metric_logger);
    228 
    229   // The profile hosting this PersonalDataManager.
    230   Profile* profile_;
    231 
    232   // True if personal data has been loaded from the web database.
    233   bool is_data_loaded_;
    234 
    235   // The loaded web profiles.
    236   ScopedVector<AutofillProfile> web_profiles_;
    237 
    238   // Auxiliary profiles.
    239   ScopedVector<AutofillProfile> auxiliary_profiles_;
    240 
    241   // Storage for combined web and auxiliary profiles.  Contents are weak
    242   // references.  Lifetime managed by |web_profiles_| and |auxiliary_profiles_|.
    243   std::vector<AutofillProfile*> profiles_;
    244 
    245   // The loaded credit cards.
    246   ScopedVector<CreditCard> credit_cards_;
    247 
    248   // The hash of the password used to store the credit card.  This is empty if
    249   // no password exists.
    250   string16 password_hash_;
    251 
    252   // When the manager makes a request from WebDataService, the database
    253   // is queried on another thread, we record the query handle until we
    254   // get called back.  We store handles for both profile and credit card queries
    255   // so they can be loaded at the same time.
    256   WebDataService::Handle pending_profiles_query_;
    257   WebDataService::Handle pending_creditcards_query_;
    258 
    259   // The observers.
    260   ObserverList<Observer> observers_;
    261 
    262  private:
    263   // For logging UMA metrics. Overridden by metrics tests.
    264   scoped_ptr<const AutofillMetrics> metric_logger_;
    265 
    266   // Whether we have already logged the number of profiles this session.
    267   mutable bool has_logged_profile_count_;
    268 
    269   DISALLOW_COPY_AND_ASSIGN(PersonalDataManager);
    270 };
    271 
    272 #endif  // CHROME_BROWSER_AUTOFILL_PERSONAL_DATA_MANAGER_H_
    273