Skip to content

Commit e0d238a

Browse files
committed
fix: timing of loading extensions
1 parent 228257b commit e0d238a

File tree

13 files changed

+66
-111
lines changed

13 files changed

+66
-111
lines changed

docs/state.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Lulumi browser takes advantage of using [vuex](https://github.com/vuejs/vuex) to store lots of information. If you are not familiar with it, I suggest taking a look at its [guide](https://vuex.vuejs.org/).
44

5-
# appState
5+
# State
66

77
```javascript
88
{

src/main/api/lulumi-extension.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import './listeners';
1414
/* tslint:disable:max-line-length */
1515

1616
const globalObject = global as Lulumi.API.GlobalObject;
17-
globalObject.persistentLoaded = false;
1817

1918
// ../../shared/store/mainStore.ts
2019
const { default: mainStore } = require('../../shared/store/mainStore');
@@ -55,6 +54,7 @@ const getManifestFromPath: (srcDirectory: string) => Lulumi.API.ManifestObject |
5554
if (!manifestNameMap[manifest.name]) {
5655
const extensionId = generateExtensionIdFromName();
5756
manifestMap[extensionId] = manifestNameMap[manifest.name] = manifest;
57+
globalObject.manifestMap = manifestMap;
5858

5959
let messages = {};
6060
if (manifest.default_locale) {
@@ -123,6 +123,7 @@ const startBackgroundPages = (manifest: Lulumi.API.ManifestObject) => {
123123
webSecurity: false,
124124
});
125125
backgroundPages[manifest.extensionId] = { html, name, webContentsId: contents.id };
126+
globalObject.backgroundPages = backgroundPages;
126127
contents.loadURL(urllib.format({
127128
protocol: 'lulumi-extension',
128129
slashes: true,
@@ -311,6 +312,9 @@ app.on('will-quit', () => {
311312
});
312313

313314
app.whenReady().then(() => {
315+
// load persisted extensions
316+
loadExtensions();
317+
314318
// the public API to add/remove extensions
315319
((BrowserWindow as any) as Lulumi.BrowserWindow).addLulumiExtension = (srcDirectory: string): string => {
316320
const manifest = getManifestFromPath(srcDirectory);
@@ -352,22 +356,19 @@ app.whenReady().then(() => {
352356
// we can not use protocol or BrowserWindow until app is ready,
353357
// and hopefully, this function will be called after app is ready
354358
const loadExtensions = () => {
355-
if (!globalObject.persistentLoaded) {
356-
try {
357-
const loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath, 'utf8'));
358-
if (Array.isArray(loadedExtensions)) {
359-
for (const srcDirectory of loadedExtensions) {
360-
// start background pages and set content scripts
361-
const manifest = getManifestFromPath(srcDirectory);
362-
if (manifest !== null) {
363-
loadExtension(manifest);
364-
}
359+
try {
360+
const loadedExtensions = JSON.parse(fs.readFileSync(loadedExtensionsPath, 'utf8'));
361+
if (Array.isArray(loadedExtensions)) {
362+
for (const srcDirectory of loadedExtensions) {
363+
// start background pages and set content scripts
364+
const manifest = getManifestFromPath(srcDirectory);
365+
if (manifest !== null) {
366+
loadExtension(manifest);
365367
}
366368
}
367-
} catch (error) {
368-
// ignore error
369369
}
370-
globalObject.persistentLoaded = true;
370+
} catch (error) {
371+
// ignore error
371372
}
372373
};
373374

@@ -376,5 +377,4 @@ export default {
376377
manifestNameMap,
377378
backgroundPages,
378379
registerLocalCommands,
379-
loadExtensions,
380380
};

src/main/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export default {
1414
lulumiPreloadPath: `${path.resolve(lulumiRootPath, 'dist')}`,
1515
lulumiPagesPath: `${path.resolve(lulumiHelperPath, 'pages')}`,
1616
lulumiPDFJSPath: `${path.resolve(lulumiHelperPath, 'pdfjs')}`,
17-
lulumiRev: '4394e37117dec5924585f5fe41a45fa27f9fb156',
17+
lulumiRev: '228257bc22568e8ef8b63c85cadbea512afb53e1',
1818
};

src/main/index.ts

+13-21
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (process.env.NODE_ENV === 'development') {
5151
const storagePath: string = path.join(app.getPath('userData'), 'lulumi-state');
5252
const langPath: string = path.join(app.getPath('userData'), 'lulumi-lang');
5353

54-
let appStateSaveHandler: any = null;
54+
let lulumiStateSaveHandler: any = null;
5555
let setLanguage: boolean = false;
5656

5757
const autoHideMenuBarSetting: boolean = is.macos;
@@ -76,7 +76,7 @@ let windowCount: number = 0;
7676
// ./api/lulumi-extension.ts
7777
const { default: lulumiExtension } = require('./api/lulumi-extension');
7878

79-
function appStateSave(soft: boolean = true): void {
79+
function lulumiStateSave(soft: boolean = true): void {
8080
if (!soft) {
8181
windowCount = Object.keys(windows).length;
8282
Object.keys(windows).forEach((key) => {
@@ -86,11 +86,11 @@ function appStateSave(soft: boolean = true): void {
8686
window.removeAllListeners('close');
8787
});
8888
}
89-
mainStore.saveAppState(soft)
89+
mainStore.saveLulumiState(soft)
9090
.then((state) => {
9191
if (state) {
9292
promisify(writeFile, storagePath, JSON.stringify(state)).then(() => {
93-
if (appStateSaveHandler === null) {
93+
if (lulumiStateSaveHandler === null) {
9494
shuttingDown = true;
9595
app.quit();
9696
}
@@ -184,7 +184,7 @@ function createWindow(options?: Electron.BrowserWindowConstructorOptions, callba
184184

185185
if (process.env.NODE_ENV !== 'testing' || process.env.TEST_ENV !== 'e2e') {
186186
// save app-state every 5 mins
187-
appStateSaveHandler = setInterval(appStateSave, 1000 * 60 * 5);
187+
lulumiStateSaveHandler = setInterval(lulumiStateSave, 1000 * 60 * 5);
188188
}
189189
if (callback) {
190190
(mainWindow as any).callback = callback;
@@ -216,6 +216,7 @@ app.whenReady().then(() => {
216216
session.registerScheme(constants.lulumiPagesCustomProtocol);
217217
session.registerCertificateVerifyProc();
218218
session.registerWebRequestListeners();
219+
219220
// load lulumi-state
220221
let data: string = '""';
221222
try {
@@ -247,9 +248,9 @@ app.whenReady().then(() => {
247248
}
248249
});
249250
(data as any).windows = [];
250-
store.dispatch('setAppState', data);
251+
store.dispatch('setLulumiState', data);
251252
} else {
252-
store.dispatch('setAppState', data);
253+
store.dispatch('setLulumiState', data);
253254
createWindow();
254255
}
255256
} else {
@@ -285,11 +286,11 @@ app.on('before-quit', (event: Electron.Event) => {
285286
}
286287
event.preventDefault();
287288
mainStore.updateWindowStates();
288-
if (appStateSaveHandler !== null) {
289-
clearInterval(appStateSaveHandler);
290-
appStateSaveHandler = null;
289+
if (lulumiStateSaveHandler !== null) {
290+
clearInterval(lulumiStateSaveHandler);
291+
lulumiStateSaveHandler = null;
291292
}
292-
appStateSave(false);
293+
lulumiStateSave(false);
293294
});
294295

295296
// https://github.com/electron/electron/pull/12782
@@ -617,23 +618,14 @@ ipcMain.on('request-lang', (event: Electron.Event) => {
617618

618619
// load extension objects for each BrowserWindow instance
619620
ipcMain.on('request-extension-objects', (event: Electron.Event) => {
620-
// load persisted extensions
621-
lulumiExtension.loadExtensions();
622-
623-
// assign extension objects to global variables
624-
globalObject.backgroundPages = lulumiExtension.backgroundPages;
625-
globalObject.manifestMap = lulumiExtension.manifestMap;
626-
627621
Object.keys(windows).forEach((key) => {
628622
const id = parseInt(key, 10);
629623
const window = windows[id];
630-
window.webContents.send(
631-
'response-extension-objects',
632-
lulumiExtension.manifestMap);
633624
Object.keys(lulumiExtension.manifestMap).forEach((manifest) => {
634625
lulumiExtension.registerLocalCommands(window, lulumiExtension.manifestMap[manifest]);
635626
});
636627
});
628+
event.sender.send('response-extension-objects', lulumiExtension.manifestMap);
637629
});
638630

639631
ipcMain.on('fetch-search-suggestions',

src/main/lib/menu.ts

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,9 @@
1-
import { app, Menu, BrowserWindow, ipcMain } from 'electron';
1+
import { app, Menu, BrowserWindow } from 'electron';
22
import { is } from 'electron-util';
33
import i18n from './i18n';
4-
import * as fs from 'fs';
5-
import * as path from 'path';
64

75
const { openProcessManager } = require('electron-process-manager');
86

9-
function reloadBrowserWindow(browserWindow: Electron.BrowserWindow): void {
10-
const globalObject = global as Lulumi.API.GlobalObject;
11-
const count = Object.keys(globalObject.backgroundPages).length;
12-
let counting = 0;
13-
14-
const objectValues = object => Object.keys(object).map(key => object[key]);
15-
if (count === 0) {
16-
browserWindow.webContents.reloadIgnoringCache();
17-
} else {
18-
const loadedExtensionsPath = path.join(app.getPath('userData'), 'lulumi-extensions');
19-
const loadedExtensions
20-
= objectValues(globalObject.manifestMap).map(manifest => manifest.srcDirectory);
21-
if (loadedExtensions.length > 0) {
22-
try {
23-
fs.mkdirSync(path.dirname(loadedExtensionsPath));
24-
} catch (error) {
25-
// Ignore error
26-
}
27-
fs.writeFileSync(loadedExtensionsPath, JSON.stringify(loadedExtensions));
28-
globalObject.persistentLoaded = false;
29-
ipcMain.on('force-reload-result', (event, success): void => {
30-
if (success) {
31-
counting += 1;
32-
if (counting === count) {
33-
ipcMain.removeAllListeners('force-reload-result');
34-
browserWindow.webContents.reloadIgnoringCache();
35-
}
36-
} else {
37-
ipcMain.removeAllListeners('force-reload-result');
38-
browserWindow.webContents.reloadIgnoringCache();
39-
}
40-
});
41-
Object.keys(globalObject.backgroundPages).forEach((extensionId) => {
42-
browserWindow.webContents.send('remove-lulumi-extension', extensionId);
43-
});
44-
} else {
45-
fs.unlinkSync(loadedExtensionsPath);
46-
}
47-
}
48-
}
49-
507
const getTemplate = () => {
518
const template: Electron.MenuItemConstructorOptions[] = [
529
{
@@ -397,7 +354,7 @@ const getTemplate = () => {
397354
click: () => {
398355
const browserWindow = BrowserWindow.getFocusedWindow();
399356
if (browserWindow !== null) {
400-
reloadBrowserWindow(browserWindow);
357+
browserWindow.webContents.reloadIgnoringCache();
401358
}
402359
},
403360
},

src/renderer/mainBrowserWindow/components/BrowserMainView.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,9 @@ export default class BrowserMainView extends Vue {
15101510
}
15111511
}
15121512
1513+
created() {
1514+
this.extensionService = new ExtensionService(this);
1515+
}
15131516
beforeMount() {
15141517
const ipc = this.$electron.ipcRenderer;
15151518
@@ -1542,8 +1545,6 @@ export default class BrowserMainView extends Vue {
15421545
this.onNewTab(this.windowId, 'https://github.com/LulumiProject/lulumi-browser', true);
15431546
}
15441547
}
1545-
1546-
this.extensionService = new ExtensionService(this);
15471548
}
15481549
mounted() {
15491550
if (is.macos) {

src/renderer/mainBrowserWindow/components/BrowserMainView/Navbar.vue

-2
Original file line numberDiff line numberDiff line change
@@ -864,11 +864,9 @@ export default class Navbar extends Vue {
864864
});
865865
ipc.on('remove-lulumi-extension-result', (event: Electron.IpcMessageEvent, data): void => {
866866
if (data.result === 'OK') {
867-
ipc.send('force-reload-result', true);
868867
(this.$parent as BrowserMainView).extensionService.update();
869868
this.$forceUpdate();
870869
} else {
871-
ipc.send('force-reload-result', false);
872870
alert(data.result);
873871
}
874872
});

src/renderer/preferenceView/components/AboutMainView/Lulumi.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
el-table-column(:label="$t('about.lulumiPage.value')", width="180", align="center")
77
template(slot-scope="scope")
88
a.cell(v-if="scope.row.key === 'rev'", :href="`https://github.com/LulumiProject/lulumi-browser/commit/${scope.row.value}`") {{ scope.row.value.substring(0, 7) }}
9-
.cell(v-else-if="scope.row.key === 'userData'", style="color: cornflowerblue; cursor: pointer;", @click="showItemInFolder(scope.row.value)") {{ scope.row.value }}
9+
.cell(v-else-if="scope.row.key === 'userData'", style="color: cornflowerblue; cursor: pointer;", @click="openItem(scope.row.value)") {{ scope.row.value }}
1010
.cell(v-else) {{ scope.row.value }}
1111
</template>
1212

@@ -28,8 +28,8 @@ export default class Lulumi extends Vue {
2828
return this.$store.getters.about;
2929
}
3030
31-
showItemInFolder(userData: string): void {
32-
ipcRenderer.send('show-item-in-folder', userData);
31+
openItem(userData: string): void {
32+
ipcRenderer.send('open-item', userData);
3333
}
3434
}
3535
</script>

src/shared/store/actions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ export const actions = {
237237
});
238238
},
239239

