Home | History | Annotate | Download | only in glue
      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 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 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;
     55 
     56   virtual std::string cache_guid() const;
     57 
     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
     60   // change.
     61   void AddObserver(Observer* observer);
     62   void RemoveObserver(Observer* observer);
     63 
     64  private:
     65   friend class SyncedDeviceTrackerTest;
     66 
     67   void InitLocalDeviceInfoContinuation(const base::Closure& callback,
     68                                        const DeviceInfo& local_info);
     69 
     70   // Helper to write specifics into our node.  Also useful for testing.
     71   void WriteLocalDeviceInfo(const DeviceInfo& info);
     72 
     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);
     77 
     78   syncer::UserShare* user_share_;
     79   const std::string cache_guid_;
     80   const std::string local_device_info_tag_;
     81 
     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_;
     86 
     87   base::WeakPtrFactory<SyncedDeviceTracker> weak_factory_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(SyncedDeviceTracker);
     90 };
     91 
     92 }  // namespace browser_sync
     93 
     94 #endif  // CHROME_BROWSER_SYNC_GLUE_SYNCED_DEVICE_TRACKER_H_
     95