1 // Copyright 2013 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_MANAGED_MODE_MANAGED_USER_SYNC_SERVICE_H_
6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SYNC_SERVICE_H_
10 #include "base/callback_forward.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "chrome/browser/managed_mode/managed_user_sync_service_observer.h"
15 #include "chrome/browser/managed_mode/managed_users.h"
16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
17 #include "sync/api/syncable_service.h"
20 class DictionaryValue
;
23 namespace user_prefs
{
24 class PrefRegistrySyncable
;
29 class ManagedUserSyncService
: public BrowserContextKeyedService
,
30 public syncer::SyncableService
{
32 // For use with GetManagedUsersAsync() below.
33 typedef base::Callback
<void(const base::DictionaryValue
*)>
36 // Dictionary keys for entry values of |prefs::kManagedUsers|.
37 static const char kAcknowledged
[];
38 static const char kChromeAvatar
[];
39 static const char kChromeOsAvatar
[];
40 static const char kMasterKey
[];
41 static const char kName
[];
43 // Represents a non-existing avatar on Chrome and Chrome OS.
44 static const int kNoAvatar
;
46 virtual ~ManagedUserSyncService();
48 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
50 // Extracts the avatar index from the input |avatar_str| and set
51 // |avatar_index| to hold the extracted value. Returns true if the
52 // index was extracted successfully and false otherwise.
53 // |avatar_str| should have the format: "chrome-avatar-index:INDEX"
54 // where INDEX is the integer to be extracted. |avatar_str| can be empty
55 // in case there is no avatar synced for a managed user in which case
56 // |avatar_index| is set to -1.
57 static bool GetAvatarIndex(const std::string
& avatar_str
,
60 // Given an |avatar_index|, it returns a string of the form:
61 // "chrome-avatar-index:INDEX" where INDEX = |avatar_index|.
62 // It is exposed for testing purposes only.
63 static std::string
BuildAvatarString(int avatar_index
);
65 void AddObserver(ManagedUserSyncServiceObserver
* observer
);
66 void RemoveObserver(ManagedUserSyncServiceObserver
* observer
);
68 void AddManagedUser(const std::string
& id
,
69 const std::string
& name
,
70 const std::string
& master_key
,
72 void DeleteManagedUser(const std::string
& id
);
74 // Updates the managed user avatar only if the managed user has
75 // no avatar and |avatar_index| is set to some value other than
76 // |kNoAvatar|. If |avatar_index| equals |kNoAvatar| and the
77 // managed user has an avatar, it will be cleared. However,
78 // to clear an avatar call the convenience method |ClearManagedUserAvatar()|.
79 // Returns true if the avatar value is changed (either updated or cleared)
80 // and false otherwise.
81 bool UpdateManagedUserAvatarIfNeeded(const std::string
& id
, int avatar_index
);
82 void ClearManagedUserAvatar(const std::string
& id
);
84 // Returns a dictionary containing all managed users managed by this
85 // custodian. This method should only be called once this service has started
86 // syncing managed users (i.e. has finished its initial merge of local and
87 // server-side data, via MergeDataAndStartSyncing), as the stored data might
88 // be outdated before that.
89 const base::DictionaryValue
* GetManagedUsers();
91 // Calls the passed |callback| with a dictionary containing all managed users
92 // managed by this custodian.
93 void GetManagedUsersAsync(const ManagedUsersCallback
& callback
);
95 // BrowserContextKeyedService implementation:
96 virtual void Shutdown() OVERRIDE
;
98 // SyncableService implementation:
99 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
100 syncer::ModelType type
,
101 const syncer::SyncDataList
& initial_sync_data
,
102 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
103 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) OVERRIDE
;
104 virtual void StopSyncing(syncer::ModelType type
) OVERRIDE
;
105 virtual syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const
107 virtual syncer::SyncError
ProcessSyncChanges(
108 const tracked_objects::Location
& from_here
,
109 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
112 friend class ManagedUserSyncServiceFactory
;
114 // Use |ManagedUserSyncServiceFactory::GetForProfile(...)| to get an
115 // instance of this service.
116 explicit ManagedUserSyncService(PrefService
* prefs
);
118 void OnLastSignedInUsernameChange();
120 void NotifyManagedUserAcknowledged(const std::string
& managed_user_id
);
121 void NotifyManagedUsersSyncingStopped();
123 void DispatchCallbacks();
126 PrefChangeRegistrar pref_change_registrar_
;
128 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
129 scoped_ptr
<syncer::SyncErrorFactory
> error_handler_
;
131 ObserverList
<ManagedUserSyncServiceObserver
> observers_
;
133 std::vector
<ManagedUsersCallback
> callbacks_
;
135 DISALLOW_COPY_AND_ASSIGN(ManagedUserSyncService
);
138 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SYNC_SERVICE_H_