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

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 browser_tests. 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 std::string GenerateDeviceLocalAccountUserId(const std::string& account_id,
39 DeviceLocalAccount::Type type) {
40 std::string domain_prefix;
41 switch (type) {
42 case DeviceLocalAccount::TYPE_PUBLIC_SESSION:
43 domain_prefix = kPublicAccountDomainPrefix;
44 break;
45 case DeviceLocalAccount::TYPE_KIOSK_APP:
46 domain_prefix = kKioskAppAccountDomainPrefix;
47 break;
48 case DeviceLocalAccount::TYPE_COUNT:
49 NOTREACHED();
50 break;
51 }
52 return base::HexEncode(account_id.c_str(), account_id.size()) + "@" +
53 domain_prefix + kDeviceLocalAccountDomainSuffix;
54 }
55
56 bool IsKioskAppUser(const std::string& user_id) {
57 return gaia::ExtractDomainName(user_id) ==
58 std::string(kKioskAppAccountDomainPrefix) +
59 kDeviceLocalAccountDomainSuffix;
60 }
61
62 void SetDeviceLocalAccounts(
63 chromeos::CrosSettings* cros_settings,
64 const std::vector<DeviceLocalAccount>& accounts) {
65 scoped_ptr<base::ListValue> list(new base::ListValue);
Joao da Silva 2013/05/17 18:10:55 Suggestion: this memory is never transferred, so w
bartfab (slow) 2013/05/21 13:27:07 Done.
66 for (std::vector<DeviceLocalAccount>::const_iterator it = accounts.begin();
67 it != accounts.end(); ++it) {
68 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
69 entry->SetStringWithoutPathExpansion(
70 chromeos::kAccountsPrefDeviceLocalAccountsKeyId,
71 it->account_id);
72 entry->SetIntegerWithoutPathExpansion(
73 chromeos::kAccountsPrefDeviceLocalAccountsKeyType,
74 it->type);
75 if (it->type == DeviceLocalAccount::TYPE_KIOSK_APP) {
76 entry->SetStringWithoutPathExpansion(
77 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId,
78 it->kiosk_app_id);
79 if (!it->kiosk_app_update_url.empty()) {
80 entry->SetStringWithoutPathExpansion(
81 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL,
82 it->kiosk_app_update_url);
83 }
84 }
85 list->Append(entry.release());
86 }
87
88 cros_settings->Set(chromeos::kAccountsPrefDeviceLocalAccounts, *list);
89 }
90
91 std::vector<DeviceLocalAccount> GetDeviceLocalAccounts(
92 chromeos::CrosSettings* cros_settings) {
93 std::vector<DeviceLocalAccount> accounts;
94
95 const base::ListValue* list = NULL;
96 cros_settings->GetList(chromeos::kAccountsPrefDeviceLocalAccounts, &list);
97 if (!list)
98 return accounts;
99
100 std::set<std::string> account_ids;
101 for (size_t i = 0; i < list->GetSize(); ++i) {
102 const base::DictionaryValue* entry = NULL;
103 if (!list->GetDictionary(i, &entry)) {
104 LOG(ERROR) << "Corrupt entry in device-local account list at index " << i
105 << ".";
106 continue;
107 }
108
109 std::string account_id;
110 if (!entry->GetStringWithoutPathExpansion(
111 chromeos::kAccountsPrefDeviceLocalAccountsKeyId, &account_id) ||
112 account_id.empty()) {
113 LOG(ERROR) << "Missing account ID in device-local account list at index "
114 << i << ".";
115 continue;
116 }
117
118 int type;
119 if (!entry->GetIntegerWithoutPathExpansion(
120 chromeos::kAccountsPrefDeviceLocalAccountsKeyType, &type) ||
121 type < 0 || type >= DeviceLocalAccount::TYPE_COUNT) {
122 LOG(ERROR) << "Missing or invalid account type in device-local account "
123 << "list at index " << i << ".";
124 continue;
125 }
126
127 std::string kiosk_app_id;
128 std::string kiosk_app_update_url;
129 if (type == DeviceLocalAccount::TYPE_KIOSK_APP) {
130 if (!entry->GetStringWithoutPathExpansion(
131 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppId,
132 &kiosk_app_id)) {
133 LOG(ERROR) << "Missing app ID in device-local account entry at index "
134 << i << ".";
135 continue;
136 }
137 entry->GetStringWithoutPathExpansion(
138 chromeos::kAccountsPrefDeviceLocalAccountsKeyKioskAppUpdateURL,
139 &kiosk_app_update_url);
140 }
141
142 if (!account_ids.insert(account_id).second) {
143 LOG(ERROR) << "Duplicate entry in device-local account list at index "
144 << i << ": " << account_id << ".";
145 continue;
146 }
147
148 accounts.push_back(DeviceLocalAccount(
149 static_cast<DeviceLocalAccount::Type>(type),
150 account_id, kiosk_app_id, kiosk_app_update_url));
151 }
152 return accounts;
153 }
154
155 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698