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