-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpassphrase-manager.js
65 lines (59 loc) · 2.02 KB
/
passphrase-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
// Copyright 2015 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.
/**
* @constructor
* @param {?string} initPassphrase Initial passphrase for the first passphrase
* request or NULL if not available. In such case, call to getPassphrase()
* will invoke a dialog.
*/
unpacker.PassphraseManager = function(initPassphrase) {
/**
* @private {?string}
*/
this.initPassphrase_ = initPassphrase;
/**
* @public {?string}
*/
this.rememberedPassphrase = initPassphrase;
};
/**
* Requests a passphrase from the user. If a passphrase was previously
* remembered, then tries it first. Otherwise shows a passphrase dialog.
* @return {!Promise.<string>}
*/
unpacker.PassphraseManager.prototype.getPassphrase = function() {
return new Promise(function(fulfill, reject) {
// For the first passphrase request try the init passphrase (which may be
// incorrect though, so do it only once).
if (this.initPassphrase_) {
fulfill(this.initPassphrase_);
this.initPassphrase_ = null;
return;
}
// Ask user for a passphrase.
chrome.app.window.create(
'../html/passphrase.html',
/** @type {!chrome.app.window.CreateWindowOptions} */ ({
innerBounds: {width: 320, height: 160},
alwaysOnTop: true,
resizable: false,
frame: 'none',
hidden: true
}),
function(passphraseWindow) {
var passphraseSucceeded = false;
passphraseWindow.onClosed.addListener(function() {
if (passphraseSucceeded)
return;
reject('FAILED');
}.bind(this));
passphraseWindow.contentWindow.onPassphraseSuccess =
function(passphrase, remember) {
passphraseSucceeded = true;
this.rememberedPassphrase = remember ? passphrase : null;
fulfill(passphrase);
}.bind(this);
}.bind(this));
}.bind(this));
}