update_applicator.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2009 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// An UpdateApplicator is used to iterate over a number of unapplied updates,
6// applying them to the client using the given syncer session.
7//
8// UpdateApplicator might resemble an iterator, but it actually keeps retrying
9// failed updates until no remaining updates can be successfully applied.
10
11#ifndef CHROME_BROWSER_SYNC_ENGINE_UPDATE_APPLICATOR_H_
12#define CHROME_BROWSER_SYNC_ENGINE_UPDATE_APPLICATOR_H_
13
14#include <set>
15#include <vector>
16
17#include "base/basictypes.h"
18#include "base/port.h"
19#include "chrome/browser/sync/engine/model_safe_worker.h"
20#include "chrome/browser/sync/syncable/syncable.h"
21
22namespace browser_sync {
23
24namespace sessions {
25class ConflictProgress;
26class UpdateProgress;
27}
28
29class ConflictResolver;
30class Cryptographer;
31
32class UpdateApplicator {
33 public:
34  typedef syncable::Directory::UnappliedUpdateMetaHandles::iterator
35      UpdateIterator;
36
37  UpdateApplicator(ConflictResolver* resolver,
38                   Cryptographer* cryptographer,
39                   const UpdateIterator& begin,
40                   const UpdateIterator& end,
41                   const ModelSafeRoutingInfo& routes,
42                   ModelSafeGroup group_filter);
43
44  // returns true if there's more we can do.
45  bool AttemptOneApplication(syncable::WriteTransaction* trans);
46  // return true if we've applied all updates.
47  bool AllUpdatesApplied() const;
48
49  // This class does not automatically save its progress into the
50  // SyncSession -- to get that to happen, call this method after update
51  // application is finished (i.e., when AttemptOneAllocation stops returning
52  // true).
53  void SaveProgressIntoSessionState(
54      sessions::ConflictProgress* conflict_progress,
55      sessions::UpdateProgress* update_progress);
56
57 private:
58  // If true, AttemptOneApplication will skip over |entry| and return true.
59  bool SkipUpdate(const syncable::Entry& entry);
60
61  // Adjusts the UpdateIterator members to move ahead by one update.
62  void Advance();
63
64  // Used to resolve conflicts when trying to apply updates.
65  ConflictResolver* const resolver_;
66
67  // Used to decrypt sensitive sync nodes.
68  Cryptographer* cryptographer_;
69
70  UpdateIterator const begin_;
71  UpdateIterator end_;
72  UpdateIterator pointer_;
73  ModelSafeGroup group_filter_;
74  bool progress_;
75
76  const ModelSafeRoutingInfo routing_info_;
77
78  // Track the result of the various items.
79  std::vector<syncable::Id> conflicting_ids_;
80  std::vector<syncable::Id> successful_ids_;
81};
82
83}  // namespace browser_sync
84
85#endif  // CHROME_BROWSER_SYNC_ENGINE_UPDATE_APPLICATOR_H_
86