-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_background.js
118 lines (101 loc) · 3.51 KB
/
nassh_background.js
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
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
(function() {
let didLaunch = false;
const onLaunched = () => { didLaunch = true; };
// We have to turn on listeners here so we can handle messages when first
// launched (but before lib.registerInit finishes).
nassh.External.addListeners();
// Used to watch for launch events that occur before we're ready to handle
// them. We'll clean this up below during init.
if (nassh.browserAction) {
nassh.browserAction.onClicked.addListener(onLaunched);
}
/**
* 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.
*/
lib.init(console.log.bind(console)).then(() => {
const app = new nassh.App();
// If omnibox is enabled, set it up.
if (window.chrome && chrome.omnibox) {
app.installOmnibox(chrome.omnibox);
}
// Bind the FSP APIs.
app.installFsp();
// Register our context menus.
app.installContextMenus();
// If we're running as an extension, finish setup.
if (nassh.browserAction) {
nassh.browserAction.onClicked.removeListener(onLaunched);
app.installBrowserAction();
}
// If the user tried to run us while we were initializing, run it now.
if (didLaunch) {
app.onLaunched();
}
// Help with live debugging.
window.app_ = app;
});
})();
/**
* Sync prefs between versions automatically.
*
* This helps when installing the dev version the first time, or migrating from
* the Chrome App variant to the standard extension.
*/
chrome.runtime.onInstalled.addListener((details) => {
console.log(`onInstalled fired due to "${details.reason}"`);
// Only sync prefs when installed the first time.
if (details.reason != 'install') {
return;
}
// 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.
const commonPref = '/nassh/profile-ids';
chrome.storage.sync.get([commonPref], (items) => {
// Prefs exist, so exit early.
if (commonPref in items) {
return;
}
const appStableId = 'pnhechapfaindjhompbnflcldabbghjo';
const appDevId = 'okddffdblfhhnmhodogpojmfkjmhinfp';
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}`);
nassh.runtimeSendMessage(srcId, {command: 'prefsExport'})
.then((response) => {
const {prefs} = response;
nassh.importPreferences(prefs);
})
.catch(onError);
};
switch (chrome.runtime.id) {
case appDevId:
case extStableId:
// Sync from stable app.
migrate(appStableId);
break;
case extDevId:
// Sync from stable ext then stable app then dev app.
migrate(extStableId, () => {
migrate(appStableId, () => {
migrate(appDevId);
});
});
break;
}
});
});