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

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

Powered by Google App Engine
This is Rietveld 408576698