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