Home | History | Annotate | Download | only in webdata
      1 // Copyright 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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
      6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/memory/ref_counted_delete_on_message_loop.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "base/supports_user_data.h"
     13 #include "components/autofill/core/browser/webdata/autofill_webdata.h"
     14 #include "components/autofill/core/browser/webdata/autofill_webdata_backend.h"
     15 #include "components/autofill/core/common/form_field_data.h"
     16 #include "components/webdata/common/web_data_results.h"
     17 #include "components/webdata/common/web_data_service_base.h"
     18 #include "components/webdata/common/web_data_service_consumer.h"
     19 #include "components/webdata/common/web_database.h"
     20 
     21 namespace base {
     22 class MessageLoopProxy;
     23 }
     24 
     25 class WebDataServiceBackend;
     26 
     27 namespace autofill {
     28 
     29 class AutofillChange;
     30 class AutofillProfile;
     31 class AutofillWebDataServiceObserverOnDBThread;
     32 class CreditCard;
     33 
     34 // Backend implentation for the AutofillWebDataService. This class runs on the
     35 // DB thread, as it handles reads and writes to the WebDatabase, and functions
     36 // in it should only be called from that thread. Most functions here are just
     37 // the implementations of the corresponding functions in the Autofill
     38 // WebDataService.
     39 // This class is destroyed on the DB thread.
     40 class AutofillWebDataBackendImpl
     41     : public base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>,
     42       public AutofillWebDataBackend {
     43  public:
     44   // |web_database_backend| is used to access the WebDatabase directly for
     45   // Sync-related operations. |ui_thread| and |db_thread| are the threads that
     46   // this class uses as its UI and DB threads respectively.
     47   // |on_changed_callback| is a closure which can be used to notify the UI
     48   // thread of changes initiated by Sync (this callback may be called multiple
     49   // times).
     50   AutofillWebDataBackendImpl(
     51       scoped_refptr<WebDataServiceBackend> web_database_backend,
     52       scoped_refptr<base::MessageLoopProxy> ui_thread,
     53       scoped_refptr<base::MessageLoopProxy> db_thread,
     54       const base::Closure& on_changed_callback);
     55 
     56   // AutofillWebDataBackend implementation.
     57   virtual void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer)
     58       OVERRIDE;
     59   virtual void RemoveObserver(
     60       AutofillWebDataServiceObserverOnDBThread* observer) OVERRIDE;
     61   virtual WebDatabase* GetDatabase() OVERRIDE;
     62   virtual void RemoveExpiredFormElements() OVERRIDE;
     63   virtual void NotifyOfMultipleAutofillChanges() OVERRIDE;
     64 
     65   // Returns a SupportsUserData objects that may be used to store data
     66   // owned by the DB thread on this object. Should be called only from
     67   // the DB thread, and will be destroyed on the DB thread soon after
     68   // |ShutdownOnUIThread()| is called.
     69   base::SupportsUserData* GetDBUserData();
     70 
     71   void ResetUserData();
     72 
     73   // Adds form fields to the web database.
     74   WebDatabase::State AddFormElements(const std::vector<FormFieldData>& fields,
     75                                      WebDatabase* db);
     76 
     77   // Returns a vector of values which have been entered in form input fields
     78   // named |name|.
     79   scoped_ptr<WDTypedResult> GetFormValuesForElementName(
     80       const base::string16& name,
     81       const base::string16& prefix,
     82       int limit,
     83       WebDatabase* db);
     84 
     85   // Returns true if there are any elements in the form.
     86   scoped_ptr<WDTypedResult> HasFormElements(WebDatabase* db);
     87 
     88   // Removes form elements recorded for Autocomplete from the database.
     89   WebDatabase::State RemoveFormElementsAddedBetween(
     90       const base::Time& delete_begin,
     91       const base::Time& delete_end,
     92       WebDatabase* db);
     93 
     94 
     95   // Removes the Form-value |value| which has been entered in form input fields
     96   // named |name| from the database.
     97   WebDatabase::State RemoveFormValueForElementName(const base::string16& name,
     98                                                    const base::string16& value,
     99                                                    WebDatabase* db);
    100 
    101   // Adds an Autofill profile to the web database.
    102   WebDatabase::State AddAutofillProfile(const AutofillProfile& profile,
    103                                         WebDatabase* db);
    104 
    105   // Updates an Autofill profile in the web database.
    106   WebDatabase::State UpdateAutofillProfile(const AutofillProfile& profile,
    107                                            WebDatabase* db);
    108 
    109   // Removes an Autofill profile from the web database.
    110   WebDatabase::State RemoveAutofillProfile(const std::string& guid,
    111                                            WebDatabase* db);
    112 
    113   // Returns all Autofill profiles from the web database.
    114   scoped_ptr<WDTypedResult> GetAutofillProfiles(WebDatabase* db);
    115 
    116   // Updates Autofill entries in the web database.
    117   WebDatabase::State UpdateAutofillEntries(
    118       const std::vector<autofill::AutofillEntry>& autofill_entries,
    119       WebDatabase* db);
    120 
    121   // Adds a credit card to the web database.
    122   WebDatabase::State AddCreditCard(const CreditCard& credit_card,
    123                                    WebDatabase* db);
    124 
    125   // Updates a credit card in the web database.
    126   WebDatabase::State UpdateCreditCard(const CreditCard& credit_card,
    127                                       WebDatabase* db);
    128 
    129   // Removes a credit card from the web database.
    130   WebDatabase::State RemoveCreditCard(const std::string& guid,
    131                                       WebDatabase* db);
    132 
    133   // Returns a vector of all credit cards from the web database.
    134   scoped_ptr<WDTypedResult> GetCreditCards(WebDatabase* db);
    135 
    136   // Removes Autofill records from the database.
    137   WebDatabase::State RemoveAutofillDataModifiedBetween(
    138       const base::Time& delete_begin,
    139       const base::Time& delete_end,
    140       WebDatabase* db);
    141 
    142   // Removes origin URLs associated with Autofill profiles and credit cards from
    143   // the database.
    144   WebDatabase::State RemoveOriginURLsModifiedBetween(
    145       const base::Time& delete_begin,
    146       const base::Time& delete_end,
    147       WebDatabase* db);
    148 
    149  protected:
    150   virtual ~AutofillWebDataBackendImpl();
    151 
    152  private:
    153   friend class base::RefCountedDeleteOnMessageLoop<AutofillWebDataBackendImpl>;
    154   friend class base::DeleteHelper<AutofillWebDataBackendImpl>;
    155 
    156   // This makes the destructor public, and thus allows us to aggregate
    157   // SupportsUserData. It is private by default to prevent incorrect
    158   // usage in class hierarchies where it is inherited by
    159   // reference-counted objects.
    160   class SupportsUserDataAggregatable : public base::SupportsUserData {
    161    public:
    162     SupportsUserDataAggregatable() {}
    163     virtual ~SupportsUserDataAggregatable() {}
    164    private:
    165     DISALLOW_COPY_AND_ASSIGN(SupportsUserDataAggregatable);
    166   };
    167 
    168   // The MessageLoopProxy that this class uses as its UI thread.
    169   scoped_refptr<base::MessageLoopProxy> ui_thread_;
    170 
    171   // The MessageLoopProxy that this class uses as its DB thread.
    172   scoped_refptr<base::MessageLoopProxy> db_thread_;
    173 
    174   // Storage for user data to be accessed only on the DB thread. May
    175   // be used e.g. for SyncableService subclasses that need to be owned
    176   // by this object. Is created on first call to |GetDBUserData()|.
    177   scoped_ptr<SupportsUserDataAggregatable> user_data_;
    178 
    179   WebDatabase::State RemoveExpiredFormElementsImpl(WebDatabase* db);
    180 
    181   // Callbacks to ensure that sensitive info is destroyed if request is
    182   // cancelled.
    183   void DestroyAutofillProfileResult(const WDTypedResult* result);
    184   void DestroyAutofillCreditCardResult(const WDTypedResult* result);
    185 
    186   ObserverList<AutofillWebDataServiceObserverOnDBThread> db_observer_list_;
    187 
    188   // WebDataServiceBackend allows direct access to DB.
    189   // TODO(caitkp): Make it so nobody but us needs direct DB access anymore.
    190   scoped_refptr<WebDataServiceBackend> web_database_backend_;
    191 
    192   base::Closure on_changed_callback_;
    193 
    194   DISALLOW_COPY_AND_ASSIGN(AutofillWebDataBackendImpl);
    195 };
    196 
    197 }  // namespace autofill
    198 
    199 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_BACKEND_IMPL_H_
    200