From 1c23c6a9d7e3e0fc69e4f73895d4ced9452bb7fb Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 1 Jun 2018 17:55:35 -0700 Subject: [PATCH] Remove window.config in favor of window.getXXX() accessors --- js/background.js | 34 ++++++++--------------------- js/conversation_controller.js | 5 ++--- js/expire.js | 2 +- js/logging.js | 4 ++-- js/views/app_view.js | 2 +- js/views/install_view.js | 2 +- libtextsecure/account_manager.js | 2 +- libtextsecure/message_receiver.js | 5 +---- libtextsecure/sendmessage.js | 2 +- preload.js | 36 ++++++++++++++++++++++--------- 10 files changed, 45 insertions(+), 49 deletions(-) diff --git a/js/background.js b/js/background.js index d0255d387..36258733c 100644 --- a/js/background.js +++ b/js/background.js @@ -29,20 +29,11 @@ window.onInvalidStateError = e => console.log(e); console.log('background page reloaded'); - console.log('environment:', window.config.environment); + console.log('environment:', window.getEnvironment()); let initialLoadComplete = false; window.owsDesktopApp = {}; - - let title = window.config.name; - if (window.config.environment !== 'production') { - title += ` - ${window.config.environment}`; - } - if (window.config.appInstance) { - title += ` - ${window.config.appInstance}`; - } - window.config.title = title; - window.document.title = title; + window.document.title = window.getTitle(); // start a background worker for ecc textsecure.startWorker('js/libsignal-protocol-worker.js'); @@ -51,8 +42,6 @@ getAccountManager().refreshPreKeys(); }); - const SERVER_URL = window.config.serverUrl; - const CDN_URL = window.config.cdnUrl; let messageReceiver; window.getSocketStatus = () => { if (messageReceiver) { @@ -66,11 +55,7 @@ if (!accountManager) { const USERNAME = storage.get('number_id'); const PASSWORD = storage.get('password'); - accountManager = new textsecure.AccountManager( - SERVER_URL, - USERNAME, - PASSWORD - ); + accountManager = new textsecure.AccountManager(USERNAME, PASSWORD); accountManager.addEventListener('registration', () => { Whisper.Registration.markDone(); console.log('dispatching registration event'); @@ -174,13 +159,15 @@ }); function start() { - const currentVersion = window.config.version; + const currentVersion = window.getVersion(); const lastVersion = storage.get('version'); const newVersion = !lastVersion || currentVersion !== lastVersion; storage.put('version', currentVersion); if (newVersion) { - console.log('New version detected:', currentVersion); + console.log( + `New version detected: ${currentVersion}; previous: ${lastVersion}` + ); } window.dispatchEvent(new Event('storage_ready')); @@ -210,7 +197,7 @@ appView.openInbox({ initialLoadComplete, }); - } else if (window.config.importMode) { + } else if (window.isImportMode()) { appView.openImporter(); } else { appView.openInstaller(); @@ -364,7 +351,6 @@ // initialize the socket and start listening for messages messageReceiver = new textsecure.MessageReceiver( - SERVER_URL, USERNAME, PASSWORD, mySignalingKey, @@ -384,10 +370,8 @@ messageReceiver.addEventListener('configuration', onConfiguration); window.textsecure.messaging = new textsecure.MessageSender( - SERVER_URL, USERNAME, - PASSWORD, - CDN_URL + PASSWORD ); // Because v0.43.2 introduced a bug that lost contact details, v0.43.4 introduces diff --git a/js/conversation_controller.js b/js/conversation_controller.js index d2e58e480..879dff9d5 100644 --- a/js/conversation_controller.js +++ b/js/conversation_controller.js @@ -62,11 +62,10 @@ if (newUnreadCount > 0) { window.setBadgeCount(newUnreadCount); - window.document.title = - window.config.title + ' (' + newUnreadCount + ')'; + window.document.title = window.getTitle() + ' (' + newUnreadCount + ')'; } else { window.setBadgeCount(0); - window.document.title = window.config.title; + window.document.title = window.getTitle(); } window.updateTrayIcon(newUnreadCount); }, diff --git a/js/expire.js b/js/expire.js index cf18a0ddc..a9fad48d3 100644 --- a/js/expire.js +++ b/js/expire.js @@ -2,7 +2,7 @@ 'use strict'; var BUILD_EXPIRATION = 0; try { - BUILD_EXPIRATION = parseInt(window.config.buildExpiration); + BUILD_EXPIRATION = parseInt(window.getExpiration()); if (BUILD_EXPIRATION) { console.log('Build expires: ', new Date(BUILD_EXPIRATION).toISOString()); } diff --git a/js/logging.js b/js/logging.js index 3c5b0d339..07a5b7f61 100644 --- a/js/logging.js +++ b/js/logging.js @@ -60,8 +60,8 @@ if (window.console) { function getHeader() { let header = window.navigator.userAgent; - header += ` node/${window.config.node_version}`; - header += ` env/${window.config.environment}`; + header += ` node/${window.getNodeVersion()}`; + header += ` env/${window.getEnvironment()}`; return header; } diff --git a/js/views/app_view.js b/js/views/app_view.js index 5ace0bf31..d03c0917a 100644 --- a/js/views/app_view.js +++ b/js/views/app_view.js @@ -91,7 +91,7 @@ } }, openStandalone: function() { - if (window.config.environment !== 'production') { + if (window.getEnvironment() !== 'production') { window.addSetupMenuItems(); this.resetViews(); this.standaloneView = new Whisper.StandaloneRegistrationView(); diff --git a/js/views/install_view.js b/js/views/install_view.js index c9b401daa..c3c7c5371 100644 --- a/js/views/install_view.js +++ b/js/views/install_view.js @@ -142,7 +142,7 @@ setDeviceNameDefault: function() { var deviceName = textsecure.storage.user.getDeviceName(); - this.$(DEVICE_NAME_SELECTOR).val(deviceName || window.config.hostname); + this.$(DEVICE_NAME_SELECTOR).val(deviceName || window.getHostName()); this.$(DEVICE_NAME_SELECTOR).focus(); }, finishLinking: function() { diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 454a0c507..f740c739f 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -4,7 +4,7 @@ var ARCHIVE_AGE = 7 * 24 * 60 * 60 * 1000; - function AccountManager(url, username, password) { + function AccountManager(username, password) { this.server = window.WebAPI.connect({ username, password }); this.pending = Promise.resolve(); } diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ce1f885e0..1d312967c 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -12,10 +12,9 @@ /* eslint-disable more/no-then */ -function MessageReceiver(url, username, password, signalingKey, options = {}) { +function MessageReceiver(username, password, signalingKey, options = {}) { this.count = 0; - this.url = url; this.signalingKey = signalingKey; this.username = username; this.password = password; @@ -1121,14 +1120,12 @@ MessageReceiver.prototype.extend({ window.textsecure = window.textsecure || {}; textsecure.MessageReceiver = function MessageReceiverWrapper( - url, username, password, signalingKey, options ) { const messageReceiver = new MessageReceiver( - url, username, password, signalingKey, diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 1a0d7f9f7..5cdcfe724 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -139,7 +139,7 @@ Message.prototype = { }, }; -function MessageSender(url, username, password, cdn_url) { +function MessageSender(username, password) { this.server = WebAPI.connect({ username, password }); this.pendingMessages = {}; } diff --git a/preload.js b/preload.js index 28b00b840..1a37b0a3b 100644 --- a/preload.js +++ b/preload.js @@ -10,12 +10,28 @@ 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; +const config = require('url').parse(window.location.toString(), true).query; + +let title = config.name; +if (config.environment !== 'production') { + title += ` - ${config.environment}`; +} +if (config.appInstance) { + title += ` - ${config.appInstance}`; +} + +window.getTitle = () => title; +window.getEnvironment = () => config.environment; +window.getVersion = () => config.version; +window.isImportMode = () => config.importMode; +window.getExpiration = () => config.buildExpiration; +window.getNodeVersion = () => config.node_version; +window.getHostName = () => config.hostname; window.wrapDeferred = deferredToPromise; const ipc = electron.ipcRenderer; -window.config.localeMessages = ipc.sendSync('locale-data'); +const localeMessages = ipc.sendSync('locale-data'); window.setBadgeCount = count => ipc.send('set-badge-count', count); @@ -77,8 +93,8 @@ window.removeSetupMenuItems = () => ipc.send('remove-setup-menu-items'); require('./js/logging'); -if (window.config.proxyUrl) { - console.log('using proxy url', window.config.proxyUrl); +if (config.proxyUrl) { + console.log('using proxy url', config.proxyUrl); } window.nodeSetImmediate = setImmediate; @@ -86,10 +102,10 @@ window.nodeSetImmediate = setImmediate; const { initialize: initializeWebAPI } = require('./js/modules/web_api'); window.WebAPI = initializeWebAPI({ - url: window.config.serverUrl, - cdnUrl: window.config.cdnUrl, - certificateAuthority: window.config.certificateAuthority, - proxyUrl: window.config.proxyUrl, + url: config.serverUrl, + cdnUrl: config.cdnUrl, + certificateAuthority: config.certificateAuthority, + proxyUrl: config.proxyUrl, }); // Linux seems to periodically let the event loop stop, so this is a global workaround @@ -123,7 +139,7 @@ const Signal = require('./js/modules/signal'); const i18n = require('./js/modules/i18n'); const Attachments = require('./app/attachments'); -const { locale, localeMessages } = window.config; +const { locale } = config; window.i18n = i18n.setup(locale, localeMessages); window.moment.updateLocale(locale, { relativeTime: { @@ -149,7 +165,7 @@ window.Signal.Logs = require('./js/modules/logs'); // /tmp mounted as noexec on Linux. require('./js/spell_check'); -if (window.config.environment === 'test') { +if (config.environment === 'test') { /* eslint-disable global-require, import/no-extraneous-dependencies */ window.test = { glob: require('glob'),