1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

code review

This commit is contained in:
gorhill 2014-08-20 09:24:16 -04:00
parent c67e126734
commit b8c2fd64d2
3 changed files with 44 additions and 35 deletions

View File

@ -67,7 +67,7 @@ var RepoMetadata = function() {
this.waiting = []; this.waiting = [];
}; };
var repoMeta = null; var repoMetadata = null;
/******************************************************************************/ /******************************************************************************/
@ -217,7 +217,7 @@ var getTextFileFromURL = function(url, onLoad, onError) {
var updateLocalChecksums = function() { var updateLocalChecksums = function() {
var localChecksums = []; var localChecksums = [];
var entries = repoMeta.entries; var entries = repoMetadata.entries;
var entry; var entry;
for ( var path in entries ) { for ( var path in entries ) {
if ( entries.hasOwnProperty(path) === false ) { if ( entries.hasOwnProperty(path) === false ) {
@ -235,17 +235,17 @@ var updateLocalChecksums = function() {
// Gather meta data of all assets. // Gather meta data of all assets.
var getRepoMeta = function(callback, update) { var getRepoMetadata = function(callback, update) {
callback = callback || nullFunc; callback = callback || nullFunc;
if ( update ) { if ( update ) {
repoMeta = null; repoMetadata = null;
} }
if ( repoMeta !== null ) { if ( repoMetadata !== null ) {
if ( repoMeta.waiting.length !== 0 ) { if ( repoMetadata.waiting.length !== 0 ) {
repoMeta.waiting.push(callback); repoMetadata.waiting.push(callback);
} else { } else {
callback(repoMeta); callback(repoMetadata);
} }
return; return;
} }
@ -253,7 +253,7 @@ var getRepoMeta = function(callback, update) {
// https://github.com/gorhill/uBlock/issues/84 // https://github.com/gorhill/uBlock/issues/84
// First try to load from the actual home server of a third-party. // First try to load from the actual home server of a third-party.
var parseHomeURLs = function(text) { var parseHomeURLs = function(text) {
var entries = repoMeta.entries; var entries = repoMetadata.entries;
var urlPairs = text.split(/\n\n+/); var urlPairs = text.split(/\n\n+/);
var i = urlPairs.length; var i = urlPairs.length;
var pair, pos, k, v; var pair, pos, k, v;
@ -273,8 +273,8 @@ var getRepoMeta = function(callback, update) {
} }
entries[k].homeURL = v; entries[k].homeURL = v;
} }
while ( callback = repoMeta.waiting.pop() ) { while ( callback = repoMetadata.waiting.pop() ) {
callback(repoMeta); callback(repoMetadata);
} }
}; };
@ -285,7 +285,7 @@ var getRepoMeta = function(callback, update) {
}; };
var onRepoHomeURLsLoaded = function(details) { var onRepoHomeURLsLoaded = function(details) {
var entries = repoMeta.entries; var entries = repoMetadata.entries;
var entry = entries[pathToHomeURLs]; var entry = entries[pathToHomeURLs];
if ( YaMD5.hashStr(details.content) !== entry.repoChecksum ) { if ( YaMD5.hashStr(details.content) !== entry.repoChecksum ) {
entry.repoChecksum = entry.localChecksum; entry.repoChecksum = entry.localChecksum;
@ -295,17 +295,15 @@ var getRepoMeta = function(callback, update) {
cachedAssetsManager.save(pathToHomeURLs, details.content, onLocalHomeURLsLoaded); cachedAssetsManager.save(pathToHomeURLs, details.content, onLocalHomeURLsLoaded);
}; };
var checksumCountdown = 2; var localChecksums;
var localChecksums = ''; var repoChecksums;
var repoChecksums = '';
var checksumsReceived = function() { var checksumsReceived = function() {
checksumCountdown -= 1; if ( localChecksums === undefined || repoChecksums === undefined ) {
if ( checksumCountdown !== 0 ) {
return; return;
} }
// Remove from cache assets which no longer exist // Remove from cache assets which no longer exist in the repo
var entries = repoMeta.entries; var entries = repoMetadata.entries;
var checksumsChanged = false; var checksumsChanged = false;
var entry; var entry;
for ( var path in entries ) { for ( var path in entries ) {
@ -347,7 +345,7 @@ var getRepoMeta = function(callback, update) {
}; };
var parseChecksums = function(text, which) { var parseChecksums = function(text, which) {
var entries = repoMeta.entries; var entries = repoMetadata.entries;
var lines = text.split(/\n+/); var lines = text.split(/\n+/);
var i = lines.length; var i = lines.length;
var fields, assetPath; var fields, assetPath;
@ -378,8 +376,8 @@ var getRepoMeta = function(callback, update) {
checksumsReceived(); checksumsReceived();
}; };
repoMeta = new RepoMetadata(); repoMetadata = new RepoMetadata();
repoMeta.waiting.push(callback); repoMetadata.waiting.push(callback);
readRepoFile('assets/checksums.txt', onRepoChecksumsLoaded); readRepoFile('assets/checksums.txt', onRepoChecksumsLoaded);
readLocalFile('assets/checksums.txt', onLocalChecksumsLoaded); readLocalFile('assets/checksums.txt', onLocalChecksumsLoaded);
}; };
@ -388,6 +386,9 @@ var getRepoMeta = function(callback, update) {
/******************************************************************************/ /******************************************************************************/
// Get a local asset, do not look-up repo or remote location if local asset
// is not found.
var readLocalFile = function(path, callback) { var readLocalFile = function(path, callback) {
var reportBack = function(content, err) { var reportBack = function(content, err) {
var details = { var details = {
@ -400,31 +401,39 @@ var readLocalFile = function(path, callback) {
callback(details); callback(details);
}; };
var onLocalFileLoaded = function() { var onInstallFileLoaded = function() {
// console.log('µBlock> onLocalFileLoaded()'); // console.log('µBlock> readLocalFile("%s") / onInstallFileLoaded()', path);
reportBack(this.responseText); reportBack(this.responseText);
this.onload = this.onerror = null; this.onload = this.onerror = null;
}; };
var onLocalFileError = function() { var onInstallFileError = function() {
console.error('µBlock> readLocalFile() / onLocalFileError("%s")', path); console.error('µBlock> readLocalFile("%s") / onInstallFileError()', path);
reportBack('', 'Error'); reportBack('', 'Error');
this.onload = this.onerror = null; this.onload = this.onerror = null;
}; };
var onCachedContentLoaded = function(details) { var onCachedContentLoaded = function(details) {
// console.log('µBlock> readLocalFile() / onCacheFileLoaded()'); // console.log('µBlock> readLocalFile("%s") / onCachedContentLoaded()', path);
reportBack(details.content); reportBack(details.content);
}; };
var onCachedContentError = function(details) { var onCachedContentError = function(details) {
// console.error('µBlock> readLocalFile() / onCacheFileError("%s")', path); // console.error('µBlock> readLocalFile("%s") / onCachedContentError()', path);
getTextFileFromURL(chrome.runtime.getURL(details.path), onLocalFileLoaded, onLocalFileError); if ( reIsExternalPath.test(path) ) {
reportBack('', 'Error: asset not found');
return;
}
getTextFileFromURL(chrome.runtime.getURL(details.path), onInstallFileLoaded, onInstallFileError);
}; };
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError); cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
}; };
exports.getLocal = readLocalFile;
// https://www.youtube.com/watch?v=r9KVpuFPtHc
/******************************************************************************/ /******************************************************************************/
// Get the repository copy of a built-in asset. // Get the repository copy of a built-in asset.
@ -654,7 +663,7 @@ var readRepoCopyAsset = function(path, callback) {
cachedAssetsManager.entries(onCacheMetaReady); cachedAssetsManager.entries(onCacheMetaReady);
}; };
getRepoMeta(onRepoMetaReady); getRepoMetadata(onRepoMetaReady);
}; };
// https://www.youtube.com/watch?v=uvUW4ozs7pY // https://www.youtube.com/watch?v=uvUW4ozs7pY
@ -753,7 +762,7 @@ var readRepoOnlyAsset = function(path, callback) {
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError); cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
}; };
getRepoMeta(onRepoMetaReady); getRepoMetadata(onRepoMetaReady);
}; };
/******************************************************************************/ /******************************************************************************/
@ -927,7 +936,7 @@ exports.get = function(path, callback) {
readRepoOnlyAsset(path, callback); readRepoOnlyAsset(path, callback);
}; };
getRepoMeta(onRepoMetaReady); getRepoMetadata(onRepoMetaReady);
}; };
// https://www.youtube.com/watch?v=98y0Q7nLGWk // https://www.youtube.com/watch?v=98y0Q7nLGWk
@ -991,7 +1000,7 @@ exports.metadata = function(callback) {
entryOut.homeURL = path; entryOut.homeURL = path;
} }
} }
getRepoMeta(onRepoMetaReady); getRepoMetadata(onRepoMetaReady);
}; };
cachedAssetsManager.entries(onCacheMetaReady); cachedAssetsManager.entries(onCacheMetaReady);

View File

@ -140,7 +140,7 @@ function defaultHandler(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'getAssetContent': case 'getAssetContent':
return µBlock.assets.get(request.url, callback); return µBlock.assets.getLocal(request.url, callback);
case 'loadUbiquitousAllowRules': case 'loadUbiquitousAllowRules':
return µBlock.loadUbiquitousWhitelists(); return µBlock.loadUbiquitousWhitelists();

View File

@ -123,7 +123,7 @@ exports.formatCount = function(count) {
return s; return s;
}; };
// https://www.youtube.com/watch?v=uvUW4ozs7pY // https://www.youtube.com/watch?v=DyvzfyqYm_s
/******************************************************************************/ /******************************************************************************/