OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_SYNC_SERVICE_H_ | |
6 #define COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SYNC_SERVICE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <memory> | |
12 #include <string> | |
13 | |
14 #include "base/macros.h" | |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/observer_list.h" | |
17 #include "base/time/time.h" | |
18 #include "base/timer/timer.h" | |
19 #include "components/sync/api/sync_change.h" | |
20 #include "components/sync/api/sync_change_processor.h" | |
21 #include "components/sync/api/sync_data.h" | |
22 #include "components/sync/api/sync_error_factory.h" | |
23 #include "components/sync/api/syncable_service.h" | |
24 #include "components/sync_driver/device_info_tracker.h" | |
25 | |
26 namespace sync_driver { | |
27 | |
28 class LocalDeviceInfoProvider; | |
29 | |
30 // SyncableService implementation for DEVICE_INFO model type. | |
31 class DeviceInfoSyncService : public syncer::SyncableService, | |
32 public DeviceInfoTracker { | |
33 public: | |
34 explicit DeviceInfoSyncService( | |
35 LocalDeviceInfoProvider* local_device_info_provider); | |
36 ~DeviceInfoSyncService() override; | |
37 | |
38 // syncer::SyncableService implementation. | |
39 syncer::SyncMergeResult MergeDataAndStartSyncing( | |
40 syncer::ModelType type, | |
41 const syncer::SyncDataList& initial_sync_data, | |
42 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, | |
43 std::unique_ptr<syncer::SyncErrorFactory> error_handler) override; | |
44 void StopSyncing(syncer::ModelType type) override; | |
45 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override; | |
46 syncer::SyncError ProcessSyncChanges( | |
47 const tracked_objects::Location& from_here, | |
48 const syncer::SyncChangeList& change_list) override; | |
49 | |
50 // DeviceInfoTracker implementation. | |
51 bool IsSyncing() const override; | |
52 std::unique_ptr<DeviceInfo> GetDeviceInfo( | |
53 const std::string& client_id) const override; | |
54 ScopedVector<DeviceInfo> GetAllDeviceInfo() const override; | |
55 void AddObserver(Observer* observer) override; | |
56 void RemoveObserver(Observer* observer) override; | |
57 int CountActiveDevices() const override; | |
58 | |
59 private: | |
60 friend class DeviceInfoSyncServiceTest; | |
61 | |
62 // Create SyncData from local DeviceInfo. | |
63 syncer::SyncData CreateLocalData(const DeviceInfo* info); | |
64 // Create SyncData from EntitySpecifics. | |
65 static syncer::SyncData CreateLocalData( | |
66 const sync_pb::EntitySpecifics& entity); | |
67 | |
68 // Allocate new DeviceInfo from SyncData. | |
69 static DeviceInfo* CreateDeviceInfo(const syncer::SyncData& sync_data); | |
70 // Store SyncData in the cache. | |
71 void StoreSyncData(const std::string& client_id, | |
72 const syncer::SyncData& sync_data); | |
73 // Delete SyncData from the cache. | |
74 void DeleteSyncData(const std::string& client_id); | |
75 // Notify all registered observers. | |
76 void NotifyObservers(); | |
77 | |
78 // Sends a copy of the current device's state to the processor/sync. | |
79 void SendLocalData(const syncer::SyncChange::SyncChangeType change_type); | |
80 | |
81 // Finds the number of active devices give the current time, which allows for | |
82 // better unit tests. | |
83 int CountActiveDevices(const base::Time now) const; | |
84 | |
85 // Find the timestamp for the last time this |device_info| was edited. | |
86 static base::Time GetLastUpdateTime(const syncer::SyncData& device_info); | |
87 | |
88 // |local_device_info_provider_| isn't owned. | |
89 const LocalDeviceInfoProvider* const local_device_info_provider_; | |
90 | |
91 // Receives ownership of |sync_processor_| and |error_handler_| in | |
92 // MergeDataAndStartSyncing() and destroy them in StopSyncing(). | |
93 std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_; | |
94 std::unique_ptr<syncer::SyncErrorFactory> error_handler_; | |
95 | |
96 // Cache of all syncable and local data. | |
97 typedef std::map<std::string, syncer::SyncData> SyncDataMap; | |
98 SyncDataMap all_data_; | |
99 | |
100 // Registered observers, not owned. | |
101 base::ObserverList<Observer, true> observers_; | |
102 | |
103 // Used to update our local device info once every pulse interval. | |
104 base::OneShotTimer pulse_timer_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(DeviceInfoSyncService); | |
107 }; | |
108 | |
109 } // namespace sync_driver | |
110 | |
111 #endif // COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SYNC_SERVICE_H_ | |
OLD | NEW |