This source file includes following definitions.
- weak_ptr_factory_
- Init
- OnAvatarChanged
- OnSharedSettingChanged
- Shutdown
- GetChromeAvatarIndex
#include "chrome/browser/managed_mode/supervised_user_pref_mapping_service.h"
#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/managed_mode/managed_user_constants.h"
#include "chrome/browser/managed_mode/managed_user_shared_settings_service.h"
#include "chrome/common/pref_names.h"
const int kNoAvatar = -1;
SupervisedUserPrefMappingService::SupervisedUserPrefMappingService(
PrefService* prefs,
ManagedUserSharedSettingsService* shared_settings)
: prefs_(prefs),
shared_settings_(shared_settings),
managed_user_id_(prefs->GetString(prefs::kManagedUserId)),
weak_ptr_factory_(this) {}
SupervisedUserPrefMappingService::~SupervisedUserPrefMappingService() {}
void SupervisedUserPrefMappingService::Init() {
subscription_ = shared_settings_->Subscribe(
base::Bind(&SupervisedUserPrefMappingService::OnSharedSettingChanged,
weak_ptr_factory_.GetWeakPtr()));
pref_change_registrar_.Init(prefs_);
pref_change_registrar_.Add(
prefs::kProfileAvatarIndex,
base::Bind(&SupervisedUserPrefMappingService::OnAvatarChanged,
weak_ptr_factory_.GetWeakPtr()));
if (GetChromeAvatarIndex() == kNoAvatar)
OnAvatarChanged();
else
OnSharedSettingChanged(managed_user_id_, managed_users::kChromeAvatarIndex);
}
void SupervisedUserPrefMappingService::OnAvatarChanged() {
int new_avatar_index = prefs_->GetInteger(prefs::kProfileAvatarIndex);
if (new_avatar_index < 0)
return;
if (GetChromeAvatarIndex() == new_avatar_index)
return;
shared_settings_->SetValue(managed_user_id_,
managed_users::kChromeAvatarIndex,
base::FundamentalValue(new_avatar_index));
}
void SupervisedUserPrefMappingService::OnSharedSettingChanged(
const std::string& mu_id,
const std::string& key) {
DCHECK_EQ(mu_id, managed_user_id_);
if (key != managed_users::kChromeAvatarIndex)
return;
const base::Value* value = shared_settings_->GetValue(mu_id, key);
int avatar_index;
bool success = value->GetAsInteger(&avatar_index);
DCHECK(success);
prefs_->SetInteger(prefs::kProfileAvatarIndex, avatar_index);
}
void SupervisedUserPrefMappingService::Shutdown() {
subscription_.reset();
}
int SupervisedUserPrefMappingService::GetChromeAvatarIndex() {
const base::Value* value = shared_settings_->GetValue(
managed_user_id_, managed_users::kChromeAvatarIndex);
if (!value)
return kNoAvatar;
int current_avatar_index;
bool success = value->GetAsInteger(¤t_avatar_index);
DCHECK(success);
return current_avatar_index;
}