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 "chrome/browser/sync/glue/change_processor.h"
21 namespace browser_sync
{
25 class SyncedDeviceTracker
: public 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(Profile
* profile
) 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 base::Closure
& callback
);
51 virtual scoped_ptr
<DeviceInfo
> ReadDeviceInfo(
52 const std::string
& client_id
) const;
53 virtual void GetAllSyncedDeviceInfo(
54 ScopedVector
<DeviceInfo
>* device_info
) const;
56 virtual std::string
cache_guid() const;
58 // Can be called on any thread. Will be notified back on the same thread
59 // they were called on. Observer will be called on every device info
61 void AddObserver(Observer
* observer
);
62 void RemoveObserver(Observer
* observer
);
65 friend class SyncedDeviceTrackerTest
;
67 void InitLocalDeviceInfoContinuation(const base::Closure
& callback
,
68 const DeviceInfo
& local_info
);
70 // Helper to write specifics into our node. Also useful for testing.
71 void WriteLocalDeviceInfo(const DeviceInfo
& info
);
73 // Helper to write arbitrary device info. Useful for writing local device
74 // info and also used by test cases to write arbitrary device infos.
75 void WriteDeviceInfo(const sync_pb::DeviceInfoSpecifics
& specifics
,
76 const std::string
& tag
);
78 syncer::UserShare
* user_share_
;
79 const std::string cache_guid_
;
80 const std::string local_device_info_tag_
;
82 // The |ObserverList| has to be thread safe as the changes happen
83 // on sync thread and the observers could be on any thread.
84 typedef ObserverListThreadSafe
<Observer
> ObserverList
;
85 scoped_refptr
<ObserverList
> observers_
;
87 base::WeakPtrFactory
<SyncedDeviceTracker
> weak_factory_
;
89 DISALLOW_COPY_AND_ASSIGN(SyncedDeviceTracker
);
92 } // namespace browser_sync
94 #endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_