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