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