summaryrefslogtreecommitdiffstats
path: root/intl/l10n/FileSource.cpp
blob: 2469f597c62244085e05a1ba464230226e1fd438 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "FileSource.h"
#include "mozilla/dom/Promise.h"

using namespace mozilla::dom;

namespace mozilla::intl {

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(L10nFileSource, mGlobal)

L10nFileSource::L10nFileSource(RefPtr<const ffi::FileSource> aRaw,
                               nsIGlobalObject* aGlobal)
    : mGlobal(aGlobal), mRaw(std::move(aRaw)) {}

/* static */
already_AddRefed<L10nFileSource> L10nFileSource::Constructor(
    const GlobalObject& aGlobal, const nsACString& aName,
    const nsACString& aMetaSource, const nsTArray<nsCString>& aLocales,
    const nsACString& aPrePath, const dom::FileSourceOptions& aOptions,
    const Optional<Sequence<nsCString>>& aIndex, ErrorResult& aRv) {
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  ffi::L10nFileSourceStatus status;

  bool allowOverrides = aOptions.mAddResourceOptions.mAllowOverrides;

  RefPtr<const ffi::FileSource> raw;
  if (aIndex.WasPassed()) {
    raw = dont_AddRef(ffi::l10nfilesource_new_with_index(
        &aName, &aMetaSource, &aLocales, &aPrePath, aIndex.Value().Elements(),
        aIndex.Value().Length(), allowOverrides, &status));
  } else {
    raw = dont_AddRef(ffi::l10nfilesource_new(
        &aName, &aMetaSource, &aLocales, &aPrePath, allowOverrides, &status));
  }

  if (PopulateError(aRv, status)) {
    return nullptr;
  }
  return MakeAndAddRef<L10nFileSource>(std::move(raw), global);
}

/* static */
already_AddRefed<L10nFileSource> L10nFileSource::CreateMock(
    const GlobalObject& aGlobal, const nsACString& aName,
    const nsACString& aMetaSource, const nsTArray<nsCString>& aLocales,
    const nsACString& aPrePath, const nsTArray<L10nFileSourceMockFile>& aFS,
    ErrorResult& aRv) {
  nsTArray<ffi::L10nFileSourceMockFile> fs(aFS.Length());
  for (const auto& file : aFS) {
    auto f = fs.AppendElement();
    f->path = file.mPath;
    f->source = file.mSource;
  }
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  ffi::L10nFileSourceStatus status;

  RefPtr<const ffi::FileSource> raw(dont_AddRef(ffi::l10nfilesource_new_mock(
      &aName, &aMetaSource, &aLocales, &aPrePath, &fs, &status)));

  if (PopulateError(aRv, status)) {
    return nullptr;
  }
  return MakeAndAddRef<L10nFileSource>(std::move(raw), global);
}

JSObject* L10nFileSource::WrapObject(JSContext* aCx,
                                     JS::Handle<JSObject*> aGivenProto) {
  return L10nFileSource_Binding::Wrap(aCx, this, aGivenProto);
}

void L10nFileSource::GetName(nsCString& aRetVal) {
  ffi::l10nfilesource_get_name(mRaw.get(), &aRetVal);
}

void L10nFileSource::GetMetaSource(nsCString& aRetVal) {
  ffi::l10nfilesource_get_metasource(mRaw.get(), &aRetVal);
}

void L10nFileSource::GetLocales(nsTArray<nsCString>& aRetVal) {
  ffi::l10nfilesource_get_locales(mRaw.get(), &aRetVal);
}

void L10nFileSource::GetPrePath(nsCString& aRetVal) {
  ffi::l10nfilesource_get_prepath(mRaw.get(), &aRetVal);
}

void L10nFileSource::GetIndex(Nullable<nsTArray<nsCString>>& aRetVal) {
  bool hasIndex =
      ffi::l10nfilesource_get_index(mRaw.get(), &aRetVal.SetValue());
  if (!hasIndex) {
    aRetVal.SetNull();
  }
}

L10nFileSourceHasFileStatus L10nFileSource::HasFile(const nsACString& aLocale,
                                                    const nsACString& aPath,
                                                    ErrorResult& aRv) {
  ffi::L10nFileSourceStatus status;

  bool isPresent = false;
  bool hasValue = ffi::l10nfilesource_has_file(mRaw.get(), &aLocale, &aPath,
                                               &status, &isPresent);

  if (!PopulateError(aRv, status) && hasValue) {
    if (isPresent) {
      return L10nFileSourceHasFileStatus::Present;
    }

    return L10nFileSourceHasFileStatus::Missing;
  }
  return L10nFileSourceHasFileStatus::Unknown;
}

already_AddRefed<FluentResource> L10nFileSource::FetchFileSync(
    const nsACString& aLocale, const nsACString& aPath, ErrorResult& aRv) {
  ffi::L10nFileSourceStatus status;

  RefPtr<const ffi::FluentResource> raw =
      dont_AddRef(ffi::l10nfilesource_fetch_file_sync(mRaw.get(), &aLocale,
                                                      &aPath, &status));

  if (!PopulateError(aRv, status) && raw) {
    return MakeAndAddRef<FluentResource>(mGlobal, raw);
  }

  return nullptr;
}

already_AddRefed<Promise> L10nFileSource::FetchFile(const nsACString& aLocale,
                                                    const nsACString& aPath,
                                                    ErrorResult& aRv) {
  RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
  if (aRv.Failed()) {
    return nullptr;
  }

  ffi::L10nFileSourceStatus status;

  ffi::l10nfilesource_fetch_file(
      mRaw.get(), &aLocale, &aPath, promise,
      [](const Promise* aPromise, const ffi::FluentResource* aRes) {
        Promise* promise = const_cast<Promise*>(aPromise);

        if (aRes) {
          nsIGlobalObject* global = promise->GetGlobalObject();
          RefPtr<FluentResource> res = new FluentResource(global, aRes);
          promise->MaybeResolve(res);
        } else {
          promise->MaybeResolve(JS::NullHandleValue);
        }
      },
      &status);

  if (PopulateError(aRv, status)) {
    return nullptr;
  }

  return promise.forget();
}

/* static */
bool L10nFileSource::PopulateError(ErrorResult& aError,
                                   ffi::L10nFileSourceStatus& aStatus) {
  switch (aStatus) {
    case ffi::L10nFileSourceStatus::InvalidLocaleCode:
      aError.ThrowTypeError("Invalid locale code");
      return true;
    case ffi::L10nFileSourceStatus::EmptyName:
      aError.ThrowTypeError("Name cannot be empty.");
      return true;
    case ffi::L10nFileSourceStatus::EmptyPrePath:
      aError.ThrowTypeError("prePath cannot be empty.");
      return true;
    case ffi::L10nFileSourceStatus::EmptyResId:
      aError.ThrowTypeError("resId cannot be empty.");
      return true;

    case ffi::L10nFileSourceStatus::None:
      return false;
  }
  MOZ_ASSERT_UNREACHABLE("Unknown status");
  return false;
}

}  // namespace mozilla::intl