Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mozilla/dom/FallbackEncoding.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/dom/EncodingUtils.h" |
michael@0 | 8 | #include "nsUConvPropertySearch.h" |
michael@0 | 9 | #include "nsIChromeRegistry.h" |
michael@0 | 10 | #include "mozilla/Preferences.h" |
michael@0 | 11 | #include "mozilla/Services.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace dom { |
michael@0 | 15 | |
michael@0 | 16 | static const char* localesFallbacks[][3] = { |
michael@0 | 17 | #include "localesfallbacks.properties.h" |
michael@0 | 18 | }; |
michael@0 | 19 | |
michael@0 | 20 | static const char* domainsFallbacks[][3] = { |
michael@0 | 21 | #include "domainsfallbacks.properties.h" |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | static const char* nonParticipatingDomains[][3] = { |
michael@0 | 25 | #include "nonparticipatingdomains.properties.h" |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | FallbackEncoding* FallbackEncoding::sInstance = nullptr; |
michael@0 | 29 | bool FallbackEncoding::sGuessFallbackFromTopLevelDomain = true; |
michael@0 | 30 | |
michael@0 | 31 | FallbackEncoding::FallbackEncoding() |
michael@0 | 32 | { |
michael@0 | 33 | MOZ_COUNT_CTOR(FallbackEncoding); |
michael@0 | 34 | MOZ_ASSERT(!FallbackEncoding::sInstance, |
michael@0 | 35 | "Singleton already exists."); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | FallbackEncoding::~FallbackEncoding() |
michael@0 | 39 | { |
michael@0 | 40 | MOZ_COUNT_DTOR(FallbackEncoding); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | void |
michael@0 | 44 | FallbackEncoding::Get(nsACString& aFallback) |
michael@0 | 45 | { |
michael@0 | 46 | if (!mFallback.IsEmpty()) { |
michael@0 | 47 | aFallback = mFallback; |
michael@0 | 48 | return; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | const nsAdoptingCString& override = |
michael@0 | 52 | Preferences::GetCString("intl.charset.fallback.override"); |
michael@0 | 53 | // Don't let the user break things by setting the override to unreasonable |
michael@0 | 54 | // values via about:config |
michael@0 | 55 | if (!EncodingUtils::FindEncodingForLabel(override, mFallback) || |
michael@0 | 56 | !EncodingUtils::IsAsciiCompatible(mFallback) || |
michael@0 | 57 | mFallback.EqualsLiteral("UTF-8")) { |
michael@0 | 58 | mFallback.Truncate(); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | if (!mFallback.IsEmpty()) { |
michael@0 | 62 | aFallback = mFallback; |
michael@0 | 63 | return; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsAutoCString locale; |
michael@0 | 67 | nsCOMPtr<nsIXULChromeRegistry> registry = |
michael@0 | 68 | mozilla::services::GetXULChromeRegistryService(); |
michael@0 | 69 | if (registry) { |
michael@0 | 70 | registry->GetSelectedLocale(NS_LITERAL_CSTRING("global"), locale); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // Let's lower case the string just in case unofficial language packs |
michael@0 | 74 | // don't stick to conventions. |
michael@0 | 75 | ToLowerCase(locale); // ASCII lowercasing with CString input! |
michael@0 | 76 | |
michael@0 | 77 | // Special case Traditional Chinese before throwing away stuff after the |
michael@0 | 78 | // language itself. Today we only ship zh-TW, but be defensive about |
michael@0 | 79 | // possible future values. |
michael@0 | 80 | if (locale.EqualsLiteral("zh-tw") || |
michael@0 | 81 | locale.EqualsLiteral("zh-hk") || |
michael@0 | 82 | locale.EqualsLiteral("zh-mo") || |
michael@0 | 83 | locale.EqualsLiteral("zh-hant")) { |
michael@0 | 84 | mFallback.AssignLiteral("Big5"); |
michael@0 | 85 | aFallback = mFallback; |
michael@0 | 86 | return; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // Throw away regions and other variants to accommodate weird stuff seen |
michael@0 | 90 | // in telemetry--apparently unofficial language packs. |
michael@0 | 91 | int32_t index = locale.FindChar('-'); |
michael@0 | 92 | if (index >= 0) { |
michael@0 | 93 | locale.Truncate(index); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue( |
michael@0 | 97 | localesFallbacks, ArrayLength(localesFallbacks), locale, mFallback))) { |
michael@0 | 98 | mFallback.AssignLiteral("windows-1252"); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | aFallback = mFallback; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void |
michael@0 | 105 | FallbackEncoding::FromLocale(nsACString& aFallback) |
michael@0 | 106 | { |
michael@0 | 107 | MOZ_ASSERT(FallbackEncoding::sInstance, |
michael@0 | 108 | "Using uninitialized fallback cache."); |
michael@0 | 109 | FallbackEncoding::sInstance->Get(aFallback); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // PrefChangedFunc |
michael@0 | 113 | void |
michael@0 | 114 | FallbackEncoding::PrefChanged(const char*, void*) |
michael@0 | 115 | { |
michael@0 | 116 | MOZ_ASSERT(FallbackEncoding::sInstance, |
michael@0 | 117 | "Pref callback called with null fallback cache."); |
michael@0 | 118 | FallbackEncoding::sInstance->Invalidate(); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | void |
michael@0 | 122 | FallbackEncoding::Initialize() |
michael@0 | 123 | { |
michael@0 | 124 | MOZ_ASSERT(!FallbackEncoding::sInstance, |
michael@0 | 125 | "Initializing pre-existing fallback cache."); |
michael@0 | 126 | FallbackEncoding::sInstance = new FallbackEncoding; |
michael@0 | 127 | Preferences::RegisterCallback(FallbackEncoding::PrefChanged, |
michael@0 | 128 | "intl.charset.fallback.override", |
michael@0 | 129 | nullptr); |
michael@0 | 130 | Preferences::RegisterCallback(FallbackEncoding::PrefChanged, |
michael@0 | 131 | "general.useragent.locale", |
michael@0 | 132 | nullptr); |
michael@0 | 133 | Preferences::AddBoolVarCache(&sGuessFallbackFromTopLevelDomain, |
michael@0 | 134 | "intl.charset.fallback.tld"); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | void |
michael@0 | 138 | FallbackEncoding::Shutdown() |
michael@0 | 139 | { |
michael@0 | 140 | MOZ_ASSERT(FallbackEncoding::sInstance, |
michael@0 | 141 | "Releasing non-existent fallback cache."); |
michael@0 | 142 | delete FallbackEncoding::sInstance; |
michael@0 | 143 | FallbackEncoding::sInstance = nullptr; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | bool |
michael@0 | 147 | FallbackEncoding::IsParticipatingTopLevelDomain(const nsACString& aTLD) |
michael@0 | 148 | { |
michael@0 | 149 | nsAutoCString dummy; |
michael@0 | 150 | return NS_FAILED(nsUConvPropertySearch::SearchPropertyValue( |
michael@0 | 151 | nonParticipatingDomains, |
michael@0 | 152 | ArrayLength(nonParticipatingDomains), |
michael@0 | 153 | aTLD, |
michael@0 | 154 | dummy)); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | void |
michael@0 | 158 | FallbackEncoding::FromTopLevelDomain(const nsACString& aTLD, |
michael@0 | 159 | nsACString& aFallback) |
michael@0 | 160 | { |
michael@0 | 161 | if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue( |
michael@0 | 162 | domainsFallbacks, ArrayLength(domainsFallbacks), aTLD, aFallback))) { |
michael@0 | 163 | aFallback.AssignLiteral("windows-1252"); |
michael@0 | 164 | } |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | } // namespace dom |
michael@0 | 168 | } // namespace mozilla |