syncer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef COMPONENTS_SYNC_ENGINE_SYNCER_H_
  5. #define COMPONENTS_SYNC_ENGINE_SYNCER_H_
  6. #include <stdint.h>
  7. #include "base/gtest_prod_util.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/sync/base/model_type.h"
  10. #include "components/sync/base/syncer_error.h"
  11. #include "components/sync/protocol/sync_enums.pb.h"
  12. namespace syncer {
  13. class CancelationSignal;
  14. class GetUpdatesDelegate;
  15. class NudgeTracker;
  16. class SyncCycle;
  17. // A Syncer provides a control interface for driving the sync cycle. These
  18. // cycles consist of downloading updates, parsing the response (aka. process
  19. // updates), applying updates while resolving conflicts, and committing local
  20. // changes. Some of these steps may be skipped if they're deemed to be
  21. // unnecessary.
  22. //
  23. // A Syncer instance expects to run on a dedicated thread. Calls to SyncShare()
  24. // may take an unbounded amount of time because it may block on network I/O, on
  25. // lock contention, or on tasks posted to other threads.
  26. class Syncer {
  27. public:
  28. explicit Syncer(CancelationSignal* cancelation_signal);
  29. Syncer(const Syncer&) = delete;
  30. Syncer& operator=(const Syncer&) = delete;
  31. virtual ~Syncer();
  32. // Whether the syncer is in the middle of a sync cycle.
  33. bool IsSyncing() const;
  34. // Fetches and applies updates, resolves conflicts and commits local changes
  35. // for |request_types| as necessary until client and server states are in
  36. // sync. The |nudge_tracker| contains state that describes why the client is
  37. // out of sync and what must be done to bring it back into sync.
  38. // Returns: false if an error occurred and retries should backoff, true
  39. // otherwise.
  40. virtual bool NormalSyncShare(ModelTypeSet request_types,
  41. NudgeTracker* nudge_tracker,
  42. SyncCycle* cycle);
  43. // Performs an initial download for the |request_types|. It is assumed that
  44. // the specified types have no local state, so none of the downloaded updates
  45. // will be applied to the model. The |source| is sent up to the server for
  46. // debug purposes. It describes the reason for performing this initial
  47. // download.
  48. // Returns: false if an error occurred and retries should backoff, true
  49. // otherwise.
  50. virtual bool ConfigureSyncShare(const ModelTypeSet& request_types,
  51. sync_pb::SyncEnums::GetUpdatesOrigin origin,
  52. SyncCycle* cycle);
  53. // Requests to download updates for the |request_types|. For a well-behaved
  54. // client with a working connection to the invalidations server, this should
  55. // be unnecessary. It may be invoked periodically to try to keep the client
  56. // in sync despite bugs or transient failures.
  57. // Returns: false if an error occurred and retries should backoff, true
  58. // otherwise.
  59. virtual bool PollSyncShare(ModelTypeSet request_types, SyncCycle* cycle);
  60. private:
  61. bool DownloadAndApplyUpdates(ModelTypeSet* request_types,
  62. SyncCycle* cycle,
  63. const GetUpdatesDelegate& delegate);
  64. // This function will commit batches of unsynced items to the server until the
  65. // number of unsynced and ready to commit items reaches zero or an error is
  66. // encountered. A request to exit early will be treated as an error and will
  67. // abort any blocking operations.
  68. SyncerError BuildAndPostCommits(const ModelTypeSet& request_types,
  69. NudgeTracker* nudge_tracker,
  70. SyncCycle* cycle);
  71. // Whether an early exist was requested due to a cancelation signal.
  72. bool ExitRequested();
  73. bool HandleCycleEnd(SyncCycle* cycle,
  74. sync_pb::SyncEnums::GetUpdatesOrigin origin);
  75. const raw_ptr<CancelationSignal> cancelation_signal_;
  76. // Whether the syncer is in the middle of a sync attempt.
  77. bool is_syncing_;
  78. };
  79. } // namespace syncer
  80. #endif // COMPONENTS_SYNC_ENGINE_SYNCER_H_