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// 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#pragma once
14
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  ~UpdateApplicator();
44
45  // returns true if there's more we can do.
46  bool AttemptOneApplication(syncable::WriteTransaction* trans);
47  // return true if we've applied all updates.
48  bool AllUpdatesApplied() const;
49
50  // This class does not automatically save its progress into the
51  // SyncSession -- to get that to happen, call this method after update
52  // application is finished (i.e., when AttemptOneAllocation stops returning
53  // true).
54  void SaveProgressIntoSessionState(
55      sessions::ConflictProgress* conflict_progress,
56      sessions::UpdateProgress* update_progress);
57
58 private:
59  // If true, AttemptOneApplication will skip over |entry| and return true.
60  bool SkipUpdate(const syncable::Entry& entry);
61
62  // Adjusts the UpdateIterator members to move ahead by one update.
63  void Advance();
64
65  // Used to resolve conflicts when trying to apply updates.
66  ConflictResolver* const resolver_;
67
68  // Used to decrypt sensitive sync nodes.
69  Cryptographer* cryptographer_;
70
71  UpdateIterator const begin_;
72  UpdateIterator end_;
73  UpdateIterator pointer_;
74  ModelSafeGroup group_filter_;
75  bool progress_;
76
77  const ModelSafeRoutingInfo routing_info_;
78
79  // Track the result of the various items.
80  std::vector<syncable::Id> conflicting_ids_;
81  std::vector<syncable::Id> successful_ids_;
82
83  DISALLOW_COPY_AND_ASSIGN(UpdateApplicator);
84};
85
86}  // namespace browser_sync
87
88#endif  // CHROME_BROWSER_SYNC_ENGINE_UPDATE_APPLICATOR_H_
89