joplin/ReactNativeClient/lib/services/DecryptionWorker.js

124 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-03-09 17:49:35 +00:00
const BaseItem = require("lib/models/BaseItem");
const { Logger } = require("lib/logger.js");
class DecryptionWorker {
constructor() {
2018-03-09 17:49:35 +00:00
this.state_ = "idle";
2017-12-31 14:23:05 +00:00
this.logger_ = new Logger();
2018-03-09 17:49:35 +00:00
this.dispatch = action => {
//console.warn('DecryptionWorker.dispatch is not defined');
};
this.scheduleId_ = null;
}
setLogger(l) {
this.logger_ = l;
}
logger() {
return this.logger_;
}
static instance() {
if (this.instance_) return this.instance_;
this.instance_ = new DecryptionWorker();
return this.instance_;
}
setEncryptionService(v) {
this.encryptionService_ = v;
}
encryptionService() {
2018-03-09 17:49:35 +00:00
if (!this.encryptionService_) throw new Error("DecryptionWorker.encryptionService_ is not set!!");
return this.encryptionService_;
}
async scheduleStart() {
if (this.scheduleId_) return;
this.scheduleId_ = setTimeout(() => {
this.scheduleId_ = null;
2017-12-26 10:38:53 +00:00
this.start({
2018-03-09 17:49:35 +00:00
materKeyNotLoadedHandler: "dispatch",
2017-12-26 10:38:53 +00:00
});
}, 1000);
}
2017-12-26 10:38:53 +00:00
async start(options = null) {
if (options === null) options = {};
2018-03-09 17:49:35 +00:00
if (!("materKeyNotLoadedHandler" in options)) options.materKeyNotLoadedHandler = "throw";
2017-12-26 10:38:53 +00:00
2018-03-09 17:49:35 +00:00
if (this.state_ !== "idle") {
2017-12-26 10:38:53 +00:00
this.logger().info('DecryptionWorker: cannot start because state is "' + this.state_ + '"');
return;
}
2018-03-09 17:49:35 +00:00
this.logger().info("DecryptionWorker: starting decryption...");
2018-03-09 17:49:35 +00:00
this.state_ = "started";
let excludedIds = [];
try {
const notLoadedMasterKeyDisptaches = [];
while (true) {
const result = await BaseItem.itemsThatNeedDecryption(excludedIds);
const items = result.items;
for (let i = 0; i < items.length; i++) {
const item = items[i];
// Temp hack
// if (['edf44b7a0e4f8cbf248e206cd8dfa800', '2ccb3c9af0b1adac2ec6b66a5961fbb1'].indexOf(item.id) >= 0) {
// excludedIds.push(item.id);
// continue;
// }
const ItemClass = BaseItem.itemClass(item);
2018-03-09 17:49:35 +00:00
// Don't log in production as it results in many messages when importing many items
// this.logger().info('DecryptionWorker: decrypting: ' + item.id + ' (' + ItemClass.tableName() + ')');
try {
await ItemClass.decrypt(item);
} catch (error) {
excludedIds.push(item.id);
2018-03-09 17:49:35 +00:00
if (error.code === "masterKeyNotLoaded" && options.materKeyNotLoadedHandler === "dispatch") {
if (notLoadedMasterKeyDisptaches.indexOf(error.masterKeyId) < 0) {
this.dispatch({
2018-03-09 17:49:35 +00:00
type: "MASTERKEY_ADD_NOT_LOADED",
id: error.masterKeyId,
});
notLoadedMasterKeyDisptaches.push(error.masterKeyId);
}
continue;
}
2018-03-09 17:49:35 +00:00
if (error.code === "masterKeyNotLoaded" && options.materKeyNotLoadedHandler === "throw") {
throw error;
}
2018-03-09 17:49:35 +00:00
this.logger().warn("DecryptionWorker: error for: " + item.id + " (" + ItemClass.tableName() + ")", error);
}
}
if (!result.hasMore) break;
}
} catch (error) {
2018-03-09 17:49:35 +00:00
this.logger().error("DecryptionWorker:", error);
this.state_ = "idle";
2017-12-26 10:38:53 +00:00
throw error;
}
2018-03-09 17:49:35 +00:00
this.logger().info("DecryptionWorker: completed decryption.");
2017-12-26 10:38:53 +00:00
2018-03-09 17:49:35 +00:00
this.state_ = "idle";
}
}
2018-03-09 17:49:35 +00:00
module.exports = DecryptionWorker;