240-
setAppState({ commit }, newState) {
241-
commit(types.SET_APP_STATE, { newState });
240+
setLulumiState({ commit }, newState) {
241+
commit(types.SET_LULUMI_STATE, { newState });
242242
},
243243

244244
createWindow({ commit }, { windowId, width, height, left, top, windowState, type }) {

src/shared/store/mainStore.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ const isWindows: boolean = is.windows;
2222

2323
const windows: Electron.BrowserWindow[] = [];
2424

25-
let close: boolean = false;
26-
2725
const broadcastMutations = (store) => {
2826
store.subscribe((mutation) => {
2927
Object.keys(windows).forEach((key) => {
@@ -80,7 +78,15 @@ function handleWindowProperty(window: Electron.BrowserWindow, action: string) {
8078

8179
const register = (storagePath: string, swipeGesture: boolean): void => {
8280
ipcMain.on('vuex-connect', (event: Electron.Event) => {
83-
const window = BrowserWindow.fromWebContents(event.sender);
81+
let close: boolean = false;
82+
const window: BrowserWindow = BrowserWindow.fromWebContents(event.sender);
83+
84+
// we've registered this window, so we just return
85+
if (windows[window.id] !== undefined) {
86+
event.returnValue = store.state;
87+
return;
88+
}
89+
8490
window.setMaxListeners(0);
8591

8692
window.on('blur', () => {
@@ -343,7 +349,7 @@ function collect(newStart: number, newTabs: Lulumi.Store.TabObject[], newCurrent
343349
};
344350
}
345351

346-
function saveAppState(soft: boolean = true, bumpWindowIdsBy: number = 0): Promise<any> {
352+
function saveLulumiState(soft: boolean = true, bumpWindowIdsBy: number = 0): Promise<any> {
347353
const newStart = Math.ceil(Math.random() * 10000);
348354
const { tabObjects: newTabs, currentTabIndexes: newCurrentTabIndexes }
349355
= tabsOrdering(newStart, bumpWindowIdsBy);
@@ -373,7 +379,7 @@ function saveAppState(soft: boolean = true, bumpWindowIdsBy: number = 0): Promis
373379
}
374380

375381
function bumpWindowIds(bumpWindowIdsBy: number) {
376-
saveAppState(true, bumpWindowIdsBy).then((state) => {
382+
saveLulumiState(true, bumpWindowIdsBy).then((state) => {
377383
if (state && state.windows.length > 0) {
378384
let tmpWindow: Electron.BrowserWindow;
379385
state.windows.forEach((window) => {
@@ -395,7 +401,7 @@ function bumpWindowIds(bumpWindowIdsBy: number) {
395401
}
396402
});
397403
state.windows = [];
398-
store.dispatch('setAppState', state);
404+
store.dispatch('setLulumiState', state);
399405
} else {
400406
(BrowserWindow as any).createWindow();
401407
}
@@ -418,7 +424,7 @@ export default {
418424
getStore: () => store,
419425
register,
420426
updateWindowStates,
421-
saveAppState,
427+
saveLulumiState,
422428
bumpWindowIds,
423429
getWindows: () => windows,
424430
};

0 commit comments

Comments
 (0)