summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/autofill_profile_change_processor.h
blob: a1d44a34117afdebc3b678b90524023677f944d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_
#define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_
#pragma once
#include <string>
#include <vector>

#include "chrome/browser/autofill/autofill_profile.h"
#include "chrome/browser/autofill/personal_data_manager.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/glue/autofill_profile_model_associator.h"
#include "chrome/browser/sync/glue/change_processor.h"
#include "chrome/browser/sync/unrecoverable_error_handler.h"
#include "chrome/browser/webdata/autofill_change.h"
#include "chrome/browser/webdata/web_database.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"

namespace browser_sync {

class AutofillProfileChangeProcessor : public ChangeProcessor,
    public NotificationObserver {
 public:
  AutofillProfileChangeProcessor(
      AutofillProfileModelAssociator *model_associator,
      WebDatabase* web_database,
      PersonalDataManager* personal_data_manager,
      UnrecoverableErrorHandler* error_handler);

  virtual ~AutofillProfileChangeProcessor();

  // Virtual methods from ChangeProcessor class.
  virtual void ApplyChangesFromSyncModel(
      const sync_api::BaseTransaction *write_trans,
      const sync_api::SyncManager::ChangeRecord* changes,
      int change_count);

  virtual void CommitChangesFromSyncModel();

  // Virtual method implemented for the observer class.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);


  static void WriteAutofillProfile(const AutoFillProfile& profile,
                                   sync_api::WriteNode* node);

 protected:
  // Protected methods from ChangeProcessor.
  virtual void StartImpl(Profile* profile) {}
  virtual void StopImpl() {}

  struct AutofillProfileChangeRecord {
    sync_api::SyncManager::ChangeRecord::Action action_;
    int64 id_;
    sync_pb::AutofillProfileSpecifics profile_specifics_;
    AutofillProfileChangeRecord(
        sync_api::SyncManager::ChangeRecord::Action action,
        int64 id,
        const sync_pb::AutofillProfileSpecifics profile_specifics)
        : action_(action),
          id_(id),
          profile_specifics_(profile_specifics) {}
  };

  std::vector<AutofillProfileChangeRecord> autofill_changes_;

  virtual void AddAutofillProfileSyncNode(sync_api::WriteTransaction* trans,
      sync_api::BaseNode& autofill_profile_root,
      const AutoFillProfile& profile);

  void ActOnChange(AutofillProfileChange* change,
      sync_api::WriteTransaction* trans,
      sync_api::ReadNode& autofill_root);

 private:

  // This ensures that startobsrving gets called after stopobserving even
  // if there is an early return in the function.
  // TODO(lipalani) - generalize this and add it to other change processors.
  class ScopedStopObserving {
   public:
    explicit ScopedStopObserving(AutofillProfileChangeProcessor* processor);
    ~ScopedStopObserving();

   private:
    ScopedStopObserving() {}
    AutofillProfileChangeProcessor* processor_;
  };

  void StartObserving();
  void StopObserving();

  void PostOptimisticRefreshTask();

  void ApplyAutofillProfileChange(
    sync_api::SyncManager::ChangeRecord::Action action,
    const sync_pb::AutofillProfileSpecifics& profile,
    int64 sync_id);

  void RemoveSyncNode(
      const std::string& guid, sync_api::WriteTransaction *trans);

  AutofillProfileModelAssociator* model_associator_;
  bool observing_;

  WebDatabase* web_database_;
  PersonalDataManager* personal_data_;
  NotificationRegistrar notification_registrar_;
};

}  // namespace browser_sync

#endif  // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_