Home | History | Annotate | Download | only in avatar
      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 CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_SYNC_OBSERVER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_SYNC_OBSERVER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "chrome/browser/prefs/pref_service_syncable_observer.h"
     13 #include "content/public/browser/notification_observer.h"
     14 
     15 class PrefChangeRegistrar;
     16 class PrefServiceSyncable;
     17 class Profile;
     18 
     19 namespace content {
     20 class NotificationRegistrar;
     21 }
     22 namespace user_prefs {
     23 class PrefRegistrySyncable;
     24 }
     25 
     26 namespace user_manager {
     27 class User;
     28 }
     29 
     30 namespace chromeos {
     31 
     32 // This class is responsible for keeping local user image synced with
     33 // image saved in syncable preference.
     34 class UserImageSyncObserver: public PrefServiceSyncableObserver,
     35                              public content::NotificationObserver {
     36  public:
     37   class Observer {
     38    public:
     39     // Called right after image info synced (i.e. |is_synced| became |true|).
     40     // |local_image_updated| indicates if we desided to update local image in
     41     // result of sync.
     42     virtual void OnInitialSync(bool local_image_updated) = 0;
     43     virtual ~Observer();
     44   };
     45 
     46  public:
     47   explicit UserImageSyncObserver(const user_manager::User* user);
     48   virtual ~UserImageSyncObserver();
     49 
     50   // Register syncable preference for profile.
     51   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     52 
     53   // Returns |true| if sync was initialized and prefs have actual state.
     54   bool is_synced() const { return is_synced_; }
     55 
     56   // Adds |observer| into observers list.
     57   void AddObserver(Observer* observer);
     58   // Removes |observer| from observers list.
     59   void RemoveObserver(Observer* observer);
     60 
     61  private:
     62   // PrefServiceSyncableObserver implementation.
     63   virtual void OnIsSyncingChanged() OVERRIDE;
     64 
     65   // content::NotificationObserver implementation.
     66   virtual void Observe(int type,
     67                        const content::NotificationSource& source,
     68                        const content::NotificationDetails& details) OVERRIDE;
     69 
     70   // Called after user profile was loaded.
     71   void OnProfileGained(Profile* profile);
     72 
     73   // Called when sync servise started it's work and we are able to sync needed
     74   // preferences.
     75   void OnInitialSync();
     76 
     77   // Called when preference |pref_name| was changed.j
     78   void OnPreferenceChanged(const std::string& pref_name);
     79 
     80   // Saves local image preferences to sync.
     81   void UpdateSyncedImageFromLocal();
     82 
     83   // Saves sync preferences to local state and updates user image.
     84   void UpdateLocalImageFromSynced();
     85 
     86   // Gets synced image index. Returns false if user has no needed preferences.
     87   bool GetSyncedImageIndex(int* result);
     88 
     89   // If it is allowed to change user image now.
     90   bool CanUpdateLocalImageNow();
     91 
     92   const user_manager::User* user_;
     93   scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
     94   scoped_ptr<content::NotificationRegistrar> notification_registrar_;
     95   PrefServiceSyncable* prefs_;
     96   bool is_synced_;
     97   // Indicates if local user image changed during initialization.
     98   bool local_image_changed_;
     99   ObserverList<Observer> observer_list_;
    100 };
    101 
    102 }  // namespace chromeos
    103 
    104 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_SYNC_OBSERVER_H_
    105 
    106