Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/chromeos/policy/device_local_account.cc

Issue 14927015: Translate device-local account IDs to user IDs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix forward declaration. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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/chromeos/policy/device_local_account.h"
6
7 #include <set>
8
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/settings/cros_settings.h"
14 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
15 #include "google_apis/gaia/gaia_auth_util.h"
16
17 namespace policy {
18
19 namespace {
20
21 const char kPublicAccountDomainPrefix[] = "public-accounts";
22 const char kKioskAppAccountDomainPrefix[] = "kiosk-apps";
23 const char kDeviceLocalAccountDomainSuffix[] = ".device-local.localhost";
24
25 } // namespace
26
27 DeviceLocalAccount::DeviceLocalAccount(Type type,
28 const std::string& account_id,
29 const std::string& kiosk_app_id,
30 const std::string& kiosk_app_update_url)
31 : type(type),
32 account_id(account_id),
33 user_id(GenerateDeviceLocalAccountUserId(account_id, type)),
34 kiosk_app_id(kiosk_app_id),
35 kiosk_app_update_url(kiosk_app_update_url) {
36 }
37
38 DeviceLocalAccount::~DeviceLocalAccount() {
39 }
40
41 std::string GenerateDeviceLocalAccountUserId(const std::string& account_id,
42 DeviceLocalAccount::Type type) {
43 std::string domain_prefix;
44 switch (type) {
45 case DeviceLocalAccount::TYPE_PUBLIC_SESSION:
46 domain_prefix = kPublicAccountDomainPrefix;
47 break;
48 case DeviceLocalAccount::TYPE_KIOSK_APP:
49 domain_prefix = kKioskAppAccountDomainPrefix;
50 break;
51 case DeviceLocalAccount::TYPE_COUNT:
52 NOTREACHED();
53 break;
54 }
55 return base::HexEncode(account_id.c_str(), account_id.size()) + "@" +
56 domain_prefix + kDeviceLocalAccountDomainSuffix;
57 }
58
59 bool IsKioskAppUser(const std::string& user_id) {
60 return gaia::ExtractDomainName(user_id) ==
61 std::string(kKioskAppAccountDomainPrefix) +
62 kDeviceLocalAccountDomainSuffix;
63 }
64
65 void SetDeviceLocalAccounts(
66 chromeos::CrosSettings* cros_settings,
67 const std::vector<DeviceLocalAccount>& accounts) {
68 base::ListValue list;
69 for (std::vector<DeviceLocalAccount>::const_iterator it = accounts.begin();
70 it != accounts.end(); ++it) {
71 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
72 entry->SetStringWithoutPathExpansion(
73 chromeos::kAccountsPrefDeviceLocalAccountsKeyId,
74 it->account_id);
75 entry->SetIntegerWithoutPathExpansion(
76 chromeos::kAccountsPrefDeviceLocalAccountsKeyType,
77 it->type);
78 if (it->type == DeviceLocalAccount::TYPE_KIOSK_APP) {
79 entry->SetStringWithoutPathExpansion(
80 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId,
81 it->kiosk_app_id);
82 if (!it->kiosk_app_update_url.empty()) {
83 entry->SetStringWithoutPathExpansion(
84 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL,
85 it->kiosk_app_update_url);
86 }
87 }
88 list.Append(entry.release());
89 }
90
91 cros_settings->Set(chromeos::kAccountsPrefDeviceLocalAccounts, list);
92 }
93
94 std::vector<DeviceLocalAccount> GetDeviceLocalAccounts(
95 chromeos::CrosSettings* cros_settings) {
96 std::vector<DeviceLocalAccount> accounts;
97
98 const base::ListValue* list = NULL;
99 cros_settings->GetList(chromeos::kAccountsPrefDeviceLocalAccounts, &list);
100 if (!list)
101 return accounts;
102
103 std::set<std::string> account_ids;
104 for (size_t i = 0; i < list->GetSize(); ++i) {
105 const base::DictionaryValue* entry = NULL;
106 if (!list->GetDictionary(i, &entry)) {
107 LOG(ERROR) << "Corrupt entry in device-local account list at index " << i
108 << ".";
109 continue;
110 }
111
112 std::string account_id;
113 if (!entry->GetStringWithoutPathExpansion(
114 chromeos::kAccountsPrefDeviceLocalAccountsKeyId, &account_id) ||
115 account_id.empty()) {
116 LOG(ERROR) << "Missing account ID in device-local account list at index "
117 << i << ".";
118 continue;
119 }
120
121 int type;
122 if (!entry->GetIntegerWithoutPathExpansion(
123 chromeos::kAccountsPrefDeviceLocalAccountsKeyType, &type) ||
124 type < 0 || type >= DeviceLocalAccount::TYPE_COUNT) {
125 LOG(ERROR) << "Missing or invalid account type in device-local account "
126 << "list at index " << i << ".";
127 continue;
128 }
129
130 std::string kiosk_app_id;
131 std::string kiosk_app_update_url;
132 if (type == DeviceLocalAccount::TYPE_KIOSK_APP) {
133 if (!entry->GetStringWithoutPathExpansion(
134 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId,
135 &kiosk_app_id)) {
136 LOG(ERROR) << "Missing app ID in device-local account entry at index "
137 << i << ".";
138 continue;
139 }
140 entry->GetStringWithoutPathExpansion(
141 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL,
142 &kiosk_app_update_url);
143 }
144
145 if (!account_ids.insert(account_id).second) {
146 LOG(ERROR) << "Duplicate entry in device-local account list at index "
147 << i << ": " << account_id << ".";
148 continue;
149 }
150
151 accounts.push_back(DeviceLocalAccount(
152 static_cast<DeviceLocalAccount::Type>(type),
153 account_id, kiosk_app_id, kiosk_app_update_url));
154 }
155 return accounts;
156 }
157
158 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698