1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/services/DecryptionWorker.js

169 lines
4.8 KiB
JavaScript
Raw Normal View History

const BaseItem = require('lib/models/BaseItem');
2018-10-08 20:11:53 +02:00
const Resource = require('lib/models/Resource');
const ResourceService = require('lib/services/ResourceService');
const { Logger } = require('lib/logger.js');
const EventEmitter = require('events');
class DecryptionWorker {
constructor() {
this.state_ = 'idle';
2017-12-31 16:23:05 +02:00
this.logger_ = new Logger();
this.dispatch = (action) => {
//console.warn('DecryptionWorker.dispatch is not defined');
};
this.scheduleId_ = null;
this.eventEmitter_ = new EventEmitter();
}
setLogger(l) {
this.logger_ = l;
}
logger() {
return this.logger_;
}
on(eventName, callback) {
return this.eventEmitter_.on(eventName, callback);
}
off(eventName, callback) {
return this.eventEmitter_.removeListener(eventName, callback);
}
static instance() {
if (this.instance_) return this.instance_;
this.instance_ = new DecryptionWorker();
return this.instance_;
}
setEncryptionService(v) {
this.encryptionService_ = v;
}
encryptionService() {
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 12:38:53 +02:00
this.start({
2018-10-08 20:11:53 +02:00
masterKeyNotLoadedHandler: 'dispatch',
2017-12-26 12:38:53 +02:00
});
}, 1000);
}
dispatchReport(report) {
const action = Object.assign({}, report);
action.type = 'DECRYPTION_WORKER_SET';
this.dispatch(action);
}
2017-12-26 12:38:53 +02:00
async start(options = null) {
if (options === null) options = {};
2018-10-08 20:11:53 +02:00
if (!('masterKeyNotLoadedHandler' in options)) options.masterKeyNotLoadedHandler = 'throw';
2017-12-26 12:38:53 +02:00
if (this.state_ !== 'idle') {
2017-12-26 12:38:53 +02:00
this.logger().info('DecryptionWorker: cannot start because state is "' + this.state_ + '"');
return;
}
this.logger().info('DecryptionWorker: starting decryption...');
this.state_ = 'started';
let excludedIds = [];
this.dispatchReport({ state: 'started' });
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];
const ItemClass = BaseItem.itemClass(item);
this.dispatchReport({
itemIndex: i,
itemCount: items.length,
});
// 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 {
const decryptedItem = await ItemClass.decrypt(item);
if (decryptedItem.type_ === Resource.modelType() && !!decryptedItem.encryption_blob_encrypted) {
// itemsThatNeedDecryption() will return the resource again if the blob has not been decrypted,
// but that will result in an infinite loop if the blob simply has not been downloaded yet.
// So skip the ID for now, and the service will try to decrypt the blob again the next time.
excludedIds.push(decryptedItem.id);
}
if (decryptedItem.type_ === Resource.modelType() && !decryptedItem.encryption_blob_encrypted) {
this.eventEmitter_.emit('resourceDecrypted', { id: decryptedItem.id });
}
} catch (error) {
excludedIds.push(item.id);
2018-10-08 20:11:53 +02:00
if (error.code === 'masterKeyNotLoaded' && options.masterKeyNotLoadedHandler === 'dispatch') {
if (notLoadedMasterKeyDisptaches.indexOf(error.masterKeyId) < 0) {
this.dispatch({
type: 'MASTERKEY_ADD_NOT_LOADED',
id: error.masterKeyId,
});
notLoadedMasterKeyDisptaches.push(error.masterKeyId);
}
continue;
}
2018-10-08 20:11:53 +02:00
if (error.code === 'masterKeyNotLoaded' && options.masterKeyNotLoadedHandler === 'throw') {
throw error;
}
2018-10-08 20:11:53 +02:00
this.logger().warn('DecryptionWorker: error for: ' + item.id + ' (' + ItemClass.tableName() + ')', error, item);
}
}
if (!result.hasMore) break;
}
} catch (error) {
this.logger().error('DecryptionWorker:', error);
this.state_ = 'idle';
this.dispatchReport({ state: 'idle' });
2017-12-26 12:38:53 +02:00
throw error;
}
// 2019-05-12: Temporary to set the file size of the resources
// that weren't set in migration/20.js due to being on the sync target
await ResourceService.autoSetFileSizes();
this.logger().info('DecryptionWorker: completed decryption.');
2017-12-26 12:38:53 +02:00
const downloadedButEncryptedBlobCount = await Resource.downloadedButEncryptedBlobCount();
this.dispatchReport({ state: 'idle' });
this.state_ = 'idle';
if (downloadedButEncryptedBlobCount) {
this.logger().info('DecryptionWorker: Some resources have been downloaded but are not decrypted yet. Scheduling another decryption. Resource count: ' + downloadedButEncryptedBlobCount);
this.scheduleStart();
}
}
}
module.exports = DecryptionWorker;