Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / users_private / users_private_api.cc
blobb33dff60838c5bedb54a32250b0636ce30c72bf8
1 // Copyright 2015 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.
5 #include "chrome/browser/chromeos/extensions/users_private/users_private_api.h"
7 #include "base/values.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chromeos/extensions/users_private/users_private_delegate.h"
10 #include "chrome/browser/chromeos/extensions/users_private/users_private_delegate_factory.h"
11 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
12 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
13 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
14 #include "chrome/browser/chromeos/profiles/profile_helper.h"
15 #include "chrome/browser/chromeos/settings/cros_settings.h"
16 #include "chrome/browser/extensions/chrome_extension_function.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/extensions/api/users_private.h"
19 #include "chromeos/login/user_names.h"
20 #include "chromeos/settings/cros_settings_names.h"
21 #include "components/user_manager/user_manager.h"
22 #include "extensions/browser/extension_function_registry.h"
23 #include "google_apis/gaia/gaia_auth_util.h"
25 namespace extensions {
27 ////////////////////////////////////////////////////////////////////////////////
28 // UsersPrivateGetWhitelistedUsersFunction
30 UsersPrivateGetWhitelistedUsersFunction::
31 UsersPrivateGetWhitelistedUsersFunction()
32 : chrome_details_(this) {
35 UsersPrivateGetWhitelistedUsersFunction::
36 ~UsersPrivateGetWhitelistedUsersFunction() {
39 ExtensionFunction::ResponseAction
40 UsersPrivateGetWhitelistedUsersFunction::Run() {
41 Profile* profile = chrome_details_.GetProfile();
42 scoped_ptr<base::ListValue> user_list(new base::ListValue);
44 // Non-owners should not be able to see the list of users.
45 if (!chromeos::ProfileHelper::IsOwnerProfile(profile))
46 return RespondNow(OneArgument(user_list.release()));
48 // Create one list to set. This is needed because user white list update is
49 // asynchronous and sequential. Before previous write comes back, cached list
50 // is stale and should not be used for appending. See http://crbug.com/127215
51 scoped_ptr<base::ListValue> email_list;
53 UsersPrivateDelegate* delegate =
54 UsersPrivateDelegateFactory::GetForBrowserContext(browser_context());
55 PrefsUtil* prefs_util = delegate->GetPrefsUtil();
57 scoped_ptr<api::settings_private::PrefObject> users_pref_object =
58 prefs_util->GetPref(chromeos::kAccountsPrefUsers);
59 if (users_pref_object->value) {
60 const base::ListValue* existing = nullptr;
61 users_pref_object->value->GetAsList(&existing);
62 email_list.reset(existing->DeepCopy());
63 } else {
64 email_list.reset(new base::ListValue());
67 // Remove all supervised users. On the next step only supervised users present
68 // on the device will be added back. Thus not present SU are removed.
69 // No need to remove usual users as they can simply login back.
70 for (size_t i = 0; i < email_list->GetSize(); ++i) {
71 std::string whitelisted_user;
72 email_list->GetString(i, &whitelisted_user);
73 if (gaia::ExtractDomainName(whitelisted_user) ==
74 chromeos::login::kSupervisedUserDomain) {
75 email_list->Remove(i, NULL);
76 --i;
80 const user_manager::UserList& users =
81 user_manager::UserManager::Get()->GetUsers();
82 for (user_manager::UserList::const_iterator it = users.begin();
83 it < users.end(); ++it)
84 email_list->AppendIfNotPresent(new base::StringValue((*it)->email()));
86 if (chromeos::OwnerSettingsServiceChromeOS* service =
87 chromeos::OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
88 profile)) {
89 service->Set(chromeos::kAccountsPrefUsers, *email_list.get());
92 // Now populate the list of User objects for returning to the JS.
93 for (size_t i = 0; i < email_list->GetSize(); ++i) {
94 api::users_private::User user;
95 email_list->GetString(i, &user.email);
97 user.is_owner = chromeos::ProfileHelper::IsOwnerProfile(profile) &&
98 user.email == profile->GetProfileUserName();
99 user_list->Append(user.ToValue().release());
102 return RespondNow(OneArgument(user_list.release()));
105 ////////////////////////////////////////////////////////////////////////////////
106 // UsersPrivateAddWhitelistedUserFunction
108 UsersPrivateAddWhitelistedUserFunction::UsersPrivateAddWhitelistedUserFunction()
109 : chrome_details_(this) {
112 UsersPrivateAddWhitelistedUserFunction::
113 ~UsersPrivateAddWhitelistedUserFunction() {
116 ExtensionFunction::ResponseAction
117 UsersPrivateAddWhitelistedUserFunction::Run() {
118 scoped_ptr<api::users_private::AddWhitelistedUser::Params> parameters =
119 api::users_private::AddWhitelistedUser::Params::Create(*args_);
120 EXTENSION_FUNCTION_VALIDATE(parameters.get());
122 // Non-owners should not be able to add users.
123 if (!chromeos::ProfileHelper::IsOwnerProfile(chrome_details_.GetProfile())) {
124 return RespondNow(OneArgument(new base::FundamentalValue(false)));
127 std::string username = gaia::CanonicalizeEmail(parameters->email);
128 if (chromeos::CrosSettings::Get()->FindEmailInList(
129 chromeos::kAccountsPrefUsers, username, NULL)) {
130 return RespondNow(OneArgument(new base::FundamentalValue(false)));
133 base::StringValue username_value(username);
135 UsersPrivateDelegate* delegate =
136 UsersPrivateDelegateFactory::GetForBrowserContext(browser_context());
137 PrefsUtil* prefs_util = delegate->GetPrefsUtil();
138 bool added = prefs_util->AppendToListCrosSetting(chromeos::kAccountsPrefUsers,
139 username_value);
140 return RespondNow(OneArgument(new base::FundamentalValue(added)));
143 ////////////////////////////////////////////////////////////////////////////////
144 // UsersPrivateRemoveWhitelistedUserFunction
146 UsersPrivateRemoveWhitelistedUserFunction::
147 UsersPrivateRemoveWhitelistedUserFunction()
148 : chrome_details_(this) {
151 UsersPrivateRemoveWhitelistedUserFunction::
152 ~UsersPrivateRemoveWhitelistedUserFunction() {
155 ExtensionFunction::ResponseAction
156 UsersPrivateRemoveWhitelistedUserFunction::Run() {
157 scoped_ptr<api::users_private::RemoveWhitelistedUser::Params> parameters =
158 api::users_private::RemoveWhitelistedUser::Params::Create(*args_);
159 EXTENSION_FUNCTION_VALIDATE(parameters.get());
161 // Non-owners should not be able to remove users.
162 if (!chromeos::ProfileHelper::IsOwnerProfile(chrome_details_.GetProfile())) {
163 return RespondNow(OneArgument(new base::FundamentalValue(false)));
166 base::StringValue canonical_email(gaia::CanonicalizeEmail(parameters->email));
168 UsersPrivateDelegate* delegate =
169 UsersPrivateDelegateFactory::GetForBrowserContext(browser_context());
170 PrefsUtil* prefs_util = delegate->GetPrefsUtil();
171 bool removed = prefs_util->RemoveFromListCrosSetting(
172 chromeos::kAccountsPrefUsers, canonical_email);
173 user_manager::UserManager::Get()->RemoveUser(parameters->email, NULL);
174 return RespondNow(OneArgument(new base::FundamentalValue(removed)));
177 ////////////////////////////////////////////////////////////////////////////////
178 // UsersPrivateIsCurrentUserOwnerFunction
180 UsersPrivateIsCurrentUserOwnerFunction::UsersPrivateIsCurrentUserOwnerFunction()
181 : chrome_details_(this) {
184 UsersPrivateIsCurrentUserOwnerFunction::
185 ~UsersPrivateIsCurrentUserOwnerFunction() {
188 ExtensionFunction::ResponseAction
189 UsersPrivateIsCurrentUserOwnerFunction::Run() {
190 bool is_owner =
191 chromeos::ProfileHelper::IsOwnerProfile(chrome_details_.GetProfile());
192 return RespondNow(OneArgument(new base::FundamentalValue(is_owner)));
195 ////////////////////////////////////////////////////////////////////////////////
196 // UsersPrivateIsWhitelistManagedFunction
198 UsersPrivateIsWhitelistManagedFunction::
199 UsersPrivateIsWhitelistManagedFunction() {
202 UsersPrivateIsWhitelistManagedFunction::
203 ~UsersPrivateIsWhitelistManagedFunction() {
206 ExtensionFunction::ResponseAction
207 UsersPrivateIsWhitelistManagedFunction::Run() {
208 bool is_managed = g_browser_process->platform_part()
209 ->browser_policy_connector_chromeos()
210 ->IsEnterpriseManaged();
211 return RespondNow(OneArgument(new base::FundamentalValue(is_managed)));
214 } // namespace extensions