| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/cros_settings_provider_user.h" | 5 #include "chrome/browser/chromeos/cros_settings_provider_user.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 #include "chrome/browser/chromeos/cros/login_library.h" | 12 #include "chrome/browser/chromeos/cros/login_library.h" |
| 12 #include "chrome/browser/chromeos/cros_settings.h" | 13 #include "chrome/browser/chromeos/cros_settings.h" |
| 13 #include "chrome/browser/chromeos/cros_settings_names.h" | 14 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 14 #include "chrome/browser/chromeos/login/user_manager.h" | 15 #include "chrome/browser/chromeos/login/user_manager.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 Value* CreateSettingsBooleanValue(bool value, bool managed) { | 20 Value* CreateSettingsBooleanValue(bool value, bool managed) { |
| 19 DictionaryValue* dict = new DictionaryValue; | 21 DictionaryValue* dict = new DictionaryValue; |
| 20 dict->Set("value", Value::CreateBooleanValue(value)); | 22 dict->Set("value", Value::CreateBooleanValue(value)); |
| 21 dict->Set("managed", Value::CreateBooleanValue(managed)); | 23 dict->Set("managed", Value::CreateBooleanValue(managed)); |
| 22 return dict; | 24 return dict; |
| 23 } | 25 } |
| 24 | 26 |
| 27 void UpdateCache(const char* name, bool value) { |
| 28 PrefService* prefs = g_browser_process->local_state(); |
| 29 prefs->SetBoolean(name, value); |
| 30 prefs->ScheduleSavePersistentPrefs(); |
| 31 } |
| 32 |
| 25 } // namespace | 33 } // namespace |
| 26 | 34 |
| 27 namespace chromeos { | 35 namespace chromeos { |
| 28 | 36 |
| 29 UserCrosSettingsProvider::UserCrosSettingsProvider() | 37 UserCrosSettingsProvider::UserCrosSettingsProvider() { |
| 30 : cache_(new DictionaryValue) { | |
| 31 current_user_is_owner_ = UserManager::Get()->current_user_is_owner(); | 38 current_user_is_owner_ = UserManager::Get()->current_user_is_owner(); |
| 32 | 39 |
| 33 StartFetchingBoolSetting(kAccountsPrefAllowBWSI, true); | 40 StartFetchingBoolSetting(kAccountsPrefAllowBWSI); |
| 34 StartFetchingBoolSetting(kAccountsPrefAllowGuest, true); | 41 StartFetchingBoolSetting(kAccountsPrefAllowGuest); |
| 35 StartFetchingBoolSetting(kAccountsPrefShowUserNamesOnSignIn, true); | 42 StartFetchingBoolSetting(kAccountsPrefShowUserNamesOnSignIn); |
| 36 | |
| 37 LoadUserWhitelist(); | |
| 38 } | 43 } |
| 39 | 44 |
| 40 UserCrosSettingsProvider::~UserCrosSettingsProvider() { | 45 UserCrosSettingsProvider::~UserCrosSettingsProvider() { |
| 41 // Cancels all pending callbacks from us. | 46 // Cancels all pending callbacks from us. |
| 42 SignedSettingsHelper::Get()->CancelCallback(this); | 47 SignedSettingsHelper::Get()->CancelCallback(this); |
| 43 } | 48 } |
| 44 | 49 |
| 50 void UserCrosSettingsProvider::RegisterPrefs(PrefService* local_state) { |
| 51 // Cached signed settings values |
| 52 local_state->RegisterBooleanPref(kAccountsPrefAllowBWSI, true); |
| 53 local_state->RegisterBooleanPref(kAccountsPrefAllowGuest, true); |
| 54 local_state->RegisterBooleanPref(kAccountsPrefShowUserNamesOnSignIn, true); |
| 55 local_state->RegisterListPref(kAccountsPrefUsers); |
| 56 } |
| 57 |
| 58 bool UserCrosSettingsProvider::cached_allow_bwsi() { |
| 59 return g_browser_process->local_state()->GetBoolean(kAccountsPrefAllowBWSI); |
| 60 } |
| 61 |
| 62 bool UserCrosSettingsProvider::cached_allow_guest() { |
| 63 return g_browser_process->local_state()->GetBoolean(kAccountsPrefAllowGuest); |
| 64 } |
| 65 |
| 66 bool UserCrosSettingsProvider::cached_show_users_on_signin() { |
| 67 return g_browser_process->local_state()->GetBoolean( |
| 68 kAccountsPrefShowUserNamesOnSignIn); |
| 69 } |
| 70 |
| 71 const ListValue* UserCrosSettingsProvider::cached_whitelist() { |
| 72 return g_browser_process->local_state()->GetList(kAccountsPrefUsers); |
| 73 } |
| 74 |
| 45 void UserCrosSettingsProvider::Set(const std::string& path, Value* in_value) { | 75 void UserCrosSettingsProvider::Set(const std::string& path, Value* in_value) { |
| 46 if (path == kAccountsPrefAllowBWSI || | 76 if (path == kAccountsPrefAllowBWSI || |
| 47 path == kAccountsPrefAllowGuest || | 77 path == kAccountsPrefAllowGuest || |
| 48 path == kAccountsPrefShowUserNamesOnSignIn) { | 78 path == kAccountsPrefShowUserNamesOnSignIn) { |
| 49 bool bool_value = false; | 79 bool bool_value = false; |
| 50 if (in_value->GetAsBoolean(&bool_value)) { | 80 if (in_value->GetAsBoolean(&bool_value)) { |
| 51 cache_->Set(path, in_value); | |
| 52 | |
| 53 std::string value = bool_value ? "true" : "false"; | 81 std::string value = bool_value ? "true" : "false"; |
| 54 SignedSettingsHelper::Get()->StartStorePropertyOp(path, value, this); | 82 SignedSettingsHelper::Get()->StartStorePropertyOp(path, value, this); |
| 83 UpdateCache(path.c_str(), bool_value); |
| 55 | 84 |
| 56 LOG(INFO) << "Set cros setting " << path << "=" << value; | 85 LOG(INFO) << "Set cros setting " << path << "=" << value; |
| 57 } | 86 } |
| 58 } else if (path == kAccountsPrefUsers) { | 87 } else if (path == kAccountsPrefUsers) { |
| 59 LOG(INFO) << "Setting user whitelist is not implemented." | 88 LOG(INFO) << "Setting user whitelist is not implemented." |
| 60 << "Please use whitelist/unwhitelist instead."; | 89 << "Please use whitelist/unwhitelist instead."; |
| 61 } else { | 90 } else { |
| 62 LOG(WARNING) << "Try to set unhandled cros setting " << path; | 91 LOG(WARNING) << "Try to set unhandled cros setting " << path; |
| 63 } | 92 } |
| 64 } | 93 } |
| 65 | 94 |
| 66 bool UserCrosSettingsProvider::Get(const std::string& path, | 95 bool UserCrosSettingsProvider::Get(const std::string& path, |
| 67 Value** out_value) const { | 96 Value** out_value) const { |
| 68 Value* value = NULL; | 97 if (path == kAccountsPrefAllowBWSI || |
| 69 if (cache_->Get(path, &value) && value) { | 98 path == kAccountsPrefAllowGuest || |
| 70 *out_value = value->DeepCopy(); | 99 path == kAccountsPrefShowUserNamesOnSignIn) { |
| 100 *out_value = Value::CreateBooleanValue( |
| 101 g_browser_process->local_state()->GetBoolean(path.c_str())); |
| 102 return true; |
| 103 } else if (path == kAccountsPrefUsers) { |
| 104 *out_value = GetUserWhitelist(); |
| 71 return true; | 105 return true; |
| 72 } | 106 } |
| 73 | 107 |
| 74 return false; | 108 return false; |
| 75 } | 109 } |
| 76 | 110 |
| 77 bool UserCrosSettingsProvider::HandlesSetting(const std::string& path) { | 111 bool UserCrosSettingsProvider::HandlesSetting(const std::string& path) { |
| 78 return ::StartsWithASCII(path, "cros.accounts.", true); | 112 return ::StartsWithASCII(path, "cros.accounts.", true); |
| 79 } | 113 } |
| 80 | 114 |
| 81 void UserCrosSettingsProvider::OnWhitelistCompleted(bool success, | 115 void UserCrosSettingsProvider::OnWhitelistCompleted(bool success, |
| 82 const std::string& email) { | 116 const std::string& email) { |
| 83 LOG(INFO) << "Add " << email << " to whitelist, success=" << success; | 117 LOG(INFO) << "Add " << email << " to whitelist, success=" << success; |
| 84 | 118 |
| 85 // Reload the whitelist on settings op failure. | 119 // Reload the whitelist on settings op failure. |
| 86 if (!success) | 120 if (!success) |
| 87 LoadUserWhitelist(); | 121 CrosSettings::Get()->FireObservers(kAccountsPrefUsers); |
| 88 } | 122 } |
| 89 | 123 |
| 90 void UserCrosSettingsProvider::OnUnwhitelistCompleted(bool success, | 124 void UserCrosSettingsProvider::OnUnwhitelistCompleted(bool success, |
| 91 const std::string& email) { | 125 const std::string& email) { |
| 92 LOG(INFO) << "Remove " << email << " from whitelist, success=" << success; | 126 LOG(INFO) << "Remove " << email << " from whitelist, success=" << success; |
| 93 | 127 |
| 94 // Reload the whitelist on settings op failure. | 128 // Reload the whitelist on settings op failure. |
| 95 if (!success) | 129 if (!success) |
| 96 LoadUserWhitelist(); | 130 CrosSettings::Get()->FireObservers(kAccountsPrefUsers); |
| 97 } | 131 } |
| 98 | 132 |
| 99 void UserCrosSettingsProvider::OnStorePropertyCompleted( | 133 void UserCrosSettingsProvider::OnStorePropertyCompleted( |
| 100 bool success, const std::string& name, const std::string& value) { | 134 bool success, const std::string& name, const std::string& value) { |
| 101 LOG(INFO) << "Store cros setting " << name << "=" << value | 135 LOG(INFO) << "Store cros setting " << name << "=" << value |
| 102 << ", success=" << success; | 136 << ", success=" << success; |
| 103 | 137 |
| 104 // Reload the setting if store op fails. | 138 // Reload the setting if store op fails. |
| 105 if (!success) | 139 if (!success) |
| 106 SignedSettingsHelper::Get()->StartRetrieveProperty(name, this); | 140 SignedSettingsHelper::Get()->StartRetrieveProperty(name, this); |
| 107 } | 141 } |
| 108 | 142 |
| 109 void UserCrosSettingsProvider::OnRetrievePropertyCompleted( | 143 void UserCrosSettingsProvider::OnRetrievePropertyCompleted( |
| 110 bool success, const std::string& name, const std::string& value) { | 144 bool success, const std::string& name, const std::string& value) { |
| 111 if (!success) { | 145 if (!success) { |
| 112 LOG(WARNING) << "Failed to retrieve cros setting, name=" << name; | 146 LOG(WARNING) << "Failed to retrieve cros setting, name=" << name; |
| 113 return; | 147 return; |
| 114 } | 148 } |
| 115 | 149 |
| 116 LOG(INFO) << "Retrieved cros setting " << name << "=" << value; | 150 LOG(INFO) << "Retrieved cros setting " << name << "=" << value; |
| 117 | 151 |
| 118 bool bool_value = value == "true" ? true : false; | 152 UpdateCache(name.c_str(), value == "true" ? true : false); |
| 119 cache_->Set(name, | |
| 120 CreateSettingsBooleanValue(bool_value, !current_user_is_owner_)); | |
| 121 CrosSettings::Get()->FireObservers(name.c_str()); | 153 CrosSettings::Get()->FireObservers(name.c_str()); |
| 122 } | 154 } |
| 123 | 155 |
| 124 void UserCrosSettingsProvider::WhitelistUser(const std::string& email) { | 156 void UserCrosSettingsProvider::WhitelistUser(const std::string& email) { |
| 125 SignedSettingsHelper::Get()->StartWhitelistOp(email, true, this); | 157 SignedSettingsHelper::Get()->StartWhitelistOp(email, true, this); |
| 158 |
| 159 PrefService* prefs = g_browser_process->local_state(); |
| 160 ListValue* cached_whitelist = prefs->GetMutableList(kAccountsPrefUsers); |
| 161 cached_whitelist->Append(Value::CreateStringValue(email)); |
| 162 prefs->ScheduleSavePersistentPrefs(); |
| 126 } | 163 } |
| 127 | 164 |
| 128 void UserCrosSettingsProvider::UnwhitelistUser(const std::string& email) { | 165 void UserCrosSettingsProvider::UnwhitelistUser(const std::string& email) { |
| 129 SignedSettingsHelper::Get()->StartWhitelistOp(email, false, this); | 166 SignedSettingsHelper::Get()->StartWhitelistOp(email, false, this); |
| 167 |
| 168 PrefService* prefs = g_browser_process->local_state(); |
| 169 ListValue* cached_whitelist = prefs->GetMutableList(kAccountsPrefUsers); |
| 170 StringValue email_value(email); |
| 171 if (cached_whitelist->Remove(email_value) != -1) |
| 172 prefs->ScheduleSavePersistentPrefs(); |
| 130 } | 173 } |
| 131 | 174 |
| 132 void UserCrosSettingsProvider::StartFetchingBoolSetting( | 175 void UserCrosSettingsProvider::StartFetchingBoolSetting( |
| 133 const std::string& name, | 176 const std::string& name) { |
| 134 bool default_value) { | |
| 135 if (!cache_->HasKey(name)) { | |
| 136 cache_->Set(name, | |
| 137 CreateSettingsBooleanValue(default_value, !current_user_is_owner_)); | |
| 138 } | |
| 139 | |
| 140 if (CrosLibrary::Get()->EnsureLoaded()) | 177 if (CrosLibrary::Get()->EnsureLoaded()) |
| 141 SignedSettingsHelper::Get()->StartRetrieveProperty(name, this); | 178 SignedSettingsHelper::Get()->StartRetrieveProperty(name, this); |
| 142 } | 179 } |
| 143 | 180 |
| 144 void UserCrosSettingsProvider::LoadUserWhitelist() { | 181 ListValue* UserCrosSettingsProvider::GetUserWhitelist() const { |
| 145 ListValue* user_list = new ListValue; | 182 ListValue* user_list = new ListValue; |
| 146 | 183 |
| 147 std::vector<std::string> whitelist; | 184 std::vector<std::string> whitelist; |
| 148 if (!CrosLibrary::Get()->EnsureLoaded() || | 185 if (!CrosLibrary::Get()->EnsureLoaded() || |
| 149 !CrosLibrary::Get()->GetLoginLibrary()->EnumerateWhitelisted( | 186 !CrosLibrary::Get()->GetLoginLibrary()->EnumerateWhitelisted( |
| 150 &whitelist)) { | 187 &whitelist)) { |
| 151 LOG(WARNING) << "Failed to retrieve user whitelist."; | 188 LOG(WARNING) << "Failed to retrieve user whitelist."; |
| 152 } else { | 189 } else { |
| 190 PrefService* prefs = g_browser_process->local_state(); |
| 191 ListValue* cached_whitelist = prefs->GetMutableList(kAccountsPrefUsers); |
| 192 cached_whitelist->Clear(); |
| 193 |
| 153 const UserManager::User& current_user = | 194 const UserManager::User& current_user = |
| 154 UserManager::Get()->logged_in_user(); | 195 UserManager::Get()->logged_in_user(); |
| 155 for (size_t i = 0; i < whitelist.size(); ++i) { | 196 for (size_t i = 0; i < whitelist.size(); ++i) { |
| 156 const std::string& email = whitelist[i]; | 197 const std::string& email = whitelist[i]; |
| 157 | 198 |
| 158 DictionaryValue* user = new DictionaryValue; | 199 DictionaryValue* user = new DictionaryValue; |
| 159 user->SetString("email", email); | 200 user->SetString("email", email); |
| 160 user->SetString("name", ""); | 201 user->SetString("name", ""); |
| 161 user->SetBoolean("owner", | 202 user->SetBoolean("owner", |
| 162 current_user_is_owner_ && email == current_user.email()); | 203 current_user_is_owner_ && email == current_user.email()); |
| 163 | 204 |
| 164 user_list->Append(user); | 205 user_list->Append(user); |
| 206 cached_whitelist->Append(Value::CreateStringValue(email)); |
| 165 } | 207 } |
| 208 |
| 209 prefs->ScheduleSavePersistentPrefs(); |
| 166 } | 210 } |
| 167 | 211 |
| 168 cache_->Set(kAccountsPrefUsers, user_list); | 212 return user_list; |
| 169 CrosSettings::Get()->FireObservers(kAccountsPrefUsers); | |
| 170 } | 213 } |
| 171 | 214 |
| 172 } // namespace chromeos | 215 } // namespace chromeos |
| OLD | NEW |