signal-desktop/libtextsecure/protobufs.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2015-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2018-07-21 21:51:20 +00:00
/* global window, dcodeIO, textsecure */
// eslint-disable-next-line func-names
(function () {
const FILES_TO_LOAD = [
'SignalService.proto',
'SignalStorage.proto',
'SubProtocol.proto',
'DeviceMessages.proto',
'Stickers.proto',
// Just for encrypting device names
'DeviceName.proto',
// Metadata-specific protos
'UnidentifiedDelivery.proto',
// Groups
'Groups.proto',
];
let remainingFilesToLoad = FILES_TO_LOAD.length;
const hasFinishedLoading = () => remainingFilesToLoad <= 0;
let onLoadCallbacks = [];
2018-05-02 16:51:22 +00:00
window.textsecure = window.textsecure || {};
window.textsecure.protobuf = {
onLoad: callback => {
if (hasFinishedLoading()) {
setTimeout(callback, 0);
} else {
onLoadCallbacks.push(callback);
}
},
};
FILES_TO_LOAD.forEach(filename => {
dcodeIO.ProtoBuf.loadProtoFile(
2018-05-02 16:51:22 +00:00
{ root: window.PROTO_ROOT, file: filename },
2018-07-21 21:51:20 +00:00
(error, result) => {
2018-05-02 16:51:22 +00:00
if (error) {
2018-07-21 21:51:20 +00:00
const text = `Error loading protos from ${filename} (root: ${
window.PROTO_ROOT
}) ${error && error.stack ? error.stack : error}`;
window.log.error(text);
2018-05-02 16:51:22 +00:00
throw error;
}
2018-07-21 21:51:20 +00:00
const protos = result.build('signalservice');
2018-05-02 16:51:22 +00:00
if (!protos) {
2020-09-09 02:25:05 +00:00
const text = `Error loading protos from ${filename} - no exported types! (root: ${window.PROTO_ROOT})`;
window.log.error(text);
2018-05-02 16:51:22 +00:00
throw new Error(text);
}
2018-07-21 21:51:20 +00:00
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const protoName in protos) {
2018-05-02 16:51:22 +00:00
textsecure.protobuf[protoName] = protos[protoName];
}
remainingFilesToLoad -= 1;
if (hasFinishedLoading()) {
onLoadCallbacks.forEach(callback => callback());
onLoadCallbacks = [];
}
2018-05-02 16:51:22 +00:00
}
);
});
})();