Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit 107bc84

Browse files
committed
this concludes fix to #334
1 parent bf037b7 commit 107bc84

File tree

7 files changed

+95
-125
lines changed

7 files changed

+95
-125
lines changed

js/about.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ var renderAssetList = function(details) {
223223
var updateAssets = function() {
224224
var httpsb = getHTTPSB();
225225
setAssetListClassBit(2, true);
226-
var onDone = function() {
227-
httpsb.loadUpdatableAssets(false);
226+
var onDone = function(details) {
227+
if ( details.changedCount !== 0 ) {
228+
httpsb.loadUpdatableAssets();
229+
}
228230
};
229231
httpsb.assetUpdater.update(updateList, onDone);
230232
};

js/asset-updater.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,22 @@ var getUpdateList = function(callback) {
131131
// If `list` is null, it will be fetched internally.
132132

133133
var update = function(list, callback) {
134-
var assetToUpdateCount;
134+
var assetChangedCount = 0;
135+
var assetProcessedCount;
135136
var updatedAssetChecksums = [];
136137

137138
var reportBack = function() {
138-
HTTPSB.utils.reportBack(callback);
139-
chrome.runtime.sendMessage({ what: 'allLocalAssetsUpdated' });
139+
var details = {
140+
what: 'allLocalAssetsUpdated',
141+
changedCount: assetChangedCount
142+
};
143+
HTTPSB.utils.reportBack(callback, details);
144+
chrome.runtime.sendMessage(details);
140145
};
141146

142-
var countdown = function() {
143-
assetToUpdateCount -= 1;
144-
if ( assetToUpdateCount > 0 ) {
147+
var doCountdown = function() {
148+
assetProcessedCount -= 1;
149+
if ( assetProcessedCount > 0 ) {
145150
return;
146151
}
147152
HTTPSB.assets.put(
@@ -159,13 +164,14 @@ var update = function(list, callback) {
159164
updatedAssetChecksums.push(entry.localChecksum + ' ' + path);
160165
} else {
161166
updatedAssetChecksums.push(entry.remoteChecksum + ' ' + path);
167+
assetChangedCount += 1;
162168
}
163-
countdown();
169+
doCountdown();
164170
};
165171

166172
var processList = function() {
167-
assetToUpdateCount = Object.keys(list).length;
168-
if ( assetToUpdateCount === 0 ) {
173+
assetProcessedCount = Object.keys(list).length;
174+
if ( assetProcessedCount === 0 ) {
169175
reportBack();
170176
return;
171177
}
@@ -185,7 +191,7 @@ var update = function(list, callback) {
185191
if ( entry.status === 'Unchanged' ) {
186192
updatedAssetChecksums.push(entry.localChecksum + ' ' + path);
187193
}
188-
countdown();
194+
doCountdown();
189195
}
190196
};
191197

js/assets.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,22 @@ var requestFileSystem = function(onSuccess, onError) {
115115

116116
/******************************************************************************/
117117

118-
// Flush cached 3rd-party assets if these are from a prior version.
118+
// Flush cached non-user assets if these are from a prior version.
119119
// https://github.com/gorhill/httpswitchboard/issues/212
120120

121121
var cacheSynchronized = false;
122122

123-
var synchronizeCache = function(onCacheSynchronized) {
123+
var synchronizeCache = function() {
124+
if ( cacheSynchronized ) {
125+
return;
126+
}
127+
cacheSynchronized = true;
128+
124129
var directoryReader;
125130
var done = function() {
126131
directoryReader = null;
127-
onCacheSynchronized();
128132
};
129133

130-
if ( cacheSynchronized ) {
131-
return done();
132-
}
133-
cacheSynchronized = true;
134-
135134
var onReadEntries = function(entries) {
136135
var n = entries.length;
137136
if ( !n ) {
@@ -140,8 +139,7 @@ var synchronizeCache = function(onCacheSynchronized) {
140139
var entry;
141140
for ( var i = 0; i < n; i++ ) {
142141
entry = entries[i];
143-
// Ignore whatever is in 'user' folder: these are
144-
// NOT cached entries.
142+
// Ignore whatever is in 'user' folder: these are NOT cached entries.
145143
if ( pathFromCachePath(entry.fullPath).indexOf('/assets/user/') >= 0 ) {
146144
continue;
147145
}
@@ -239,11 +237,7 @@ var readLocalFile = function(path, callback) {
239237
getTextFileFromURL(chrome.runtime.getURL(path), onLocalFileLoaded, onLocalFileError);
240238
};
241239

242-
var onCacheSynchronized = function() {
243-
requestFileSystem(onRequestFileSystemSuccess, onRequestFileSystemError);
244-
};
245-
246-
synchronizeCache(onCacheSynchronized);
240+
requestFileSystem(onRequestFileSystemSuccess, onRequestFileSystemError);
247241
};
248242

249243
/******************************************************************************/
@@ -397,6 +391,13 @@ var updateFromRemote = function(details, callback) {
397391

398392
/******************************************************************************/
399393

394+
// Flush cached assets if cache content is from an older version: the extension
395+
// always ships with the most up-to-date assets.
396+
397+
synchronizeCache();
398+
399+
/******************************************************************************/
400+
400401
// Export API
401402

402403
return {

js/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var HTTPSB = {
6161
clearBrowserCacheCycle: 0,
6262
inlineFieldSeparator: '#',
6363

64-
updateAssetsEvery: 2 * 24 * 60 * 60 * 1000,
64+
updateAssetsEvery: 5 * 24 * 60 * 60 * 1000,
6565
projectServerRoot: 'https://raw2.github.com/gorhill/httpswitchboard/master/',
6666

6767
// list of remote blacklist locations

js/presets.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -491,36 +491,21 @@ HTTPSB.reloadAllPresets = function() {
491491
} else {
492492
presetManager.resetAll();
493493
}
494-
var presetListCount = 2;
495-
var onMessage = function(request) {
496-
if ( !request || !request.what ) {
497-
return;
498-
}
499-
if ( request.what === '1stPartyPresetRecipesLoaded' ) {
500-
if ( !request.error ) {
501-
presetManager.merge1stPartyPresets(request);
502-
}
503-
onePresetListLoaded();
504-
return;
505-
}
506-
if ( request.what === '3rdPartyPresetRecipesLoaded' ) {
507-
if ( !request.error ) {
508-
presetManager.merge3rdPartyPresets(request);
509-
}
510-
onePresetListLoaded();
511-
return;
494+
495+
var firstPartyPresetRecipesLoaded = function(details) {
496+
if ( !details.error ) {
497+
presetManager.merge1stPartyPresets(details);
512498
}
513499
};
514-
var onePresetListLoaded = function() {
515-
presetListCount -= 1;
516-
if ( presetListCount === 0 ) {
517-
chrome.runtime.onMessage.removeListener(onMessage);
500+
501+
var thirdPartyPresetRecipesLoaded = function(details) {
502+
if ( !details.error ) {
503+
presetManager.merge3rdPartyPresets(details);
518504
}
519505
};
520-
chrome.runtime.onMessage.addListener(onMessage);
521506

522-
this.assets.get('assets/httpsb/preset-recipes-1st.yaml', '1stPartyPresetRecipesLoaded');
523-
this.assets.get('assets/httpsb/preset-recipes-3rd.yaml', '3rdPartyPresetRecipesLoaded');
507+
this.assets.get('assets/httpsb/preset-recipes-1st.yaml', firstPartyPresetRecipesLoaded);
508+
this.assets.get('assets/httpsb/preset-recipes-3rd.yaml', thirdPartyPresetRecipesLoaded);
524509
};
525510

526511
/******************************************************************************/

js/start.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,45 @@ chrome.tabs.query({ url: '<all_urls>' }, function(tabs) {
171171

172172
// Browser data jobs
173173

174-
function clearBrowserCacheCallback() {
175-
var httpsb = HTTPSB;
176-
if ( httpsb.userSettings.clearBrowserCache ) {
174+
(function() {
175+
var jobCallback = function() {
176+
var httpsb = HTTPSB;
177+
if ( !httpsb.userSettings.clearBrowserCache ) {
178+
return;
179+
}
177180
httpsb.clearBrowserCacheCycle -= 15;
178-
if ( httpsb.clearBrowserCacheCycle <= 0 ) {
179-
httpsb.clearBrowserCacheCycle = httpsb.userSettings.clearBrowserCacheAfter;
180-
httpsb.browserCacheClearedCounter++;
181-
chrome.browsingData.removeCache({ since: 0 });
182-
// console.debug('clearBrowserCacheCallback()> chrome.browsingData.removeCache() called');
181+
if ( httpsb.clearBrowserCacheCycle > 0 ) {
182+
return;
183183
}
184-
}
185-
}
184+
httpsb.clearBrowserCacheCycle = httpsb.userSettings.clearBrowserCacheAfter;
185+
httpsb.browserCacheClearedCounter++;
186+
chrome.browsingData.removeCache({ since: 0 });
187+
// console.debug('clearBrowserCacheCallback()> chrome.browsingData.removeCache() called');
188+
};
189+
190+
HTTPSB.asyncJobs.add('clearBrowserCache', null, jobCallback, 15 * 60 * 1000, true);
191+
})();
192+
193+
/******************************************************************************/
186194

187-
HTTPSB.asyncJobs.add('clearBrowserCache', null, clearBrowserCacheCallback, 15 * 60 * 1000, true);
195+
// Automatic update of non-user assets
196+
// https://github.com/gorhill/httpswitchboard/issues/334
197+
198+
(function() {
199+
var httpsb = HTTPSB;
200+
201+
var jobDone = function(details) {
202+
if ( details.changedCount === 0 ) {
203+
return;
204+
}
205+
httpsb.loadUpdatableAssets();
206+
};
207+
208+
var jobCallback = function() {
209+
httpsb.assetUpdater.update(null, jobDone);
210+
};
211+
212+
httpsb.asyncJobs.add('autoUpdateAssets', null, jobCallback, httpsb.updateAssetsEvery, true);
213+
})();
188214

189215
/******************************************************************************/

js/storage.js

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,6 @@ HTTPSB.loadUbiquitousBlacklists = function() {
228228
var blacklistLoadCount;
229229
var obsoleteBlacklists = [];
230230

231-
var onMessageHandler = function(details) {
232-
if ( !details || !details.what ) {
233-
return;
234-
}
235-
if ( details.what === 'mergeUbiquitousBlacklist' ) {
236-
mergeBlacklist(details);
237-
return;
238-
}
239-
if ( details.what === 'listOfBlockListsLoaded' ) {
240-
onListOfBlockListsLoaded(details);
241-
return;
242-
}
243-
};
244-
chrome.runtime.onMessage.addListener(onMessageHandler);
245-
246231
var removeObsoleteBlacklistsHandler = function(store) {
247232
if ( !store.remoteBlacklists ) {
248233
return;
@@ -277,7 +262,6 @@ HTTPSB.loadUbiquitousBlacklists = function() {
277262
HTTPSB.abpFilters.freeze();
278263
HTTPSB.abpHideFilters.freeze();
279264
removeObsoleteBlacklists();
280-
chrome.runtime.onMessage.removeListener(onMessageHandler);
281265
chrome.runtime.sendMessage({ what: 'loadUbiquitousBlacklistCompleted' });
282266
};
283267

@@ -320,7 +304,7 @@ HTTPSB.loadUbiquitousBlacklists = function() {
320304
blacklistLoadCount -= 1;
321305
continue;
322306
}
323-
httpsb.assets.get(location, 'mergeUbiquitousBlacklist');
307+
httpsb.assets.get(location, mergeBlacklist);
324308
}
325309
};
326310

@@ -349,7 +333,7 @@ HTTPSB.loadUbiquitousBlacklists = function() {
349333
}
350334

351335
// Get new list of 3rd-party block lists.
352-
this.assets.get('assets/httpsb/ubiquitous-block-lists.json', 'listOfBlockListsLoaded');
336+
this.assets.get('assets/httpsb/ubiquitous-block-lists.json', onListOfBlockListsLoaded);
353337
};
354338

355339
/******************************************************************************/
@@ -502,61 +486,27 @@ HTTPSB.reloadPresetBlacklists = function(switches) {
502486
/******************************************************************************/
503487

504488
HTTPSB.loadPublicSuffixList = function() {
505-
var onMessage = function(request) {
506-
if ( !request || !request.what ) {
507-
return;
508-
}
509-
if ( request.what === 'publicSuffixListLoaded' ) {
510-
applyPublicSuffixList(request);
511-
}
512-
};
513489
var applyPublicSuffixList = function(details) {
490+
// TODO: Not getting proper suffix list is a bit serious, I think
491+
// the extension should be force-restarted if it occurs..
514492
if ( !details.error ) {
515493
publicSuffixList.parse(details.content, punycode.toASCII);
516494
}
517-
chrome.runtime.onMessage.removeListener(onMessage);
518495
};
519-
chrome.runtime.onMessage.addListener(onMessage);
520496
this.assets.get(
521497
'assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat',
522-
'publicSuffixListLoaded'
498+
applyPublicSuffixList
523499
);
524500
};
525501

526502
/******************************************************************************/
527503

528504
// Load updatable assets
529505

530-
HTTPSB.loadUpdatableAssets = function(update) {
531-
var load = function(httpsb) {
532-
httpsb.loadUbiquitousBlacklists();
533-
httpsb.loadPublicSuffixList();
534-
httpsb.reloadAllPresets();
535-
};
536-
537-
if ( update !== true ) {
538-
load(this);
539-
return;
540-
}
541-
542-
var assetsUpdated = function() {
543-
load(HTTPSB);
544-
};
545-
546-
var timestampLoaded = function(store) {
547-
var httpsb = HTTPSB;
548-
var elapsed = Date.now();
549-
if ( typeof store.assetsUpdateTimestamp === 'number' ) {
550-
elapsed -= store.assetsUpdateTimestamp;
551-
}
552-
if ( elapsed < httpsb.updateAssetsEvery ) {
553-
load(httpsb);
554-
} else {
555-
httpsb.assetUpdater.update(null, assetsUpdated);
556-
}
557-
};
558-
559-
chrome.storage.local.get('assetsUpdateTimestamp', timestampLoaded);
506+
HTTPSB.loadUpdatableAssets = function() {
507+
this.loadUbiquitousBlacklists();
508+
this.loadPublicSuffixList();
509+
this.reloadAllPresets();
560510
};
561511

562512
/******************************************************************************/
@@ -569,8 +519,8 @@ HTTPSB.load = function() {
569519
this.loadScopedRules();
570520
this.loadUbiquitousWhitelists();
571521

572-
// updatable assets
573-
this.loadUpdatableAssets(true);
522+
// load updatable assets -- after updating them if needed
523+
this.assetUpdater.update(null, this.loadUpdatableAssets.bind(this));
574524

575525
this.getBytesInUse();
576526
};

0 commit comments

Comments
 (0)