Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/locale/nsLanguageAtomService.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "nsLanguageAtomService.h"
7
#include "nsUConvPropertySearch.h"
8
#include "nsUnicharUtils.h"
9
#include "nsAtom.h"
10
#include "mozilla/ArrayUtils.h"
11
#include "mozilla/ClearOnShutdown.h"
12
#include "mozilla/Encoding.h"
13
#include "mozilla/intl/OSPreferences.h"
14
#include "mozilla/ServoBindings.h"
15
16
using namespace mozilla;
17
using mozilla::intl::OSPreferences;
18
19
static constexpr nsUConvProp encodingsGroups[] = {
20
#include "encodingsgroups.properties.h"
21
};
22
23
static constexpr nsUConvProp kLangGroups[] = {
24
#include "langGroups.properties.h"
25
};
26
27
// static
28
nsLanguageAtomService*
29
nsLanguageAtomService::GetService()
30
3
{
31
3
  static UniquePtr<nsLanguageAtomService> gLangAtomService;
32
3
  if (!gLangAtomService) {
33
3
    gLangAtomService = MakeUnique<nsLanguageAtomService>();
34
3
    ClearOnShutdown(&gLangAtomService);
35
3
  }
36
3
  return gLangAtomService.get();
37
3
}
38
39
nsAtom*
40
nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage)
41
0
{
42
0
  nsAutoCString lowered(aLanguage);
43
0
  ToLowerCase(lowered);
44
0
45
0
  RefPtr<nsAtom> lang = NS_Atomize(lowered);
46
0
  return GetLanguageGroup(lang);
47
0
}
48
49
already_AddRefed<nsAtom>
50
nsLanguageAtomService::LookupCharSet(NotNull<const Encoding*> aEncoding)
51
0
{
52
0
  nsAutoCString charset;
53
0
  aEncoding->Name(charset);
54
0
  nsAutoCString group;
55
0
  if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
56
0
      encodingsGroups, ArrayLength(encodingsGroups), charset, group))) {
57
0
    return RefPtr<nsAtom>(nsGkAtoms::Unicode).forget();
58
0
  }
59
0
  return NS_Atomize(group);
60
0
}
61
62
nsAtom*
63
nsLanguageAtomService::GetLocaleLanguage()
64
0
{
65
0
  do {
66
0
    if (!mLocaleLanguage) {
67
0
      AutoTArray<nsCString, 10> regionalPrefsLocales;
68
0
      if (NS_SUCCEEDED(OSPreferences::GetInstance()->GetRegionalPrefsLocales(
69
0
            regionalPrefsLocales))) {
70
0
        // use lowercase for all language atoms
71
0
        ToLowerCase(regionalPrefsLocales[0]);
72
0
        mLocaleLanguage = NS_Atomize(regionalPrefsLocales[0]);
73
0
      } else {
74
0
        nsAutoCString locale;
75
0
        OSPreferences::GetInstance()->GetSystemLocale(locale);
76
0
77
0
        ToLowerCase(locale); // use lowercase for all language atoms
78
0
        mLocaleLanguage = NS_Atomize(locale);
79
0
      }
80
0
    }
81
0
  } while (0);
82
0
83
0
  return mLocaleLanguage;
84
0
}
85
86
nsAtom*
87
nsLanguageAtomService::GetLanguageGroup(nsAtom *aLanguage, bool* aNeedsToCache)
88
0
{
89
0
  nsAtom *retVal = mLangToGroup.GetWeak(aLanguage);
90
0
91
0
  if (!retVal) {
92
0
    if (aNeedsToCache) {
93
0
      *aNeedsToCache = true;
94
0
      return nullptr;
95
0
    }
96
0
    RefPtr<nsAtom> uncached = GetUncachedLanguageGroup(aLanguage);
97
0
    retVal = uncached.get();
98
0
99
0
    AssertIsMainThreadOrServoLangFontPrefsCacheLocked();
100
0
    // The hashtable will keep an owning reference to the atom
101
0
    mLangToGroup.Put(aLanguage, uncached);
102
0
  }
103
0
104
0
  return retVal;
105
0
}
106
107
already_AddRefed<nsAtom>
108
nsLanguageAtomService::GetUncachedLanguageGroup(nsAtom* aLanguage) const
109
0
{
110
0
  nsAutoCString langStr;
111
0
  aLanguage->ToUTF8String(langStr);
112
0
  ToLowerCase(langStr);
113
0
114
0
  nsAutoCString langGroupStr;
115
0
  nsresult res =
116
0
    nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
117
0
                                               ArrayLength(kLangGroups),
118
0
                                               langStr, langGroupStr);
119
0
  while (NS_FAILED(res)) {
120
0
    int32_t hyphen = langStr.RFindChar('-');
121
0
    if (hyphen <= 0) {
122
0
      langGroupStr.AssignLiteral("x-unicode");
123
0
      break;
124
0
    }
125
0
    langStr.Truncate(hyphen);
126
0
    res = nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
127
0
                                                     ArrayLength(kLangGroups),
128
0
                                                     langStr, langGroupStr);
129
0
  }
130
0
131
0
  RefPtr<nsAtom> langGroup = NS_Atomize(langGroupStr);
132
0
133
0
  return langGroup.forget();
134
0
}