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