Index: chrome/browser/chromeos/policy/device_local_account.cc |
diff --git a/chrome/browser/chromeos/policy/device_local_account.cc b/chrome/browser/chromeos/policy/device_local_account.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a2c9c0930ddacfd1cc0fee672f7723fd1ebf8ed1 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/device_local_account.cc |
@@ -0,0 +1,158 @@ |
+// Copyright (c) 2013 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/policy/device_local_account.h" |
+ |
+#include <set> |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/values.h" |
+#include "chrome/browser/chromeos/settings/cros_settings.h" |
+#include "chrome/browser/chromeos/settings/cros_settings_names.h" |
+#include "google_apis/gaia/gaia_auth_util.h" |
+ |
+namespace policy { |
+ |
+namespace { |
+ |
+const char kPublicAccountDomainPrefix[] = "public-accounts"; |
+const char kKioskAppAccountDomainPrefix[] = "kiosk-apps"; |
+const char kDeviceLocalAccountDomainSuffix[] = ".device-local.localhost"; |
+ |
+} // namespace |
+ |
+DeviceLocalAccount::DeviceLocalAccount(Type type, |
+ const std::string& account_id, |
+ const std::string& kiosk_app_id, |
+ const std::string& kiosk_app_update_url) |
+ : type(type), |
+ account_id(account_id), |
+ user_id(GenerateDeviceLocalAccountUserId(account_id, type)), |
+ kiosk_app_id(kiosk_app_id), |
+ kiosk_app_update_url(kiosk_app_update_url) { |
+} |
+ |
+DeviceLocalAccount::~DeviceLocalAccount() { |
+} |
+ |
+std::string GenerateDeviceLocalAccountUserId(const std::string& account_id, |
+ DeviceLocalAccount::Type type) { |
+ std::string domain_prefix; |
+ switch (type) { |
+ case DeviceLocalAccount::TYPE_PUBLIC_SESSION: |
+ domain_prefix = kPublicAccountDomainPrefix; |
+ break; |
+ case DeviceLocalAccount::TYPE_KIOSK_APP: |
+ domain_prefix = kKioskAppAccountDomainPrefix; |
+ break; |
+ case DeviceLocalAccount::TYPE_COUNT: |
+ NOTREACHED(); |
+ break; |
+ } |
+ return base::HexEncode(account_id.c_str(), account_id.size()) + "@" + |
+ domain_prefix + kDeviceLocalAccountDomainSuffix; |
+} |
+ |
+bool IsKioskAppUser(const std::string& user_id) { |
+ return gaia::ExtractDomainName(user_id) == |
+ std::string(kKioskAppAccountDomainPrefix) + |
+ kDeviceLocalAccountDomainSuffix; |
+} |
+ |
+void SetDeviceLocalAccounts( |
+ chromeos::CrosSettings* cros_settings, |
+ const std::vector<DeviceLocalAccount>& accounts) { |
+ base::ListValue list; |
+ for (std::vector<DeviceLocalAccount>::const_iterator it = accounts.begin(); |
+ it != accounts.end(); ++it) { |
+ scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
+ entry->SetStringWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyId, |
+ it->account_id); |
+ entry->SetIntegerWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyType, |
+ it->type); |
+ if (it->type == DeviceLocalAccount::TYPE_KIOSK_APP) { |
+ entry->SetStringWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId, |
+ it->kiosk_app_id); |
+ if (!it->kiosk_app_update_url.empty()) { |
+ entry->SetStringWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL, |
+ it->kiosk_app_update_url); |
+ } |
+ } |
+ list.Append(entry.release()); |
+ } |
+ |
+ cros_settings->Set(chromeos::kAccountsPrefDeviceLocalAccounts, list); |
+} |
+ |
+std::vector<DeviceLocalAccount> GetDeviceLocalAccounts( |
+ chromeos::CrosSettings* cros_settings) { |
+ std::vector<DeviceLocalAccount> accounts; |
+ |
+ const base::ListValue* list = NULL; |
+ cros_settings->GetList(chromeos::kAccountsPrefDeviceLocalAccounts, &list); |
+ if (!list) |
+ return accounts; |
+ |
+ std::set<std::string> account_ids; |
+ for (size_t i = 0; i < list->GetSize(); ++i) { |
+ const base::DictionaryValue* entry = NULL; |
+ if (!list->GetDictionary(i, &entry)) { |
+ LOG(ERROR) << "Corrupt entry in device-local account list at index " << i |
+ << "."; |
+ continue; |
+ } |
+ |
+ std::string account_id; |
+ if (!entry->GetStringWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyId, &account_id) || |
+ account_id.empty()) { |
+ LOG(ERROR) << "Missing account ID in device-local account list at index " |
+ << i << "."; |
+ continue; |
+ } |
+ |
+ int type; |
+ if (!entry->GetIntegerWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyType, &type) || |
+ type < 0 || type >= DeviceLocalAccount::TYPE_COUNT) { |
+ LOG(ERROR) << "Missing or invalid account type in device-local account " |
+ << "list at index " << i << "."; |
+ continue; |
+ } |
+ |
+ std::string kiosk_app_id; |
+ std::string kiosk_app_update_url; |
+ if (type == DeviceLocalAccount::TYPE_KIOSK_APP) { |
+ if (!entry->GetStringWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId, |
+ &kiosk_app_id)) { |
+ LOG(ERROR) << "Missing app ID in device-local account entry at index " |
+ << i << "."; |
+ continue; |
+ } |
+ entry->GetStringWithoutPathExpansion( |
+ chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL, |
+ &kiosk_app_update_url); |
+ } |
+ |
+ if (!account_ids.insert(account_id).second) { |
+ LOG(ERROR) << "Duplicate entry in device-local account list at index " |
+ << i << ": " << account_id << "."; |
+ continue; |
+ } |
+ |
+ accounts.push_back(DeviceLocalAccount( |
+ static_cast<DeviceLocalAccount::Type>(type), |
+ account_id, kiosk_app_id, kiosk_app_update_url)); |
+ } |
+ return accounts; |
+} |
+ |
+} // namespace policy |