diff --git a/testing/modules/tests/xpcshell/test_mockRegistrar.js b/testing/modules/tests/xpcshell/test_mockRegistrar.js index cfeadcfa56f0..e24fb556bdd9 100644 --- a/testing/modules/tests/xpcshell/test_mockRegistrar.js +++ b/testing/modules/tests/xpcshell/test_mockRegistrar.js @@ -3,42 +3,39 @@ const {MockRegistrar} = ChromeUtils.import("resource://testing-common/MockRegistrar.jsm"); -function userInfo(username) { - this.username = username; +function platformInfo(injectedValue) { + this.platformVersion = injectedValue; } -userInfo.prototype = { - fullname: "fullname", - emailAddress: "emailAddress", - domain: "domain", - QueryInterface: ChromeUtils.generateQI([Ci.nsIUserInfo]), +platformInfo.prototype = { + platformVersion: "some version", + platformBuildID: "some id", + QueryInterface: ChromeUtils.generateQI([Ci.nsIPlatformInfo]), }; add_test(function test_register() { - let localUserInfo = { - fullname: "fullname", - username: "localusername", - emailAddress: "emailAddress", - domain: "domain", - QueryInterface: ChromeUtils.generateQI([Ci.nsIUserInfo]), + let localPlatformInfo = { + platformVersion: "local version", + platformBuildID: "local id", + QueryInterface: ChromeUtils.generateQI([Ci.nsIPlatformInfo]), }; - MockRegistrar.register("@mozilla.org/userinfo;1", localUserInfo); - Assert.equal(Cc["@mozilla.org/userinfo;1"].createInstance(Ci.nsIUserInfo).username, "localusername"); + MockRegistrar.register("@mozilla.org/xre/app-info;1", localPlatformInfo); + Assert.equal(Cc["@mozilla.org/xre/app-info;1"].createInstance(Ci.nsIPlatformInfo).platformVersion, "local version"); run_next_test(); }); add_test(function test_register_with_arguments() { - MockRegistrar.register("@mozilla.org/userinfo;1", userInfo, ["username"]); - Assert.equal(Cc["@mozilla.org/userinfo;1"].createInstance(Ci.nsIUserInfo).username, "username"); + MockRegistrar.register("@mozilla.org/xre/app-info;1", platformInfo, ["override"]); + Assert.equal(Cc["@mozilla.org/xre/app-info;1"].createInstance(Ci.nsIPlatformInfo).platformVersion, "override"); run_next_test(); }); add_test(function test_register_twice() { - MockRegistrar.register("@mozilla.org/userinfo;1", userInfo, ["originalname"]); - Assert.equal(Cc["@mozilla.org/userinfo;1"].createInstance(Ci.nsIUserInfo).username, "originalname"); + MockRegistrar.register("@mozilla.org/xre/app-info;1", platformInfo, ["override"]); + Assert.equal(Cc["@mozilla.org/xre/app-info;1"].createInstance(Ci.nsIPlatformInfo).platformVersion, "override"); - MockRegistrar.register("@mozilla.org/userinfo;1", userInfo, ["newname"]); - Assert.equal(Cc["@mozilla.org/userinfo;1"].createInstance(Ci.nsIUserInfo).username, "newname"); + MockRegistrar.register("@mozilla.org/xre/app-info;1", platformInfo, ["override again"]); + Assert.equal(Cc["@mozilla.org/xre/app-info;1"].createInstance(Ci.nsIPlatformInfo).platformVersion, "override again"); run_next_test(); }); diff --git a/toolkit/components/build/components.conf b/toolkit/components/build/components.conf index 2609db6e6b61..b6cf5df60238 100644 --- a/toolkit/components/build/components.conf +++ b/toolkit/components/build/components.conf @@ -157,13 +157,6 @@ Classes = [ 'headers': ['/toolkit/components/url-classifier/nsUrlClassifierUtils.h'], 'constructor': 'nsUrlClassifierUtils::GetXPCOMSingleton', }, - { - 'name': 'UserInfo', - 'cid': '{14c13684-1dd2-11b2-9463-bb10ba742554}', - 'contract_ids': ['@mozilla.org/userinfo;1'], - 'type': 'nsUserInfo', - 'headers': ['/toolkit/components/startup/nsUserInfo.h'], - }, ] if defined('MOZ_UPDATER') and not IS_ANDROID: diff --git a/toolkit/components/startup/moz.build b/toolkit/components/startup/moz.build index dbd580384615..12d7cba32eda 100644 --- a/toolkit/components/startup/moz.build +++ b/toolkit/components/startup/moz.build @@ -18,20 +18,6 @@ UNIFIED_SOURCES += [ 'StartupTimeline.cpp', ] -if CONFIG['OS_ARCH'] == 'WINNT': - # This file cannot be built in unified mode because of name clashes with Windows headers. - SOURCES += [ - 'nsUserInfoWin.cpp', - ] -elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa': - UNIFIED_SOURCES += [ - 'nsUserInfoMac.mm', - ] -else: - UNIFIED_SOURCES += [ - 'nsUserInfoUnix.cpp', - ] - FINAL_LIBRARY = 'xul' with Files('**'): diff --git a/toolkit/components/startup/nsUserInfo.h b/toolkit/components/startup/nsUserInfo.h deleted file mode 100644 index df3f81adefcb..000000000000 --- a/toolkit/components/startup/nsUserInfo.h +++ /dev/null @@ -1,23 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef __nsUserInfo_h -#define __nsUserInfo_h - -#include "nsIUserInfo.h" - -class nsUserInfo : public nsIUserInfo - -{ - public: - nsUserInfo(void); - - NS_DECL_ISUPPORTS - NS_DECL_NSIUSERINFO - - protected: - virtual ~nsUserInfo(); -}; - -#endif /* __nsUserInfo_h */ diff --git a/toolkit/components/startup/nsUserInfoMac.h b/toolkit/components/startup/nsUserInfoMac.h deleted file mode 100644 index 9b259d53f05e..000000000000 --- a/toolkit/components/startup/nsUserInfoMac.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef __nsUserInfoMac_h -#define __nsUserInfoMac_h - -#include "nsIUserInfo.h" -#include "nsReadableUtils.h" - -class nsUserInfo : public nsIUserInfo { - public: - nsUserInfo(); - - NS_DECL_ISUPPORTS - NS_DECL_NSIUSERINFO - - nsresult GetPrimaryEmailAddress(nsACString &aEmailAddress); - - protected: - virtual ~nsUserInfo() {} -}; - -#endif /* __nsUserInfo_h */ diff --git a/toolkit/components/startup/nsUserInfoMac.mm b/toolkit/components/startup/nsUserInfoMac.mm deleted file mode 100644 index ac6806519a7b..000000000000 --- a/toolkit/components/startup/nsUserInfoMac.mm +++ /dev/null @@ -1,73 +0,0 @@ -/* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "nsUserInfoMac.h" -#include "nsObjCExceptions.h" -#include "nsString.h" -#include "mozilla/Span.h" - -#import -#import - -NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) - -nsUserInfo::nsUserInfo() {} - -NS_IMETHODIMP -nsUserInfo::GetFullname(nsAString& aFullname) { - NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT - - CopyUTF8toUTF16(mozilla::MakeStringSpan([NSFullUserName() UTF8String]), aFullname); - return NS_OK; - - NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT -} - -NS_IMETHODIMP -nsUserInfo::GetUsername(nsACString& aUsername) { - NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT - - aUsername.Assign([NSUserName() UTF8String]); - return NS_OK; - - NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT -} - -nsresult nsUserInfo::GetPrimaryEmailAddress(nsACString& aEmailAddress) { - NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT - - // Try to get this user's primary email from the system addressbook's "me card" - // (if they've filled it) - ABPerson* me = [[ABAddressBook sharedAddressBook] me]; - ABMultiValue* emailAddresses = [me valueForProperty:kABEmailProperty]; - if ([emailAddresses count] > 0) { - // get the index of the primary email, in case there are more than one - int primaryEmailIndex = [emailAddresses indexForIdentifier:[emailAddresses primaryIdentifier]]; - aEmailAddress.Assign([[emailAddresses valueAtIndex:primaryEmailIndex] UTF8String]); - return NS_OK; - } - - return NS_ERROR_FAILURE; - - NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT -} - -NS_IMETHODIMP -nsUserInfo::GetEmailAddress(nsACString& aEmailAddress) { - return GetPrimaryEmailAddress(aEmailAddress); -} - -NS_IMETHODIMP -nsUserInfo::GetDomain(nsACString& aDomain) { - nsAutoCString email; - if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) { - int32_t index = email.FindChar('@'); - if (index != -1) { - // chop off everything before, and including the '@' - aDomain.Assign(Substring(email, index + 1)); - } - } - return NS_OK; -} diff --git a/toolkit/components/startup/nsUserInfoUnix.cpp b/toolkit/components/startup/nsUserInfoUnix.cpp deleted file mode 100644 index 0a281d712de3..000000000000 --- a/toolkit/components/startup/nsUserInfoUnix.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "nsUserInfo.h" -#include "nsCRT.h" - -#include -#include -#include -#include - -#include "nsString.h" -#include "nsReadableUtils.h" -#include "nsNativeCharsetUtils.h" - -/* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */ -#if defined(NO_PW_GECOS) -# define PW_GECOS pw_name -#else -# define PW_GECOS pw_gecos -#endif - -nsUserInfo::nsUserInfo() {} - -nsUserInfo::~nsUserInfo() {} - -NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) - -NS_IMETHODIMP -nsUserInfo::GetFullname(nsAString& aFullname) { - struct passwd* pw = nullptr; - - pw = getpwuid(geteuid()); - - if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE; - -#ifdef DEBUG_sspitzer - printf("fullname = %s\n", pw->PW_GECOS); -#endif - - nsAutoCString fullname(pw->PW_GECOS); - - // now try to parse the GECOS information, which will be in the form - // Full Name, - eliminate the ", - // also, sometimes GECOS uses "&" to mean "the user name" so do - // the appropriate substitution - - // truncate at first comma (field delimiter) - int32_t index; - if ((index = fullname.Find(",")) != kNotFound) fullname.Truncate(index); - - // replace ampersand with username - if (pw->pw_name) { - nsAutoCString username(pw->pw_name); - if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0))) - username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0); - - fullname.ReplaceSubstring("&", username.get()); - } - - NS_CopyNativeToUnicode(fullname, aFullname); - - return NS_OK; -} - -NS_IMETHODIMP -nsUserInfo::GetUsername(nsACString& aUsername) { - struct passwd* pw = nullptr; - - // is this portable? those are POSIX compliant calls, but I need to check - pw = getpwuid(geteuid()); - - if (!pw || !pw->pw_name) return NS_ERROR_FAILURE; - -#ifdef DEBUG_sspitzer - printf("username = %s\n", pw->pw_name); -#endif - - aUsername.Assign(pw->pw_name); - - return NS_OK; -} - -NS_IMETHODIMP -nsUserInfo::GetDomain(nsACString& aDomain) { - nsresult rv = NS_ERROR_FAILURE; - - struct utsname buf; - char* domainname = nullptr; - - if (uname(&buf) < 0) { - return rv; - } - -#if defined(__linux__) - domainname = buf.domainname; -#endif - - if (domainname && domainname[0]) { - aDomain.Assign(domainname); - rv = NS_OK; - } else { - // try to get the hostname from the nodename - // on machines that use DHCP, domainname may not be set - // but the nodename might. - if (buf.nodename[0]) { - // if the nodename is foo.bar.org, use bar.org as the domain - char* pos = strchr(buf.nodename, '.'); - if (pos) { - aDomain.Assign(pos + 1); - rv = NS_OK; - } - } - } - - return rv; -} - -NS_IMETHODIMP -nsUserInfo::GetEmailAddress(nsACString& aEmailAddress) { - // use username + "@" + domain for the email address - nsresult rv; - - nsCString username; - nsCString domain; - - rv = GetUsername(username); - if (NS_FAILED(rv)) return rv; - - rv = GetDomain(domain); - if (NS_FAILED(rv)) return rv; - - if (username.IsEmpty() || domain.IsEmpty()) { - return NS_ERROR_FAILURE; - } - - aEmailAddress = username + NS_LITERAL_CSTRING("@") + domain; - return NS_OK; -} diff --git a/toolkit/components/startup/nsUserInfoWin.cpp b/toolkit/components/startup/nsUserInfoWin.cpp deleted file mode 100644 index d5741b381002..000000000000 --- a/toolkit/components/startup/nsUserInfoWin.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "nsUserInfo.h" - -#include "mozilla/ArrayUtils.h" // ArrayLength -#include "mozilla/Span.h" // MakeStringSpan -#include "nsString.h" -#include "windows.h" -#include "nsCRT.h" - -#define SECURITY_WIN32 -#include "lm.h" -#include "security.h" - -nsUserInfo::nsUserInfo() {} - -nsUserInfo::~nsUserInfo() {} - -NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) - -NS_IMETHODIMP -nsUserInfo::GetUsername(nsACString& aUsername) { - // UNLEN is the max username length as defined in lmcons.h - wchar_t username[UNLEN + 1]; - DWORD size = mozilla::ArrayLength(username); - if (!GetUserNameW(username, &size)) return NS_ERROR_FAILURE; - - CopyUTF16toUTF8(nsDependentString(username), aUsername); - return NS_OK; -} - -NS_IMETHODIMP -nsUserInfo::GetFullname(nsAString& aFullname) { - wchar_t fullName[512]; - DWORD size = mozilla::ArrayLength(fullName); - - if (GetUserNameExW(NameDisplay, fullName, &size)) { - aFullname.Assign(fullName); - } else { - DWORD getUsernameError = GetLastError(); - - // Try to use the net APIs regardless of the error because it may be - // able to obtain the information. - wchar_t username[UNLEN + 1]; - size = mozilla::ArrayLength(username); - if (!GetUserNameW(username, &size)) { - // ERROR_NONE_MAPPED means the user info is not filled out on this - // computer - return getUsernameError == ERROR_NONE_MAPPED ? NS_ERROR_NOT_AVAILABLE - : NS_ERROR_FAILURE; - } - - const DWORD level = 2; - LPBYTE info; - // If the NetUserGetInfo function has no full name info it will return - // success with an empty string. - NET_API_STATUS status = NetUserGetInfo(nullptr, username, level, &info); - if (status != NERR_Success) { - // We have an error with NetUserGetInfo but we know the info is not - // filled in because GetUserNameExW returned ERROR_NONE_MAPPED. - return getUsernameError == ERROR_NONE_MAPPED ? NS_ERROR_NOT_AVAILABLE - : NS_ERROR_FAILURE; - } - - nsDependentString fullName = nsDependentString( - reinterpret_cast(info)->usri2_full_name); - - // NetUserGetInfo returns an empty string if the full name is not filled out - if (fullName.Length() == 0) { - NetApiBufferFree(info); - return NS_ERROR_NOT_AVAILABLE; - } - - aFullname.Assign(fullName); - NetApiBufferFree(info); - } - - return NS_OK; -} - -NS_IMETHODIMP -nsUserInfo::GetDomain(nsACString& aDomain) { - const DWORD level = 100; - LPBYTE info; - NET_API_STATUS status = NetWkstaGetInfo(nullptr, level, &info); - if (status != NERR_Success) { - return NS_ERROR_FAILURE; - } - - CopyUTF16toUTF8(nsDependentString( - reinterpret_cast(info)->wki100_langroup), - aDomain); - NetApiBufferFree(info); - - return NS_OK; -} - -NS_IMETHODIMP -nsUserInfo::GetEmailAddress(nsACString& aEmailAddress) { - // RFC3696 says max length of an email address is 254 - wchar_t emailAddress[255]; - DWORD size = mozilla::ArrayLength(emailAddress); - - if (!GetUserNameExW(NameUserPrincipal, emailAddress, &size)) { - DWORD getUsernameError = GetLastError(); - return getUsernameError == ERROR_NONE_MAPPED ? NS_ERROR_NOT_AVAILABLE - : NS_ERROR_FAILURE; - } - - CopyUTF16toUTF8(nsDependentString(emailAddress), aEmailAddress); - return NS_OK; -} diff --git a/toolkit/components/startup/public/moz.build b/toolkit/components/startup/public/moz.build index 5894b6c51792..e810d9172ee4 100644 --- a/toolkit/components/startup/public/moz.build +++ b/toolkit/components/startup/public/moz.build @@ -6,7 +6,6 @@ XPIDL_SOURCES += [ 'nsIAppStartup.idl', - 'nsIUserInfo.idl', ] XPIDL_MODULE = 'appstartup' diff --git a/toolkit/components/startup/public/nsIUserInfo.idl b/toolkit/components/startup/public/nsIUserInfo.idl deleted file mode 100644 index 9b8e01f16548..000000000000 --- a/toolkit/components/startup/public/nsIUserInfo.idl +++ /dev/null @@ -1,32 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "nsISupports.idl" - -[scriptable, uuid(6c1034f0-1dd2-11b2-aa14-e6657ed7bb0b)] -interface nsIUserInfo : nsISupports -{ - /* these are things the system may know about the current user */ - - readonly attribute AString fullname; - - readonly attribute ACString emailAddress; - - /* should this be an AString? */ - readonly attribute ACString username; - - readonly attribute ACString domain; -}; - -%{C++ - -// 14c13684-1dd2-11b2-9463-bb10ba742554 -#define NS_USERINFO_CID \ -{ 0x14c13684, 0x1dd2, 0x11b2, \ - {0x94, 0x63, 0xbb, 0x10, 0xba, 0x74, 0x25, 0x54}} - -#define NS_USERINFO_CONTRACTID "@mozilla.org/userinfo;1" - -%}