syncable_service.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_MODEL_SYNCABLE_SERVICE_H_
  5. #define COMPONENTS_SYNC_MODEL_SYNCABLE_SERVICE_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "components/sync/base/model_type.h"
  10. #include "components/sync/model/model_error.h"
  11. #include "components/sync/model/sync_change.h"
  12. #include "components/sync/model/sync_data.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace syncer {
  15. class SyncChangeProcessor;
  16. class SyncErrorFactory;
  17. // TODO(zea): remove SupportsWeakPtr in favor of having all SyncableService
  18. // implementers provide a way of getting a weak pointer to themselves.
  19. // See crbug.com/100114.
  20. class SyncableService : public base::SupportsWeakPtr<SyncableService> {
  21. public:
  22. SyncableService() = default;
  23. SyncableService(const SyncableService&) = delete;
  24. SyncableService& operator=(const SyncableService&) = delete;
  25. virtual ~SyncableService() = default;
  26. // A StartSyncFlare is useful when your SyncableService has a need for sync
  27. // to start ASAP. This is typically for one of three reasons:
  28. // 1) Because a local change event has occurred but MergeDataAndStartSyncing
  29. // hasn't been called yet, meaning you don't have a SyncChangeProcessor. The
  30. // sync subsystem will respond soon after invoking Run() on your flare by
  31. // calling MergeDataAndStartSyncing.
  32. // 2) You want remote data to be visible immediately; for example if the
  33. // history page is open, you want remote sessions data to be available there.
  34. // 3) You want to signal to sync that it's safe to start now that the
  35. // browser's IO-intensive startup process is over. The ModelType parameter is
  36. // included so that the recieving end can track usage and timing statistics,
  37. // make optimizations or tradeoffs by type, etc.
  38. using StartSyncFlare = base::RepeatingCallback<void(ModelType)>;
  39. // Allows the SyncableService to delay sync events (all below) until the model
  40. // becomes ready to sync. Callers must ensure there is no previous ongoing
  41. // wait (per datatype, if the SyncableService supports multiple).
  42. virtual void WaitUntilReadyToSync(base::OnceClosure done) = 0;
  43. // Informs the service to begin syncing the specified synced datatype |type|.
  44. // The service should then merge |initial_sync_data| into it's local data,
  45. // calling |sync_processor|'s ProcessSyncChanges as necessary to reconcile the
  46. // two. After this, the SyncableService's local data should match the server
  47. // data, and the service should be ready to receive and process any further
  48. // SyncChange's as they occur.
  49. // Returns: absl::nullopt if no error was encountered while merging the two
  50. // models, otherwise a absl::optional filled with such error.
  51. virtual absl::optional<syncer::ModelError> MergeDataAndStartSyncing(
  52. ModelType type,
  53. const SyncDataList& initial_sync_data,
  54. std::unique_ptr<SyncChangeProcessor> sync_processor,
  55. std::unique_ptr<SyncErrorFactory> error_handler) = 0;
  56. // Stop syncing the specified type and reset state.
  57. virtual void StopSyncing(ModelType type) = 0;
  58. // SyncChangeProcessor interface.
  59. // Process a list of new SyncChanges and update the local data as necessary.
  60. // Returns: absl::nullopt if no error was encountered, otherwise a
  61. // absl::optional filled with such error.
  62. virtual absl::optional<ModelError> ProcessSyncChanges(
  63. const base::Location& from_here,
  64. const SyncChangeList& change_list) = 0;
  65. };
  66. } // namespace syncer
  67. #endif // COMPONENTS_SYNC_MODEL_SYNCABLE_SERVICE_H_