Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / managed_mode / supervised_user_pref_mapping_service.cc
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/managed_mode/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/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"
13
14 const int kNoAvatar = -1;
15
16 SupervisedUserPrefMappingService::SupervisedUserPrefMappingService(
17     PrefService* prefs,
18     ManagedUserSharedSettingsService* shared_settings)
19     : prefs_(prefs),
20       shared_settings_(shared_settings),
21       managed_user_id_(prefs->GetString(prefs::kManagedUserId)),
22       weak_ptr_factory_(this) {}
23
24 SupervisedUserPrefMappingService::~SupervisedUserPrefMappingService() {}
25
26 void 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(managed_user_id_, managed_users::kChromeAvatarIndex);
43 }
44
45 void SupervisedUserPrefMappingService::OnAvatarChanged() {
46   int new_avatar_index = prefs_->GetInteger(prefs::kProfileAvatarIndex);
47   if (new_avatar_index < 0)
48     return;
49
50   // First check if the avatar index is a new value.
51   if (GetChromeAvatarIndex() == new_avatar_index)
52     return;
53
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));
58 }
59
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)
65     return;
66
67   const base::Value* value = shared_settings_->GetValue(mu_id, key);
68   int avatar_index;
69   bool success = value->GetAsInteger(&avatar_index);
70   DCHECK(success);
71   prefs_->SetInteger(prefs::kProfileAvatarIndex, avatar_index);
72 }
73
74 void SupervisedUserPrefMappingService::Shutdown() {
75   subscription_.reset();
76 }
77
78 int SupervisedUserPrefMappingService::GetChromeAvatarIndex() {
79   const base::Value* value = shared_settings_->GetValue(
80       managed_user_id_, managed_users::kChromeAvatarIndex);
81   if (!value)
82     return kNoAvatar;
83
84   int current_avatar_index;
85   bool success = value->GetAsInteger(&current_avatar_index);
86   DCHECK(success);
87   return current_avatar_index;
88 }