Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / synced_device_tracker.h
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.
4
5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_
7
8 #include <string>
9
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"
16
17 namespace syncer {
18 struct UserShare;
19 }
20
21 namespace browser_sync {
22
23 class DeviceInfo;
24
25 class SyncedDeviceTracker : public sync_driver::ChangeProcessor {
26  public:
27   SyncedDeviceTracker(syncer::UserShare* user_share,
28                       const std::string& cache_guid);
29   virtual ~SyncedDeviceTracker();
30
31   // Observer class for listening to device info changes.
32   class Observer {
33    public:
34     virtual void OnDeviceInfoChange() = 0;
35   };
36
37   // ChangeProcessor methods
38   virtual void StartImpl() OVERRIDE;
39   virtual void ApplyChangesFromSyncModel(
40       const syncer::BaseTransaction* trans,
41       int64 model_version,
42       const syncer::ImmutableChangeRecordList& changes) OVERRIDE;
43   virtual void CommitChangesFromSyncModel() OVERRIDE;
44
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;
56
57   virtual std::string cache_guid() const;
58
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
61   // change.
62   void AddObserver(Observer* observer);
63   void RemoveObserver(Observer* observer);
64
65   // Update |backup_timestamp| in local device info specifics to |backup_time|
66   // if different.
67   void UpdateLocalDeviceBackupTime(base::Time backup_time);
68
69   // Return time derived from |backup_timestamp| in local device info specifics.
70   base::Time GetLocalDeviceBackupTime() const;
71
72  private:
73   friend class SyncedDeviceTrackerTest;
74
75   void InitLocalDeviceInfoContinuation(const base::Closure& callback,
76                                        const DeviceInfo& local_info);
77
78   // Helper to write specifics into our node.  Also useful for testing.
79   void WriteLocalDeviceInfo(const DeviceInfo& info);
80
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);
84
85   syncer::UserShare* user_share_;
86   const std::string cache_guid_;
87   const std::string local_device_info_tag_;
88
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_;
93
94   base::WeakPtrFactory<SyncedDeviceTracker> weak_factory_;
95
96   DISALLOW_COPY_AND_ASSIGN(SyncedDeviceTracker);
97 };
98
99 }  // namespace browser_sync
100
101 #endif  // CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_