work on auto-update, backup/restore/reset
parent
b94047f9f7
commit
a39bd536b5
@ -1,224 +0,0 @@
|
||||
/*******************************************************************************
|
||||
|
||||
µMatrix - a Chromium browser extension to black/white list requests.
|
||||
Copyright (C) 2013 Raymond Hill
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
|
||||
Home: https://github.com/gorhill/uMatrix
|
||||
*/
|
||||
|
||||
/* global chrome, µMatrix */
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// Asset update manager
|
||||
|
||||
µMatrix.assetUpdater = (function() {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var getUpdateList = function(callback) {
|
||||
var localChecksumsText = '';
|
||||
var remoteChecksumsText = '';
|
||||
|
||||
var compareChecksums = function() {
|
||||
var parseChecksumsText = function(text) {
|
||||
var result = {};
|
||||
var lines = text.split(/\n+/);
|
||||
var i = lines.length;
|
||||
var fields;
|
||||
while ( i-- ) {
|
||||
fields = lines[i].trim().split(/\s+/);
|
||||
if ( fields.length !== 2 ) {
|
||||
continue;
|
||||
}
|
||||
result[fields[1]] = fields[0];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
if ( remoteChecksumsText === 'Error' || localChecksumsText === 'Error' ) {
|
||||
remoteChecksumsText = localChecksumsText = '';
|
||||
}
|
||||
var localAssetChecksums = parseChecksumsText(localChecksumsText);
|
||||
var remoteAssetChecksums = parseChecksumsText(remoteChecksumsText);
|
||||
|
||||
var toUpdate = {};
|
||||
var path;
|
||||
for ( path in remoteAssetChecksums ) {
|
||||
if ( !remoteAssetChecksums.hasOwnProperty(path) ) {
|
||||
continue;
|
||||
}
|
||||
if ( localAssetChecksums[path] === undefined ) {
|
||||
toUpdate[path] = {
|
||||
status: 'Added',
|
||||
remoteChecksum: remoteAssetChecksums[path],
|
||||
localChecksum: ''
|
||||
};
|
||||
continue;
|
||||
}
|
||||
if ( localAssetChecksums[path] === remoteAssetChecksums[path] ) {
|
||||
toUpdate[path] = {
|
||||
status: 'Unchanged',
|
||||
remoteChecksum: remoteAssetChecksums[path],
|
||||
localChecksum: localAssetChecksums[path]
|
||||
};
|
||||
continue;
|
||||
}
|
||||
toUpdate[path] = {
|
||||
status: 'Changed',
|
||||
remoteChecksum: remoteAssetChecksums[path],
|
||||
localChecksum: localAssetChecksums[path]
|
||||
};
|
||||
}
|
||||
for ( path in localAssetChecksums ) {
|
||||
if ( !localAssetChecksums.hasOwnProperty(path) ) {
|
||||
continue;
|
||||
}
|
||||
if ( remoteAssetChecksums[path] === undefined ) {
|
||||
toUpdate[path] = {
|
||||
status: 'Removed',
|
||||
remoteChecksum: '',
|
||||
localChecksum: localAssetChecksums[path]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
callback({ 'list': toUpdate });
|
||||
};
|
||||
|
||||
var validateChecksums = function(details) {
|
||||
if ( details.error || details.content === '' ) {
|
||||
return 'Error';
|
||||
}
|
||||
if ( /^(?:[0-9a-f]{32}\s+\S+(\s+|$))+/.test(details.content) ) {
|
||||
return details.content;
|
||||
}
|
||||
return 'Error';
|
||||
};
|
||||
|
||||
var onLocalChecksumsLoaded = function(details) {
|
||||
localChecksumsText = validateChecksums(details);
|
||||
if ( remoteChecksumsText !== '' ) {
|
||||
compareChecksums();
|
||||
}
|
||||
};
|
||||
|
||||
var onRemoteChecksumsLoaded = function(details) {
|
||||
remoteChecksumsText = validateChecksums(details);
|
||||
if ( localChecksumsText !== '' ) {
|
||||
compareChecksums();
|
||||
}
|
||||
};
|
||||
|
||||
µMatrix.assets.getRemote('assets/checksums.txt', onRemoteChecksumsLoaded);
|
||||
µMatrix.assets.get('assets/checksums.txt', onLocalChecksumsLoaded);
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// If `list` is null, it will be fetched internally.
|
||||
|
||||
var update = function(list, callback) {
|
||||
var assetChangedCount = 0;
|
||||
var assetProcessedCount;
|
||||
var updatedAssetChecksums = [];
|
||||
|
||||
var onCompleted = function() {
|
||||
var details = {
|
||||
what: 'allLocalAssetsUpdated',
|
||||
changedCount: assetChangedCount
|
||||
};
|
||||
callback(details);
|
||||
µMatrix.messaging.announce(details);
|
||||
};
|
||||
|
||||
var doCountdown = function() {
|
||||
assetProcessedCount -= 1;
|
||||
if ( assetProcessedCount > 0 ) {
|
||||
return;
|
||||
}
|
||||
µMatrix.assets.put(
|
||||
'assets/checksums.txt',
|
||||
updatedAssetChecksums.join('\n'),
|
||||
onCompleted
|
||||
);
|
||||
chrome.storage.local.set({ 'assetsUpdateTimestamp': Date.now() });
|
||||
};
|
||||
|
||||
var assetUpdated = function(details) {
|
||||
var path = details.path;
|
||||
var entry = list[path];
|
||||
if ( details.error ) {
|
||||
updatedAssetChecksums.push(entry.localChecksum + ' ' + path);
|
||||
} else {
|
||||
updatedAssetChecksums.push(entry.remoteChecksum + ' ' + path);
|
||||
assetChangedCount += 1;
|
||||
}
|
||||
doCountdown();
|
||||
};
|
||||
|
||||
var processList = function() {
|
||||
assetProcessedCount = Object.keys(list).length;
|
||||
if ( assetProcessedCount === 0 ) {
|
||||
onCompleted();
|
||||
return;
|
||||
}
|
||||
var entry;
|
||||
var details = { path: '', md5: '' };
|
||||
for ( var path in list ) {
|
||||
if ( list.hasOwnProperty(path) === false ) {
|
||||
continue;
|
||||
}
|
||||
entry = list[path];
|
||||
if ( entry.status === 'Added' || entry.status === 'Changed' ) {
|
||||
details.path = path;
|
||||
details.md5 = entry.remoteChecksum;
|
||||
µMatrix.assets.update(details, assetUpdated);
|
||||
continue;
|
||||
}
|
||||
if ( entry.status === 'Unchanged' ) {
|
||||
updatedAssetChecksums.push(entry.localChecksum + ' ' + path);
|
||||
}
|
||||
doCountdown();
|
||||
}
|
||||
};
|
||||
|
||||
var listLoaded = function(details) {
|
||||
list = details.list;
|
||||
processList();
|
||||
};
|
||||
|
||||
if ( list ) {
|
||||
processList();
|
||||
} else {
|
||||
getUpdateList(listLoaded);
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// Export API
|
||||
|
||||
return {
|
||||
'getList': getUpdateList,
|
||||
'update': update
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})();
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -0,0 +1,105 @@
|
||||
/*******************************************************************************
|
||||
|
||||
µMatrix - a Chromium browser extension to black/white list requests.
|
||||
Copyright (C) 2014 Raymond Hill
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
|
||||
Home: https://github.com/gorhill/uMatrix
|
||||
*/
|
||||
|
||||
/* global µMatrix */
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// Automatic update of non-user assets
|
||||
// https://github.com/gorhill/httpswitchboard/issues/334
|
||||
|
||||
µMatrix.updater = (function() {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var µm = µMatrix;
|
||||
|
||||
var jobCallback = function() {
|
||||
// Simpler to fire restart here, and safe given how far this will happen
|
||||
// in the future.
|
||||
restart();
|
||||
|
||||
// If auto-update is disabled, check again in a while.
|
||||
if ( µm.userSettings.autoUpdate !== true ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var onMetadataReady = function(metadata) {
|
||||
// Check PSL
|
||||
var mdEntry = metadata[µm.pslPath];
|
||||
if ( mdEntry.repoObsolete ) {
|
||||
µm.loadUpdatableAssets(true);
|
||||
return;
|
||||
}
|
||||
// Check used hosts files
|
||||
var hostsFiles = µm.liveHostsFiles;
|
||||
for ( var path in hostsFiles ) {
|
||||
if ( hostsFiles.hasOwnProperty(path) === false ) {
|
||||
continue;
|
||||
}
|
||||
if ( hostsFiles[path].off ) {
|
||||
continue;
|
||||
}
|
||||
if ( metadata.hasOwnProperty(path) === false ) {
|
||||
continue;
|
||||
}
|
||||
mdEntry = metadata[path];
|
||||
if ( mdEntry.cacheObsolete || mdEntry.repoObsolete ) {
|
||||
µm.loadUpdatableAssets(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// console.log('updater.js > all is up to date');
|
||||
};
|
||||
|
||||
µm.assets.metadata(onMetadataReady);
|
||||
};
|
||||
|
||||
// https://www.youtube.com/watch?v=cIrGQD84F1g
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var restart = function(after) {
|
||||
if ( after === undefined ) {
|
||||
after = µm.nextUpdateAfter;
|
||||
}
|
||||
|
||||
µm.asyncJobs.add(
|
||||
'autoUpdateAssets',
|
||||
null,
|
||||
jobCallback,
|
||||
after,
|
||||
false
|
||||
);
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
return {
|
||||
restart: restart
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})();
|
||||
|
||||
/******************************************************************************/
|
Loading…
Reference in New Issue