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