summaryrefslogtreecommitdiffhomepage
path: root/app/app.js
blob: 429c7e39cd49d07948ffcfe5e69324b14383e641 (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
// @flow

import React from 'react';
import { Component} from 'reactxp';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'react-router-redux';
import { createMemoryHistory } from 'history';
import { webFrame, ipcRenderer } from 'electron';
import { log } from './lib/platform';
import makeRoutes from './routes';
import configureStore from './redux/store';
import { Backend, BackendError } from './lib/backend';

import type { ConnectionState } from './redux/connection/reducers';
import type { TrayIconType } from './lib/tray-icon-manager';

const initialState = null;
const memoryHistory = createMemoryHistory();
const store = configureStore(initialState, memoryHistory);

//////////////////////////////////////////////////////////////////////////
// Backend
//////////////////////////////////////////////////////////////////////////
const backend = new Backend(store);
ipcRenderer.on('backend-info', async (_event, args) => {
  backend.setCredentials(args.credentials);
  backend.sync();
  try {
    await backend.autologin();
    await backend.fetchRelaySettings();
    await backend.fetchSecurityState();
    await backend.connect();
  } catch (e) {
    if(e instanceof BackendError) {
      if(e.type === 'NO_ACCOUNT') {
        log.debug('No user set in the backend, showing window');
        ipcRenderer.send('show-window');
      }
    }
  }
});

ipcRenderer.on('shutdown', () => {
  log.info('Been told by the node process to shutdown');
  backend.shutdown()
    .catch( e => {
      log.warn('Unable to shut down the backend', e.message);
    });
});

ipcRenderer.on('disconnect', () => {
  log.info('Been told by the node process to disconnect the tunnel');
  backend.disconnect()
    .catch( e => {
      log.warn('Unable to disconnect the tunnel', e.message);
    });
});

ipcRenderer.on('app-shutdown', () => {
  log.info('Been told by the renderer process that the app is shutting down');
  // The shutdown behaviour may have to be different on mobile platforms
  const shutdown_func = process.platform === 'darwin' ? () => backend.shutdown() : () => backend.disconnect();
  shutdown_func().catch( e => {
    log.error('Failed to shutdown tunnel: ', e);
  });

  // no matter what, don't block the frontend from shutting down, I guess.
  ipcRenderer.send('daemon-shutdown', true);
});

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// Tray icon
//////////////////////////////////////////////////////////////////////////

/**
 * Get tray icon type based on connection state
 */
const getIconType = (s: ConnectionState): TrayIconType => {
  switch(s) {
  case 'connected': return 'secured';
  case 'connecting': return 'securing';
  default: return 'unsecured';
  }
};

/**
 * Update tray icon via IPC call
 */
const updateTrayIcon = () => {
  const { connection } = store.getState();
  // TODO: Only update the tray icon if the connection status changed
  ipcRenderer.send('changeTrayIcon', getIconType(connection.status));
};
store.subscribe(updateTrayIcon);

// force update tray
updateTrayIcon();
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

// disable smart pinch.
webFrame.setZoomLevelLimits(1, 1);

ipcRenderer.send('on-browser-window-ready');


export default class App extends Component{
  render() {
    return (
      <Provider store={ store }>
        <ConnectedRouter history={ memoryHistory }>
          { makeRoutes(store.getState, { backend }) }
        </ConnectedRouter>
      </Provider>
    );
  }
}