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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/supports_user_data.h"
14 #include "components/autofill/core/browser/webdata/autofill_webdata.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"
21 class WebDatabaseService;
24 class MessageLoopProxy;
31 class AutofillProfile;
32 class AutofillWebDataBackend;
33 class AutofillWebDataBackendImpl;
34 class AutofillWebDataServiceObserverOnDBThread;
35 class AutofillWebDataServiceObserverOnUIThread;
38 // API for Autofill web data.
39 class AutofillWebDataService : public AutofillWebData,
40 public WebDataServiceBase {
42 AutofillWebDataService(scoped_refptr<base::MessageLoopProxy> ui_thread,
43 scoped_refptr<base::MessageLoopProxy> db_thread);
44 AutofillWebDataService(scoped_refptr<WebDatabaseService> wdbs,
45 scoped_refptr<base::MessageLoopProxy> ui_thread,
46 scoped_refptr<base::MessageLoopProxy> db_thread,
47 const ProfileErrorCallback& callback);
49 // WebDataServiceBase implementation.
50 void ShutdownOnUIThread() override;
52 // AutofillWebData implementation.
53 void AddFormFields(const std::vector<FormFieldData>& fields) override;
54 WebDataServiceBase::Handle GetFormValuesForElementName(
55 const base::string16& name,
56 const base::string16& prefix,
58 WebDataServiceConsumer* consumer) override;
60 WebDataServiceBase::Handle HasFormElements(
61 WebDataServiceConsumer* consumer) override;
62 void RemoveFormElementsAddedBetween(const base::Time& delete_begin,
63 const base::Time& delete_end) override;
64 void RemoveFormValueForElementName(const base::string16& name,
65 const base::string16& value) override;
66 void AddAutofillProfile(const AutofillProfile& profile) override;
67 void UpdateAutofillProfile(const AutofillProfile& profile) override;
68 void RemoveAutofillProfile(const std::string& guid) override;
69 WebDataServiceBase::Handle GetAutofillProfiles(
70 WebDataServiceConsumer* consumer) override;
71 void UpdateAutofillEntries(
72 const std::vector<AutofillEntry>& autofill_entries) override;
73 void AddCreditCard(const CreditCard& credit_card) override;
74 void UpdateCreditCard(const CreditCard& credit_card) override;
75 void RemoveCreditCard(const std::string& guid) override;
76 WebDataServiceBase::Handle GetCreditCards(
77 WebDataServiceConsumer* consumer) override;
78 void RemoveAutofillDataModifiedBetween(const base::Time& delete_begin,
79 const base::Time& delete_end) override;
80 void RemoveOriginURLsModifiedBetween(const base::Time& delete_begin,
81 const base::Time& delete_end) override;
83 void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer);
84 void RemoveObserver(AutofillWebDataServiceObserverOnDBThread* observer);
86 void AddObserver(AutofillWebDataServiceObserverOnUIThread* observer);
87 void RemoveObserver(AutofillWebDataServiceObserverOnUIThread* observer);
89 // Returns a SupportsUserData objects that may be used to store data
90 // owned by the DB thread on this object. Should be called only from
91 // the DB thread, and will be destroyed on the DB thread soon after
92 // |ShutdownOnUIThread()| is called.
93 base::SupportsUserData* GetDBUserData();
95 // Takes a callback which will be called on the DB thread with a pointer to an
96 // |AutofillWebdataBackend|. This backend can be used to access or update the
97 // WebDatabase directly on the DB thread.
98 void GetAutofillBackend(
99 const base::Callback<void(AutofillWebDataBackend*)>& callback);
102 ~AutofillWebDataService() override;
104 virtual void NotifyAutofillMultipleChangedOnUIThread();
106 base::WeakPtr<AutofillWebDataService> AsWeakPtr() {
107 return weak_ptr_factory_.GetWeakPtr();
111 ObserverList<AutofillWebDataServiceObserverOnUIThread> ui_observer_list_;
113 // The MessageLoopProxy that this class uses as its UI thread.
114 scoped_refptr<base::MessageLoopProxy> ui_thread_;
116 // The MessageLoopProxy that this class uses as its DB thread.
117 scoped_refptr<base::MessageLoopProxy> db_thread_;
119 scoped_refptr<AutofillWebDataBackendImpl> autofill_backend_;
121 // This factory is used on the UI thread. All vended weak pointers are
122 // invalidated in ShutdownOnUIThread().
123 base::WeakPtrFactory<AutofillWebDataService> weak_ptr_factory_;
125 DISALLOW_COPY_AND_ASSIGN(AutofillWebDataService);
128 } // namespace autofill
130 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_