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 #include "chrome/browser/managed_mode/supervised_user_pref_mapping_service.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/managed_mode/managed_user_constants.h"
11 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h"
12 #include "chrome/common/pref_names.h"
14 const int kNoAvatar
= -1;
16 SupervisedUserPrefMappingService::SupervisedUserPrefMappingService(
18 ManagedUserSharedSettingsService
* shared_settings
)
20 shared_settings_(shared_settings
),
21 managed_user_id_(prefs
->GetString(prefs::kManagedUserId
)),
22 weak_ptr_factory_(this) {}
24 SupervisedUserPrefMappingService::~SupervisedUserPrefMappingService() {}
26 void SupervisedUserPrefMappingService::Init() {
27 subscription_
= shared_settings_
->Subscribe(
28 base::Bind(&SupervisedUserPrefMappingService::OnSharedSettingChanged
,
29 weak_ptr_factory_
.GetWeakPtr()));
31 pref_change_registrar_
.Init(prefs_
);
32 pref_change_registrar_
.Add(
33 prefs::kProfileAvatarIndex
,
34 base::Bind(&SupervisedUserPrefMappingService::OnAvatarChanged
,
35 weak_ptr_factory_
.GetWeakPtr()));
37 // Check if we need to update the shared setting with the avatar index.
38 // Otherwise we update the user pref in case we missed a notification.
39 if (GetChromeAvatarIndex() == kNoAvatar
)
42 OnSharedSettingChanged(managed_user_id_
, managed_users::kChromeAvatarIndex
);
45 void SupervisedUserPrefMappingService::OnAvatarChanged() {
46 int new_avatar_index
= prefs_
->GetInteger(prefs::kProfileAvatarIndex
);
47 if (new_avatar_index
< 0)
50 // First check if the avatar index is a new value.
51 if (GetChromeAvatarIndex() == new_avatar_index
)
54 // If yes, update the shared settings value.
55 shared_settings_
->SetValue(managed_user_id_
,
56 managed_users::kChromeAvatarIndex
,
57 base::FundamentalValue(new_avatar_index
));
60 void SupervisedUserPrefMappingService::OnSharedSettingChanged(
61 const std::string
& mu_id
,
62 const std::string
& key
) {
63 DCHECK_EQ(mu_id
, managed_user_id_
);
64 if (key
!= managed_users::kChromeAvatarIndex
)
67 const base::Value
* value
= shared_settings_
->GetValue(mu_id
, key
);
69 bool success
= value
->GetAsInteger(&avatar_index
);
71 prefs_
->SetInteger(prefs::kProfileAvatarIndex
, avatar_index
);
74 void SupervisedUserPrefMappingService::Shutdown() {
75 subscription_
.reset();
78 int SupervisedUserPrefMappingService::GetChromeAvatarIndex() {
79 const base::Value
* value
= shared_settings_
->GetValue(
80 managed_user_id_
, managed_users::kChromeAvatarIndex
);
84 int current_avatar_index
;
85 bool success
= value
->GetAsInteger(¤t_avatar_index
);
87 return current_avatar_index
;