Reland [Sync] Refactoring polling to be reliable.
[chromium-blink-merge.git] / sync / engine / syncer.h
blobe473bae86381349629d1028ee75c92fce8e898a9
1 // Copyright (c) 2012 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.
5 #ifndef SYNC_ENGINE_SYNCER_H_
6 #define SYNC_ENGINE_SYNCER_H_
8 #include <utility>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/synchronization/lock.h"
15 #include "sync/base/sync_export.h"
16 #include "sync/engine/conflict_resolver.h"
17 #include "sync/engine/syncer_types.h"
18 #include "sync/internal_api/public/base/model_type.h"
19 #include "sync/sessions/sync_session.h"
20 #include "sync/util/extensions_activity.h"
22 namespace syncer {
24 class CancelationSignal;
25 class CommitProcessor;
26 class GetUpdatesProcessor;
28 // A Syncer provides a control interface for driving the sync cycle. These
29 // cycles consist of downloading updates, parsing the response (aka. process
30 // updates), applying updates while resolving conflicts, and committing local
31 // changes. Some of these steps may be skipped if they're deemed to be
32 // unnecessary.
34 // A Syncer instance expects to run on a dedicated thread. Calls to SyncShare()
35 // may take an unbounded amount of time because it may block on network I/O, on
36 // lock contention, or on tasks posted to other threads.
37 class SYNC_EXPORT_PRIVATE Syncer {
38 public:
39 typedef std::vector<int64> UnsyncedMetaHandles;
41 Syncer(CancelationSignal* cancelation_signal);
42 virtual ~Syncer();
44 // Whether an early exist was requested due to a cancelation signal.
45 bool ExitRequested();
47 // Whether the syncer is in the middle of a sync cycle.
48 bool IsSyncing() const;
50 // Fetches and applies updates, resolves conflicts and commits local changes
51 // for |request_types| as necessary until client and server states are in
52 // sync. The |nudge_tracker| contains state that describes why the client is
53 // out of sync and what must be done to bring it back into sync.
54 // Returns: false if an error occurred and retries should backoff, true
55 // otherwise.
56 virtual bool NormalSyncShare(ModelTypeSet request_types,
57 sessions::NudgeTracker* nudge_tracker,
58 sessions::SyncSession* session);
60 // Performs an initial download for the |request_types|. It is assumed that
61 // the specified types have no local state, and that their associated change
62 // processors are in "passive" mode, so none of the downloaded updates will be
63 // applied to the model. The |source| is sent up to the server for debug
64 // purposes. It describes the reson for performing this initial download.
65 // Returns: false if an error occurred and retries should backoff, true
66 // otherwise.
67 virtual bool ConfigureSyncShare(
68 ModelTypeSet request_types,
69 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source,
70 sessions::SyncSession* session);
72 // Requests to download updates for the |request_types|. For a well-behaved
73 // client with a working connection to the invalidations server, this should
74 // be unnecessary. It may be invoked periodically to try to keep the client
75 // in sync despite bugs or transient failures.
76 // Returns: false if an error occurred and retries should backoff, true
77 // otherwise.
78 virtual bool PollSyncShare(ModelTypeSet request_types,
79 sessions::SyncSession* session);
81 private:
82 friend class SyncerTest;
83 FRIEND_TEST_ALL_PREFIXES(SyncerTest, NameClashWithResolver);
84 FRIEND_TEST_ALL_PREFIXES(SyncerTest, IllegalAndLegalUpdates);
85 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingAndNewParent);
86 FRIEND_TEST_ALL_PREFIXES(SyncerTest,
87 TestCommitListOrderingAndNewParentAndChild);
88 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingCounterexample);
89 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingWithNesting);
90 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingWithNewItems);
91 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestGetUnsyncedAndSimpleCommit);
92 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestPurgeWhileUnsynced);
93 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestPurgeWhileUnapplied);
94 FRIEND_TEST_ALL_PREFIXES(SyncerTest, UnappliedUpdateDuringCommit);
95 FRIEND_TEST_ALL_PREFIXES(SyncerTest, DeletingEntryInFolder);
96 FRIEND_TEST_ALL_PREFIXES(SyncerTest,
97 LongChangelistCreatesFakeOrphanedEntries);
98 FRIEND_TEST_ALL_PREFIXES(SyncerTest, QuicklyMergeDualCreatedHierarchy);
99 FRIEND_TEST_ALL_PREFIXES(SyncerTest, LongChangelistWithApplicationConflict);
100 FRIEND_TEST_ALL_PREFIXES(SyncerTest, DeletingEntryWithLocalEdits);
101 FRIEND_TEST_ALL_PREFIXES(EntryCreatedInNewFolderTest,
102 EntryCreatedInNewFolderMidSync);
104 bool DownloadAndApplyUpdates(
105 ModelTypeSet* request_types,
106 sessions::SyncSession* session,
107 GetUpdatesProcessor* get_updates_processor,
108 bool create_mobile_bookmarks_folder);
110 // This function will commit batches of unsynced items to the server until the
111 // number of unsynced and ready to commit items reaches zero or an error is
112 // encountered. A request to exit early will be treated as an error and will
113 // abort any blocking operations.
114 SyncerError BuildAndPostCommits(ModelTypeSet request_types,
115 sessions::NudgeTracker* nudge_tracker,
116 sessions::SyncSession* session,
117 CommitProcessor* commit_processor);
119 void HandleCycleBegin(sessions::SyncSession* session);
120 bool HandleCycleEnd(
121 sessions::SyncSession* session,
122 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
124 syncer::CancelationSignal* const cancelation_signal_;
126 // Whether the syncer is in the middle of a sync attempt.
127 bool is_syncing_;
129 DISALLOW_COPY_AND_ASSIGN(Syncer);
132 } // namespace syncer
134 #endif // SYNC_ENGINE_SYNCER_H_