/src/mozilla-central/intl/locale/LocaleService.h
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 | | #ifndef mozilla_intl_LocaleService_h__ |
7 | | #define mozilla_intl_LocaleService_h__ |
8 | | |
9 | | #include "nsIObserver.h" |
10 | | #include "nsString.h" |
11 | | #include "nsTArray.h" |
12 | | #include "nsWeakReference.h" |
13 | | |
14 | | #include "mozILocaleService.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace intl { |
18 | | |
19 | | /** |
20 | | * LocaleService is a manager of language negotiation in Gecko. |
21 | | * |
22 | | * It's intended to be the core place for collecting available and |
23 | | * requested languages and negotiating them to produce a fallback |
24 | | * chain of locales for the application. |
25 | | * |
26 | | * Client / Server |
27 | | * |
28 | | * LocaleService may operate in one of two modes: |
29 | | * |
30 | | * server |
31 | | * in the server mode, LocaleService is collecting and negotiating |
32 | | * languages. It also subscribes to relevant observers. |
33 | | * There should be at most one server per application instance. |
34 | | * |
35 | | * client |
36 | | * in the client mode, LocaleService is not responsible for collecting |
37 | | * or reacting to any system changes. It still distributes information |
38 | | * about locales, but internally, it gets information from the server instance |
39 | | * instead of collecting it on its own. |
40 | | * This prevents any data desynchronization and minimizes the cost |
41 | | * of running the service. |
42 | | * |
43 | | * In both modes, all get* methods should work the same way and all |
44 | | * static methods are available. |
45 | | * |
46 | | * In the server mode, other components may inform LocaleService about their |
47 | | * status either via calls to set* methods or via observer events. |
48 | | * In the client mode, only the process communication should provide data |
49 | | * to the LocaleService. |
50 | | * |
51 | | * At the moment desktop apps use the parent process in the server mode, and |
52 | | * content processes in the client mode. |
53 | | * |
54 | | * Locale / Language |
55 | | * |
56 | | * The terms `Locale ID` and `Language ID` are used slightly differently |
57 | | * by different organizations. Mozilla uses the term `Language ID` to describe |
58 | | * a string that contains information about the language itself, script, |
59 | | * region and variant. For example "en-Latn-US-mac" is a correct Language ID. |
60 | | * |
61 | | * Locale ID contains a Language ID plus a number of extension tags that |
62 | | * contain information that go beyond language inforamation such as |
63 | | * preferred currency, date/time formatting etc. |
64 | | * |
65 | | * An example of a Locale ID is `en-Latn-US-x-hc-h12-ca-gregory` |
66 | | * |
67 | | * At the moment we do not support full extension tag system, but we |
68 | | * try to be specific when naming APIs, so the service is for locales, |
69 | | * but we negotiate between languages etc. |
70 | | */ |
71 | | class LocaleService final : public mozILocaleService, |
72 | | public nsIObserver, |
73 | | public nsSupportsWeakReference |
74 | | { |
75 | | public: |
76 | | NS_DECL_ISUPPORTS |
77 | | NS_DECL_NSIOBSERVER |
78 | | NS_DECL_MOZILOCALESERVICE |
79 | | |
80 | | /** |
81 | | * List of available language negotiation strategies. |
82 | | * |
83 | | * See the mozILocaleService.idl for detailed description of the |
84 | | * strategies. |
85 | | */ |
86 | | static const int32_t kLangNegStrategyFiltering = 0; |
87 | | static const int32_t kLangNegStrategyMatching = 1; |
88 | | static const int32_t kLangNegStrategyLookup = 2; |
89 | | |
90 | | explicit LocaleService(bool aIsServer); |
91 | | |
92 | | /** |
93 | | * Create (if necessary) and return a raw pointer to the singleton instance. |
94 | | * Use this accessor in C++ code that just wants to call a method on the |
95 | | * instance, but does not need to hold a reference, as in |
96 | | * nsAutoCString str; |
97 | | * LocaleService::GetInstance()->GetAppLocaleAsLangTag(str); |
98 | | */ |
99 | | static LocaleService* GetInstance(); |
100 | | |
101 | | /** |
102 | | * Return an addRef'd pointer to the singleton instance. This is used by the |
103 | | * XPCOM constructor that exists to support usage from JS. |
104 | | */ |
105 | | static already_AddRefed<LocaleService> GetInstanceAddRefed() |
106 | 0 | { |
107 | 0 | return RefPtr<LocaleService>(GetInstance()).forget(); |
108 | 0 | } |
109 | | |
110 | | /** |
111 | | * This method should only be called in the client mode. |
112 | | * |
113 | | * It replaces all the language negotiation and is supposed to be called |
114 | | * in order to bring the client LocaleService in sync with the server |
115 | | * LocaleService. |
116 | | * |
117 | | * Currently, it's called by the IPC code. |
118 | | */ |
119 | | void AssignAppLocales(const nsTArray<nsCString>& aAppLocales); |
120 | | void AssignRequestedLocales(const nsTArray<nsCString>& aRequestedLocales); |
121 | | |
122 | | /** |
123 | | * Those two functions allow to trigger cache invalidation on one of the |
124 | | * three cached values. |
125 | | * |
126 | | * In most cases, the functions will be called by the observer in |
127 | | * LocaleService itself, but in a couple special cases, we have the |
128 | | * other component call this manually instead of sending a global event. |
129 | | * |
130 | | * If the result differs from the previous list, it will additionally |
131 | | * trigger a corresponding event |
132 | | * |
133 | | * This code should be called only in the server mode.. |
134 | | */ |
135 | | void RequestedLocalesChanged(); |
136 | | void LocalesChanged(); |
137 | | |
138 | | /** |
139 | | * Returns whether the current app locale is RTL. |
140 | | */ |
141 | | bool IsAppLocaleRTL(); |
142 | | |
143 | | static bool LanguagesMatch(const nsACString& aRequested, |
144 | | const nsACString& aAvailable); |
145 | | |
146 | | bool IsServer(); |
147 | | |
148 | | private: |
149 | | void FilterMatches(const nsTArray<nsCString>& aRequested, |
150 | | const nsTArray<nsCString>& aAvailable, |
151 | | int32_t aStrategy, |
152 | | nsTArray<nsCString>& aRetVal); |
153 | | |
154 | | void NegotiateAppLocales(nsTArray<nsCString>& aRetVal); |
155 | | |
156 | | void InitPackagedLocales(); |
157 | | |
158 | | virtual ~LocaleService(); |
159 | | |
160 | | nsAutoCStringN<16> mDefaultLocale; |
161 | | nsTArray<nsCString> mAppLocales; |
162 | | nsTArray<nsCString> mRequestedLocales; |
163 | | nsTArray<nsCString> mAvailableLocales; |
164 | | nsTArray<nsCString> mPackagedLocales; |
165 | | const bool mIsServer; |
166 | | |
167 | | static StaticRefPtr<LocaleService> sInstance; |
168 | | }; |
169 | | } // intl |
170 | | } // namespace mozilla |
171 | | |
172 | | #endif /* mozilla_intl_LocaleService_h__ */ |