Index: chrome/browser/managed_mode/supervised_user_pref_mapping_service.cc |
diff --git a/chrome/browser/managed_mode/supervised_user_pref_mapping_service.cc b/chrome/browser/managed_mode/supervised_user_pref_mapping_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..69278c44af4f056aada7daacd978e0b9f66bffb6 |
--- /dev/null |
+++ b/chrome/browser/managed_mode/supervised_user_pref_mapping_service.cc |
@@ -0,0 +1,88 @@ |
+// 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/managed_mode/supervised_user_pref_mapping_service.h" |
+ |
+#include "base/bind.h" |
+#include "base/prefs/pref_service.h" |
+#include "base/values.h" |
+#include "chrome/browser/managed_mode/managed_user_constants.h" |
+#include "chrome/browser/managed_mode/managed_user_shared_settings_service.h" |
+#include "chrome/common/pref_names.h" |
+ |
+const int kNoAvatar = -1; |
+ |
+SupervisedUserPrefMappingService::SupervisedUserPrefMappingService( |
+ PrefService* prefs, |
+ ManagedUserSharedSettingsService* shared_settings) |
+ : prefs_(prefs), |
+ shared_settings_(shared_settings), |
+ managed_user_id_(prefs->GetString(prefs::kManagedUserId)), |
+ 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(managed_user_id_, managed_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(managed_user_id_, |
+ managed_users::kChromeAvatarIndex, |
+ base::FundamentalValue(new_avatar_index)); |
+} |
+ |
+void SupervisedUserPrefMappingService::OnSharedSettingChanged( |
+ const std::string& mu_id, |
+ const std::string& key) { |
+ DCHECK_EQ(mu_id, managed_user_id_); |
+ if (key != managed_users::kChromeAvatarIndex) |
+ return; |
+ |
+ const base::Value* value = shared_settings_->GetValue(mu_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( |
+ managed_user_id_, managed_users::kChromeAvatarIndex); |
+ if (!value) |
+ return kNoAvatar; |
+ |
+ int current_avatar_index; |
+ bool success = value->GetAsInteger(¤t_avatar_index); |
+ DCHECK(success); |
+ return current_avatar_index; |
+} |