Skip to content

Commit

Permalink
fix: some opt
Browse files Browse the repository at this point in the history
Signed-off-by: 王山栋 <[email protected]>
  • Loading branch information
ezailWang committed Oct 20, 2023
1 parent ad451a8 commit 69f5f13
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 106 deletions.
76 changes: 43 additions & 33 deletions apps/desktop/src-electron/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import logger from 'electron-log';

// eslint-disable-next-line @typescript-eslint/no-unused-vars

import { ipcMessageKeys } from './config';
import { registerShortcuts, unregisterShortcuts } from './libs/shortcuts';
import * as store from './libs/store';
import initProcess, { restartBridge } from './process/index';
Expand Down Expand Up @@ -161,9 +162,12 @@ function handleDeepLinkUrl(
if (mainWindow) {
showMainWindow();
if (process.env.NODE_ENV !== 'production') {
mainWindow.webContents.send('OPEN_URL_DEEP_LINK_MESSAGE', eventData);
mainWindow.webContents.send(
ipcMessageKeys.OPEN_DEEP_LINK_URL,
eventData,
);
}
mainWindow.webContents.send('event-open-url', eventData);
mainWindow.webContents.send(ipcMessageKeys.EVENT_OPEN_URL, eventData);
}
};
if (isAppReady && mainWindow) {
Expand Down Expand Up @@ -239,7 +243,7 @@ function createMainWindow() {

browserWindow.webContents.on('did-finish-load', () => {
console.log('browserWindow >>>> did-finish-load');
browserWindow.webContents.send('SET_ONEKEY_DESKTOP_GLOBALS', {
browserWindow.webContents.send(ipcMessageKeys.SET_ONEKEY_DESKTOP_GLOBALS, {
resourcesPath: (global as any).resourcesPath,
staticPath: `file://${staticPath}`,
preloadJsUrl: `file://${preloadJsUrl}?timestamp=${Date.now()}`,
Expand Down Expand Up @@ -274,27 +278,27 @@ function createMainWindow() {
return { action: 'deny' };
});

ipcMain.on('app/ready', () => {
ipcMain.on(ipcMessageKeys.APP_READY, () => {
isAppReady = true;
console.log('set isAppReady on ipcMain app/ready', isAppReady);
emitter.emit('ready');
});
ipcMain.on('app/reload', () => {
ipcMain.on(ipcMessageKeys.APP_READY, () => {
if (!process.mas) {
app.relaunch();
}
app.exit(0);
disposeContextMenu();
});
ipcMain.on('app/focus', () => {
ipcMain.on(ipcMessageKeys.APP_FOCUS, () => {
showMainWindow();
});
ipcMain.on('app/quit', () => {
ipcMain.on(ipcMessageKeys.APP_QUIT, () => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
quitOrMinimizeApp();
});

ipcMain.on('app/openPrefs', (_event, prefType: PrefType) => {
ipcMain.on(ipcMessageKeys.APP_OPEN_PREFS, (_event, prefType: PrefType) => {
const platform = os.type();
if (platform === 'Darwin') {
shell.openPath('/System/Library/PreferencePanes/Security.prefPane');
Expand All @@ -309,7 +313,7 @@ function createMainWindow() {
}
});

ipcMain.on('app/toggleMaximizeWindow', () => {
ipcMain.on(ipcMessageKeys.APP_TOGGLE_MAXIMIZE_WINDOW, () => {
if (browserWindow.isMaximized()) {
// Restore the original window size
browserWindow.unmaximize();
Expand All @@ -319,81 +323,87 @@ function createMainWindow() {
}
});

ipcMain.on('app/canPromptTouchID', (event) => {
ipcMain.on(ipcMessageKeys.TOUCH_ID_CAN_PROMPT, (event) => {
const result = systemPreferences?.canPromptTouchID?.();
event.returnValue = !!result;
});

ipcMain.on('app/promptTouchID', async (event, msg: string) => {
ipcMain.on(ipcMessageKeys.TOUCH_ID_PROMPT, async (event, msg: string) => {
try {
await systemPreferences.promptTouchID(msg);
event.reply('app/promptTouchID/res', { success: true });
event.reply(ipcMessageKeys.TOUCH_ID_PROMPT_RES, { success: true });
} catch (e: any) {
event.reply('app/promptTouchID/res', {
event.reply(ipcMessageKeys.TOUCH_ID_PROMPT_RES, {
success: false,
error: e.message,
});
}
});

ipcMain.on(
'app/secureSetItemAsync',
ipcMessageKeys.SECURE_SET_ITEM_ASYNC,
(event, { key, value }: { key: string; value: string }) => {
store.setSecureItem(key, value);
event.returnValue = '';
},
);

ipcMain.on('app/secureGetItemAsync', (event, { key }: { key: string }) => {
const value = store.getSecureItem(key);
event.returnValue = value;
});
ipcMain.on(
ipcMessageKeys.SECURE_GET_ITEM_ASYNC,
(event, { key }: { key: string }) => {
const value = store.getSecureItem(key);
event.returnValue = value;
},
);

ipcMain.on('app/secureDelItemAsync', (event, { key }: { key: string }) => {
store.clearSecureItem(key);
event.returnValue = '';
});
ipcMain.on(
ipcMessageKeys.SECURE_DEL_ITEM_ASYNC,
(event, { key }: { key: string }) => {
store.deleteSecureItem(key);
event.returnValue = '';
},
);

ipcMain.on('app/reloadBridgeProcess', (event) => {
ipcMain.on(ipcMessageKeys.APP_RELOAD_BRIDGE_PROCESS, (event) => {
logger.debug('reloadBridgeProcess receive');
restartBridge();
event.reply('app/reloadBridgeProcess', true);
event.reply(ipcMessageKeys.APP_RELOAD_BRIDGE_PROCESS, true);
});

ipcMain.on('app/restoreMainWindow', (event) => {
ipcMain.on(ipcMessageKeys.APP_RESTORE_MAIN_WINDOW, (event) => {
logger.debug('restoreMainWindow receive');
browserWindow.show();
event.reply('app/restoreMainWindow', true);
event.reply(ipcMessageKeys.APP_RESTORE_MAIN_WINDOW, true);
});

ipcMain.on('app/clearWebViewData', () => {
ipcMain.on(ipcMessageKeys.APP_CLEAR_WEBVIEW_DATA, () => {
clearWebData();
});

// reset appState to undefined to avoid screen lock.
browserWindow.on('enter-full-screen', () => {
browserWindow.webContents.send('appState', undefined);
browserWindow.webContents.send(ipcMessageKeys.APP_STATE, undefined);
});

// reset appState to undefined to avoid screen lock.
browserWindow.on('leave-full-screen', () => {
browserWindow.webContents.send('appState', undefined);
browserWindow.webContents.send(ipcMessageKeys.APP_STATE, undefined);
});

browserWindow.on('focus', () => {
browserWindow.webContents.send('appState', 'active');
browserWindow.webContents.send(ipcMessageKeys.APP_STATE, 'active');
registerShortcuts((event) => {
browserWindow.webContents.send('shortcut', event);
browserWindow.webContents.send(ipcMessageKeys.APP_SHORCUT, event);
});
});

browserWindow.on('blur', () => {
browserWindow.webContents.send('appState', 'blur');
browserWindow.webContents.send(ipcMessageKeys.APP_STATE, 'blur');
unregisterShortcuts();
});

browserWindow.on('hide', () => {
browserWindow.webContents.send('appState', 'background');
browserWindow.webContents.send(ipcMessageKeys.APP_STATE, 'background');
unregisterShortcuts();
});

Expand Down
57 changes: 57 additions & 0 deletions apps/desktop/src-electron/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,60 @@ export const cspRules = [
// Allow images from trezor.io
"img-src 'self' onekey.243096.com devs.243096.com onekey.so *.onekey.so onekey-asset.com",
];

export const ipcMessageKeys = {
// Updater
UPDATE_CHECK: 'update/check',
UPDATE_SETTINGS: 'update/settings',
UPDATE_CLEAR_SETTINGS: 'update/clearSettings',
UPDATE_DOWNLOAD: 'update/download',
UPDATE_INSTALL: 'update/install',
UPDATE_CHECKING: 'update/checking',
UPDATE_AVAILABLE: 'update/available',
UPDATE_NOT_AVAILABLE: 'update/not-available',
UPDATE_ERROR: 'update/error',
UPDATE_DOWNLOADING: 'update/downloading',
UPDATE_DOWNLOADED: 'update/downloaded',
TOUCH_UPDATE_RES_SUCCESS: 'touch/update-res-success',
TOUCH_UPDATE_PROGRESS: 'touch/update-progress',

// App
APP_STATE: 'appState',
APP_READY: 'app/ready',
APP_RELOAD: 'app/reload',
APP_FOCUS: 'app/focus',
APP_QUIT: 'app/quit',
APP_RESTORE_MAIN_WINDOW: 'app/restoreMainWindow',
APP_CLEAR_WEBVIEW_DATA: 'app/clearWebViewData',
APP_OPEN_PREFS: 'app/openPrefs',
APP_TOGGLE_MAXIMIZE_WINDOW: 'app/toggleMaximizeWindow',
APP_RELOAD_BRIDGE_PROCESS: 'app/reloadBridgeProcess',
APP_SHORCUT: 'app/shortcut',

// Touch
TOUCH_ID_CAN_PROMPT: 'touchId/canPrompt',
TOUCH_ID_PROMPT_RES: 'touchId/prompt-res',
TOUCH_ID_PROMPT: 'touchId/prompt',
TOUCH_RES: 'touch/res',
TOUCH_OPEN_PRIVACY_PANEL: 'touch/openPrivacyPanel',

// Secure storage
SECURE_SET_ITEM_ASYNC: 'secure/setItemAsync',
SECURE_GET_ITEM_ASYNC: 'secure/getItemAsync',
SECURE_DEL_ITEM_ASYNC: 'secure/delItemAsync',

// Server
SERVER_START: 'server/start',
SERVER_STOP: 'server/stop',
SERVER_START_RES: 'server/start/res',
SERVER_LISTENER: 'server/listener',
SERVER_RESPOND: 'server/respond',

// Evnet
EVENT_OPEN_URL: 'event-open-url',

// DeepLink
OPEN_DEEP_LINK_URL: 'deepLink/openUrl',

SET_ONEKEY_DESKTOP_GLOBALS: 'onekey/setDesktopGlobals',
};
6 changes: 3 additions & 3 deletions apps/desktop/src-electron/libs/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { safeStorage } from 'electron';
import Store from 'electron-store';

const store = new Store();
const store = new Store({ name: 'OneKey' });

export type LocalStore = {
getUpdateSettings(): UpdateSettings;
Expand All @@ -13,7 +13,7 @@ export type UpdateSettings = {
useTestFeedUrl: boolean;
};

const EncryptedData = 'EncryptedData';
const EncryptedData = 'OneKey_EncryptedData';

export const getUpdateSettings = (): UpdateSettings =>
store.get('updateSettings', { useTestFeedUrl: false }) as UpdateSettings;
Expand Down Expand Up @@ -45,7 +45,7 @@ export const setSecureItem = (key: string, value: string): void => {
store.set(EncryptedData, items);
};

export const clearSecureItem = (key: string) => {
export const deleteSecureItem = (key: string) => {
const items = store.get(EncryptedData, {}) as Record<string, string>;
delete items[key];
store.set(EncryptedData, items);
Expand Down
Loading

0 comments on commit 69f5f13

Please sign in to comment.