Skip to content

Commit

Permalink
this fixes gorhill#832
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 21, 2015
1 parent c036bd3 commit 892913d
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 75 deletions.
10 changes: 10 additions & 0 deletions platform/chromium/managed_storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "object",
"properties": {
"adminSettings": {
"title": "A valid JSON string compliant with uBO's backup format.",
"description": "All entries present will overwrite local settings.",
"type": "string"
}
}
}
5 changes: 4 additions & 1 deletion platform/chromium/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@
"http://*/*",
"https://*/*"
],
"short_name": "uBlock₀"
"short_name": "uBlock₀",
"storage": {
"managed_schema": "managed_storage.json"
}
}
13 changes: 13 additions & 0 deletions platform/chromium/vapi-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ try {

/******************************************************************************/

// https://github.com/gorhill/uBlock/issues/531
// Storage area dedicated to admin settings. Read-only.

vAPI.adminStorage = {
getItem: function(key, callback) {
chrome.storage.managed.get(key, function(store) {
callback(store[key] || undefined);
});
}
};

/******************************************************************************/

})();

/******************************************************************************/
14 changes: 14 additions & 0 deletions platform/firefox/vapi-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ vAPI.localStorage.init('extensions.' + location.host + '.');

/******************************************************************************/

// https://github.com/gorhill/uBlock/issues/531
// Storage area dedicated to admin settings. Read-only.

vAPI.adminStorage = {
getItem: function(key, callback) {
if ( typeof callback !== 'function' ) {
return;
}
callback(vAPI.localStorage.getItem(key));
}
};

/******************************************************************************/

})();

/******************************************************************************/
51 changes: 27 additions & 24 deletions src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,32 +247,35 @@ var fromFetch = function(to, fetched) {
/******************************************************************************/

return function() {
// https://github.com/gorhill/uBlock/issues/531
µb.restoreAdminSettings();

// Forbid remote fetching of assets
µb.assets.remoteFetchBarrier += 1;

var fetchableProps = {
'compiledMagic': '',
'dynamicFilteringString': '',
'urlFilteringString': '',
'hostnameSwitchesString': '',
'lastRestoreFile': '',
'lastRestoreTime': 0,
'lastBackupFile': '',
'lastBackupTime': 0,
'netWhitelist': '',
'selfie': null,
'selfieMagic': '',
'version': '0.0.0.0'
};

toFetch(µb.localSettings, fetchableProps);
toFetch(µb.userSettings, fetchableProps);
toFetch(µb.restoreBackupSettings, fetchableProps);
var onAdminSettingsRestored = function() {
// Forbid remote fetching of assets
µb.assets.remoteFetchBarrier += 1;

var fetchableProps = {
'compiledMagic': '',
'dynamicFilteringString': '',
'urlFilteringString': '',
'hostnameSwitchesString': '',
'lastRestoreFile': '',
'lastRestoreTime': 0,
'lastBackupFile': '',
'lastBackupTime': 0,
'netWhitelist': '',
'selfie': null,
'selfieMagic': '',
'version': '0.0.0.0'
};

toFetch(µb.localSettings, fetchableProps);
toFetch(µb.userSettings, fetchableProps);
toFetch(µb.restoreBackupSettings, fetchableProps);

vAPI.storage.get(fetchableProps, onFirstFetchReady);
};

vAPI.storage.get(fetchableProps, onFirstFetchReady);
// https://github.com/gorhill/uBlock/issues/531
µb.restoreAdminSettings(onAdminSettingsRestored);
};

/******************************************************************************/
Expand Down
105 changes: 56 additions & 49 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,69 +725,76 @@
// necessarily present, i.e. administrators may removed entries which
// values are left to the user's choice.

µBlock.restoreAdminSettings = function() {
var data = null;
var json = vAPI.localStorage.getItem('adminSettings');
if ( typeof json === 'string' && json !== '' ) {
try {
data = JSON.parse(json);
} catch (ex) {
console.error(ex);
µBlock.restoreAdminSettings = function(callback) {
var onRead = function(json) {
var µb = µBlock;
var data;
if ( typeof json === 'string' && json !== '' ) {
try {
data = JSON.parse(json);
} catch (ex) {
console.error(ex);
}
}
}

if ( typeof data !== 'object' || data === null ) {
return;
}
if ( typeof data !== 'object' || data === null ) {
callback();
return;
}

var bin = {};
var binNotEmpty = false;
var bin = {};
var binNotEmpty = false;

if ( typeof data.userSettings === 'object' ) {
for ( var name in this.userSettings ) {
if ( this.userSettings.hasOwnProperty(name) === false ) {
continue;
}
if ( data.userSettings.hasOwnProperty(name) === false ) {
continue;
if ( typeof data.userSettings === 'object' ) {
for ( var name in µb.userSettings ) {
if ( µb.userSettings.hasOwnProperty(name) === false ) {
continue;
}
if ( data.userSettings.hasOwnProperty(name) === false ) {
continue;
}
bin[name] = data.userSettings[name];
binNotEmpty = true;
}
bin[name] = data.userSettings[name];
}

if ( typeof data.filterLists === 'object' ) {
bin.remoteBlacklists = data.filterLists;
binNotEmpty = true;
}
}

if ( typeof data.filterLists === 'object' ) {
bin.remoteBlacklists = data.filterLists;
binNotEmpty = true;
}
if ( typeof data.netWhitelist === 'string' ) {
bin.netWhitelist = data.netWhitelist;
binNotEmpty = true;
}

if ( typeof data.netWhitelist === 'string' ) {
bin.netWhitelist = data.netWhitelist;
binNotEmpty = true;
}
if ( typeof data.dynamicFilteringString === 'string' ) {
bin.dynamicFilteringString = data.dynamicFilteringString;
binNotEmpty = true;
}

if ( typeof data.dynamicFilteringString === 'string' ) {
bin.dynamicFilteringString = data.dynamicFilteringString;
binNotEmpty = true;
}
if ( typeof data.urlFilteringString === 'string' ) {
bin.urlFilteringString = data.urlFilteringString;
binNotEmpty = true;
}

if ( typeof data.urlFilteringString === 'string' ) {
bin.urlFilteringString = data.urlFilteringString;
binNotEmpty = true;
}
if ( typeof data.hostnameSwitchesString === 'string' ) {
bin.hostnameSwitchesString = data.hostnameSwitchesString;
binNotEmpty = true;
}

if ( typeof data.hostnameSwitchesString === 'string' ) {
bin.hostnameSwitchesString = data.hostnameSwitchesString;
binNotEmpty = true;
}
if ( binNotEmpty ) {
vAPI.storage.set(bin);
}

if ( binNotEmpty ) {
vAPI.storage.set(bin);
}
if ( typeof data.userFilters === 'string' ) {
µb.assets.put('assets/user/filters.txt', data.userFilters);
}

if ( typeof data.userFilters === 'string' ) {
this.assets.put('assets/user/filters.txt', data.userFilters);
}
callback();
};

vAPI.adminStorage.getItem('adminSettings', onRead);
};

/******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion tools/make-chromium.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cp src/*.html $DES/
cp platform/chromium/*.js $DES/js/
cp -R platform/chromium/img $DES/
cp platform/chromium/*.html $DES/
cp platform/chromium/manifest.json $DES/
cp platform/chromium/*.json $DES/
cp LICENSE.txt $DES/

if [ "$1" = all ]; then
Expand Down

0 comments on commit 892913d

Please sign in to comment.