a214fc0482374553bb6fdc4028dcd721a9c4dfc0
[platform/framework/web/crosswalk.git] / src / components / sync_driver / device_info_sync_service.h
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 "base/observer_list.h"
9 #include "components/sync_driver/device_info_tracker.h"
10 #include "sync/api/sync_change_processor.h"
11 #include "sync/api/sync_data.h"
12 #include "sync/api/sync_error_factory.h"
13 #include "sync/api/syncable_service.h"
14
15 namespace sync_driver {
16
17 class LocalDeviceInfoProvider;
18
19 // SyncableService implementation for DEVICE_INFO model type.
20 class DeviceInfoSyncService : public syncer::SyncableService,
21                               public DeviceInfoTracker {
22  public:
23   explicit DeviceInfoSyncService(
24       LocalDeviceInfoProvider* local_device_info_provider);
25   ~DeviceInfoSyncService() override;
26
27   // syncer::SyncableService implementation.
28   syncer::SyncMergeResult MergeDataAndStartSyncing(
29       syncer::ModelType type,
30       const syncer::SyncDataList& initial_sync_data,
31       scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
32       scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
33   void StopSyncing(syncer::ModelType type) override;
34   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
35   syncer::SyncError ProcessSyncChanges(
36       const tracked_objects::Location& from_here,
37       const syncer::SyncChangeList& change_list) override;
38
39   // DeviceInfoTracker implementation.
40   scoped_ptr<DeviceInfo> GetDeviceInfo(
41       const std::string& client_id) const override;
42   ScopedVector<DeviceInfo> GetAllDeviceInfo() const override;
43   void AddObserver(Observer* observer) override;
44   void RemoveObserver(Observer* observer) override;
45
46   // Called to update local device backup time.
47   void UpdateLocalDeviceBackupTime(base::Time backup_time);
48   // Gets the most recently set local device backup time.
49   base::Time GetLocalDeviceBackupTime() const;
50
51  private:
52   // Create SyncData from local DeviceInfo and |local_device_backup_time_|.
53   syncer::SyncData CreateLocalData(const DeviceInfo* info);
54   // Create SyncData from EntitySpecifics.
55   static syncer::SyncData CreateLocalData(
56       const sync_pb::EntitySpecifics& entity);
57
58   // Allocate new DeviceInfo from SyncData.
59   static DeviceInfo* CreateDeviceInfo(const syncer::SyncData sync_data);
60   // Store SyncData in the cache.
61   void StoreSyncData(const std::string& client_id,
62                      const syncer::SyncData& sync_data);
63   // Delete SyncData from the cache.
64   void DeleteSyncData(const std::string& client_id);
65   // Notify all registered observers.
66   void NotifyObservers();
67
68   // Updates backup time in place in |sync_data| if it is different than
69   // the one stored in |local_device_backup_time_|.
70   // Returns true if backup time was updated.
71   bool UpdateBackupTime(syncer::SyncData* sync_data);
72
73   // |local_device_backup_time_| accessors.
74   int64 local_device_backup_time() const { return local_device_backup_time_; }
75   bool has_local_device_backup_time() const {
76     return local_device_backup_time_ >= 0;
77   }
78   void set_local_device_backup_time(int64 value) {
79     local_device_backup_time_ = value;
80   }
81   void clear_local_device_backup_time() { local_device_backup_time_ = -1; }
82
83   // Local device last set backup time (in proto format).
84   // -1 if the value hasn't been specified
85   int64 local_device_backup_time_;
86
87   // |local_device_info_provider_| isn't owned.
88   const LocalDeviceInfoProvider* const local_device_info_provider_;
89
90   // Receives ownership of |sync_processor_| and |error_handler_| in
91   // MergeDataAndStartSyncing() and destroy them in StopSyncing().
92   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
93   scoped_ptr<syncer::SyncErrorFactory> error_handler_;
94
95   // Cache of all syncable and local data.
96   typedef std::map<std::string, syncer::SyncData> SyncDataMap;
97   SyncDataMap all_data_;
98
99   // Registered observers, not owned.
100   ObserverList<Observer, true> observers_;
101
102   DISALLOW_COPY_AND_ASSIGN(DeviceInfoSyncService);
103 };
104
105 }  // namespace sync_driver
106
107 #endif  // COMPONENTS_SYNC_DRIVER_DEVICE_INFO_SYNC_SERVICE_H_