summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros_settings_provider_user.cc
blob: f5685a286373d26e30735c93ae578b504e252c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2010 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/chromeos/cros_settings_provider_user.h"

#include "base/logging.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/login_library.h"
#include "chrome/browser/chromeos/cros_settings.h"
#include "chrome/browser/chromeos/cros_settings_names.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/prefs/pref_service.h"

namespace chromeos {

namespace {

Value* CreateSettingsBooleanValue(bool value, bool managed) {
  DictionaryValue* dict = new DictionaryValue;
  dict->Set("value", Value::CreateBooleanValue(value));
  dict->Set("managed", Value::CreateBooleanValue(managed));
  return dict;
}

// static
void UpdateCacheBool(const char* name, bool value) {
  PrefService* prefs = g_browser_process->local_state();
  prefs->SetBoolean(name, value);
  prefs->ScheduleSavePersistentPrefs();
}

void UpdateCacheString(const char* name, const std::string& value) {
  PrefService* prefs = g_browser_process->local_state();
  prefs->SetString(name, value);
  prefs->ScheduleSavePersistentPrefs();
}

bool GetUserWhitelist(ListValue* user_list) {
  std::vector<std::string> whitelist;
  if (!CrosLibrary::Get()->EnsureLoaded() ||
      !CrosLibrary::Get()->GetLoginLibrary()->EnumerateWhitelisted(
          &whitelist)) {
    LOG(WARNING) << "Failed to retrieve user whitelist.";
    return false;
  }

  PrefService* prefs = g_browser_process->local_state();
  ListValue* cached_whitelist = prefs->GetMutableList(kAccountsPrefUsers);
  cached_whitelist->Clear();

  const UserManager::User& self = UserManager::Get()->logged_in_user();
  bool is_owner = UserManager::Get()->current_user_is_owner();

  for (size_t i = 0; i < whitelist.size(); ++i) {
    const std::string& email = whitelist[i];

    if (user_list) {
      DictionaryValue* user = new DictionaryValue;
      user->SetString("email", email);
      user->SetString("name", "");
      user->SetBoolean("owner", is_owner && email == self.email());
      user_list->Append(user);
    }

    cached_whitelist->Append(Value::CreateStringValue(email));
  }

  prefs->ScheduleSavePersistentPrefs();

  return true;
}

}  // namespace

UserCrosSettingsProvider::UserCrosSettingsProvider() {
  StartFetchingBoolSetting(kAccountsPrefAllowGuest);
  StartFetchingBoolSetting(kAccountsPrefAllowNewUser);
  StartFetchingBoolSetting(kAccountsPrefShowUserNamesOnSignIn);
  StartFetchingStringSetting(kDeviceOwner);
}

UserCrosSettingsProvider::~UserCrosSettingsProvider() {
  // Cancels all pending callbacks from us.
  SignedSettingsHelper::Get()->CancelCallback(this);
}

// static
void UserCrosSettingsProvider::RegisterPrefs(PrefService* local_state) {
  // Cached signed settings values
  local_state->RegisterBooleanPref(kAccountsPrefAllowGuest, true);
  local_state->RegisterBooleanPref(kAccountsPrefAllowNewUser, true);
  local_state->RegisterBooleanPref(kAccountsPrefShowUserNamesOnSignIn, true);
  local_state->RegisterListPref(kAccountsPrefUsers);
  local_state->RegisterStringPref(kDeviceOwner, "");
}

// static
bool UserCrosSettingsProvider::cached_allow_guest() {
  return g_browser_process->local_state()->GetBoolean(kAccountsPrefAllowGuest);
}

// static
bool UserCrosSettingsProvider::cached_allow_new_user() {
  return g_browser_process->local_state()->GetBoolean(
    kAccountsPrefAllowNewUser);
}

// static
bool UserCrosSettingsProvider::cached_show_users_on_signin() {
  return g_browser_process->local_state()->GetBoolean(
      kAccountsPrefShowUserNamesOnSignIn);
}

// static
const ListValue* UserCrosSettingsProvider::cached_whitelist() {
  PrefService* prefs = g_browser_process->local_state();
  const ListValue* cached_users = prefs->GetList(kAccountsPrefUsers);

  if (!cached_users) {
    // Update whitelist cache.
    GetUserWhitelist(NULL);

    cached_users = prefs->GetList(kAccountsPrefUsers);
  }

  return cached_users;
}

// static
std::string UserCrosSettingsProvider::cached_owner() {
  return g_browser_process->local_state()->GetString(kDeviceOwner);
}

// static
bool UserCrosSettingsProvider::IsEmailInCachedWhitelist(
    const std::string& email) {
  const ListValue* whitelist = cached_whitelist();
  if (whitelist) {
    StringValue email_value(email);
    for (ListValue::const_iterator i(whitelist->begin());
        i != whitelist->end(); ++i) {
      if ((*i)->Equals(&email_value))
        return true;
    }
  }
  return false;
}

void UserCrosSettingsProvider::DoSet(const std::string& path,
                                     Value* in_value) {
  if (!UserManager::Get()->current_user_is_owner()) {
    LOG(WARNING) << "Changing settings from non-owner, setting=" << path;

    // Revert UI change.
    CrosSettings::Get()->FireObservers(path.c_str());
    return;
  }

  if (path == kAccountsPrefAllowGuest ||
      path == kAccountsPrefAllowNewUser ||
      path == kAccountsPrefShowUserNamesOnSignIn) {
    bool bool_value = false;
    if (in_value->GetAsBoolean(&bool_value)) {
      std::string value = bool_value ? "true" : "false";
      SignedSettingsHelper::Get()->StartStorePropertyOp(path, value, this);
      UpdateCacheBool(path.c_str(), bool_value);

      LOG(INFO) << "Set cros setting " << path << "=" << value;
    }
  } else if (path == kDeviceOwner) {
    LOG(INFO) << "Setting owner is not supported. "
              << "Please use 'UpdateCachedOwner' instead.";
  } else if (path == kAccountsPrefUsers) {
    LOG(INFO) << "Setting user whitelist is not implemented."
              << "Please use whitelist/unwhitelist instead.";
  } else {
    LOG(WARNING) << "Try to set unhandled cros setting " << path;
  }
}

bool UserCrosSettingsProvider::Get(const std::string& path,
                                   Value** out_value) const {
  if (path == kAccountsPrefAllowGuest ||
      path == kAccountsPrefAllowNewUser ||
      path == kAccountsPrefShowUserNamesOnSignIn) {
    *out_value = CreateSettingsBooleanValue(
        g_browser_process->local_state()->GetBoolean(path.c_str()),
        !UserManager::Get()->current_user_is_owner());
    return true;
  } else if (path == kAccountsPrefUsers) {
    ListValue* user_list = new ListValue;
    GetUserWhitelist(user_list);
    *out_value = user_list;
    return true;
  }

  return false;
}

bool UserCrosSettingsProvider::HandlesSetting(const std::string& path) {
  return ::StartsWithASCII(path, "cros.accounts.", true);
}

void UserCrosSettingsProvider::OnWhitelistCompleted(bool success,
    const std::string& email) {
  LOG(INFO) << "Add " << email << " to whitelist, success=" << success;

  // Reload the whitelist on settings op failure.
  if (!success)
    CrosSettings::Get()->FireObservers(kAccountsPrefUsers);
}

void UserCrosSettingsProvider::OnUnwhitelistCompleted(bool success,
    const std::string& email) {
  LOG(INFO) << "Remove " << email << " from whitelist, success=" << success;

  // Reload the whitelist on settings op failure.
  if (!success)
    CrosSettings::Get()->FireObservers(kAccountsPrefUsers);
}

void UserCrosSettingsProvider::OnStorePropertyCompleted(
    bool success, const std::string& name, const std::string& value) {
  LOG(INFO) << "Store cros setting " << name << "=" << value
            << ", success=" << success;

  // Reload the setting if store op fails.
  if (!success)
    SignedSettingsHelper::Get()->StartRetrieveProperty(name, this);
}

void UserCrosSettingsProvider::OnRetrievePropertyCompleted(
    bool success, const std::string& name, const std::string& value) {
  if (!success) {
    LOG(WARNING) << "Failed to retrieve cros setting, name=" << name;
    return;
  }

  LOG(INFO) << "Retrieved cros setting " << name << "=" << value;

  if (bool_settings_.count(name)) {
    UpdateCacheBool(name.c_str(), value == "true" ? true : false);
  }

  if (string_settings_.count(name)) {
    UpdateCacheString(name.c_str(), value);
  }

  CrosSettings::Get()->FireObservers(name.c_str());
}

void UserCrosSettingsProvider::WhitelistUser(const std::string& email) {
  SignedSettingsHelper::Get()->StartWhitelistOp(email, true, this);

  PrefService* prefs = g_browser_process->local_state();
  ListValue* cached_whitelist = prefs->GetMutableList(kAccountsPrefUsers);
  cached_whitelist->Append(Value::CreateStringValue(email));
  prefs->ScheduleSavePersistentPrefs();
}

void UserCrosSettingsProvider::UnwhitelistUser(const std::string& email) {
  SignedSettingsHelper::Get()->StartWhitelistOp(email, false, this);

  PrefService* prefs = g_browser_process->local_state();
  ListValue* cached_whitelist = prefs->GetMutableList(kAccountsPrefUsers);
  StringValue email_value(email);
  if (cached_whitelist->Remove(email_value) != -1)
    prefs->ScheduleSavePersistentPrefs();
}

// static
void UserCrosSettingsProvider::UpdateCachedOwner(const std::string& email) {
  UpdateCacheString(kDeviceOwner, email);
}

void UserCrosSettingsProvider::StartFetchingBoolSetting(
    const std::string& name) {
  bool_settings_.insert(name);
  StartFetchingSetting(name);
}

void UserCrosSettingsProvider::StartFetchingStringSetting(
    const std::string& name) {
  string_settings_.insert(name);
  StartFetchingSetting(name);
}

void UserCrosSettingsProvider::StartFetchingSetting(
    const std::string& name) {
  if (CrosLibrary::Get()->EnsureLoaded())
    SignedSettingsHelper::Get()->StartRetrieveProperty(name, this);
}

}  // namespace chromeos