Add GCMChannelStatusSyncer to schedule requests and enable/disable GCM
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_pref_mapping_service.cc
blobb70ae6334920ecb4b3040889ab0d3c204459eb17
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/supervised_user/supervised_user_pref_mapping_service.h"
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"
14 const int kNoAvatar = -1;
16 SupervisedUserPrefMappingService::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) {}
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) {
40 OnAvatarChanged();
41 } else {
42 OnSharedSettingChanged(supervised_user_id_,
43 supervised_users::kChromeAvatarIndex);
47 void SupervisedUserPrefMappingService::OnAvatarChanged() {
48 int new_avatar_index = prefs_->GetInteger(prefs::kProfileAvatarIndex);
49 if (new_avatar_index < 0)
50 return;
52 // First check if the avatar index is a new value.
53 if (GetChromeAvatarIndex() == new_avatar_index)
54 return;
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));
62 void 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;
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);
76 void SupervisedUserPrefMappingService::Shutdown() {
77 subscription_.reset();
80 int SupervisedUserPrefMappingService::GetChromeAvatarIndex() {
81 const base::Value* value = shared_settings_->GetValue(
82 supervised_user_id_, supervised_users::kChromeAvatarIndex);
83 if (!value)
84 return kNoAvatar;
86 int current_avatar_index;
87 bool success = value->GetAsInteger(&current_avatar_index);
88 DCHECK(success);
89 return current_avatar_index;