-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_app.js
304 lines (275 loc) · 9.43 KB
/
nassh_app.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 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';
/**
* The singleton app instance for the nassh packaged app, created by the
* background page.
*
* @constructor
*/
nassh.App = function() {
this.prefs_ = new nassh.PreferenceManager();
this.localPrefs_ = new nassh.LocalPreferenceManager();
this.omniMatches_ = [];
this.omniDefault_ = null;
this.prefs_.readStorage();
this.localPrefs_.readStorage();
};
/**
* Set up the file system provider APIs.
*/
nassh.App.prototype.installFsp = function() {
nassh.sftp.fsp.addListeners();
};
/**
* Set up context menus.
*
* NB: We omit "Options" because Chrome takes care of populating that entry.
*/
nassh.App.prototype.installContextMenus = function() {
// Remove any previous entries. This comes up when reloading the page.
chrome.contextMenus.removeAll();
chrome.contextMenus.onClicked.addListener(this.onContextMenu_.bind(this));
/** @type {!Array<!chrome.contextMenus.CreateProperties>} */
const entries = [
{
'type': 'normal',
'id': 'connect-dialog',
'title': nassh.msg('CONNECTION_DIALOG_NAME'),
'contexts': ['browser_action'],
},
{
'type': 'normal',
'id': 'mosh',
'title': '⸘m🍪sh‽',
'contexts': ['browser_action'],
},
{
'type': 'normal',
'id': 'feedback',
'title': nassh.msg('SEND_FEEDBACK_LABEL'),
'contexts': ['browser_action'],
},
];
entries.forEach((entry) => chrome.contextMenus.create(entry));
};
/**
* Callback from context menu clicks.
*
* @param {!Object} info The item clicked.
* @param {!Tab=} tab When relevant, the active tab.
*/
nassh.App.prototype.onContextMenu_ = function(info, tab = undefined) {
switch (info.menuItemId) {
case 'connect-dialog':
lib.f.openWindow(chrome.runtime.getURL('/html/nassh.html'), '',
'chrome=no,close=yes,resize=yes,minimizable=yes,' +
'scrollbars=yes,width=900,height=600');
break;
case 'mosh':
lib.f.openWindow(
chrome.runtime.getURL('/plugin/mosh/mosh_client.html'), '',
'chrome=no,close=yes,resize=yes,minimizable=yes,' +
'scrollbars=yes,width=900,height=600');
break;
case 'feedback':
nassh.sendFeedback();
break;
default:
console.error('Unknown menu item', info);
break;
}
};
/**
* Set the default help text in the omnibox when completing an ssh connection.
*/
nassh.App.prototype.setDefaultOmnibox_ = function() {
this.omnibox_.setDefaultSuggestion({
description: nassh.msg('OMNIBOX_DEFAULT'),
});
};
/**
* Callback when user first interacts with the ssh shortcut.
*/
nassh.App.prototype.omniboxOnInputStarted_ = function() {
this.omniMatches_.length = 0;
this.omniDefault_ = null;
// Convert a nassh profile into an object we can easily match later on.
const profileIdToOmni = (id) => {
const profile = this.prefs_.getProfile(id);
let port = profile.get('port') || '';
if (port) {
port = ':' + port;
}
return {
uhp: profile.get('username') + '@' + profile.get('hostname') + port,
desc: profile.get('description'),
id: id,
};
};
// Read our saved settings and construct the partial matches from all of
// our active profiles.
const ids = this.prefs_.get('profile-ids');
for (let i = 0; i < ids.length; ++i) {
this.omniMatches_.push(profileIdToOmni(ids[i]));
}
this.omniDefault_ = profileIdToOmni(
this.localPrefs_.get('connectDialog/lastProfileId'));
};
/**
* Callback when the user changes the input at all.
*
* @param {string} text Current input in the omnibox.
* @param {function(!Array<{content: string, description: string}>)} suggest
* Function for us to call to notify of our matches against the text.
*/
nassh.App.prototype.omniboxOnInputChanged_ = function(text, suggest) {
const resultsUhp = [];
const resultsDescLeading = [];
const resultsDescSubstr = [];
this.omniMatches_.forEach((match) => {
if (match.uhp.startsWith(text)) {
resultsUhp.push({
content: 'profile-id:' + match.id,
description: lib.f.replaceVars(
'<match>%escapeHTML(uhp)</match>: %escapeHTML(desc)', match),
});
}
if (match.desc.startsWith(text)) {
resultsDescLeading.push({
content: 'profile-id:' + match.id,
description: lib.f.replaceVars(
'%escapeHTML(uhp): <match>%escapeHTML(desc)</match>', match),
});
}
if (match.desc.includes(text)) {
resultsDescSubstr.push({
content: 'profile-id:' + match.id,
description: lib.f.replaceVars(
'%escapeHTML(uhp): <match>%escapeHTML(desc)</match>', match),
});
}
});
// Now merge the suggestions together in order.
const results = resultsUhp.concat(resultsDescLeading, resultsDescSubstr);
if (results.length == 0 || text.trim().length == 0) {
// If they're just starting input, or if we have no matches, then show the
// last connection used first.
if (this.omniDefault_) {
results.unshift({
content: 'profile-id:' + this.omniDefault_.id,
description: lib.f.replaceVars('%escapeHTML(uhp): %escapeHTML(desc)',
this.omniDefault_),
});
}
}
if (results.length) {
suggest(results);
}
};
/**
* Callback when the user has finish entering text.
*
* See the omnibox source for what key strokes generate which dispositions.
* https://chromium.googlesource.com/chromium/src/+/69.0.3487.1/chrome/browser/ui/views/omnibox/omnibox_view_views.cc#1188
*
* @param {string} text The text to operate on.
* @param {string} disposition Mode the user wants us to open as.
*/
nassh.App.prototype.omniboxOnInputEntered_ = function(text, disposition) {
// If the user types out the profile name exactly, connect to it. It might
// overlap with a valid URI, but if that's a problem, they can change the
// description to something else.
for (let i = 0; i < this.omniMatches_.length; ++i) {
const match = this.omniMatches_[i];
if (match.desc == text) {
text = 'profile-id:' + match.id;
break;
}
}
const url = chrome.runtime.getURL('/html/nassh.html#' + text);
switch (disposition) {
default:
console.warn('unknown disposition: ' + disposition);
case 'currentTab':
// Fired when pressing Enter.
// Ideally we'd just call chrome.tabs.update, but that won't focus the
// new ssh session. We close the current tab and then open a new one
// right away -- Chrome will focus the content for us.
//
// TODO(crbug.com/1075427#c6): Incognito mode is a bit buggy, so we have
// to use getCurrent+windowId instead of currentWindow directly for now.
chrome.windows.getCurrent((win) => {
chrome.tabs.query({active: true, windowId: win.id}, (tabs) => {
chrome.tabs.remove(tabs[0].id);
});
chrome.tabs.create({windowId: win.id, url: url, active: true});
});
break;
case 'newBackgroundTab':
// Fired when pressing Meta+Enter/Command+Enter.
chrome.tabs.create({url: url, active: false});
break;
case 'newForegroundTab':
// Fired when pressing Alt+Enter.
// Close the active tab. We need to do this before opening a new window
// in case Chrome selects that as the new active tab. It won't kill us
// right away though as the JS execution model guarantees we'll finish
// running this func before the callback runs.
//
// TODO(crbug.com/1075427#c6): Incognito mode is a bit buggy, so we have
// to use getCurrent+windowId instead of currentWindow directly for now.
chrome.windows.getCurrent((win) => {
chrome.tabs.query({active: true, windowId: win.id}, (tabs) => {
chrome.tabs.remove(tabs[0].id);
});
});
// We'll abuse this to open a window instead of a tab.
lib.f.openWindow(url, '',
'chrome=no,close=yes,resize=yes,minimizable=yes,' +
'scrollbars=yes,width=900,height=600');
break;
}
};
/**
* Callback when the user has aborted input.
*/
nassh.App.prototype.omniboxOnInputCancelled_ = function() {
// this.setDefaultOmnibox_(); needed?
this.omniMatches_.length = 0;
this.omniDefault_ = null;
};
/**
* Bind our callbacks to the omnibox.
*
* @param {!typeof chrome.omnibox} omnibox The omnibox instance to bind to.
*/
nassh.App.prototype.installOmnibox = function(omnibox) {
this.omnibox_ = omnibox;
this.setDefaultOmnibox_();
omnibox.onInputStarted.addListener(this.omniboxOnInputStarted_.bind(this));
omnibox.onInputChanged.addListener(this.omniboxOnInputChanged_.bind(this));
omnibox.onInputEntered.addListener(this.omniboxOnInputEntered_.bind(this));
omnibox.onInputCancelled.addListener(
this.omniboxOnInputCancelled_.bind(this));
};
/**
* Bind our callbacks to the browser action button (for extensions).
*/
nassh.App.prototype.installBrowserAction = function() {
if (!nassh.browserAction) {
return;
}
nassh.browserAction.onClicked.addListener(this.onLaunched.bind(this));
};
/**
* Called on app launch.
*/
nassh.App.prototype.onLaunched = function() {
const width = 900;
const height = 600;
lib.f.openWindow(lib.f.getURL('/html/nassh.html'), '',
'chrome=no,close=yes,resize=yes,scrollbars=yes,' +
`minimizable=yes,width=${width},height=${height}`);
};