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.
4
5#ifndef SYNC_ENGINE_SYNCER_H_
6#define SYNC_ENGINE_SYNCER_H_
7
8#include <utility>
9#include <vector>
10
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"
21
22namespace syncer {
23
24class CancelationSignal;
25class CommitProcessor;
26class GetUpdatesProcessor;
27
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.
33//
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.
37class SYNC_EXPORT_PRIVATE Syncer {
38 public:
39  typedef std::vector<int64> UnsyncedMetaHandles;
40
41  Syncer(CancelationSignal* cancelation_signal);
42  virtual ~Syncer();
43
44  bool ExitRequested();
45
46  // Fetches and applies updates, resolves conflicts and commits local changes
47  // for |request_types| as necessary until client and server states are in
48  // sync.  The |nudge_tracker| contains state that describes why the client is
49  // out of sync and what must be done to bring it back into sync.
50  virtual bool NormalSyncShare(ModelTypeSet request_types,
51                               const sessions::NudgeTracker& nudge_tracker,
52                               sessions::SyncSession* session);
53
54  // Performs an initial download for the |request_types|.  It is assumed that
55  // the specified types have no local state, and that their associated change
56  // processors are in "passive" mode, so none of the downloaded updates will be
57  // applied to the model.  The |source| is sent up to the server for debug
58  // purposes.  It describes the reson for performing this initial download.
59  virtual bool ConfigureSyncShare(
60      ModelTypeSet request_types,
61      sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source,
62      sessions::SyncSession* session);
63
64  // Requests to download updates for the |request_types|.  For a well-behaved
65  // client with a working connection to the invalidations server, this should
66  // be unnecessary.  It may be invoked periodically to try to keep the client
67  // in sync despite bugs or transient failures.
68  virtual bool PollSyncShare(ModelTypeSet request_types,
69                             sessions::SyncSession* session);
70
71 private:
72  bool DownloadAndApplyUpdates(
73      ModelTypeSet request_types,
74      sessions::SyncSession* session,
75      GetUpdatesProcessor* get_updates_processor,
76      bool create_mobile_bookmarks_folder);
77
78  // This function will commit batches of unsynced items to the server until the
79  // number of unsynced and ready to commit items reaches zero or an error is
80  // encountered.  A request to exit early will be treated as an error and will
81  // abort any blocking operations.
82  SyncerError BuildAndPostCommits(
83      ModelTypeSet request_types,
84      sessions::SyncSession* session,
85      CommitProcessor* commit_processor);
86
87  void HandleCycleBegin(sessions::SyncSession* session);
88  bool HandleCycleEnd(
89      sessions::SyncSession* session,
90      sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
91
92  syncer::CancelationSignal* const cancelation_signal_;
93
94  friend class SyncerTest;
95  FRIEND_TEST_ALL_PREFIXES(SyncerTest, NameClashWithResolver);
96  FRIEND_TEST_ALL_PREFIXES(SyncerTest, IllegalAndLegalUpdates);
97  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingAndNewParent);
98  FRIEND_TEST_ALL_PREFIXES(SyncerTest,
99                           TestCommitListOrderingAndNewParentAndChild);
100  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingCounterexample);
101  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingWithNesting);
102  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingWithNewItems);
103  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestGetUnsyncedAndSimpleCommit);
104  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestPurgeWhileUnsynced);
105  FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestPurgeWhileUnapplied);
106  FRIEND_TEST_ALL_PREFIXES(SyncerTest, UnappliedUpdateDuringCommit);
107  FRIEND_TEST_ALL_PREFIXES(SyncerTest, DeletingEntryInFolder);
108  FRIEND_TEST_ALL_PREFIXES(SyncerTest,
109                           LongChangelistCreatesFakeOrphanedEntries);
110  FRIEND_TEST_ALL_PREFIXES(SyncerTest, QuicklyMergeDualCreatedHierarchy);
111  FRIEND_TEST_ALL_PREFIXES(SyncerTest, LongChangelistWithApplicationConflict);
112  FRIEND_TEST_ALL_PREFIXES(SyncerTest, DeletingEntryWithLocalEdits);
113  FRIEND_TEST_ALL_PREFIXES(EntryCreatedInNewFolderTest,
114                           EntryCreatedInNewFolderMidSync);
115
116  DISALLOW_COPY_AND_ASSIGN(Syncer);
117};
118
119}  // namespace syncer
120
121#endif  // SYNC_ENGINE_SYNCER_H_
122