Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 namespace { | |
| 15 | |
| 16 const int kNoAvatar = -1; | |
|
Bernhard Bauer
2014/02/07 09:54:24
Nit: const variables automatically get internal li
Adrian Kuegel
2014/02/07 10:24:47
Done.
| |
| 17 | |
| 18 } | |
| 19 | |
| 20 SupervisedUserPrefMappingService::SupervisedUserPrefMappingService( | |
| 21 PrefService* prefs, | |
| 22 ManagedUserSharedSettingsService* shared_settings) | |
| 23 : prefs_(prefs), | |
| 24 shared_settings_(shared_settings), | |
| 25 managed_user_id_(prefs->GetString(prefs::kManagedUserId)), | |
| 26 weak_ptr_factory_(this) {} | |
| 27 | |
| 28 SupervisedUserPrefMappingService::~SupervisedUserPrefMappingService() {} | |
| 29 | |
| 30 void SupervisedUserPrefMappingService::Init() { | |
| 31 subscription_ = shared_settings_->Subscribe( | |
| 32 base::Bind(&SupervisedUserPrefMappingService::OnSharedSettingChanged, | |
| 33 weak_ptr_factory_.GetWeakPtr())); | |
| 34 | |
| 35 pref_change_registrar_.Init(prefs_); | |
| 36 pref_change_registrar_.Add( | |
| 37 prefs::kProfileAvatarIndex, | |
| 38 base::Bind(&SupervisedUserPrefMappingService::OnAvatarChanged, | |
| 39 weak_ptr_factory_.GetWeakPtr())); | |
| 40 | |
| 41 // Check if we need to update the shared setting with the avatar index. | |
| 42 // Otherwise we update the user pref in case we missed a notification. | |
| 43 if (GetChromeAvatarIndex() == kNoAvatar) | |
| 44 OnAvatarChanged(); | |
| 45 else | |
| 46 OnSharedSettingChanged(managed_user_id_, managed_users::kChromeAvatarIndex); | |
| 47 } | |
| 48 | |
| 49 void SupervisedUserPrefMappingService::OnAvatarChanged() { | |
| 50 int new_avatar_index = prefs_->GetInteger(prefs::kProfileAvatarIndex); | |
| 51 if (new_avatar_index < 0) | |
| 52 return; | |
| 53 | |
| 54 // First check if the avatar index is a new value. | |
| 55 if (GetChromeAvatarIndex() == new_avatar_index) | |
| 56 return; | |
| 57 | |
| 58 // If yes, update the shared settings value. | |
| 59 shared_settings_->SetValue(managed_user_id_, | |
| 60 managed_users::kChromeAvatarIndex, | |
| 61 base::FundamentalValue(new_avatar_index)); | |
| 62 } | |
| 63 | |
| 64 void SupervisedUserPrefMappingService::OnSharedSettingChanged( | |
| 65 const std::string& mu_id, | |
| 66 const std::string& key) { | |
| 67 if (mu_id != managed_user_id_ || key != managed_users::kChromeAvatarIndex) | |
|
Bernhard Bauer
2014/02/07 09:54:24
Can you DCHECK that we are being called with our o
Adrian Kuegel
2014/02/07 10:24:47
A, right, since the SupervisedUserPrefMappingServi
| |
| 68 return; | |
| 69 | |
| 70 const base::Value* value = shared_settings_->GetValue(mu_id, key); | |
| 71 int avatar_index; | |
| 72 bool success = value->GetAsInteger(&avatar_index); | |
| 73 DCHECK(success); | |
| 74 prefs_->SetInteger(prefs::kProfileAvatarIndex, avatar_index); | |
| 75 } | |
| 76 | |
| 77 void SupervisedUserPrefMappingService::Shutdown() { | |
| 78 subscription_.reset(); | |
| 79 } | |
| 80 | |
| 81 int SupervisedUserPrefMappingService::GetChromeAvatarIndex() { | |
| 82 const base::Value* value = shared_settings_->GetValue( | |
| 83 managed_user_id_, managed_users::kChromeAvatarIndex); | |
| 84 if (!value) | |
| 85 return kNoAvatar; | |
| 86 | |
| 87 int current_avatar_index; | |
| 88 bool success = value->GetAsInteger(¤t_avatar_index); | |
| 89 DCHECK(success); | |
| 90 return current_avatar_index; | |
| 91 } | |
| OLD | NEW |