autofill_profile_change_processor.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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#ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_
5#define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_
6#pragma once
7#include <string>
8#include <vector>
9
10#include "chrome/browser/autofill/autofill_profile.h"
11#include "chrome/browser/autofill/personal_data_manager.h"
12#include "chrome/browser/sync/engine/syncapi.h"
13#include "chrome/browser/sync/glue/autofill_profile_model_associator.h"
14#include "chrome/browser/sync/glue/change_processor.h"
15#include "chrome/browser/sync/unrecoverable_error_handler.h"
16#include "chrome/browser/webdata/autofill_change.h"
17#include "chrome/browser/webdata/web_database.h"
18#include "chrome/common/notification_observer.h"
19#include "chrome/common/notification_registrar.h"
20#include "chrome/common/notification_service.h"
21#include "chrome/common/notification_type.h"
22
23namespace browser_sync {
24
25class AutofillProfileChangeProcessor : public ChangeProcessor,
26    public NotificationObserver {
27 public:
28  AutofillProfileChangeProcessor(
29      AutofillProfileModelAssociator *model_associator,
30      WebDatabase* web_database,
31      PersonalDataManager* personal_data_manager,
32      UnrecoverableErrorHandler* error_handler);
33
34  virtual ~AutofillProfileChangeProcessor() {}
35
36  // Virtual methods from ChangeProcessor class.
37  virtual void ApplyChangesFromSyncModel(
38      const sync_api::BaseTransaction *write_trans,
39      const sync_api::SyncManager::ChangeRecord* changes,
40      int change_count);
41
42  virtual void CommitChangesFromSyncModel();
43
44  // Virtual method implemented for the observer class.
45  virtual void Observe(NotificationType type,
46                       const NotificationSource& source,
47                       const NotificationDetails& details);
48
49
50  static void WriteAutofillProfile(const AutoFillProfile& profile,
51                                   sync_api::WriteNode* node);
52
53 protected:
54  // Protected methods from ChangeProcessor.
55  virtual void StartImpl(Profile* profile) {}
56  virtual void StopImpl() {}
57
58  struct AutofillProfileChangeRecord {
59    sync_api::SyncManager::ChangeRecord::Action action_;
60    int64 id_;
61    sync_pb::AutofillProfileSpecifics profile_specifics_;
62    AutofillProfileChangeRecord(
63        sync_api::SyncManager::ChangeRecord::Action action,
64        int64 id,
65        const sync_pb::AutofillProfileSpecifics profile_specifics)
66        : action_(action),
67          id_(id),
68          profile_specifics_(profile_specifics) {}
69  };
70
71  std::vector<AutofillProfileChangeRecord> autofill_changes_;
72
73  virtual void AddAutofillProfileSyncNode(sync_api::WriteTransaction* trans,
74      sync_api::BaseNode& autofill_profile_root,
75      const AutoFillProfile& profile);
76
77  void ActOnChange(AutofillProfileChangeGUID* change,
78      sync_api::WriteTransaction* trans,
79      sync_api::ReadNode& autofill_root);
80
81 private:
82
83  // This ensures that startobsrving gets called after stopobserving even
84  // if there is an early return in the function.
85  // TODO(lipalani) - generalize this and add it to other change processors.
86  class ScopedStopObserving {
87   public:
88    explicit ScopedStopObserving(AutofillProfileChangeProcessor* processor);
89    ~ScopedStopObserving();
90
91   private:
92    ScopedStopObserving() {}
93    AutofillProfileChangeProcessor* processor_;
94  };
95
96  void StartObserving();
97  void StopObserving();
98
99  void PostOptimisticRefreshTask();
100
101  void ApplyAutofillProfileChange(
102    sync_api::SyncManager::ChangeRecord::Action action,
103    const sync_pb::AutofillProfileSpecifics& profile,
104    int64 sync_id);
105
106  void RemoveSyncNode(
107      const std::string& guid, sync_api::WriteTransaction *trans);
108
109  AutofillProfileModelAssociator* model_associator_;
110  bool observing_;
111
112  WebDatabase* web_database_;
113  PersonalDataManager* personal_data_;
114  NotificationRegistrar notification_registrar_;
115};
116
117}  // namespace browser_sync
118
119#endif  // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_
120
121