From 06130165810656ed756def7e6531f93e46a174ca Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 24 Sep 2014 12:46:19 +0200 Subject: [PATCH] Remove chrome.filesystem and use only standard apis --- src/js/controller/account.js | 18 ++------------- src/js/controller/read.js | 8 +++---- src/js/util/download.js | 25 ++------------------ src/manifest.json | 4 +--- test/unit/account-ctrl-test.js | 42 +++++++--------------------------- 5 files changed, 17 insertions(+), 80 deletions(-) diff --git a/src/js/controller/account.js b/src/js/controller/account.js index da29625..7c409dd 100644 --- a/src/js/controller/account.js +++ b/src/js/controller/account.js @@ -49,26 +49,12 @@ define(function(require) { var file = 'whiteout_mail_' + userId + '_' + keyId.substring(8, keyId.length); dl.createDownload({ - content: keys.publicKey.publicKey + keys.privateKey.encryptedKey, + content: keys.publicKey.publicKey + '\r\n' + keys.privateKey.encryptedKey, filename: file + '.asc', contentType: 'text/plain' - }, onExport); + }); }); }; - - function onExport(err) { - if (err) { - $scope.onError(err); - return; - } - - $scope.state.account.toggle(false); - $scope.$apply(); - $scope.onError({ - title: 'Success', - message: 'Exported keypair to file.' - }); - } }; return AccountCtrl; diff --git a/src/js/controller/read.js b/src/js/controller/read.js index fae2c06..dc3ea9c 100644 --- a/src/js/controller/read.js +++ b/src/js/controller/read.js @@ -67,7 +67,7 @@ define(function(require) { function checkPublicKey(user) { user.secure = undefined; - if(!keychain) { + if (!keychain) { return; } @@ -94,7 +94,7 @@ define(function(require) { content: attachment.content, filename: attachment.filename, contentType: attachment.mimeType - }, $scope.onError); + }); return; } @@ -200,7 +200,7 @@ define(function(require) { var iframe = elm[0]; scope.$watch('state.read.open', function(open) { - if(open) { + if (open) { // trigger rendering of iframe // otherwise scale to fit would not compute correct dimensions on mobile displayText(scope.state.mailList.selected ? scope.state.mailList.selected.body : undefined); @@ -267,7 +267,7 @@ define(function(require) { var w = elm.width(); var scale = ''; - if(w > parentWidth) { + if (w > parentWidth) { scale = parentWidth / w; scale = 'scale(' + scale + ',' + scale + ')'; } diff --git a/src/js/util/download.js b/src/js/util/download.js index 61c06ff..2ca1846 100644 --- a/src/js/util/download.js +++ b/src/js/util/download.js @@ -5,7 +5,7 @@ define(function(require) { var dl = {}; - dl.createDownload = function(options, callback) { + dl.createDownload = function(options) { var contentType = options.contentType || 'application/octet-stream'; var filename = options.filename || 'file'; var content = options.content; @@ -16,28 +16,7 @@ define(function(require) { supportsBlob = !!new Blob(); } catch (e) {} - if (window.chrome && window.chrome.fileSystem) { - // chrome app - chrome.fileSystem.chooseEntry({ - type: 'saveFile', - suggestedName: filename - }, function(file) { - if (!file) { - callback(); - return; - } - file.createWriter(function(writer) { - writer.onerror = callback; - writer.onwriteend = function() { - callback(); - }; - writer.write(new Blob([content], { - type: contentType - })); - }, callback); - }); - return; - } else if (typeof a.download !== "undefined" && supportsBlob) { + if (typeof a.download !== "undefined" && supportsBlob) { // ff 30+, chrome 27+ (android: 37+) document.body.appendChild(a); a.style = "display: none"; diff --git a/src/manifest.json b/src/manifest.json index aec0803..2c377c7 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -11,9 +11,7 @@ "196": "img/icon-196.png" }, "permissions": [ - "unlimitedStorage", { - "fileSystem": ["write"] - }, + "unlimitedStorage", "notifications", "https://keys-test.whiteout.io/", "https://keychain-test.whiteout.io/", diff --git a/test/unit/account-ctrl-test.js b/test/unit/account-ctrl-test.js index f5c58e0..1902d0b 100644 --- a/test/unit/account-ctrl-test.js +++ b/test/unit/account-ctrl-test.js @@ -63,7 +63,7 @@ define(function(require) { }); }); describe('export to key file', function() { - it('should work', function(done) { + it('should work', function() { var createDownloadMock = sinon.stub(dl, 'createDownload'); keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, { publicKey: { @@ -75,18 +75,15 @@ define(function(require) { } }); createDownloadMock.withArgs(sinon.match(function(arg) { - return arg.content === 'ab' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain'; - })).yields(); - scope.onError = function(err) { - expect(err.title).to.equal('Success'); - expect(scope.state.lightbox).to.equal(undefined); - expect(keychainMock.getUserKeyPair.calledOnce).to.be.true; - expect(dl.createDownload.calledOnce).to.be.true; - dl.createDownload.restore(); - done(); - }; + return arg.content === 'a\r\nb' && arg.filename === 'whiteout_mail_' + emailAddress + '_' + expectedKeyId + '.asc' && arg.contentType === 'text/plain'; + })).returns(); scope.exportKeyFile(); + + expect(scope.state.lightbox).to.equal(undefined); + expect(keychainMock.getUserKeyPair.calledOnce).to.be.true; + expect(dl.createDownload.calledOnce).to.be.true; + dl.createDownload.restore(); }); it('should not work when key export failed', function(done) { @@ -99,29 +96,6 @@ define(function(require) { scope.exportKeyFile(); }); - - it('should not work when create download failed', function(done) { - var createDownloadMock = sinon.stub(dl, 'createDownload'); - keychainMock.getUserKeyPair.withArgs(emailAddress).yields(null, { - publicKey: { - _id: dummyKeyId, - publicKey: 'a' - }, - privateKey: { - encryptedKey: 'b' - } - }); - createDownloadMock.withArgs().yields(new Error('asdasd')); - scope.onError = function(err) { - expect(err.message).to.equal('asdasd'); - expect(keychainMock.getUserKeyPair.calledOnce).to.be.true; - expect(dl.createDownload.calledOnce).to.be.true; - dl.createDownload.restore(); - done(); - }; - - scope.exportKeyFile(); - }); }); }); }); \ No newline at end of file