signal-desktop/preload.js

237 lines
7.7 KiB
JavaScript
Raw Normal View History

2018-04-03 19:03:57 +00:00
/* global Whisper: false */
/* global window: false */
console.log('preload');
const electron = require('electron');
const Attachment = require('./js/modules/types/attachment');
const Attachments = require('./app/attachments');
const Message = require('./js/modules/types/message');
const { deferredToPromise } = require('./js/modules/deferred_to_promise');
const { app } = electron.remote;
window.PROTO_ROOT = 'protos';
window.config = require('url').parse(window.location.toString(), true).query;
window.wrapDeferred = deferredToPromise;
const ipc = electron.ipcRenderer;
window.config.localeMessages = ipc.sendSync('locale-data');
window.setBadgeCount = count =>
ipc.send('set-badge-count', count);
window.drawAttention = () => {
console.log('draw attention');
ipc.send('draw-attention');
};
window.showWindow = () => {
console.log('show window');
ipc.send('show-window');
};
window.setAutoHideMenuBar = autoHide =>
ipc.send('set-auto-hide-menu-bar', autoHide);
window.setMenuBarVisibility = visibility =>
ipc.send('set-menu-bar-visibility', visibility);
window.restart = () => {
console.log('restart');
ipc.send('restart');
};
window.closeAbout = () =>
ipc.send('close-about');
window.updateTrayIcon = unreadCount =>
ipc.send('update-tray-icon', unreadCount);
ipc.on('debug-log', () => {
Whisper.events.trigger('showDebugLog');
});
ipc.on('set-up-with-import', () => {
Whisper.events.trigger('setupWithImport');
});
ipc.on('set-up-as-new-device', () => {
Whisper.events.trigger('setupAsNewDevice');
});
ipc.on('set-up-as-standalone', () => {
Whisper.events.trigger('setupAsStandalone');
});
ipc.on('show-settings', () => {
Whisper.events.trigger('showSettings');
});
window.addSetupMenuItems = () =>
ipc.send('add-setup-menu-items');
window.removeSetupMenuItems = () =>
ipc.send('remove-setup-menu-items');
// We pull these dependencies in now, from here, because they have Node.js dependencies
require('./js/logging');
if (window.config.proxyUrl) {
console.log('using proxy url', window.config.proxyUrl);
}
window.nodeSetImmediate = setImmediate;
window.nodeWebSocket = require('websocket').w3cwebsocket;
// Linux seems to periodically let the event loop stop, so this is a global workaround
setInterval(() => {
window.nodeSetImmediate(() => {});
}, 1000);
const { autoOrientImage } = require('./js/modules/auto_orient_image');
window.autoOrientImage = autoOrientImage;
window.dataURLToBlobSync = require('blueimp-canvas-to-blob');
window.EmojiConvertor = require('emoji-js');
window.emojiData = require('emoji-datasource');
window.EmojiPanel = require('emoji-panel');
2018-04-24 15:19:39 +00:00
window.filesize = require('filesize');
2018-04-03 19:03:57 +00:00
window.libphonenumber = require('google-libphonenumber').PhoneNumberUtil.getInstance();
window.libphonenumber.PhoneNumberFormat =
require('google-libphonenumber').PhoneNumberFormat;
window.loadImage = require('blueimp-load-image');
window.nodeBuffer = Buffer;
window.nodeFetch = require('node-fetch');
window.nodeNotifier = require('node-notifier');
window.ProxyAgent = require('proxy-agent');
// Note: when modifying this file, consider whether our React Components or Backbone Views
// will need these things to render in the Style Guide. If so, go update one of these
// two locations:
//
// 1) test/styleguide/legacy_bridge.js
2018-04-11 20:31:35 +00:00
// 2) ts/styleguide/StyleGuideUtil.js
window.React = require('react');
window.ReactDOM = require('react-dom');
window.moment = require('moment');
2018-04-03 19:03:57 +00:00
2018-04-05 19:51:29 +00:00
const { setup } = require('./js/modules/i18n');
const { locale, localeMessages } = window.config;
window.i18n = setup(locale, localeMessages);
window.moment.updateLocale(locale, {
relativeTime: {
s: window.i18n('timestamp_s'),
m: window.i18n('timestamp_m'),
h: window.i18n('timestamp_h'),
},
});
window.moment.locale(locale);
2018-04-03 19:03:57 +00:00
// ES2015+ modules
const attachmentsPath = Attachments.getPath(app.getPath('userData'));
const deleteAttachmentData = Attachments.createDeleter(attachmentsPath);
const readAttachmentData = Attachments.createReader(attachmentsPath);
const writeNewAttachmentData = Attachments.createWriterForNew(attachmentsPath);
2018-04-04 01:10:34 +00:00
const writeExistingAttachmentData = Attachments.createWriterForExisting(attachmentsPath);
2018-04-03 19:03:57 +00:00
const loadAttachmentData = Attachment.loadData(readAttachmentData);
2018-04-03 19:03:57 +00:00
// Injected context functions to keep `Message` agnostic from Electron:
const upgradeSchemaContext = {
writeNewAttachmentData,
2018-04-03 19:03:57 +00:00
};
const upgradeMessageSchema = message =>
Message.upgradeSchema(message, upgradeSchemaContext);
const { getPlaceholderMigrations } =
require('./js/modules/migrations/get_placeholder_migrations');
const { IdleDetector } = require('./js/modules/idle_detector');
window.Signal = {};
2018-04-14 02:16:09 +00:00
window.Signal.Backbone = require('./ts/backbone');
2018-04-03 19:03:57 +00:00
window.Signal.Backup = require('./js/modules/backup');
window.Signal.Crypto = require('./js/modules/crypto');
window.Signal.Database = require('./js/modules/database');
window.Signal.Debug = require('./js/modules/debug');
window.Signal.HTML = require('./ts/html');
2018-04-03 19:03:57 +00:00
window.Signal.Logs = require('./js/modules/logs');
2018-04-15 03:27:03 +00:00
// React components
const { Lightbox } = require('./ts/components/Lightbox');
2018-04-11 15:57:31 +00:00
const { MediaGallery } =
require('./ts/components/conversation/media-gallery/MediaGallery');
const { Quote } = require('./ts/components/conversation/Quote');
2018-04-09 23:38:40 +00:00
const MediaGalleryMessage =
require('./ts/components/conversation/media-gallery/types/Message');
2018-04-15 00:09:52 +00:00
2018-04-09 23:38:40 +00:00
window.Signal.Components = {
2018-04-15 03:27:03 +00:00
Lightbox,
2018-04-11 15:57:31 +00:00
MediaGallery,
Types: {
Message: MediaGalleryMessage,
2018-04-15 00:09:52 +00:00
},
2018-04-09 23:38:40 +00:00
Quote,
};
2018-04-03 19:03:57 +00:00
window.Signal.Migrations = {};
window.Signal.Migrations.deleteAttachmentData =
Attachment.deleteData(deleteAttachmentData);
window.Signal.Migrations.getPlaceholderMigrations = getPlaceholderMigrations;
window.Signal.Migrations.writeMessageAttachments =
Message.createAttachmentDataWriter(writeExistingAttachmentData);
window.Signal.Migrations.loadAttachmentData = loadAttachmentData;
2018-04-04 01:17:48 +00:00
window.Signal.Migrations.loadMessage = Message.createAttachmentLoader(loadAttachmentData);
2018-04-03 19:03:57 +00:00
window.Signal.Migrations.Migrations0DatabaseWithAttachmentData =
require('./js/modules/migrations/migrations_0_database_with_attachment_data');
window.Signal.Migrations.Migrations1DatabaseWithoutAttachmentData =
require('./js/modules/migrations/migrations_1_database_without_attachment_data');
window.Signal.Migrations.upgradeMessageSchema = upgradeMessageSchema;
window.Signal.OS = require('./js/modules/os');
window.Signal.Settings = require('./js/modules/settings');
2018-04-04 22:10:34 +00:00
window.Signal.Startup = require('./js/modules/startup');
2018-04-03 19:03:57 +00:00
window.Signal.Types = {};
window.Signal.Types.Attachment = Attachment;
window.Signal.Types.AttachmentTS = require('./ts/types/Attachment');
2018-04-11 23:23:03 +00:00
window.Signal.Types.Conversation = require('./ts/types/Conversation');
2018-04-03 19:03:57 +00:00
window.Signal.Types.Errors = require('./js/modules/types/errors');
window.Signal.Types.Message = Message;
2018-04-09 23:24:24 +00:00
window.Signal.Types.MIME = require('./ts/types/MIME');
2018-04-03 19:03:57 +00:00
window.Signal.Types.Settings = require('./js/modules/types/settings');
window.Signal.Util = require('./ts/util');
2018-04-03 19:03:57 +00:00
window.Signal.Views = {};
window.Signal.Views.Initialization = require('./js/modules/views/initialization');
window.Signal.Workflow = {};
window.Signal.Workflow.IdleDetector = IdleDetector;
window.Signal.Workflow.MessageDataMigrator =
require('./js/modules/messages_data_migrator');
// We pull this in last, because the native module involved appears to be sensitive to
// /tmp mounted as noexec on Linux.
require('./js/spell_check');
2018-04-20 21:55:33 +00:00
if (window.config.environment === 'test') {
/* eslint-disable global-require, import/no-extraneous-dependencies */
window.test = {
glob: require('glob'),
fse: require('fs-extra'),
tmp: require('tmp'),
path: require('path'),
basePath: __dirname,
attachmentsPath,
};
/* eslint-enable global-require, import/no-extraneous-dependencies */
}