sync_scheduler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 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_SYNC_SCHEDULER_H_
  5. #define COMPONENTS_SYNC_ENGINE_SYNC_SCHEDULER_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/time/time.h"
  10. #include "components/sync/base/sync_invalidation.h"
  11. #include "components/sync/engine/cycle/sync_cycle.h"
  12. #include "services/network/public/mojom/network_change_manager.mojom.h"
  13. namespace syncer {
  14. // A class to schedule syncer tasks intelligently.
  15. class SyncScheduler : public SyncCycle::Delegate {
  16. public:
  17. enum Mode {
  18. // In this mode, the thread only performs configuration tasks. This is
  19. // designed to make the case where we want to download updates for a
  20. // specific type only, and not continue syncing until we are moved into
  21. // normal mode.
  22. CONFIGURATION_MODE,
  23. // Resumes polling and allows nudges, drops configuration tasks. Runs
  24. // through entire sync cycle.
  25. NORMAL_MODE,
  26. };
  27. // All methods of SyncScheduler must be called on the same thread
  28. // (except for RequestEarlyExit()).
  29. SyncScheduler() = default;
  30. ~SyncScheduler() override = default;
  31. // Start the scheduler with the given mode. If the scheduler is
  32. // already started, switch to the given mode, although some
  33. // scheduled tasks from the old mode may still run. |last_poll_time| will
  34. // be used to decide what the poll timer should be initialized with.
  35. virtual void Start(Mode mode, base::Time last_poll_time) = 0;
  36. // Schedules the configuration task. |ready_task| is invoked when the
  37. // configuration finishes.
  38. // Note: must already be in CONFIGURATION mode.
  39. virtual void ScheduleConfiguration(
  40. sync_pb::SyncEnums::GetUpdatesOrigin origin,
  41. ModelTypeSet types_to_download,
  42. base::OnceClosure ready_task) = 0;
  43. // Request that the syncer avoid starting any new tasks and prepare for
  44. // shutdown.
  45. virtual void Stop() = 0;
  46. // The meat and potatoes. All three of the following methods will post a
  47. // delayed task to attempt the actual nudge (see ScheduleNudgeImpl).
  48. //
  49. // NOTE: |desired_delay| is best-effort. If a nudge is already scheduled to
  50. // depart earlier than Now() + delay, the scheduler can and will prefer to
  51. // batch the two so that only one nudge is sent (at the earlier time). Also,
  52. // as always with delayed tasks and timers, it's possible the task gets run
  53. // any time after |desired_delay|.
  54. // The LocalNudge indicates that we've made a local change, and that the
  55. // syncer should plan to commit this to the server some time soon.
  56. virtual void ScheduleLocalNudge(ModelType type) = 0;
  57. // The LocalRefreshRequest occurs when we decide for some reason to manually
  58. // request updates. This should be used sparingly. For example, one of its
  59. // uses is to fetch the latest tab sync data when it's relevant to the UI on
  60. // platforms where tab sync is not registered for invalidations.
  61. virtual void ScheduleLocalRefreshRequest(ModelTypeSet types) = 0;
  62. // Invalidations are notifications the server sends to let us know when other
  63. // clients have committed data. We need to contact the sync server (being
  64. // careful to pass along the "hints" delivered with those invalidations) in
  65. // order to fetch the update.
  66. virtual void ScheduleInvalidationNudge(
  67. ModelType type,
  68. std::unique_ptr<SyncInvalidation> invalidation) = 0;
  69. // Requests a non-blocking initial sync request for the specified type.
  70. //
  71. // Many types can only complete initial sync while the scheduler is in
  72. // configure mode, but a few of them are able to perform their initial sync
  73. // while the scheduler is in normal mode. This non-blocking initial sync
  74. // can be requested through this function.
  75. virtual void ScheduleInitialSyncNudge(ModelType model_type) = 0;
  76. // Change status of notifications in the SyncCycleContext.
  77. virtual void SetNotificationsEnabled(bool notifications_enabled) = 0;
  78. // Called when credentials are updated by the user.
  79. virtual void OnCredentialsUpdated() = 0;
  80. // Called when the network layer detects a connection status change.
  81. virtual void OnConnectionStatusChange(
  82. network::mojom::ConnectionType type) = 0;
  83. };
  84. } // namespace syncer
  85. #endif // COMPONENTS_SYNC_ENGINE_SYNC_SCHEDULER_H_