| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_ | 5 #ifndef COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_ |
| 6 #define COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_ | 6 #define COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 18 #include "components/sync/device_info/device_info_tracker.h" | 18 #include "components/sync/device_info/device_info_tracker.h" |
| 19 #include "components/sync/device_info/local_device_info_provider.h" | 19 #include "components/sync/device_info/local_device_info_provider.h" |
| 20 #include "components/sync/model/model_type_store.h" | 20 #include "components/sync/model/model_type_store.h" |
| 21 #include "components/sync/model/model_type_sync_bridge.h" | 21 #include "components/sync/model/model_type_sync_bridge.h" |
| 22 | 22 |
| 23 namespace sync_pb { | 23 namespace sync_pb { |
| 24 class DeviceInfoSpecifics; | 24 class DeviceInfoSpecifics; |
| 25 } // namespace sync_pb | 25 } // namespace sync_pb |
| 26 | 26 |
| 27 namespace syncer { | 27 namespace syncer { |
| 28 | 28 |
| 29 class SyncError; | |
| 30 | |
| 31 // Sync bridge implementation for DEVICE_INFO model type. Handles storage of | 29 // Sync bridge implementation for DEVICE_INFO model type. Handles storage of |
| 32 // device info and associated sync metadata, applying/merging foreign changes, | 30 // device info and associated sync metadata, applying/merging foreign changes, |
| 33 // and allows public read access. | 31 // and allows public read access. |
| 34 class DeviceInfoSyncBridge : public ModelTypeSyncBridge, | 32 class DeviceInfoSyncBridge : public ModelTypeSyncBridge, |
| 35 public DeviceInfoTracker { | 33 public DeviceInfoTracker { |
| 36 public: | 34 public: |
| 37 typedef base::Callback<void(const ModelTypeStore::InitCallback& callback)> | 35 typedef base::Callback<void(const ModelTypeStore::InitCallback& callback)> |
| 38 StoreFactoryFunction; | 36 StoreFactoryFunction; |
| 39 | 37 |
| 40 DeviceInfoSyncBridge(LocalDeviceInfoProvider* local_device_info_provider, | 38 DeviceInfoSyncBridge(LocalDeviceInfoProvider* local_device_info_provider, |
| 41 const StoreFactoryFunction& callback, | 39 const StoreFactoryFunction& callback, |
| 42 const ChangeProcessorFactory& change_processor_factory); | 40 const ChangeProcessorFactory& change_processor_factory); |
| 43 ~DeviceInfoSyncBridge() override; | 41 ~DeviceInfoSyncBridge() override; |
| 44 | 42 |
| 45 // ModelTypeSyncBridge implementation. | 43 // ModelTypeSyncBridge implementation. |
| 46 std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() override; | 44 std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() override; |
| 47 SyncError MergeSyncData( | 45 ModelError MergeSyncData( |
| 48 std::unique_ptr<MetadataChangeList> metadata_change_list, | 46 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 49 EntityDataMap entity_data_map) override; | 47 EntityDataMap entity_data_map) override; |
| 50 SyncError ApplySyncChanges( | 48 ModelError ApplySyncChanges( |
| 51 std::unique_ptr<MetadataChangeList> metadata_change_list, | 49 std::unique_ptr<MetadataChangeList> metadata_change_list, |
| 52 EntityChangeList entity_changes) override; | 50 EntityChangeList entity_changes) override; |
| 53 void GetData(StorageKeyList storage_keys, DataCallback callback) override; | 51 void GetData(StorageKeyList storage_keys, DataCallback callback) override; |
| 54 void GetAllData(DataCallback callback) override; | 52 void GetAllData(DataCallback callback) override; |
| 55 std::string GetClientTag(const EntityData& entity_data) override; | 53 std::string GetClientTag(const EntityData& entity_data) override; |
| 56 std::string GetStorageKey(const EntityData& entity_data) override; | 54 std::string GetStorageKey(const EntityData& entity_data) override; |
| 57 void DisableSync() override; | 55 void DisableSync() override; |
| 58 | 56 |
| 59 // DeviceInfoTracker implementation. | 57 // DeviceInfoTracker implementation. |
| 60 bool IsSyncing() const override; | 58 bool IsSyncing() const override; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 84 void NotifyObservers(); | 82 void NotifyObservers(); |
| 85 | 83 |
| 86 // Used as callback given to LocalDeviceInfoProvider. | 84 // Used as callback given to LocalDeviceInfoProvider. |
| 87 void OnProviderInitialized(); | 85 void OnProviderInitialized(); |
| 88 | 86 |
| 89 // Methods used as callbacks given to DataTypeStore. | 87 // Methods used as callbacks given to DataTypeStore. |
| 90 void OnStoreCreated(ModelTypeStore::Result result, | 88 void OnStoreCreated(ModelTypeStore::Result result, |
| 91 std::unique_ptr<ModelTypeStore> store); | 89 std::unique_ptr<ModelTypeStore> store); |
| 92 void OnReadAllData(ModelTypeStore::Result result, | 90 void OnReadAllData(ModelTypeStore::Result result, |
| 93 std::unique_ptr<ModelTypeStore::RecordList> record_list); | 91 std::unique_ptr<ModelTypeStore::RecordList> record_list); |
| 94 void OnReadAllMetadata(SyncError error, | 92 void OnReadAllMetadata(ModelError error, |
| 95 std::unique_ptr<MetadataBatch> metadata_batch); | 93 std::unique_ptr<MetadataBatch> metadata_batch); |
| 96 void OnCommit(ModelTypeStore::Result result); | 94 void OnCommit(ModelTypeStore::Result result); |
| 97 | 95 |
| 98 // Load metadata if the data is loaded and the provider is initialized. | 96 // Load metadata if the data is loaded and the provider is initialized. |
| 99 void LoadMetadataIfReady(); | 97 void LoadMetadataIfReady(); |
| 100 | 98 |
| 101 // Performs reconciliation between the locally provided device info and the | 99 // Performs reconciliation between the locally provided device info and the |
| 102 // stored device info data. If the sets of data differ, then we consider this | 100 // stored device info data. If the sets of data differ, then we consider this |
| 103 // a local change and we send it to the processor. | 101 // a local change and we send it to the processor. |
| 104 void ReconcileLocalAndStored(); | 102 void ReconcileLocalAndStored(); |
| 105 | 103 |
| 106 // Stores the updated version of the local copy of device info in durable | 104 // Stores the updated version of the local copy of device info in durable |
| 107 // storage, in memory, and informs sync of the change. Should not be called | 105 // storage, in memory, and informs sync of the change. Should not be called |
| 108 // before the provider and processor have initialized. | 106 // before the provider and processor have initialized. |
| 109 void SendLocalData(); | 107 void SendLocalData(); |
| 110 | 108 |
| 111 // Persists the changes in the given aggregators and notifies observers if | 109 // Persists the changes in the given aggregators and notifies observers if |
| 112 // indicated to do as such. | 110 // indicated to do as such. |
| 113 void CommitAndNotify(std::unique_ptr<ModelTypeStore::WriteBatch> batch, | 111 void CommitAndNotify(std::unique_ptr<ModelTypeStore::WriteBatch> batch, |
| 114 bool should_notify); | 112 bool should_notify); |
| 115 | 113 |
| 116 // Counts the number of active devices relative to |now|. The activeness of a | 114 // Counts the number of active devices relative to |now|. The activeness of a |
| 117 // device depends on the amount of time since it was updated, which means | 115 // device depends on the amount of time since it was updated, which means |
| 118 // comparing it against the current time. |now| is passed into this method to | 116 // comparing it against the current time. |now| is passed into this method to |
| 119 // allow unit tests to control expected results. | 117 // allow unit tests to control expected results. |
| 120 int CountActiveDevices(const base::Time now) const; | 118 int CountActiveDevices(const base::Time now) const; |
| 121 | 119 |
| 122 // Report an error starting up to sync if it tries to connect to this | |
| 123 // datatype, since these errors prevent us from knowing if sync is enabled. | |
| 124 void ReportStartupErrorToSync(const std::string& msg); | |
| 125 | |
| 126 // |local_device_info_provider_| isn't owned. | 120 // |local_device_info_provider_| isn't owned. |
| 127 const LocalDeviceInfoProvider* const local_device_info_provider_; | 121 const LocalDeviceInfoProvider* const local_device_info_provider_; |
| 128 | 122 |
| 129 ClientIdToSpecifics all_data_; | 123 ClientIdToSpecifics all_data_; |
| 130 | 124 |
| 131 // Registered observers, not owned. | 125 // Registered observers, not owned. |
| 132 base::ObserverList<Observer, true> observers_; | 126 base::ObserverList<Observer, true> observers_; |
| 133 | 127 |
| 134 // Used to listen for provider initialization. If the provider is already | 128 // Used to listen for provider initialization. If the provider is already |
| 135 // initialized during our constructor then the subscription is never used. | 129 // initialized during our constructor then the subscription is never used. |
| 136 std::unique_ptr<LocalDeviceInfoProvider::Subscription> subscription_; | 130 std::unique_ptr<LocalDeviceInfoProvider::Subscription> subscription_; |
| 137 | 131 |
| 138 // In charge of actually persiting changes to disk, or loading previous data. | 132 // In charge of actually persiting changes to disk, or loading previous data. |
| 139 std::unique_ptr<ModelTypeStore> store_; | 133 std::unique_ptr<ModelTypeStore> store_; |
| 140 | 134 |
| 141 // If |local_device_info_provider_| has initialized. | 135 // If |local_device_info_provider_| has initialized. |
| 142 bool has_provider_initialized_ = false; | 136 bool has_provider_initialized_ = false; |
| 143 // If data has been loaded from the store. | 137 // If data has been loaded from the store. |
| 144 bool has_data_loaded_ = false; | 138 bool has_data_loaded_ = false; |
| 145 | 139 |
| 146 // Used to update our local device info once every pulse interval. | 140 // Used to update our local device info once every pulse interval. |
| 147 base::OneShotTimer pulse_timer_; | 141 base::OneShotTimer pulse_timer_; |
| 148 | 142 |
| 149 DISALLOW_COPY_AND_ASSIGN(DeviceInfoSyncBridge); | 143 DISALLOW_COPY_AND_ASSIGN(DeviceInfoSyncBridge); |
| 150 }; | 144 }; |
| 151 | 145 |
| 152 } // namespace syncer | 146 } // namespace syncer |
| 153 | 147 |
| 154 #endif // COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_ | 148 #endif // COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SYNC_BRIDGE_H_ |
| OLD | NEW |