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.
4
5#include "chrome/browser/supervised_user/supervised_user_pref_mapping_service.h"
6
7#include "base/bind.h"
8#include "base/prefs/pref_service.h"
9#include "base/values.h"
10#include "chrome/browser/supervised_user/supervised_user_constants.h"
11#include "chrome/browser/supervised_user/supervised_user_shared_settings_service.h"
12#include "chrome/common/pref_names.h"
13
14const int kNoAvatar = -1;
15
16SupervisedUserPrefMappingService::SupervisedUserPrefMappingService(
17    PrefService* prefs,
18    SupervisedUserSharedSettingsService* shared_settings)
19    : prefs_(prefs),
20      shared_settings_(shared_settings),
21      supervised_user_id_(prefs->GetString(prefs::kSupervisedUserId)),
22      weak_ptr_factory_(this) {}
23
24SupervisedUserPrefMappingService::~SupervisedUserPrefMappingService() {}
25
26void SupervisedUserPrefMappingService::Init() {
27  subscription_ = shared_settings_->Subscribe(
28      base::Bind(&SupervisedUserPrefMappingService::OnSharedSettingChanged,
29                 weak_ptr_factory_.GetWeakPtr()));
30
31  pref_change_registrar_.Init(prefs_);
32  pref_change_registrar_.Add(
33      prefs::kProfileAvatarIndex,
34      base::Bind(&SupervisedUserPrefMappingService::OnAvatarChanged,
35                 weak_ptr_factory_.GetWeakPtr()));
36
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) {
40    OnAvatarChanged();
41  } else {
42    OnSharedSettingChanged(supervised_user_id_,
43                           supervised_users::kChromeAvatarIndex);
44  }
45}
46
47void SupervisedUserPrefMappingService::OnAvatarChanged() {
48  int new_avatar_index = prefs_->GetInteger(prefs::kProfileAvatarIndex);
49  if (new_avatar_index < 0)
50    return;
51
52  // First check if the avatar index is a new value.
53  if (GetChromeAvatarIndex() == new_avatar_index)
54    return;
55
56  // If yes, update the shared settings value.
57  shared_settings_->SetValue(supervised_user_id_,
58                             supervised_users::kChromeAvatarIndex,
59                             base::FundamentalValue(new_avatar_index));
60}
61
62void SupervisedUserPrefMappingService::OnSharedSettingChanged(
63    const std::string& su_id,
64    const std::string& key) {
65  DCHECK_EQ(su_id, supervised_user_id_);
66  if (key != supervised_users::kChromeAvatarIndex)
67    return;
68
69  const base::Value* value = shared_settings_->GetValue(su_id, key);
70  int avatar_index;
71  bool success = value->GetAsInteger(&avatar_index);
72  DCHECK(success);
73  prefs_->SetInteger(prefs::kProfileAvatarIndex, avatar_index);
74}
75
76void SupervisedUserPrefMappingService::Shutdown() {
77  subscription_.reset();
78}
79
80int SupervisedUserPrefMappingService::GetChromeAvatarIndex() {
81  const base::Value* value = shared_settings_->GetValue(
82      supervised_user_id_, supervised_users::kChromeAvatarIndex);
83  if (!value)
84    return kNoAvatar;
85
86  int current_avatar_index;
87  bool success = value->GetAsInteger(&current_avatar_index);
88  DCHECK(success);
89  return current_avatar_index;
90}
91