• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_
7 
8 #include <vector>
9 
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"
20 
21 class WebDatabaseService;
22 
23 namespace base {
24 class MessageLoopProxy;
25 }
26 
27 namespace autofill {
28 
29 class AutofillChange;
30 class AutofillProfile;
31 class AutofillWebDataBackend;
32 class AutofillWebDataBackendImpl;
33 class AutofillWebDataServiceObserverOnDBThread;
34 class AutofillWebDataServiceObserverOnUIThread;
35 class CreditCard;
36 
37 // API for Autofill web data.
38 class AutofillWebDataService : public AutofillWebData,
39                                public WebDataServiceBase {
40  public:
41   AutofillWebDataService(scoped_refptr<base::MessageLoopProxy> ui_thread,
42                          scoped_refptr<base::MessageLoopProxy> db_thread);
43   AutofillWebDataService(scoped_refptr<WebDatabaseService> wdbs,
44                          scoped_refptr<base::MessageLoopProxy> ui_thread,
45                          scoped_refptr<base::MessageLoopProxy> db_thread,
46                          const ProfileErrorCallback& callback);
47 
48   // WebDataServiceBase implementation.
49   virtual void ShutdownOnUIThread() OVERRIDE;
50 
51   // AutofillWebData implementation.
52   virtual void AddFormFields(
53       const std::vector<FormFieldData>& fields) OVERRIDE;
54   virtual WebDataServiceBase::Handle GetFormValuesForElementName(
55       const base::string16& name,
56       const base::string16& prefix,
57       int limit,
58       WebDataServiceConsumer* consumer) OVERRIDE;
59 
60   virtual WebDataServiceBase::Handle HasFormElements(
61       WebDataServiceConsumer* consumer) OVERRIDE;
62   virtual void RemoveFormElementsAddedBetween(
63       const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE;
64   virtual void RemoveFormValueForElementName(
65       const base::string16& name,
66       const base::string16& value) OVERRIDE;
67   virtual void AddAutofillProfile(const AutofillProfile& profile) OVERRIDE;
68   virtual void UpdateAutofillProfile(const AutofillProfile& profile) OVERRIDE;
69   virtual void RemoveAutofillProfile(const std::string& guid) OVERRIDE;
70   virtual WebDataServiceBase::Handle GetAutofillProfiles(
71       WebDataServiceConsumer* consumer) OVERRIDE;
72   virtual void AddCreditCard(const CreditCard& credit_card) OVERRIDE;
73   virtual void UpdateCreditCard(const CreditCard& credit_card) OVERRIDE;
74   virtual void RemoveCreditCard(const std::string& guid) OVERRIDE;
75   virtual WebDataServiceBase::Handle GetCreditCards(
76       WebDataServiceConsumer* consumer) OVERRIDE;
77   virtual void RemoveAutofillDataModifiedBetween(
78       const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE;
79   virtual void RemoveOriginURLsModifiedBetween(
80       const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE;
81 
82   void AddObserver(AutofillWebDataServiceObserverOnDBThread* observer);
83   void RemoveObserver(AutofillWebDataServiceObserverOnDBThread* observer);
84 
85   void AddObserver(AutofillWebDataServiceObserverOnUIThread* observer);
86   void RemoveObserver(AutofillWebDataServiceObserverOnUIThread* observer);
87 
88   // Returns a SupportsUserData objects that may be used to store data
89   // owned by the DB thread on this object. Should be called only from
90   // the DB thread, and will be destroyed on the DB thread soon after
91   // |ShutdownOnUIThread()| is called.
92   base::SupportsUserData* GetDBUserData();
93 
94   // Takes a callback which will be called on the DB thread with a pointer to an
95   // |AutofillWebdataBackend|. This backend can be used to access or update the
96   // WebDatabase directly on the DB thread.
97   void GetAutofillBackend(
98       const base::Callback<void(AutofillWebDataBackend*)>& callback);
99 
100  protected:
101   virtual ~AutofillWebDataService();
102 
103   virtual void NotifyAutofillMultipleChangedOnUIThread();
104 
AsWeakPtr()105   base::WeakPtr<AutofillWebDataService> AsWeakPtr() {
106     return weak_ptr_factory_.GetWeakPtr();
107   }
108 
109  private:
110   ObserverList<AutofillWebDataServiceObserverOnUIThread> ui_observer_list_;
111 
112     // The MessageLoopProxy that this class uses as its UI thread.
113   scoped_refptr<base::MessageLoopProxy> ui_thread_;
114 
115   // The MessageLoopProxy that this class uses as its DB thread.
116   scoped_refptr<base::MessageLoopProxy> db_thread_;
117 
118   // This factory is used on the UI thread. All vended weak pointers are
119   // invalidated in ShutdownOnUIThread().
120   base::WeakPtrFactory<AutofillWebDataService> weak_ptr_factory_;
121 
122   scoped_refptr<AutofillWebDataBackendImpl> autofill_backend_;
123 
124   DISALLOW_COPY_AND_ASSIGN(AutofillWebDataService);
125 };
126 
127 }  // namespace autofill
128 
129 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_WEBDATA_SERVICE_H_
130