autofill_change_processor.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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 CHROME_BROWSER_SYNC_GLUE_AUTOFILL_CHANGE_PROCESSOR_H_
6#define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_CHANGE_PROCESSOR_H_
7
8#include "base/scoped_ptr.h"
9#include "chrome/browser/autofill/autofill_profile.h"
10#include "chrome/browser/autofill/credit_card.h"
11#include "chrome/browser/autofill/personal_data_manager.h"
12#include "chrome/browser/sync/engine/syncapi.h"
13#include "chrome/browser/sync/glue/change_processor.h"
14#include "chrome/browser/sync/glue/sync_backend_host.h"
15#include "chrome/browser/sync/protocol/autofill_specifics.pb.h"
16#include "chrome/browser/webdata/web_data_service.h"
17#include "chrome/common/notification_observer.h"
18#include "chrome/common/notification_registrar.h"
19
20class AutofillCreditCardChange;
21class AutofillEntry;
22class AutofillProfileChange;
23class PersonalDataManager;
24class WebDatabase;
25
26namespace browser_sync {
27
28class AutofillModelAssociator;
29class UnrecoverableErrorHandler;
30
31// This class is responsible for taking changes from the web data service and
32// applying them to the sync_api 'syncable' model, and vice versa. All
33// operations and use of this class are from the DB thread.
34class AutofillChangeProcessor : public ChangeProcessor,
35                                public NotificationObserver {
36 public:
37  AutofillChangeProcessor(AutofillModelAssociator* model_associator,
38                          WebDatabase* web_database,
39                          PersonalDataManager* personal_data,
40                          UnrecoverableErrorHandler* error_handler);
41  virtual ~AutofillChangeProcessor() {}
42
43  // NotificationObserver implementation.
44  // WebDataService -> sync_api model change application.
45  virtual void Observe(NotificationType type,
46                       const NotificationSource& source,
47                       const NotificationDetails& details);
48
49  // sync_api model -> WebDataService change application.
50  virtual void ApplyChangesFromSyncModel(
51      const sync_api::BaseTransaction* trans,
52      const sync_api::SyncManager::ChangeRecord* changes,
53      int change_count);
54
55  // Copy the properties of the given Autofill entry into the sync
56  // node.
57  static void WriteAutofillEntry(const AutofillEntry& entry,
58                                 sync_api::WriteNode* node);
59  // As above, for autofill profiles.
60  static void WriteAutofillProfile(const AutoFillProfile& profile,
61                                   sync_api::WriteNode* node);
62  // TODO(georgey) : add the same processing for CC info (already in protocol
63  // buffers).
64
65 protected:
66  virtual void StartImpl(Profile* profile);
67  virtual void StopImpl();
68
69 private:
70  void StartObserving();
71  void StopObserving();
72
73  // A function to remove the sync node for an autofill entry or profile.
74  void RemoveSyncNode(const std::string& tag,
75                      sync_api::WriteTransaction* trans);
76
77  // These two methods are dispatched to by Observe depending on the type.
78  void ObserveAutofillEntriesChanged(AutofillChangeList* changes,
79      sync_api::WriteTransaction* trans,
80      const sync_api::ReadNode& autofill_root);
81  void ObserveAutofillProfileChanged(AutofillProfileChange* change,
82      sync_api::WriteTransaction* trans,
83      const sync_api::ReadNode& autofill_root);
84
85  // The following methods are the implementation of ApplyChangeFromSyncModel
86  // for the respective autofill subtypes.
87  void ApplySyncAutofillEntryChange(
88      sync_api::SyncManager::ChangeRecord::Action action,
89      const sync_pb::AutofillSpecifics& autofill,
90      std::vector<AutofillEntry>* new_entries,
91      int64 sync_id);
92  void ApplySyncAutofillProfileChange(
93      sync_api::SyncManager::ChangeRecord::Action action,
94      const sync_pb::AutofillProfileSpecifics& profile,
95      int64 sync_id);
96
97  // Delete is a special case of change application.
98  void ApplySyncAutofillEntryDelete(
99      const sync_pb::AutofillSpecifics& autofill);
100  void ApplySyncAutofillProfileDelete(
101      const sync_pb::AutofillProfileSpecifics& profile,
102      int64 sync_id);
103
104  // If the chrome model tries to add an AutoFillProfile with a label that
105  // is already in use, we perform a move-aside by calling-back into the chrome
106  // model and overwriting the label with a unique value we can apply for sync.
107  // This method should be called on an ADD notification from the chrome model.
108  // |tag| contains the unique sync client tag identifier for |profile|, which
109  // is derived from the profile label using ProfileLabelToTag.
110  void HandleMoveAsideIfNeeded(
111      sync_api::BaseTransaction* trans, AutoFillProfile* profile,
112      std::string* tag);
113
114  // Helper to create a sync node with tag |tag|, storing |profile| as
115  // the node's AutofillSpecifics.
116  void AddAutofillProfileSyncNode(
117      sync_api::WriteTransaction* trans,
118      const sync_api::BaseNode& autofill,
119      const std::string& tag,
120      const AutoFillProfile* profile);
121
122  // Helper to post a task to the UI loop to inform the PersonalDataManager
123  // it needs to refresh itself.
124  void PostOptimisticRefreshTask();
125
126  // The two models should be associated according to this ModelAssociator.
127  AutofillModelAssociator* model_associator_;
128
129  // The model we are processing changes from.  This is owned by the
130  // WebDataService which is kept alive by our data type controller
131  // holding a reference.
132  WebDatabase* web_database_;
133
134  // We periodically tell the PersonalDataManager to refresh as we make
135  // changes to the autofill data in the WebDatabase.
136  PersonalDataManager* personal_data_;
137
138  NotificationRegistrar notification_registrar_;
139
140  bool observing_;
141
142  DISALLOW_COPY_AND_ASSIGN(AutofillChangeProcessor);
143};
144
145}  // namespace browser_sync
146
147#endif  // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_CHANGE_PROCESSOR_H_
148