Skip to content

Commit

Permalink
Bug 1335983 - Migrate nsCollation::CreateCollection to use LocaleServ…
Browse files Browse the repository at this point in the history
…ice::GetAppLocale. r=jfkthame

MozReview-Commit-ID: 5GEnL7Kihpj
  • Loading branch information
Zibi Braniecki committed Feb 7, 2017
1 parent 303d429 commit 146c4b9
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 221 deletions.
23 changes: 6 additions & 17 deletions dom/xslt/xslt/txXPathResultComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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;
Expand Down
29 changes: 7 additions & 22 deletions dom/xul/templates/nsXULContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
46 changes: 3 additions & 43 deletions intl/locale/mac/nsCollationMacUC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,7 +15,6 @@ NS_IMPL_ISUPPORTS(nsCollationMacUC, nsICollation)
nsCollationMacUC::nsCollationMacUC()
: mInit(false)
, mHasCollator(false)
, mLocaleICU(nullptr)
, mLastStrength(-1)
, mCollatorICU(nullptr)
{ }
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions intl/locale/mac/nsCollationMacUC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -26,7 +27,6 @@ class nsCollationMacUC final : public nsICollation {
protected:
~nsCollationMacUC();

nsresult ConvertLocaleICU(nsILocale* aNSLocale, char** aICULocale);
nsresult ConvertStrength(const int32_t aStrength,
UCollationStrength* aStrengthOut,
UColAttributeValue* aCaseLevelOut);
Expand All @@ -36,7 +36,7 @@ class nsCollationMacUC final : public nsICollation {
private:
bool mInit;
bool mHasCollator;
char* mLocaleICU;
nsCString mLocale;
int32_t mLastStrength;
UCollator* mCollatorICU;
};
Expand Down
15 changes: 13 additions & 2 deletions intl/locale/nsCollation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nsIUnicodeEncoder.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/dom/EncodingUtils.h"
#include "mozilla/intl/LocaleService.h"

using mozilla::dom::EncodingUtils;

Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions intl/locale/nsCollation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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() {}
};
Expand Down
19 changes: 10 additions & 9 deletions intl/locale/nsICollation.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* @return A new collation.
*/
nsICollation CreateCollation();

/**
* Create a new collation for a given locale.
*
* @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 CreateCollationForLocale(in ACString locale);
};

[scriptable, uuid(b0132cc0-3786-4557-9874-910d7def5f93)]
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions intl/locale/tests/unit/test_collation_mac_icu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 5 additions & 40 deletions intl/locale/unix/nsCollationUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand All @@ -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());
}
}

Expand Down
Loading

0 comments on commit 146c4b9

Please sign in to comment.