Bug 1335983 - Migrate nsCollation::CreateCollection to use LocaleService::GetAppLocale. r=jfkthame

MozReview-Commit-ID: 5GEnL7Kihpj

--HG--
extra : rebase_source : 413d880035b49da82db0530379425d31ac31a82e
This commit is contained in:
Zibi Braniecki 2017-02-07 12:52:03 -08:00
parent 0f8ff5260b
commit b8f991e41b
16 changed files with 65 additions and 222 deletions

View File

@ -9,8 +9,6 @@
#include "txExpr.h"
#include "txCore.h"
#include "nsCollationCID.h"
#include "nsILocale.h"
#include "nsILocaleService.h"
#include "nsIServiceManager.h"
#include "prmem.h"
@ -35,25 +33,16 @@ nsresult txResultStringComparator::init(const nsAFlatString& aLanguage)
{
nsresult rv;
nsCOMPtr<nsILocaleService> localeService =
do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsILocale> locale;
if (!aLanguage.IsEmpty()) {
rv = localeService->NewLocale(aLanguage,
getter_AddRefs(locale));
}
else {
rv = localeService->GetApplicationLocale(getter_AddRefs(locale));
}
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = colFactory->CreateCollation(locale, getter_AddRefs(mCollation));
if (aLanguage.IsEmpty()) {
rv = colFactory->CreateCollation(getter_AddRefs(mCollation));
} else {
rv = colFactory->CreateCollationForLocale(NS_ConvertUTF16toUTF8(aLanguage), getter_AddRefs(mCollation));
}
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;

View File

@ -52,8 +52,6 @@
#include "nsIScriptableDateFormat.h"
#include "nsICollation.h"
#include "nsCollationCID.h"
#include "nsILocale.h"
#include "nsILocaleService.h"
#include "nsIConsoleService.h"
#include "nsEscape.h"
@ -125,27 +123,14 @@ nsICollation*
nsXULContentUtils::GetCollation()
{
if (!gCollation) {
nsresult rv;
// get a locale service
nsCOMPtr<nsILocaleService> localeService =
do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsILocale> locale;
rv = localeService->GetApplicationLocale(getter_AddRefs(locale));
if (NS_SUCCEEDED(rv) && locale) {
nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
if (colFactory) {
rv = colFactory->CreateCollation(locale, &gCollation);
NS_ASSERTION(NS_SUCCEEDED(rv),
"couldn't create collation instance");
} else
NS_ERROR("couldn't create instance of collation factory");
} else
NS_ERROR("unable to get application locale");
nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
if (colFactory) {
DebugOnly<nsresult> rv = colFactory->CreateCollation(&gCollation);
NS_ASSERTION(NS_SUCCEEDED(rv),
"couldn't create collation instance");
} else
NS_ERROR("couldn't get locale factory");
NS_ERROR("couldn't create instance of collation factory");
}
return gCollation;

View File

@ -4,7 +4,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCollationMacUC.h"
#include "nsILocaleService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsIServiceManager.h"
@ -16,7 +15,6 @@ NS_IMPL_ISUPPORTS(nsCollationMacUC, nsICollation)
nsCollationMacUC::nsCollationMacUC()
: mInit(false)
, mHasCollator(false)
, mLocaleICU(nullptr)
, mLastStrength(-1)
, mCollatorICU(nullptr)
{ }
@ -28,10 +26,6 @@ nsCollationMacUC::~nsCollationMacUC()
#endif
CleanUpCollator();
NS_ASSERTION(NS_SUCCEEDED(res), "CleanUpCollator failed");
if (mLocaleICU) {
free(mLocaleICU);
mLocaleICU = nullptr;
}
}
nsresult nsCollationMacUC::ConvertStrength(const int32_t aNSStrength,
@ -68,28 +62,6 @@ nsresult nsCollationMacUC::ConvertStrength(const int32_t aNSStrength,
return NS_OK;
}
nsresult nsCollationMacUC::ConvertLocaleICU(nsILocale* aNSLocale, char** aICULocale)
{
NS_ENSURE_ARG_POINTER(aNSLocale);
NS_ENSURE_ARG_POINTER(aICULocale);
nsAutoString localeString;
nsresult res = aNSLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"), localeString);
NS_ENSURE_TRUE(NS_SUCCEEDED(res) && !localeString.IsEmpty(),
NS_ERROR_FAILURE);
NS_LossyConvertUTF16toASCII tmp(localeString);
tmp.ReplaceChar('-', '_');
char* locale = (char*)malloc(tmp.Length() + 1);
if (!locale) {
return NS_ERROR_OUT_OF_MEMORY;
}
strcpy(locale, tmp.get());
*aICULocale = locale;
return NS_OK;
}
nsresult nsCollationMacUC::EnsureCollator(const int32_t newStrength)
{
NS_ENSURE_TRUE(mInit, NS_ERROR_NOT_INITIALIZED);
@ -100,11 +72,9 @@ nsresult nsCollationMacUC::EnsureCollator(const int32_t newStrength)
res = CleanUpCollator();
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(mLocaleICU, NS_ERROR_NOT_INITIALIZED);
UErrorCode status;
status = U_ZERO_ERROR;
mCollatorICU = ucol_open(mLocaleICU, &status);
mCollatorICU = ucol_open(mLocale.get(), &status);
NS_ENSURE_TRUE(U_SUCCESS(status), NS_ERROR_FAILURE);
UCollationStrength strength;
@ -142,22 +112,12 @@ nsresult nsCollationMacUC::CleanUpCollator(void)
return NS_OK;
}
NS_IMETHODIMP nsCollationMacUC::Initialize(nsILocale* locale)
NS_IMETHODIMP nsCollationMacUC::Initialize(const nsACString& locale)
{
NS_ENSURE_TRUE((!mInit), NS_ERROR_ALREADY_INITIALIZED);
nsCOMPtr<nsILocale> appLocale;
nsresult rv;
if (!locale) {
nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
NS_ENSURE_SUCCESS(rv, rv);
locale = appLocale;
}
rv = ConvertLocaleICU(locale, &mLocaleICU);
NS_ENSURE_SUCCESS(rv, rv);
mLocale = locale;
mInit = true;
return NS_OK;

View File

@ -6,9 +6,10 @@
#ifndef nsCollationMacUC_h_
#define nsCollationMacUC_h_
#include "mozilla/Attributes.h"
#include "nsICollation.h"
#include "nsCollation.h"
#include "mozilla/Attributes.h"
#include "nsString.h"
#include "unicode/ucol.h"
@ -26,7 +27,6 @@ public:
protected:
~nsCollationMacUC();
nsresult ConvertLocaleICU(nsILocale* aNSLocale, char** aICULocale);
nsresult ConvertStrength(const int32_t aStrength,
UCollationStrength* aStrengthOut,
UColAttributeValue* aCaseLevelOut);
@ -36,7 +36,7 @@ protected:
private:
bool mInit;
bool mHasCollator;
char* mLocaleICU;
nsCString mLocale;
int32_t mLastStrength;
UCollator* mCollatorICU;
};

View File

@ -10,6 +10,7 @@
#include "nsIUnicodeEncoder.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/dom/EncodingUtils.h"
#include "mozilla/intl/LocaleService.h"
using mozilla::dom::EncodingUtils;
@ -19,19 +20,29 @@ NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID);
NS_IMPL_ISUPPORTS(nsCollationFactory, nsICollationFactory)
nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** instancePtr)
nsresult nsCollationFactory::CreateCollation(nsICollation** instancePtr)
{
nsAutoCString appLocale;
mozilla::intl::LocaleService::GetInstance()->GetAppLocale(appLocale);
return CreateCollationForLocale(appLocale, instancePtr);
}
nsresult
nsCollationFactory::CreateCollationForLocale(const nsACString& locale, nsICollation** instancePtr)
{
// Create a collation interface instance.
//
nsICollation *inst;
nsresult res;
res = CallCreateInstance(kCollationCID, &inst);
if (NS_FAILED(res)) {
return res;
}
inst->Initialize(locale);
*instancePtr = inst;
return res;

View File

@ -14,7 +14,7 @@
class nsIUnicodeEncoder;
// Create a collation interface for an input locale.
// Create a collation interface for the current app's locale.
//
class nsCollationFactory final : public nsICollationFactory {
@ -23,7 +23,8 @@ class nsCollationFactory final : public nsICollationFactory {
public:
NS_DECL_ISUPPORTS
NS_IMETHOD CreateCollation(nsILocale* locale, nsICollation** instancePtr) override;
NS_IMETHOD CreateCollation(nsICollation** instancePtr) override;
NS_IMETHOD CreateCollationForLocale(const nsACString& locale, nsICollation** instancePtr) override;
nsCollationFactory() {}
};

View File

@ -11,17 +11,18 @@ interface nsICollation;
interface nsICollationFactory : nsISupports
{
/**
* Create the collation for a given locale.
* Create a new collation for the current application locale.
*
* Use NULL as the locale parameter to use the user's locale preference
* from the operating system.
*
* @param locale
* The locale for which to create the collation or null to use
* user preference.
* @return A collation for the given locale.
* @return A new collation.
*/
nsICollation CreateCollation(in nsILocale locale);
nsICollation CreateCollation();
/**
* Create a new collation for a given locale.
*
* @return A new collation.
*/
nsICollation CreateCollationForLocale(in ACString locale);
};
[scriptable, uuid(b0132cc0-3786-4557-9874-910d7def5f93)]
@ -43,7 +44,7 @@ interface nsICollation : nsISupports {
const long kCollationCaseInSensitive = (kCollationCaseInsensitiveAscii | kCollationAccentInsenstive);
// init this interface to a specified locale (should only be called by collation factory)
void initialize(in nsILocale locale);
void initialize(in ACString locale);
// compare two strings
// result is same as strcmp

View File

@ -20,11 +20,9 @@ function run_test()
];
function test(locale, expected) {
var localeSvc = Cc["@mozilla.org/intl/nslocaleservice;1"].
getService(Ci.nsILocaleService);
var collator = Cc["@mozilla.org/intl/collation-factory;1"].
createInstance(Ci.nsICollationFactory).
CreateCollation(localeSvc.newLocale(locale));
CreateCollationForLocale(locale);
var strength = Ci.nsICollation.kCollationStrengthDefault;
var actual = input.sort((x, y) => collator.compareString(strength, x,y));
deepEqual(actual, expected, locale);

View File

@ -8,7 +8,6 @@
#include "nsCollationUnix.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsILocaleService.h"
#include "nsIPlatformCharset.h"
#include "nsPosixLocale.h"
#include "nsCOMPtr.h"
@ -44,7 +43,7 @@ nsCollationUnix::~nsCollationUnix()
NS_IMPL_ISUPPORTS(nsCollationUnix, nsICollation)
nsresult nsCollationUnix::Initialize(nsILocale* locale)
nsresult nsCollationUnix::Initialize(const nsACString& locale)
{
#define kPlatformLocaleLength 64
NS_ASSERTION(!mCollation, "Should only be initialized once");
@ -53,46 +52,12 @@ nsresult nsCollationUnix::Initialize(nsILocale* locale)
mCollation = new nsCollation;
// default platform locale
mLocale.Assign('C');
nsAutoString localeStr;
NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_COLLATE##PLATFORM");
// get locale string, use app default if no locale specified
if (locale == nullptr) {
nsCOMPtr<nsILocaleService> localeService =
do_GetService(NS_LOCALESERVICE_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
nsCOMPtr<nsILocale> appLocale;
res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
if (NS_SUCCEEDED(res)) {
res = appLocale->GetCategory(aCategory, localeStr);
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info");
}
}
}
else {
res = locale->GetCategory(aCategory, localeStr);
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info");
}
// Get platform locale and charset name from locale, if available
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
// keep the same behavior as 4.x as well as avoiding Linux collation key problem
if (localeStr.LowerCaseEqualsLiteral("en_us")) { // note: locale is in platform format
localeStr.Assign('C');
}
nsPosixLocale::GetPlatformLocale(localeStr, mLocale);
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
nsAutoCString mappedCharset;
res = platformCharset->GetDefaultCharsetForLocale(NS_ConvertUTF8toUTF16(locale), mappedCharset);
if (NS_SUCCEEDED(res)) {
nsAutoCString mappedCharset;
res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
if (NS_SUCCEEDED(res)) {
mCollation->SetCharset(mappedCharset.get());
}
mCollation->SetCharset(mappedCharset.get());
}
}

View File

@ -7,7 +7,6 @@
#include "nsCollationWin.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsILocaleService.h"
#include "nsIPlatformCharset.h"
#include "nsWin32Locale.h"
#include "nsCOMPtr.h"
@ -30,7 +29,7 @@ nsCollationWin::~nsCollationWin()
delete mCollation;
}
nsresult nsCollationWin::Initialize(nsILocale* locale)
nsresult nsCollationWin::Initialize(const nsACString& locale)
{
NS_ASSERTION(!mCollation, "Should only be initialized once.");
@ -38,32 +37,14 @@ nsresult nsCollationWin::Initialize(nsILocale* locale)
mCollation = new nsCollation;
NS_ConvertASCIItoUTF16 wideLocale(locale);
// default LCID (en-US)
mLCID = 1033;
nsAutoString localeStr;
// get locale string, use app default if no locale specified
if (!locale) {
nsCOMPtr<nsILocaleService> localeService =
do_GetService(NS_LOCALESERVICE_CONTRACTID);
if (localeService) {
nsCOMPtr<nsILocale> appLocale;
res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
if (NS_SUCCEEDED(res)) {
res = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"),
localeStr);
}
}
}
else {
res = locale->GetCategory(NS_LITERAL_STRING("NSILOCALE_COLLATE"),
localeStr);
}
// Get LCID and charset name from locale, if available
LCID lcid;
res = nsWin32Locale::GetPlatformLocale(localeStr, &lcid);
res = nsWin32Locale::GetPlatformLocale(wideLocale, &lcid);
if (NS_SUCCEEDED(res)) {
mLCID = lcid;
}
@ -72,7 +53,7 @@ nsresult nsCollationWin::Initialize(nsILocale* locale)
do_GetService(NS_PLATFORMCHARSET_CONTRACTID);
if (platformCharset) {
nsAutoCString mappedCharset;
res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
res = platformCharset->GetDefaultCharsetForLocale(wideLocale, mappedCharset);
if (NS_SUCCEEDED(res)) {
mCollation->SetCharset(mappedCharset.get());
}

View File

@ -126,21 +126,11 @@ private:
nsresult rv;
if (!mCollation) {
nsCOMPtr<nsILocaleService> localeService =
do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsILocale> locale;
rv = localeService->GetApplicationLocale(getter_AddRefs(locale));
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = colFactory->CreateCollation(locale, getter_AddRefs(mCollation));
}
}
rv = colFactory->CreateCollation(getter_AddRefs(mCollation));
}
if (NS_FAILED(rv)) {

View File

@ -22,8 +22,6 @@
#ifdef THREADSAFE_I18N
#include "nsCollationCID.h"
#include "nsICollation.h"
#include "nsILocale.h"
#include "nsILocaleService.h"
#endif
#include "nsIFile.h"
#include "nsURLHelper.h"
@ -120,20 +118,12 @@ nsDirectoryIndexStream::Init(nsIFile* aDir)
}
#ifdef THREADSAFE_I18N
nsCOMPtr<nsILocaleService> ls = do_GetService(NS_LOCALESERVICE_CONTRACTID,
&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsILocale> locale;
rv = ls->GetApplicationLocale(getter_AddRefs(locale));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsICollationFactory> cf = do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID,
&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsICollation> coll;
rv = cf->CreateCollation(locale, getter_AddRefs(coll));
rv = cf->CreateCollation(getter_AddRefs(coll));
if (NS_FAILED(rv)) return rv;
mArray.Sort(compare, coll);

View File

@ -14,8 +14,6 @@
#include "nsEmbedCID.h"
#include "nsThreadUtils.h"
#include "mozStoragePrivateHelpers.h"
#include "nsILocale.h"
#include "nsILocaleService.h"
#include "nsIXPConnect.h"
#include "nsIObserverService.h"
#include "nsIPropertyBag2.h"
@ -608,19 +606,6 @@ Service::getLocaleCollation()
if (mLocaleCollation)
return mLocaleCollation;
nsCOMPtr<nsILocaleService> svc(do_GetService(NS_LOCALESERVICE_CONTRACTID));
if (!svc) {
NS_WARNING("Could not get locale service");
return nullptr;
}
nsCOMPtr<nsILocale> appLocale;
nsresult rv = svc->GetApplicationLocale(getter_AddRefs(appLocale));
if (NS_FAILED(rv)) {
NS_WARNING("Could not get application locale");
return nullptr;
}
nsCOMPtr<nsICollationFactory> collFact =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
if (!collFact) {
@ -628,7 +613,7 @@ Service::getLocaleCollation()
return nullptr;
}
rv = collFact->CreateCollation(appLocale, getter_AddRefs(mLocaleCollation));
nsresult rv = collFact->CreateCollation(getter_AddRefs(mLocaleCollation));
if (NS_FAILED(rv)) {
NS_WARNING("Could not create collation");
return nullptr;

View File

@ -232,11 +232,9 @@ function setup() {
gUtf16Conn = createUtf16Database();
initTableWithStrings(gStrings, gUtf16Conn);
let localeSvc = Cc["@mozilla.org/intl/nslocaleservice;1"].
getService(Ci.nsILocaleService);
let collFact = Cc["@mozilla.org/intl/collation-factory;1"].
createInstance(Ci.nsICollationFactory);
gLocaleCollation = collFact.CreateCollation(localeSvc.getApplicationLocale());
gLocaleCollation = collFact.CreateCollation();
}
// Test Runs

View File

@ -20,7 +20,6 @@
#include "nsTArray.h"
#include "nsCollationCID.h"
#include "nsILocaleService.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
#include "nsPromiseFlatString.h"
@ -4479,18 +4478,11 @@ nsNavHistory::GetCollation()
if (mCollation)
return mCollation;
// locale
nsCOMPtr<nsILocale> locale;
nsCOMPtr<nsILocaleService> ls(do_GetService(NS_LOCALESERVICE_CONTRACTID));
NS_ENSURE_TRUE(ls, nullptr);
nsresult rv = ls->GetApplicationLocale(getter_AddRefs(locale));
NS_ENSURE_SUCCESS(rv, nullptr);
// collation
nsCOMPtr<nsICollationFactory> cfact =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
NS_ENSURE_TRUE(cfact, nullptr);
rv = cfact->CreateCollation(locale, getter_AddRefs(mCollation));
nsresult rv = cfact->CreateCollation(getter_AddRefs(mCollation));
NS_ENSURE_SUCCESS(rv, nullptr);
return mCollation;

View File

@ -3868,12 +3868,9 @@ SearchService.prototype = {
alphaEngines.push(this._engines[engine.name]);
}
let locale = Cc["@mozilla.org/intl/nslocaleservice;1"]
.getService(Ci.nsILocaleService)
.newLocale(getLocale());
let collation = Cc["@mozilla.org/intl/collation-factory;1"]
.createInstance(Ci.nsICollationFactory)
.CreateCollation(locale);
.CreateCollation();
const strength = Ci.nsICollation.kCollationCaseInsensitiveAscii;
let comparator = (a, b) => collation.compareString(strength, a.name, b.name);
alphaEngines.sort(comparator);