1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "L10nRegistry.h"
#include "mozilla/RefPtr.h"
#include "mozilla/URLPreloader.h"
#include "nsIChannel.h"
#include "nsILoadInfo.h"
#include "nsNetUtil.h"
#include "nsString.h"
#include "nsContentUtils.h"
#include "FluentResource.h"
#include "FileSource.h"
#include "nsICategoryManager.h"
#include "mozilla/SimpleEnumerator.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PContent.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/Preferences.h"
using namespace mozilla;
using namespace mozilla::dom;
namespace mozilla::intl {
/* FluentBundleIterator */
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FluentBundleIterator, mGlobal)
FluentBundleIterator::FluentBundleIterator(
nsIGlobalObject* aGlobal, UniquePtr<ffi::GeckoFluentBundleIterator> aRaw)
: mGlobal(aGlobal), mRaw(std::move(aRaw)) {}
JSObject* FluentBundleIterator::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return FluentBundleIterator_Binding::Wrap(aCx, this, aGivenProto);
}
void FluentBundleIterator::Next(FluentBundleIteratorResult& aResult) {
UniquePtr<ffi::FluentBundleRc> raw(
ffi::fluent_bundle_iterator_next(mRaw.get()));
if (!raw) {
aResult.mDone = true;
return;
}
aResult.mDone = false;
aResult.mValue = new FluentBundle(mGlobal, std::move(raw));
}
already_AddRefed<FluentBundleIterator> FluentBundleIterator::Values() {
return do_AddRef(this);
}
/* FluentBundleAsyncIterator */
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FluentBundleAsyncIterator, mGlobal)
FluentBundleAsyncIterator::FluentBundleAsyncIterator(
nsIGlobalObject* aGlobal,
UniquePtr<ffi::GeckoFluentBundleAsyncIteratorWrapper> aRaw)
: mGlobal(aGlobal), mRaw(std::move(aRaw)) {}
JSObject* FluentBundleAsyncIterator::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return FluentBundleAsyncIterator_Binding::Wrap(aCx, this, aGivenProto);
}
already_AddRefed<Promise> FluentBundleAsyncIterator::Next(ErrorResult& aError) {
RefPtr<Promise> promise = Promise::Create(mGlobal, aError);
if (aError.Failed()) {
return nullptr;
}
ffi::fluent_bundle_async_iterator_next(
mRaw.get(), promise,
// callback function which will be invoked by the rust code, passing the
// promise back in.
[](auto* aPromise, ffi::FluentBundleRc* aBundle) {
Promise* promise = const_cast<Promise*>(aPromise);
FluentBundleIteratorResult res;
if (aBundle) {
// The Rust caller will transfer the ownership to us.
UniquePtr<ffi::FluentBundleRc> b(aBundle);
nsIGlobalObject* global = promise->GetGlobalObject();
res.mValue = new FluentBundle(global, std::move(b));
res.mDone = false;
} else {
res.mDone = true;
}
promise->MaybeResolve(res);
});
return promise.forget();
}
already_AddRefed<FluentBundleAsyncIterator>
FluentBundleAsyncIterator::Values() {
return do_AddRef(this);
}
/* L10nRegistry */
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(L10nRegistry, mGlobal)
L10nRegistry::L10nRegistry(nsIGlobalObject* aGlobal, bool aUseIsolating)
: mGlobal(aGlobal),
mRaw(dont_AddRef(ffi::l10nregistry_new(aUseIsolating))) {}
L10nRegistry::L10nRegistry(nsIGlobalObject* aGlobal,
RefPtr<const ffi::GeckoL10nRegistry> aRaw)
: mGlobal(aGlobal), mRaw(std::move(aRaw)) {}
/* static */
already_AddRefed<L10nRegistry> L10nRegistry::Constructor(
const GlobalObject& aGlobal, const L10nRegistryOptions& aOptions) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
return MakeAndAddRef<L10nRegistry>(global,
aOptions.mBundleOptions.mUseIsolating);
}
/* static */
already_AddRefed<L10nRegistry> L10nRegistry::GetInstance(
const GlobalObject& aGlobal) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
return MakeAndAddRef<L10nRegistry>(
global, dont_AddRef(ffi::l10nregistry_instance_get()));
}
JSObject* L10nRegistry::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return L10nRegistry_Binding::Wrap(aCx, this, aGivenProto);
}
void L10nRegistry::GetAvailableLocales(nsTArray<nsCString>& aRetVal) {
ffi::l10nregistry_get_available_locales(mRaw.get(), &aRetVal);
}
void L10nRegistry::RegisterSources(
const Sequence<OwningNonNull<L10nFileSource>>& aSources) {
nsTArray<const ffi::FileSource*> sources(aSources.Length());
for (const auto& source : aSources) {
sources.AppendElement(source->Raw());
}
ffi::l10nregistry_register_sources(mRaw.get(), &sources);
}
void L10nRegistry::UpdateSources(
const Sequence<OwningNonNull<L10nFileSource>>& aSources) {
nsTArray<const ffi::FileSource*> sources(aSources.Length());
for (const auto& source : aSources) {
sources.AppendElement(source->Raw());
}
ffi::l10nregistry_update_sources(mRaw.get(), &sources);
}
void L10nRegistry::RemoveSources(const Sequence<nsCString>& aSources) {
ffi::l10nregistry_remove_sources(mRaw.get(), aSources.Elements(),
aSources.Length());
}
bool L10nRegistry::HasSource(const nsACString& aName, ErrorResult& aRv) {
ffi::L10nRegistryStatus status;
bool result = ffi::l10nregistry_has_source(mRaw.get(), &aName, &status);
PopulateError(aRv, status);
return result;
}
already_AddRefed<L10nFileSource> L10nRegistry::GetSource(
const nsACString& aName, ErrorResult& aRv) {
ffi::L10nRegistryStatus status;
RefPtr<const ffi::FileSource> raw(
dont_AddRef(ffi::l10nregistry_get_source(mRaw.get(), &aName, &status)));
if (PopulateError(aRv, status)) {
return nullptr;
}
return MakeAndAddRef<L10nFileSource>(std::move(raw));
}
void L10nRegistry::GetSourceNames(nsTArray<nsCString>& aRetVal) {
ffi::l10nregistry_get_source_names(mRaw.get(), &aRetVal);
}
void L10nRegistry::ClearSources() {
ffi::l10nregistry_clear_sources(mRaw.get());
}
/* static */
ffi::GeckoResourceId L10nRegistry::ResourceIdToFFI(
const nsCString& aResourceId) {
return ffi::GeckoResourceId{
aResourceId,
ffi::GeckoResourceType::Required,
};
}
/* static */
ffi::GeckoResourceId L10nRegistry::ResourceIdToFFI(
const dom::OwningUTF8StringOrResourceId& aResourceId) {
if (aResourceId.IsUTF8String()) {
return ffi::GeckoResourceId{
aResourceId.GetAsUTF8String(),
ffi::GeckoResourceType::Required,
};
}
return ffi::GeckoResourceId{
aResourceId.GetAsResourceId().mPath,
aResourceId.GetAsResourceId().mOptional
? ffi::GeckoResourceType::Optional
: ffi::GeckoResourceType::Required,
};
}
/* static */
nsTArray<ffi::GeckoResourceId> L10nRegistry::ResourceIdsToFFI(
const nsTArray<nsCString>& aResourceIds) {
nsTArray<ffi::GeckoResourceId> ffiResourceIds;
for (const auto& resourceId : aResourceIds) {
ffiResourceIds.EmplaceBack(ResourceIdToFFI(resourceId));
}
return ffiResourceIds;
}
/* static */
nsTArray<ffi::GeckoResourceId> L10nRegistry::ResourceIdsToFFI(
const nsTArray<dom::OwningUTF8StringOrResourceId>& aResourceIds) {
nsTArray<ffi::GeckoResourceId> ffiResourceIds;
for (const auto& resourceId : aResourceIds) {
ffiResourceIds.EmplaceBack(ResourceIdToFFI(resourceId));
}
return ffiResourceIds;
}
already_AddRefed<FluentBundleIterator> L10nRegistry::GenerateBundlesSync(
const nsTArray<nsCString>& aLocales,
const nsTArray<ffi::GeckoResourceId>& aResourceIds, ErrorResult& aRv) {
ffi::L10nRegistryStatus status;
UniquePtr<ffi::GeckoFluentBundleIterator> iter(
ffi::l10nregistry_generate_bundles_sync(
mRaw, aLocales.Elements(), aLocales.Length(), aResourceIds.Elements(),
aResourceIds.Length(), &status));
if (PopulateError(aRv, status) || !iter) {
return nullptr;
}
return do_AddRef(new FluentBundleIterator(mGlobal, std::move(iter)));
}
already_AddRefed<FluentBundleIterator> L10nRegistry::GenerateBundlesSync(
const dom::Sequence<nsCString>& aLocales,
const dom::Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
ErrorResult& aRv) {
auto ffiResourceIds{ResourceIdsToFFI(aResourceIds)};
return GenerateBundlesSync(aLocales, ffiResourceIds, aRv);
}
already_AddRefed<FluentBundleAsyncIterator> L10nRegistry::GenerateBundles(
const nsTArray<nsCString>& aLocales,
const nsTArray<ffi::GeckoResourceId>& aResourceIds, ErrorResult& aRv) {
ffi::L10nRegistryStatus status;
UniquePtr<ffi::GeckoFluentBundleAsyncIteratorWrapper> iter(
ffi::l10nregistry_generate_bundles(
mRaw, aLocales.Elements(), aLocales.Length(), aResourceIds.Elements(),
aResourceIds.Length(), &status));
if (PopulateError(aRv, status) || !iter) {
return nullptr;
}
return do_AddRef(new FluentBundleAsyncIterator(mGlobal, std::move(iter)));
}
already_AddRefed<FluentBundleAsyncIterator> L10nRegistry::GenerateBundles(
const dom::Sequence<nsCString>& aLocales,
const dom::Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
ErrorResult& aRv) {
nsTArray<ffi::GeckoResourceId> resourceIds;
for (const auto& resourceId : aResourceIds) {
resourceIds.EmplaceBack(ResourceIdToFFI(resourceId));
}
return GenerateBundles(aLocales, resourceIds, aRv);
}
/* static */
void L10nRegistry::GetParentProcessFileSourceDescriptors(
nsTArray<L10nFileSourceDescriptor>& aRetVal) {
MOZ_ASSERT(XRE_IsParentProcess());
nsTArray<ffi::L10nFileSourceDescriptor> sources;
ffi::l10nregistry_get_parent_process_sources(&sources);
for (const auto& source : sources) {
auto descriptor = aRetVal.AppendElement();
descriptor->name() = source.name;
descriptor->metasource() = source.metasource;
descriptor->locales().AppendElements(std::move(source.locales));
descriptor->prePath() = source.pre_path;
descriptor->index().AppendElements(std::move(source.index));
}
}
/* static */
void L10nRegistry::RegisterFileSourcesFromParentProcess(
const nsTArray<L10nFileSourceDescriptor>& aDescriptors) {
// This means that in content processes the L10nRegistry
// service instance is created eagerly, not lazily.
// It is necessary so that the instance can store the sources
// provided in the IPC init, which, in turn, is necessary
// for the service to be avialable for sync bundle generation.
//
// L10nRegistry is lightweight and performs no operations, so
// we believe this behavior to be acceptable.
MOZ_ASSERT(XRE_IsContentProcess());
nsTArray<ffi::L10nFileSourceDescriptor> sources;
for (const auto& desc : aDescriptors) {
auto source = sources.AppendElement();
source->name = desc.name();
source->metasource = desc.metasource();
source->locales.AppendElements(desc.locales());
source->pre_path = desc.prePath();
source->index.AppendElements(desc.index());
}
ffi::l10nregistry_register_parent_process_sources(&sources);
}
/* static */
nsresult L10nRegistry::Load(const nsACString& aPath,
nsIStreamLoaderObserver* aObserver) {
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aPath);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(uri, NS_ERROR_INVALID_ARG);
RefPtr<nsIStreamLoader> loader;
rv = NS_NewStreamLoader(
getter_AddRefs(loader), uri, aObserver,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
nsIContentPolicy::TYPE_OTHER);
return rv;
}
/* static */
nsresult L10nRegistry::LoadSync(const nsACString& aPath, void** aData,
uint64_t* aSize) {
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aPath);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(uri, NS_ERROR_INVALID_ARG);
auto result = URLPreloader::ReadURI(uri);
if (result.isOk()) {
auto uri = result.unwrap();
*aData = ToNewCString(uri);
*aSize = uri.Length();
return NS_OK;
}
auto err = result.unwrapErr();
if (err != NS_ERROR_INVALID_ARG && err != NS_ERROR_NOT_INITIALIZED) {
return err;
}
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(
getter_AddRefs(channel), uri, nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
nsIContentPolicy::TYPE_OTHER, nullptr, /* nsICookieJarSettings */
nullptr, /* aPerformanceStorage */
nullptr, /* aLoadGroup */
nullptr, /* aCallbacks */
nsIRequest::LOAD_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIInputStream> input;
rv = channel->Open(getter_AddRefs(input));
NS_ENSURE_SUCCESS(rv, NS_ERROR_INVALID_ARG);
return NS_ReadInputStreamToBuffer(input, aData, -1, aSize);
}
/* static */
bool L10nRegistry::PopulateError(ErrorResult& aError,
ffi::L10nRegistryStatus& aStatus) {
switch (aStatus) {
case ffi::L10nRegistryStatus::InvalidLocaleCode:
aError.ThrowTypeError("Invalid locale code");
return true;
case ffi::L10nRegistryStatus::EmptyName:
aError.ThrowTypeError("Name cannot be empty.");
return true;
case ffi::L10nRegistryStatus::None:
return false;
}
MOZ_ASSERT_UNREACHABLE("Unknown status");
return false;
}
extern "C" {
nsresult L10nRegistryLoad(const nsACString* aPath,
const nsIStreamLoaderObserver* aObserver) {
if (!aPath || !aObserver) {
return NS_ERROR_INVALID_ARG;
}
return mozilla::intl::L10nRegistry::Load(
*aPath, const_cast<nsIStreamLoaderObserver*>(aObserver));
}
nsresult L10nRegistryLoadSync(const nsACString* aPath, void** aData,
uint64_t* aSize) {
if (!aPath || !aData || !aSize) {
return NS_ERROR_INVALID_ARG;
}
return mozilla::intl::L10nRegistry::LoadSync(*aPath, aData, aSize);
}
void L10nRegistrySendUpdateL10nFileSources() {
MOZ_ASSERT(XRE_IsParentProcess());
nsTArray<L10nFileSourceDescriptor> sources;
L10nRegistry::GetParentProcessFileSourceDescriptors(sources);
nsTArray<ContentParent*> parents;
ContentParent::GetAll(parents);
for (ContentParent* parent : parents) {
Unused << parent->SendUpdateL10nFileSources(sources);
}
}
} // extern "C"
} // namespace mozilla::intl
|