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 22 namespace syncer { 23 24 // A Syncer provides a control interface for driving the individual steps 25 // of the sync cycle. Each cycle (hopefully) moves the client into closer 26 // synchronization with the server. The individual steps are modeled 27 // as SyncerCommands, and the ordering of the steps is expressed using 28 // the SyncerStep enum. 29 // 30 // A Syncer instance expects to run on a dedicated thread. Calls 31 // to SyncShare() may take an unbounded amount of time, as SyncerCommands 32 // may block on network i/o, on lock contention, or on tasks posted to 33 // other threads. 34 class SYNC_EXPORT_PRIVATE Syncer { 35 public: 36 typedef std::vector<int64> UnsyncedMetaHandles; 37 38 Syncer(); 39 virtual ~Syncer(); 40 41 // Called by other threads to tell the syncer to stop what it's doing 42 // and return early from SyncShare, if possible. 43 bool ExitRequested(); 44 void RequestEarlyExit(); 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 void ApplyUpdates(sessions::SyncSession* session); 73 bool DownloadAndApplyUpdates( 74 sessions::SyncSession* session, 75 base::Callback<SyncerError(void)> download_fn); 76 77 void HandleCycleBegin(sessions::SyncSession* session); 78 bool HandleCycleEnd( 79 sessions::SyncSession* session, 80 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source); 81 82 bool early_exit_requested_; 83 base::Lock early_exit_requested_lock_; 84 85 friend class SyncerTest; 86 FRIEND_TEST_ALL_PREFIXES(SyncerTest, NameClashWithResolver); 87 FRIEND_TEST_ALL_PREFIXES(SyncerTest, IllegalAndLegalUpdates); 88 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingAndNewParent); 89 FRIEND_TEST_ALL_PREFIXES(SyncerTest, 90 TestCommitListOrderingAndNewParentAndChild); 91 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingCounterexample); 92 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingWithNesting); 93 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestCommitListOrderingWithNewItems); 94 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestGetUnsyncedAndSimpleCommit); 95 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestPurgeWhileUnsynced); 96 FRIEND_TEST_ALL_PREFIXES(SyncerTest, TestPurgeWhileUnapplied); 97 FRIEND_TEST_ALL_PREFIXES(SyncerTest, UnappliedUpdateDuringCommit); 98 FRIEND_TEST_ALL_PREFIXES(SyncerTest, DeletingEntryInFolder); 99 FRIEND_TEST_ALL_PREFIXES(SyncerTest, 100 LongChangelistCreatesFakeOrphanedEntries); 101 FRIEND_TEST_ALL_PREFIXES(SyncerTest, QuicklyMergeDualCreatedHierarchy); 102 FRIEND_TEST_ALL_PREFIXES(SyncerTest, LongChangelistWithApplicationConflict); 103 FRIEND_TEST_ALL_PREFIXES(SyncerTest, DeletingEntryWithLocalEdits); 104 FRIEND_TEST_ALL_PREFIXES(EntryCreatedInNewFolderTest, 105 EntryCreatedInNewFolderMidSync); 106 107 DISALLOW_COPY_AND_ASSIGN(Syncer); 108 }; 109 110 } // namespace syncer 111 112 #endif // SYNC_ENGINE_SYNCER_H_ 113