device_info_sync_bridge.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2015 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_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_
  5. #define COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback_list.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/time/time.h"
  14. #include "base/timer/timer.h"
  15. #include "components/sync/base/sync_mode.h"
  16. #include "components/sync/model/model_error.h"
  17. #include "components/sync/model/model_type_store.h"
  18. #include "components/sync/model/model_type_sync_bridge.h"
  19. #include "components/sync_device_info/device_info_tracker.h"
  20. #include "components/sync_device_info/local_device_info_provider.h"
  21. #include "components/sync_device_info/local_device_info_util.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace sync_pb {
  24. class DeviceInfoSpecifics;
  25. enum SyncEnums_DeviceType : int;
  26. } // namespace sync_pb
  27. namespace syncer {
  28. class DeviceInfoPrefs;
  29. // Sync bridge implementation for DEVICE_INFO model type. Handles storage of
  30. // device info and associated sync metadata, applying/merging foreign changes,
  31. // and allows public read access.
  32. class DeviceInfoSyncBridge : public ModelTypeSyncBridge,
  33. public DeviceInfoTracker {
  34. public:
  35. DeviceInfoSyncBridge(
  36. std::unique_ptr<MutableLocalDeviceInfoProvider>
  37. local_device_info_provider,
  38. OnceModelTypeStoreFactory store_factory,
  39. std::unique_ptr<ModelTypeChangeProcessor> change_processor,
  40. std::unique_ptr<DeviceInfoPrefs> device_info_prefs);
  41. DeviceInfoSyncBridge(const DeviceInfoSyncBridge&) = delete;
  42. DeviceInfoSyncBridge& operator=(const DeviceInfoSyncBridge&) = delete;
  43. ~DeviceInfoSyncBridge() override;
  44. LocalDeviceInfoProvider* GetLocalDeviceInfoProvider();
  45. // Refresh local copy of device info in memory, and informs sync of the
  46. // change. Used when the caller knows a property of local device info has
  47. // changed (e.g. SharingInfo), and must be sync-ed to other devices as soon as
  48. // possible, without waiting for the periodic commits. The device info will be
  49. // compared with the local copy. If the data has been updated, then it will be
  50. // committed. Otherwise nothing happens.
  51. void RefreshLocalDeviceInfoIfNeeded();
  52. // The |callback| will be invoked on each successful commit with newly enabled
  53. // data types list. This is needed to invoke an additional GetUpdates request
  54. // for the data types which have been just enabled and subscribed for new
  55. // invalidations.
  56. void SetCommittedAdditionalInterestedDataTypesCallback(
  57. base::RepeatingCallback<void(const ModelTypeSet&)> callback);
  58. // ModelTypeSyncBridge implementation.
  59. void OnSyncStarting(const DataTypeActivationRequest& request) override;
  60. std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() override;
  61. absl::optional<ModelError> MergeSyncData(
  62. std::unique_ptr<MetadataChangeList> metadata_change_list,
  63. EntityChangeList entity_data) override;
  64. absl::optional<ModelError> ApplySyncChanges(
  65. std::unique_ptr<MetadataChangeList> metadata_change_list,
  66. EntityChangeList entity_changes) override;
  67. void GetData(StorageKeyList storage_keys, DataCallback callback) override;
  68. void GetAllDataForDebugging(DataCallback callback) override;
  69. std::string GetClientTag(const EntityData& entity_data) override;
  70. std::string GetStorageKey(const EntityData& entity_data) override;
  71. void ApplyStopSyncChanges(
  72. std::unique_ptr<MetadataChangeList> delete_metadata_change_list) override;
  73. ModelTypeSyncBridge::CommitAttemptFailedBehavior OnCommitAttemptFailed(
  74. syncer::SyncCommitError commit_error) override;
  75. // DeviceInfoTracker implementation.
  76. bool IsSyncing() const override;
  77. std::unique_ptr<DeviceInfo> GetDeviceInfo(
  78. const std::string& client_id) const override;
  79. std::vector<std::unique_ptr<DeviceInfo>> GetAllDeviceInfo() const override;
  80. void AddObserver(Observer* observer) override;
  81. void RemoveObserver(Observer* observer) override;
  82. std::map<sync_pb::SyncEnums_DeviceType, int> CountActiveDevicesByType()
  83. const override;
  84. bool IsRecentLocalCacheGuid(const std::string& cache_guid) const override;
  85. // For testing only.
  86. bool IsPulseTimerRunningForTest() const;
  87. void ForcePulseForTest() override;
  88. private:
  89. // Cache of all syncable and local data, stored by device cache guid.
  90. using ClientIdToSpecifics =
  91. std::map<std::string, std::unique_ptr<sync_pb::DeviceInfoSpecifics>>;
  92. // Store SyncData in the cache and durable storage.
  93. void StoreSpecifics(std::unique_ptr<sync_pb::DeviceInfoSpecifics> specifics,
  94. ModelTypeStore::WriteBatch* batch);
  95. // Delete SyncData from the cache and durable storage, returns true if there
  96. // was actually anything at the given tag.
  97. bool DeleteSpecifics(const std::string& tag,
  98. ModelTypeStore::WriteBatch* batch);
  99. // Returns the device name based on |sync_mode_|. For transport only mode,
  100. // the device model name is returned. For full sync mode,
  101. // |local_personalizable_device_name_| is returned.
  102. std::string GetLocalClientName() const;
  103. // Notify all registered observers.
  104. void NotifyObservers();
  105. // Methods used as callbacks given to DataTypeStore.
  106. void OnStoreCreated(const absl::optional<syncer::ModelError>& error,
  107. std::unique_ptr<ModelTypeStore> store);
  108. void OnLocalDeviceNameInfoRetrieved(
  109. LocalDeviceNameInfo local_device_name_info);
  110. void OnReadAllData(std::unique_ptr<ClientIdToSpecifics> all_data,
  111. const absl::optional<syncer::ModelError>& error);
  112. void OnSyncInvalidationsInitialized();
  113. void OnReadAllMetadata(const absl::optional<syncer::ModelError>& error,
  114. std::unique_ptr<MetadataBatch> metadata_batch);
  115. void OnCommit(const absl::optional<syncer::ModelError>& error);
  116. // Performs reconciliation between the locally provided device info and the
  117. // stored device info data. If the sets of data differ, then we consider this
  118. // a local change and we send it to the processor. Returns true if the local
  119. // data has been changed and sent to the processor.
  120. bool ReconcileLocalAndStored();
  121. // Stores the updated version of the local copy of device info in durable
  122. // storage, in memory, and informs sync of the change. Must not be called
  123. // before the provider and processor have initialized.
  124. void SendLocalData();
  125. // Same as above but allows callers to specify a WriteBatch
  126. void SendLocalDataWithBatch(
  127. std::unique_ptr<ModelTypeStore::WriteBatch> batch);
  128. // Persists the changes in the given aggregators and notifies observers if
  129. // indicated to do as such.
  130. void CommitAndNotify(std::unique_ptr<ModelTypeStore::WriteBatch> batch,
  131. bool should_notify);
  132. // Deletes locally old data and metadata entries without issuing tombstones.
  133. void ExpireOldEntries();
  134. const std::unique_ptr<MutableLocalDeviceInfoProvider>
  135. local_device_info_provider_;
  136. std::string local_cache_guid_;
  137. ClientIdToSpecifics all_data_;
  138. LocalDeviceNameInfo local_device_name_info_;
  139. absl::optional<SyncMode> sync_mode_;
  140. // Used to restrict reuploads of local device info on incoming tombstones.
  141. // This is necessary to prevent uncontrolled commits based on incoming
  142. // updates.
  143. bool reuploaded_on_tombstone_ = false;
  144. // Registered observers, not owned.
  145. base::ObserverList<Observer, true>::Unchecked observers_;
  146. // In charge of actually persisting changes to disk, or loading previous data.
  147. std::unique_ptr<ModelTypeStore> store_;
  148. // Used to update our local device info once every pulse interval.
  149. base::OneShotTimer pulse_timer_;
  150. // Used to force upload of local device info after initialization. Used in
  151. // tests only.
  152. bool force_reupload_for_test_ = false;
  153. std::vector<base::OnceClosure> device_info_synced_callback_list_;
  154. // Called when a new interested data type list has been committed. Only newly
  155. // enabled data types will be passed. May be empty.
  156. base::RepeatingCallback<void(const ModelTypeSet&)>
  157. new_interested_data_types_callback_;
  158. const std::unique_ptr<DeviceInfoPrefs> device_info_prefs_;
  159. base::WeakPtrFactory<DeviceInfoSyncBridge> weak_ptr_factory_{this};
  160. };
  161. } // namespace syncer
  162. #endif // COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_