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