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