-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathnassh_background_main.js
More file actions
115 lines (96 loc) · 3.24 KB
/
nassh_background_main.js
File metadata and controls
115 lines (96 loc) · 3.24 KB
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
// Copyright 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview The background page that loads & runs everything. Try to keep
* code in here minimal as this cannot be unittested.
*/
import {hterm} from '../../hterm/index.js';
import {getSyncStorage, runtimeSendMessage} from './nassh.js';
import {importPreferences} from './nassh_background.js';
import {ContextMenusHandler} from './nassh_context_menus.js';
import {ExternalApi} from './nassh_external_api.js';
import {probeExtensions} from './nassh_google.js';
import {OmniboxHandler} from './nassh_omnibox.js';
import {SftpFsp} from './nassh_sftp_fsp.js';
let omniboxHandler = null;
if (globalThis.chrome?.omnibox) {
omniboxHandler = new OmniboxHandler({
omnibox: chrome.omnibox,
storage: getSyncStorage(),
});
// Set up basic listeners so we don't miss events before we're ready.
omniboxHandler.earlyInstall();
}
// We have to turn on listeners here so we can handle messages when first
// launched.
const externalApi_ = new ExternalApi();
externalApi_.addListeners();
/**
* Perform any required async initialization, then create our app instance.
*
* The window.app_ property will contain the new app instance so it can be
* reached from the background page's JS console.
*/
function init() {
const fsp = new SftpFsp();
externalApi_.init(fsp);
// Register our context menus.
const contextMenusHandler = new ContextMenusHandler({
contextMenus: chrome.contextMenus,
});
contextMenusHandler.install();
// If omnibox is enabled, set it up.
if (omniboxHandler) {
omniboxHandler.install();
}
// Probe Google extensions.
probeExtensions();
// Bind the FSP APIs.
fsp.addListeners();
}
/**
* Sync prefs between versions automatically.
*
* This helps when installing the dev version the first time.
*/
chrome.runtime.onInstalled.addListener(async (details) => {
console.log(`onInstalled fired due to "${details.reason}"`);
// Only sync prefs when installed the first time.
if (details.reason != 'install') {
return;
}
const storage = getSyncStorage();
// We'll get called when logging into a new device for the first time when we
// get installed automatically as part of the overall sync. We'll have prefs
// in that case already, so no need to sync.
if (await storage.getItem('/nassh/profile-ids') !== undefined) {
// Prefs exist, so exit early.
return;
}
const extStableId = 'iodihamcpbpeioajjeobimgagajmlibd';
const extDevId = 'algkcnfjnajfhgimadimbjhmpaeohhln';
/**
* Try to import prefs from another install into our own.
*
* @param {string} srcId The extension to import from.
* @param {function()=} onError Callback if extension doesn't exist.
*/
const migrate = (srcId, onError = () => {}) => {
console.log(`Trying to sync prefs from ${srcId}`);
runtimeSendMessage(srcId, {command: 'prefsExport'})
.then((response) => {
const {prefs} = response;
return importPreferences(prefs);
})
.catch(onError);
};
switch (chrome.runtime.id) {
case extDevId:
// Sync from stable ext.
migrate(extStableId);
break;
}
});
// Initialize the page!
hterm.initPromise.then(init);