-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_preference_manager.js
248 lines (212 loc) · 6.31 KB
/
nassh_preference_manager.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// 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';
/**
* PreferenceManager subclass managing global NaSSH preferences.
*
* This is currently just an ordered list of known connection profiles.
*
* @param {!lib.Storage=} storage
* @constructor
* @extends {lib.PreferenceManager}
*/
nassh.PreferenceManager = function(storage = undefined) {
if (!storage) {
storage = new lib.Storage.Chrome(chrome.storage.sync);
}
lib.PreferenceManager.call(this, storage, '/nassh/');
this.defineChildren('profile-ids', function(parent, id) {
return new nassh.ProfilePreferenceManager(parent, id);
});
this.definePreferences([
/**
* The last version we showed release notes for.
*/
['welcome/notes-version', ''],
/**
* How many times we've shown the current release notes.
*/
['welcome/show-count', 0],
]);
};
nassh.PreferenceManager.prototype =
Object.create(lib.PreferenceManager.prototype);
/** @override */
nassh.PreferenceManager.constructor = nassh.PreferenceManager;
/** @return {!nassh.PreferenceManager} */
nassh.PreferenceManager.prototype.createProfile = function() {
return /** @type {!nassh.PreferenceManager} */ (
this.createChild('profile-ids'));
};
/** @param {string} id */
nassh.PreferenceManager.prototype.removeProfile = function(id) {
this.removeChild('profile-ids', id);
};
/**
* @param {string} id
* @return {!nassh.PreferenceManager}
*/
nassh.PreferenceManager.prototype.getProfile = function(id) {
return /** @type {!nassh.PreferenceManager} */ (
this.getChild('profile-ids', id));
};
/**
* lib.PreferenceManager subclass managing per-connection preferences.
*
* @param {!lib.PreferenceManager} parent
* @param {string} id
* @constructor
* @extends {lib.PreferenceManager}
*/
nassh.ProfilePreferenceManager = function(parent, id) {
lib.PreferenceManager.call(this, parent.storage,
'/nassh/profiles/' + id);
this.id = id;
this.definePreferences([
/**
* The free-form description of this connection profile.
*/
['description', ''],
/**
* The username.
*/
['username', ''],
/**
* The hostname or IP address.
*/
['hostname', ''],
/**
* The port, or null to use the default port.
*/
['port', null],
/**
* Options string for nassh itself (e.g. relay settings).
*/
['nassh-options', ''],
/**
* The private key file to use as the identity for this extension.
*
* Must be relative to the /.ssh/ directory.
*/
['identity', ''],
/**
* The argument string to pass to the ssh executable.
*
* Use '--' to separate ssh arguments from the target command/arguments.
*/
['argstr', ''],
/**
* The terminal profile to use for this connection.
*/
['terminal-profile', ''],
/**
* The base path used when mounting via SFTP.
*/
['mount-path', ''],
]);
};
nassh.ProfilePreferenceManager.prototype =
Object.create(lib.PreferenceManager.prototype);
/** @override */
nassh.ProfilePreferenceManager.constructor = nassh.ProfilePreferenceManager;
/**
* Settings that are not synced between systems.
*
* Most code should use nassh.PreferenceManager instead which syncs user
* settings between systems.
*
* @param {!lib.Storage=} storage
* @constructor
* @extends {lib.PreferenceManager}
*/
nassh.LocalPreferenceManager = function(storage = undefined) {
if (!storage) {
storage = new lib.Storage.Local();
}
lib.PreferenceManager.call(this, storage, '/nassh/');
this.definePreferences([
/* The last profile the user selected. */
['connectDialog/lastProfileId', ''],
/* How many times we've shown the migration message. */
['migrate/showCount', 0],
]);
this.defineChildren('profile-ids', function(parent, id) {
return new nassh.ProfileLocalPreferenceManager(parent, id);
});
};
nassh.LocalPreferenceManager.prototype =
Object.create(lib.PreferenceManager.prototype);
/** @override */
nassh.LocalPreferenceManager.constructor = nassh.LocalPreferenceManager;
/**
* Sync remote & local profiles to simplify code.
*
* @param {!nassh.PreferenceManager} remotePrefs The remote set of profiles to
* sync against.
*/
nassh.LocalPreferenceManager.prototype.syncProfiles = function(remotePrefs) {
const localIds = new Set(
/** @type {!Array<string>} */ (this.get('profile-ids')));
const remoteIds = new Set(
/** @type {!Array<string>} */ (remotePrefs.get('profile-ids')));
// Delete any local prefs that no longer exist.
localIds.forEach((id) => {
if (!remoteIds.has(id)) {
this.removeProfile(id);
}
});
// Initialize local perfs that have shown up from the remote.
remoteIds.forEach((id) => {
if (!localIds.has(id)) {
this.createProfile(id);
}
});
};
/**
* Create a new profile child.
*
* @param {string=} id The specific profile id to use.
* @return {!nassh.LocalPreferenceManager}
*/
nassh.LocalPreferenceManager.prototype.createProfile = function(
id = undefined) {
return /** @type {!nassh.LocalPreferenceManager} */ (
this.createChild('profile-ids', undefined, id));
};
/** @param {string} id */
nassh.LocalPreferenceManager.prototype.removeProfile = function(id) {
this.removeChild('profile-ids', id);
};
/**
* @param {string} id
* @return {!nassh.LocalPreferenceManager}
*/
nassh.LocalPreferenceManager.prototype.getProfile = function(id) {
return /** @type {!nassh.LocalPreferenceManager} */ (
this.getChild('profile-ids', id));
};
/**
* lib.PreferenceManager subclass managing per-connection preferences.
*
* @param {!lib.PreferenceManager} parent
* @param {string} id
* @constructor
* @extends {lib.PreferenceManager}
*/
nassh.ProfileLocalPreferenceManager = function(parent, id) {
lib.PreferenceManager.call(this, parent.storage, `/nassh/profiles/${id}`);
this.id = id;
this.definePreferences([
['win/top', '0'],
['win/left', '0'],
['win/height', '600'],
['win/width', '900'],
['win/state', 'normal'],
]);
};
nassh.ProfileLocalPreferenceManager.prototype =
Object.create(lib.PreferenceManager.prototype);
/** @override */
nassh.ProfileLocalPreferenceManager.constructor =
nassh.ProfileLocalPreferenceManager;