Bug 1553558 - collect countryCode in system info off the main thread. r=mconley,Standard8

Differential Revision: https://phabricator.services.mozilla.com/D39343

--HG--
extra : moz-landing-system : lando
This commit is contained in:
mandy cheang 2019-08-02 12:55:38 +00:00
Родитель cd0cc4ccd7
Коммит dcf8e881eb
5 изменённых файлов: 95 добавлений и 19 удалений

Просмотреть файл

@ -191,7 +191,7 @@ var ensureKnownRegion = async function(ss) {
// Store the result of the geoip request as well as any other values and
// telemetry which depend on it.
function storeRegion(region) {
async function storeRegion(region) {
let isTimezoneUS = isUSTimezone();
// If it's a US region, but not a US timezone, we don't store the value.
// This works because no region defaults to ZZ (unknown) in nsURLFormatter
@ -214,7 +214,7 @@ function storeRegion(region) {
}
// telemetry to compare our geoip response with platform-specific country data.
// On Mac and Windows, we can get a country code via sysinfo
let platformCC = Services.sysinfo.get("countryCode");
let platformCC = await Services.sysinfo.countryCode;
if (platformCC) {
let probeUSMismatched, probeNonUSMismatched;
switch (Services.appinfo.OS) {
@ -300,7 +300,7 @@ function fetchRegion(ss) {
// Even if we timed out, we want to save the region and everything
// related so next startup sees the value and doesn't retry this dance.
if (result) {
storeRegion(result);
storeRegion(result).catch(Cu.reportError);
}
Services.telemetry
.getHistogramById("SEARCH_SERVICE_COUNTRY_FETCH_RESULT")

Просмотреть файл

@ -45,7 +45,7 @@ add_task(async function test_location() {
}
if (probeUSMismatched && probeNonUSMismatched) {
let countryCode = Services.sysinfo.get("countryCode");
let countryCode = await Services.sysinfo.countryCode;
print("Platform says the country-code is", countryCode);
let expectedResult;
let hid;

Просмотреть файл

@ -253,7 +253,7 @@ nsresult GetInstallYear(uint32_t& aYear) {
return NS_OK;
}
nsresult GetCountryCode(nsAString& aCountryCode) {
nsresult CollectCountryCode(nsAString& aCountryCode) {
GEOID geoid = GetUserGeoID(GEOCLASS_NATION);
if (geoid == GEOID_NOT_AVAILABLE) {
return NS_ERROR_NOT_AVAILABLE;
@ -827,12 +827,6 @@ nsresult nsSystemInfo::Init() {
}
}
nsAutoString countryCode;
if (NS_SUCCEEDED(GetCountryCode(countryCode))) {
rv = SetPropertyAsAString(NS_LITERAL_STRING("countryCode"), countryCode);
NS_ENSURE_SUCCESS(rv, rv);
}
uint32_t installYear = 0;
if (NS_SUCCEEDED(GetInstallYear(installYear))) {
rv = SetPropertyAsUint32(NS_LITERAL_STRING("installYear"), installYear);
@ -873,12 +867,6 @@ nsresult nsSystemInfo::Init() {
#endif
#if defined(XP_MACOSX)
nsAutoString countryCode;
if (NS_SUCCEEDED(GetSelectedCityInfo(countryCode))) {
rv = SetPropertyAsAString(NS_LITERAL_STRING("countryCode"), countryCode);
NS_ENSURE_SUCCESS(rv, rv);
}
nsAutoCString modelId;
if (NS_SUCCEEDED(GetAppleModelId(modelId))) {
rv = SetPropertyAsACString(NS_LITERAL_STRING("appleModelId"), modelId);
@ -1114,6 +1102,14 @@ static bool GetJSObjForDiskInfo(JSContext* aCx, JS::Handle<JSObject*> aParent,
}
#endif
RefPtr<mozilla::LazyIdleThread> nsSystemInfo::GetHelperThread() {
if (!mLazyHelperThread) {
mLazyHelperThread =
new LazyIdleThread(3000, NS_LITERAL_CSTRING("SystemInfoIdleThread"));
}
return mLazyHelperThread;
}
NS_IMETHODIMP
nsSystemInfo::GetDiskInfo(JSContext* aCx, Promise** aResult) {
NS_ENSURE_ARG_POINTER(aResult);
@ -1133,8 +1129,8 @@ nsSystemInfo::GetDiskInfo(JSContext* aCx, Promise** aResult) {
}
if (!mDiskInfoPromise) {
RefPtr<LazyIdleThread> lazyIOThread =
new LazyIdleThread(3000, NS_LITERAL_CSTRING("SystemInfoIdleThread"));
RefPtr<mozilla::LazyIdleThread> lazyIOThread = GetHelperThread();
mDiskInfoPromise = InvokeAsync(lazyIOThread, __func__, []() {
DiskInfo info;
nsresult rv = CollectDiskInfo(info);
@ -1183,3 +1179,69 @@ nsSystemInfo::GetDiskInfo(JSContext* aCx, Promise** aResult) {
}
NS_IMPL_ISUPPORTS_INHERITED(nsSystemInfo, nsHashPropertyBag, nsISystemInfo)
NS_IMETHODIMP
nsSystemInfo::GetCountryCode(JSContext* aCx, Promise** aResult) {
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nullptr;
if (!XRE_IsParentProcess()) {
return NS_ERROR_FAILURE;
}
#if defined(XP_MACOSX) || defined(XP_WIN)
nsIGlobalObject* global = xpc::CurrentNativeGlobal(aCx);
if (NS_WARN_IF(!global)) {
return NS_ERROR_FAILURE;
}
ErrorResult erv;
RefPtr<Promise> promise = Promise::Create(global, erv);
if (NS_WARN_IF(erv.Failed())) {
return erv.StealNSResult();
}
if (!mCountryCodePromise) {
RefPtr<mozilla::LazyIdleThread> lazyIOThread = GetHelperThread();
mCountryCodePromise = InvokeAsync(lazyIOThread, __func__, []() {
nsAutoString countryCode;
# ifdef XP_MACOSX
nsresult rv = GetSelectedCityInfo(countryCode);
# endif
# ifdef XP_WIN
nsresult rv = CollectCountryCode(countryCode);
# endif
if (NS_SUCCEEDED(rv)) {
return CountryCodePromise::CreateAndResolve(countryCode, __func__);
}
return CountryCodePromise::CreateAndReject(rv, __func__);
});
}
RefPtr<Promise> capturedPromise = promise;
mCountryCodePromise->Then(
GetMainThreadSerialEventTarget(), __func__,
[capturedPromise](const nsString& countryCode) {
RefPtr<nsIGlobalObject> global = capturedPromise->GetGlobalObject();
AutoJSAPI jsapi;
if (!global || !jsapi.Init(global)) {
capturedPromise->MaybeReject(NS_ERROR_UNEXPECTED);
return;
}
JSContext* cx = jsapi.cx();
JS::Rooted<JSString*> jsCountryCode(
cx, JS_NewUCStringCopyZ(cx, countryCode.get()));
JS::Rooted<JS::Value> val(cx, JS::StringValue(jsCountryCode));
capturedPromise->MaybeResolve(val);
},
[capturedPromise](const nsresult rv) {
// Resolve with null when countryCode is not available from the system
capturedPromise->MaybeResolve(JS::NullHandleValue);
});
promise.forget(aResult);
#endif
return NS_OK;
}

Просмотреть файл

@ -10,6 +10,7 @@
#include "nsHashPropertyBag.h"
#include "nsISystemInfo.h"
#include "mozilla/MozPromise.h"
#include "mozilla/LazyIdleThread.h"
#ifdef MOZ_WIDGET_ANDROID
# include "mozilla/dom/PContent.h"
@ -30,6 +31,9 @@ struct DiskInfo {
typedef mozilla::MozPromise<DiskInfo, nsresult, /* IsExclusive */ false>
DiskInfoPromise;
typedef mozilla::MozPromise<nsAutoString, nsresult, /* IsExclusive */ false>
CountryCodePromise;
class nsSystemInfo final : public nsISystemInfo, public nsHashPropertyBag {
public:
NS_DECL_ISUPPORTS_INHERITED
@ -59,6 +63,9 @@ class nsSystemInfo final : public nsISystemInfo, public nsHashPropertyBag {
~nsSystemInfo();
RefPtr<DiskInfoPromise> mDiskInfoPromise;
RefPtr<CountryCodePromise> mCountryCodePromise;
RefPtr<mozilla::LazyIdleThread> mLazyHelperThread;
RefPtr<mozilla::LazyIdleThread> GetHelperThread();
};
#define NS_SYSTEMINFO_CONTRACTID "@mozilla.org/system-info;1"

Просмотреть файл

@ -15,4 +15,11 @@ interface nsISystemInfo : nsISupports
*/
[implicit_jscontext]
readonly attribute Promise diskInfo;
/**
* Asynchronously get CountryCode info.
* Note: only implemented on macOS and Windows, will return null elsewhere.
*/
[implicit_jscontext]
readonly attribute Promise countryCode;
};