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. 4 5 #ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SYNC_SERVICE_H_ 6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SYNC_SERVICE_H_ 7 8 #include <vector> 9 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" 18 19 namespace base { 20 class DictionaryValue; 21 } 22 23 namespace user_prefs { 24 class PrefRegistrySyncable; 25 } 26 27 class PrefService; 28 29 class ManagedUserSyncService : public BrowserContextKeyedService, 30 public syncer::SyncableService { 31 public: 32 // For use with GetManagedUsersAsync() below. 33 typedef base::Callback<void(const base::DictionaryValue*)> 34 ManagedUsersCallback; 35 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[]; 42 43 // Represents a non-existing avatar on Chrome and Chrome OS. 44 static const int kNoAvatar; 45 46 virtual ~ManagedUserSyncService(); 47 48 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 49 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, 58 int* avatar_index); 59 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); 64 65 void AddObserver(ManagedUserSyncServiceObserver* observer); 66 void RemoveObserver(ManagedUserSyncServiceObserver* observer); 67 68 void AddManagedUser(const std::string& id, 69 const std::string& name, 70 const std::string& master_key, 71 int avatar_index); 72 void DeleteManagedUser(const std::string& id); 73 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); 83 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(); 90 91 // Calls the passed |callback| with a dictionary containing all managed users 92 // managed by this custodian. 93 void GetManagedUsersAsync(const ManagedUsersCallback& callback); 94 95 // BrowserContextKeyedService implementation: 96 virtual void Shutdown() OVERRIDE; 97 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 106 OVERRIDE; 107 virtual syncer::SyncError ProcessSyncChanges( 108 const tracked_objects::Location& from_here, 109 const syncer::SyncChangeList& change_list) OVERRIDE; 110 111 private: 112 friend class ManagedUserSyncServiceFactory; 113 114 // Use |ManagedUserSyncServiceFactory::GetForProfile(...)| to get an 115 // instance of this service. 116 explicit ManagedUserSyncService(PrefService* prefs); 117 118 void OnLastSignedInUsernameChange(); 119 120 void NotifyManagedUserAcknowledged(const std::string& managed_user_id); 121 void NotifyManagedUsersSyncingStopped(); 122 123 void DispatchCallbacks(); 124 125 PrefService* prefs_; 126 PrefChangeRegistrar pref_change_registrar_; 127 128 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; 129 scoped_ptr<syncer::SyncErrorFactory> error_handler_; 130 131 ObserverList<ManagedUserSyncServiceObserver> observers_; 132 133 std::vector<ManagedUsersCallback> callbacks_; 134 135 DISALLOW_COPY_AND_ASSIGN(ManagedUserSyncService); 136 }; 137 138 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_SYNC_SERVICE_H_ 139