1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list_threadsafe.h"
15 #include "components/sync_driver/change_processor.h"
21 namespace browser_sync {
25 class SyncedDeviceTracker : public sync_driver::ChangeProcessor {
27 SyncedDeviceTracker(syncer::UserShare* user_share,
28 const std::string& cache_guid);
29 virtual ~SyncedDeviceTracker();
31 // Observer class for listening to device info changes.
34 virtual void OnDeviceInfoChange() = 0;
37 // ChangeProcessor methods
38 virtual void StartImpl() OVERRIDE;
39 virtual void ApplyChangesFromSyncModel(
40 const syncer::BaseTransaction* trans,
42 const syncer::ImmutableChangeRecordList& changes) OVERRIDE;
43 virtual void CommitChangesFromSyncModel() OVERRIDE;
45 // Methods for accessing and updating the device_info list.
46 // These are virtual for testing.
47 virtual scoped_ptr<DeviceInfo> ReadLocalDeviceInfo(
48 const syncer::BaseTransaction &trans) const;
49 virtual scoped_ptr<DeviceInfo> ReadLocalDeviceInfo() const;
50 virtual void InitLocalDeviceInfo(const std::string& signin_scoped_device_id,
51 const base::Closure& callback);
52 virtual scoped_ptr<DeviceInfo> ReadDeviceInfo(
53 const std::string& client_id) const;
54 virtual void GetAllSyncedDeviceInfo(
55 ScopedVector<DeviceInfo>* device_info) const;
57 virtual std::string cache_guid() const;
59 // Can be called on any thread. Will be notified back on the same thread
60 // they were called on. Observer will be called on every device info
62 void AddObserver(Observer* observer);
63 void RemoveObserver(Observer* observer);
65 // Update |backup_timestamp| in local device info specifics to |backup_time|
67 void UpdateLocalDeviceBackupTime(base::Time backup_time);
69 // Return time derived from |backup_timestamp| in local device info specifics.
70 base::Time GetLocalDeviceBackupTime() const;
73 friend class SyncedDeviceTrackerTest;
75 void InitLocalDeviceInfoContinuation(const base::Closure& callback,
76 const DeviceInfo& local_info);
78 // Helper to write specifics into our node. Also useful for testing.
79 void WriteLocalDeviceInfo(const DeviceInfo& info);
81 // Helper to write arbitrary device info. Useful for writing local device
82 // info and also used by test cases to write arbitrary device infos.
83 void WriteDeviceInfo(const DeviceInfo& info, const std::string& tag);
85 syncer::UserShare* user_share_;
86 const std::string cache_guid_;
87 const std::string local_device_info_tag_;
89 // The |ObserverList| has to be thread safe as the changes happen
90 // on sync thread and the observers could be on any thread.
91 typedef ObserverListThreadSafe<Observer> ObserverList;
92 scoped_refptr<ObserverList> observers_;
94 base::WeakPtrFactory<SyncedDeviceTracker> weak_factory_;
96 DISALLOW_COPY_AND_ASSIGN(SyncedDeviceTracker);
99 } // namespace browser_sync
101 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_