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 /*
7  * The nsILanguageAtomService provides a mapping from languages or charsets
8  * to language groups, and access to the system locale language.
9  */
10 
11 #ifndef nsLanguageAtomService_h_
12 #define nsLanguageAtomService_h_
13 
14 #include "mozilla/NotNull.h"
15 #include "nsCOMPtr.h"
16 #include "nsAtom.h"
17 #include "nsTHashMap.h"
18 
19 namespace mozilla {
20 class Encoding;
21 }
22 
23 class nsLanguageAtomService final {
24   using Encoding = mozilla::Encoding;
25   template <typename T>
26   using NotNull = mozilla::NotNull<T>;
27 
28  public:
29   static nsLanguageAtomService* GetService();
30 
31   static void Shutdown();
32 
33   nsStaticAtom* LookupLanguage(const nsACString& aLanguage);
34   already_AddRefed<nsAtom> LookupCharSet(NotNull<const Encoding*> aCharSet);
35   nsAtom* GetLocaleLanguage();
36 
37   // Returns the language group that the specified language is a part of.
38   //
39   // aNeedsToCache is used for two things.  If null, it indicates that
40   // the nsLanguageAtomService is safe to cache the result of the
41   // language group lookup, either because we're on the main thread,
42   // or because we're on a style worker thread but the font lock has
43   // been acquired.  If non-null, it indicates that it's not safe to
44   // cache the result of the language group lookup (because we're on
45   // a style worker thread without the lock acquired).  In this case,
46   // GetLanguageGroup will store true in *aNeedsToCache true if we
47   // would have cached the result of a new lookup, and false if we
48   // were able to use an existing cached result.  Thus, callers that
49   // get a true *aNeedsToCache outparam value should make an effort
50   // to re-call GetLanguageGroup when it is safe to cache, to avoid
51   // recomputing the language group again later.
52   nsStaticAtom* GetLanguageGroup(nsAtom* aLanguage,
53                                  bool* aNeedsToCache = nullptr);
54   nsStaticAtom* GetUncachedLanguageGroup(nsAtom* aLanguage) const;
55 
56  private:
57   nsTHashMap<nsRefPtrHashKey<nsAtom>, nsStaticAtom*> mLangToGroup;
58   RefPtr<nsAtom> mLocaleLanguage;
59 };
60 
61 #endif
62