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_SERVICE_H_ | 5 #ifndef COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ |
6 #define COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ | 6 #define COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
20 #include "components/sync/api/model_type_service.h" | 20 #include "components/sync/api/model_type_service.h" |
21 #include "components/sync/api/model_type_store.h" | 21 #include "components/sync/api/model_type_store.h" |
22 #include "components/sync/core/simple_metadata_change_list.h" | 22 #include "components/sync/core/simple_metadata_change_list.h" |
23 #include "components/sync/device_info/device_info_tracker.h" | 23 #include "components/sync/device_info/device_info_tracker.h" |
24 #include "components/sync/device_info/local_device_info_provider.h" | 24 #include "components/sync/device_info/local_device_info_provider.h" |
25 | 25 |
26 namespace syncer { | 26 namespace syncer { |
27 class SyncError; | 27 class SyncError; |
28 } // namespace syncer | 28 } // namespace syncer |
29 | 29 |
30 namespace syncer { | 30 namespace syncer_v2 { |
31 class ModelTypeChangeProcessor; | 31 class ModelTypeChangeProcessor; |
32 } // namespace syncer | 32 } // namespace syncer_v2 |
33 | 33 |
34 namespace sync_pb { | 34 namespace sync_pb { |
35 class DeviceInfoSpecifics; | 35 class DeviceInfoSpecifics; |
36 } // namespace sync_pb | 36 } // namespace sync_pb |
37 | 37 |
38 namespace syncer { | 38 namespace sync_driver_v2 { |
39 | 39 |
40 // USS service implementation for DEVICE_INFO model type. Handles storage of | 40 // USS service implementation for DEVICE_INFO model type. Handles storage of |
41 // device info and associated sync metadata, applying/merging foreign changes, | 41 // device info and associated sync metadata, applying/merging foreign changes, |
42 // and allows public read access. | 42 // and allows public read access. |
43 class DeviceInfoService : public ModelTypeService, public DeviceInfoTracker { | 43 class DeviceInfoService : public syncer_v2::ModelTypeService, |
| 44 public sync_driver::DeviceInfoTracker { |
44 public: | 45 public: |
45 typedef base::Callback<void(const ModelTypeStore::InitCallback& callback)> | 46 typedef base::Callback<void( |
| 47 const syncer_v2::ModelTypeStore::InitCallback& callback)> |
46 StoreFactoryFunction; | 48 StoreFactoryFunction; |
47 | 49 |
48 DeviceInfoService(LocalDeviceInfoProvider* local_device_info_provider, | 50 DeviceInfoService( |
49 const StoreFactoryFunction& callback, | 51 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, |
50 const ChangeProcessorFactory& change_processor_factory); | 52 const StoreFactoryFunction& callback, |
| 53 const ChangeProcessorFactory& change_processor_factory); |
51 ~DeviceInfoService() override; | 54 ~DeviceInfoService() override; |
52 | 55 |
53 // ModelTypeService implementation. | 56 // ModelTypeService implementation. |
54 std::unique_ptr<MetadataChangeList> CreateMetadataChangeList() override; | 57 std::unique_ptr<syncer_v2::MetadataChangeList> CreateMetadataChangeList() |
55 SyncError MergeSyncData( | 58 override; |
56 std::unique_ptr<MetadataChangeList> metadata_change_list, | 59 syncer::SyncError MergeSyncData( |
57 EntityDataMap entity_data_map) override; | 60 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_change_list, |
58 SyncError ApplySyncChanges( | 61 syncer_v2::EntityDataMap entity_data_map) override; |
59 std::unique_ptr<MetadataChangeList> metadata_change_list, | 62 syncer::SyncError ApplySyncChanges( |
60 EntityChangeList entity_changes) override; | 63 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_change_list, |
| 64 syncer_v2::EntityChangeList entity_changes) override; |
61 void GetData(StorageKeyList storage_keys, DataCallback callback) override; | 65 void GetData(StorageKeyList storage_keys, DataCallback callback) override; |
62 void GetAllData(DataCallback callback) override; | 66 void GetAllData(DataCallback callback) override; |
63 std::string GetClientTag(const EntityData& entity_data) override; | 67 std::string GetClientTag(const syncer_v2::EntityData& entity_data) override; |
64 std::string GetStorageKey(const EntityData& entity_data) override; | 68 std::string GetStorageKey(const syncer_v2::EntityData& entity_data) override; |
65 void OnChangeProcessorSet() override; | 69 void OnChangeProcessorSet() override; |
66 | 70 |
67 // DeviceInfoTracker implementation. | 71 // DeviceInfoTracker implementation. |
68 bool IsSyncing() const override; | 72 bool IsSyncing() const override; |
69 std::unique_ptr<DeviceInfo> GetDeviceInfo( | 73 std::unique_ptr<sync_driver::DeviceInfo> GetDeviceInfo( |
70 const std::string& client_id) const override; | 74 const std::string& client_id) const override; |
71 std::vector<std::unique_ptr<DeviceInfo>> GetAllDeviceInfo() const override; | 75 std::vector<std::unique_ptr<sync_driver::DeviceInfo>> GetAllDeviceInfo() |
| 76 const override; |
72 void AddObserver(Observer* observer) override; | 77 void AddObserver(Observer* observer) override; |
73 void RemoveObserver(Observer* observer) override; | 78 void RemoveObserver(Observer* observer) override; |
74 int CountActiveDevices() const override; | 79 int CountActiveDevices() const override; |
75 | 80 |
76 private: | 81 private: |
77 friend class DeviceInfoServiceTest; | 82 friend class DeviceInfoServiceTest; |
78 | 83 |
79 // Cache of all syncable and local data, stored by device cache guid. | 84 // Cache of all syncable and local data, stored by device cache guid. |
80 using ClientIdToSpecifics = | 85 using ClientIdToSpecifics = |
81 std::map<std::string, std::unique_ptr<sync_pb::DeviceInfoSpecifics>>; | 86 std::map<std::string, std::unique_ptr<sync_pb::DeviceInfoSpecifics>>; |
82 | 87 |
83 static std::unique_ptr<sync_pb::DeviceInfoSpecifics> CopyToSpecifics( | 88 static std::unique_ptr<sync_pb::DeviceInfoSpecifics> CopyToSpecifics( |
84 const DeviceInfo& info); | 89 const sync_driver::DeviceInfo& info); |
85 | 90 |
86 // Allocate new DeviceInfo from SyncData. | 91 // Allocate new DeviceInfo from SyncData. |
87 static std::unique_ptr<DeviceInfo> CopyToModel( | 92 static std::unique_ptr<sync_driver::DeviceInfo> CopyToModel( |
88 const sync_pb::DeviceInfoSpecifics& specifics); | 93 const sync_pb::DeviceInfoSpecifics& specifics); |
89 // Conversion as we prepare to hand data to the processor. | 94 // Conversion as we prepare to hand data to the processor. |
90 static std::unique_ptr<EntityData> CopyToEntityData( | 95 static std::unique_ptr<syncer_v2::EntityData> CopyToEntityData( |
91 const sync_pb::DeviceInfoSpecifics& specifics); | 96 const sync_pb::DeviceInfoSpecifics& specifics); |
92 | 97 |
93 // Store SyncData in the cache and durable storage. | 98 // Store SyncData in the cache and durable storage. |
94 void StoreSpecifics(std::unique_ptr<sync_pb::DeviceInfoSpecifics> specifics, | 99 void StoreSpecifics(std::unique_ptr<sync_pb::DeviceInfoSpecifics> specifics, |
95 ModelTypeStore::WriteBatch* batch); | 100 syncer_v2::ModelTypeStore::WriteBatch* batch); |
96 // Delete SyncData from the cache and durable storage, returns true if there | 101 // Delete SyncData from the cache and durable storage, returns true if there |
97 // was actually anything at the given tag. | 102 // was actually anything at the given tag. |
98 bool DeleteSpecifics(const std::string& tag, | 103 bool DeleteSpecifics(const std::string& tag, |
99 ModelTypeStore::WriteBatch* batch); | 104 syncer_v2::ModelTypeStore::WriteBatch* batch); |
100 | 105 |
101 // Notify all registered observers. | 106 // Notify all registered observers. |
102 void NotifyObservers(); | 107 void NotifyObservers(); |
103 | 108 |
104 // Used as callback given to LocalDeviceInfoProvider. | 109 // Used as callback given to LocalDeviceInfoProvider. |
105 void OnProviderInitialized(); | 110 void OnProviderInitialized(); |
106 | 111 |
107 // Methods used as callbacks given to DataTypeStore. | 112 // Methods used as callbacks given to DataTypeStore. |
108 void OnStoreCreated(ModelTypeStore::Result result, | 113 void OnStoreCreated(syncer_v2::ModelTypeStore::Result result, |
109 std::unique_ptr<ModelTypeStore> store); | 114 std::unique_ptr<syncer_v2::ModelTypeStore> store); |
110 void OnReadAllData(ModelTypeStore::Result result, | 115 void OnReadAllData( |
111 std::unique_ptr<ModelTypeStore::RecordList> record_list); | 116 syncer_v2::ModelTypeStore::Result result, |
| 117 std::unique_ptr<syncer_v2::ModelTypeStore::RecordList> record_list); |
112 void OnReadAllMetadata( | 118 void OnReadAllMetadata( |
113 ModelTypeStore::Result result, | 119 syncer_v2::ModelTypeStore::Result result, |
114 std::unique_ptr<ModelTypeStore::RecordList> metadata_records, | 120 std::unique_ptr<syncer_v2::ModelTypeStore::RecordList> metadata_records, |
115 const std::string& global_metadata); | 121 const std::string& global_metadata); |
116 void OnCommit(ModelTypeStore::Result result); | 122 void OnCommit(syncer_v2::ModelTypeStore::Result result); |
117 | 123 |
118 // Load metadata if the data is loaded and the provider is initialized. | 124 // Load metadata if the data is loaded and the provider is initialized. |
119 void LoadMetadataIfReady(); | 125 void LoadMetadataIfReady(); |
120 | 126 |
121 // Performs reconciliation between the locally provided device info and the | 127 // Performs reconciliation between the locally provided device info and the |
122 // stored device info data. If the sets of data differ, then we consider this | 128 // stored device info data. If the sets of data differ, then we consider this |
123 // a local change and we send it to the processor. | 129 // a local change and we send it to the processor. |
124 void ReconcileLocalAndStored(); | 130 void ReconcileLocalAndStored(); |
125 | 131 |
126 // Stores the updated version of the local copy of device info in durable | 132 // Stores the updated version of the local copy of device info in durable |
127 // storage, in memory, and informs sync of the change. Should not be called | 133 // storage, in memory, and informs sync of the change. Should not be called |
128 // before the provider and processor have initialized. | 134 // before the provider and processor have initialized. |
129 void SendLocalData(); | 135 void SendLocalData(); |
130 | 136 |
131 // Persists the changes in the given aggregators and notifies observers if | 137 // Persists the changes in the given aggregators and notifies observers if |
132 // indicated to do as such. | 138 // indicated to do as such. |
133 void CommitAndNotify(std::unique_ptr<ModelTypeStore::WriteBatch> batch, | 139 void CommitAndNotify( |
134 std::unique_ptr<MetadataChangeList> metadata_change_list, | 140 std::unique_ptr<syncer_v2::ModelTypeStore::WriteBatch> batch, |
135 bool should_notify); | 141 std::unique_ptr<syncer_v2::MetadataChangeList> metadata_change_list, |
| 142 bool should_notify); |
136 | 143 |
137 // Counts the number of active devices relative to |now|. The activeness of a | 144 // Counts the number of active devices relative to |now|. The activeness of a |
138 // device depends on the amount of time since it was updated, which means | 145 // device depends on the amount of time since it was updated, which means |
139 // comparing it against the current time. |now| is passed into this method to | 146 // comparing it against the current time. |now| is passed into this method to |
140 // allow unit tests to control expected results. | 147 // allow unit tests to control expected results. |
141 int CountActiveDevices(const base::Time now) const; | 148 int CountActiveDevices(const base::Time now) const; |
142 | 149 |
143 // Report an error starting up to sync if it tries to connect to this | 150 // Report an error starting up to sync if it tries to connect to this |
144 // datatype, since these errors prevent us from knowing if sync is enabled. | 151 // datatype, since these errors prevent us from knowing if sync is enabled. |
145 void ReportStartupErrorToSync(const std::string& msg); | 152 void ReportStartupErrorToSync(const std::string& msg); |
146 | 153 |
147 // Find the timestamp for the last time this |device_info| was edited. | 154 // Find the timestamp for the last time this |device_info| was edited. |
148 static base::Time GetLastUpdateTime( | 155 static base::Time GetLastUpdateTime( |
149 const sync_pb::DeviceInfoSpecifics& specifics); | 156 const sync_pb::DeviceInfoSpecifics& specifics); |
150 | 157 |
151 // |local_device_info_provider_| isn't owned. | 158 // |local_device_info_provider_| isn't owned. |
152 const LocalDeviceInfoProvider* const local_device_info_provider_; | 159 const sync_driver::LocalDeviceInfoProvider* const local_device_info_provider_; |
153 | 160 |
154 ClientIdToSpecifics all_data_; | 161 ClientIdToSpecifics all_data_; |
155 | 162 |
156 // Registered observers, not owned. | 163 // Registered observers, not owned. |
157 base::ObserverList<Observer, true> observers_; | 164 base::ObserverList<Observer, true> observers_; |
158 | 165 |
159 // Used to listen for provider initialization. If the provider is already | 166 // Used to listen for provider initialization. If the provider is already |
160 // initialized during our constructor then the subscription is never used. | 167 // initialized during our constructor then the subscription is never used. |
161 std::unique_ptr<LocalDeviceInfoProvider::Subscription> subscription_; | 168 std::unique_ptr<sync_driver::LocalDeviceInfoProvider::Subscription> |
| 169 subscription_; |
162 | 170 |
163 // In charge of actually persiting changes to disk, or loading previous data. | 171 // In charge of actually persiting changes to disk, or loading previous data. |
164 std::unique_ptr<ModelTypeStore> store_; | 172 std::unique_ptr<syncer_v2::ModelTypeStore> store_; |
165 | 173 |
166 // If |local_device_info_provider_| has initialized. | 174 // If |local_device_info_provider_| has initialized. |
167 bool has_provider_initialized_ = false; | 175 bool has_provider_initialized_ = false; |
168 // If data has been loaded from the store. | 176 // If data has been loaded from the store. |
169 bool has_data_loaded_ = false; | 177 bool has_data_loaded_ = false; |
170 // if |change_processor()| has been given metadata. | 178 // if |change_processor()| has been given metadata. |
171 bool has_metadata_loaded_ = false; | 179 bool has_metadata_loaded_ = false; |
172 | 180 |
173 // Used to update our local device info once every pulse interval. | 181 // Used to update our local device info once every pulse interval. |
174 base::OneShotTimer pulse_timer_; | 182 base::OneShotTimer pulse_timer_; |
175 | 183 |
176 DISALLOW_COPY_AND_ASSIGN(DeviceInfoService); | 184 DISALLOW_COPY_AND_ASSIGN(DeviceInfoService); |
177 }; | 185 }; |
178 | 186 |
179 } // namespace syncer | 187 } // namespace sync_driver_v2 |
180 | 188 |
181 #endif // COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ | 189 #endif // COMPONENTS_SYNC_DEVICE_INFO_DEVICE_INFO_SERVICE_H_ |
OLD | NEW |