1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsUserInfo.h"
8 #include "mozilla/ArrayUtils.h" // ArrayLength
12 #include "nsXPIDLString.h"
14 #define SECURITY_WIN32
18 nsUserInfo::nsUserInfo()
22 nsUserInfo::~nsUserInfo()
26 NS_IMPL_ISUPPORTS(nsUserInfo
, nsIUserInfo
)
29 nsUserInfo::GetUsername(char **aUsername
)
31 NS_ENSURE_ARG_POINTER(aUsername
);
34 // ULEN is the max username length as defined in lmcons.h
35 wchar_t username
[UNLEN
+1];
36 DWORD size
= mozilla::ArrayLength(username
);
37 if (!GetUserNameW(username
, &size
))
38 return NS_ERROR_FAILURE
;
40 *aUsername
= ToNewUTF8String(nsDependentString(username
));
41 return (*aUsername
) ? NS_OK
: NS_ERROR_FAILURE
;
45 nsUserInfo::GetFullname(char16_t
**aFullname
)
47 NS_ENSURE_ARG_POINTER(aFullname
);
50 wchar_t fullName
[512];
51 DWORD size
= mozilla::ArrayLength(fullName
);
53 if (GetUserNameExW(NameDisplay
, fullName
, &size
)) {
54 *aFullname
= ToNewUnicode(nsDependentString(fullName
));
56 DWORD getUsernameError
= GetLastError();
58 // Try to use the net APIs regardless of the error because it may be
59 // able to obtain the information.
60 wchar_t username
[UNLEN
+ 1];
61 size
= mozilla::ArrayLength(username
);
62 if (!GetUserNameW(username
, &size
)) {
63 // ERROR_NONE_MAPPED means the user info is not filled out on this computer
64 return getUsernameError
== ERROR_NONE_MAPPED
?
65 NS_ERROR_NOT_AVAILABLE
: NS_ERROR_FAILURE
;
68 const DWORD level
= 2;
70 // If the NetUserGetInfo function has no full name info it will return
71 // success with an empty string.
72 NET_API_STATUS status
= NetUserGetInfo(nullptr, username
, level
, &info
);
73 if (status
!= NERR_Success
) {
74 // We have an error with NetUserGetInfo but we know the info is not
75 // filled in because GetUserNameExW returned ERROR_NONE_MAPPED.
76 return getUsernameError
== ERROR_NONE_MAPPED
?
77 NS_ERROR_NOT_AVAILABLE
: NS_ERROR_FAILURE
;
80 nsDependentString fullName
=
81 nsDependentString(reinterpret_cast<USER_INFO_2
*>(info
)->usri2_full_name
);
83 // NetUserGetInfo returns an empty string if the full name is not filled out
84 if (fullName
.Length() == 0) {
85 NetApiBufferFree(info
);
86 return NS_ERROR_NOT_AVAILABLE
;
89 *aFullname
= ToNewUnicode(fullName
);
90 NetApiBufferFree(info
);
93 return (*aFullname
) ? NS_OK
: NS_ERROR_FAILURE
;
97 nsUserInfo::GetDomain(char **aDomain
)
99 NS_ENSURE_ARG_POINTER(aDomain
);
102 const DWORD level
= 100;
104 NET_API_STATUS status
= NetWkstaGetInfo(nullptr, level
, &info
);
105 if (status
== NERR_Success
) {
107 ToNewUTF8String(nsDependentString(reinterpret_cast<WKSTA_INFO_100
*>(info
)->
109 NetApiBufferFree(info
);
112 return (*aDomain
) ? NS_OK
: NS_ERROR_FAILURE
;
116 nsUserInfo::GetEmailAddress(char **aEmailAddress
)
118 NS_ENSURE_ARG_POINTER(aEmailAddress
);
119 *aEmailAddress
= nullptr;
121 // RFC3696 says max length of an email address is 254
122 wchar_t emailAddress
[255];
123 DWORD size
= mozilla::ArrayLength(emailAddress
);
125 if (!GetUserNameExW(NameUserPrincipal
, emailAddress
, &size
)) {
126 DWORD getUsernameError
= GetLastError();
127 return getUsernameError
== ERROR_NONE_MAPPED
?
128 NS_ERROR_NOT_AVAILABLE
: NS_ERROR_FAILURE
;
131 *aEmailAddress
= ToNewUTF8String(nsDependentString(emailAddress
));
132 return (*aEmailAddress
) ? NS_OK
: NS_ERROR_FAILURE
;