Skip to content

Commit

Permalink
Implement policy::GetOSUsername for POSIX platforms.
Browse files Browse the repository at this point in the history
Bug: 847265
Change-Id: I332cd4d5bd6996019e132abbd313e765f4e080d3
Reviewed-on: https://chromium-review.googlesource.com/1082662
Reviewed-by: Owen Min <zmin@chromium.org>
Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
Commit-Queue: Georges Khalil <georgesak@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567791}
  • Loading branch information
georgesak authored and Commit Bot committed Jun 15, 2018
1 parent 4e91e22 commit 4fee109
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions components/policy/core/common/cloud/cloud_policy_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#include <wincred.h>
#endif

#if defined(OS_LINUX) && !defined(OS_CHROMEOS) || defined(OS_MACOSX)
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#if defined(OS_MACOSX)
#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#include <stddef.h>
Expand Down Expand Up @@ -125,7 +131,13 @@ std::string GetOSArchitecture() {
}

std::string GetOSUsername() {
#if defined(OS_WIN)
#if defined(OS_LINUX) && !defined(OS_CHROMEOS) || defined(OS_MACOSX)
struct passwd* creds = getpwuid(getuid());
if (!creds || !creds->pw_name)
return std::string();

return creds->pw_name;
#elif defined(OS_WIN)
WCHAR username[CREDUI_MAX_USERNAME_LENGTH + 1] = {};
DWORD username_length = sizeof(username);

Expand All @@ -139,10 +151,9 @@ std::string GetOSUsername() {

return base::WideToUTF8(username);
#else
// TODO(crbug.com/847265): For now, this is only supported on Windows. Other
// platforms will be added when enabled.
NOTREACHED();
return std::string();
#endif // OS_WIN
#endif
}

} // namespace policy
3 changes: 2 additions & 1 deletion components/policy/core/common/cloud/cloud_policy_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ POLICY_EXPORT std::string GetOSPlatform();
POLICY_EXPORT std::string GetOSArchitecture();

// Returns the username of the logged in user in the OS. This function is
// platform specific.
// platform specific. Note that on Windows, this returns the username including
// the domain, whereas on POSIX, this just returns the username.
POLICY_EXPORT std::string GetOSUsername();

} // namespace policy
Expand Down

0 comments on commit 4fee109

Please sign in to comment.