summaryrefslogtreecommitdiffstats
path: root/chrome/browser/supervised_user/supervised_user_pref_mapping_service.cc
blob: b70ae6334920ecb4b3040889ab0d3c204459eb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/supervised_user/supervised_user_pref_mapping_service.h"

#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/supervised_user/supervised_user_constants.h"
#include "chrome/browser/supervised_user/supervised_user_shared_settings_service.h"
#include "chrome/common/pref_names.h"

const int kNoAvatar = -1;

SupervisedUserPrefMappingService::SupervisedUserPrefMappingService(
    PrefService* prefs,
    SupervisedUserSharedSettingsService* shared_settings)
    : prefs_(prefs),
      shared_settings_(shared_settings),
      supervised_user_id_(prefs->GetString(prefs::kSupervisedUserId)),
      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()));

  // Check if we need to update the shared setting with the avatar index.
  // Otherwise we update the user pref in case we missed a notification.
  if (GetChromeAvatarIndex() == kNoAvatar) {
    OnAvatarChanged();
  } else {
    OnSharedSettingChanged(supervised_user_id_,
                           supervised_users::kChromeAvatarIndex);
  }
}

void SupervisedUserPrefMappingService::OnAvatarChanged() {
  int new_avatar_index = prefs_->GetInteger(prefs::kProfileAvatarIndex);
  if (new_avatar_index < 0)
    return;

  // First check if the avatar index is a new value.
  if (GetChromeAvatarIndex() == new_avatar_index)
    return;

  // If yes, update the shared settings value.
  shared_settings_->SetValue(supervised_user_id_,
                             supervised_users::kChromeAvatarIndex,
                             base::FundamentalValue(new_avatar_index));
}

void SupervisedUserPrefMappingService::OnSharedSettingChanged(
    const std::string& su_id,
    const std::string& key) {
  DCHECK_EQ(su_id, supervised_user_id_);
  if (key != supervised_users::kChromeAvatarIndex)
    return;

  const base::Value* value = shared_settings_->GetValue(su_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(
      supervised_user_id_, supervised_users::kChromeAvatarIndex);
  if (!value)
    return kNoAvatar;

  int current_avatar_index;
  bool success = value->GetAsInteger(&current_avatar_index);
  DCHECK(success);
  return current_avatar_index;
}