From 892913d185d8aa2eaa7ab10aa812a5fa3814f2ea Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 21 Oct 2015 11:53:03 -0400 Subject: [PATCH] this fixes #832 --- platform/chromium/managed_storage.json | 10 +++ platform/chromium/manifest.json | 5 +- platform/chromium/vapi-common.js | 13 +++ platform/firefox/vapi-common.js | 14 ++++ src/js/start.js | 51 ++++++------ src/js/storage.js | 105 +++++++++++++------------ tools/make-chromium.sh | 2 +- 7 files changed, 125 insertions(+), 75 deletions(-) create mode 100644 platform/chromium/managed_storage.json diff --git a/platform/chromium/managed_storage.json b/platform/chromium/managed_storage.json new file mode 100644 index 0000000000000..08d531da093f1 --- /dev/null +++ b/platform/chromium/managed_storage.json @@ -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" + } + } +} diff --git a/platform/chromium/manifest.json b/platform/chromium/manifest.json index 2f924ab9b1a5f..e764ed60acd26 100644 --- a/platform/chromium/manifest.json +++ b/platform/chromium/manifest.json @@ -66,5 +66,8 @@ "http://*/*", "https://*/*" ], - "short_name": "uBlock₀" + "short_name": "uBlock₀", + "storage": { + "managed_schema": "managed_storage.json" + } } diff --git a/platform/chromium/vapi-common.js b/platform/chromium/vapi-common.js index 4df7b99689565..3f78ba06b334f 100644 --- a/platform/chromium/vapi-common.js +++ b/platform/chromium/vapi-common.js @@ -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); + }); + } +}; + +/******************************************************************************/ + })(); /******************************************************************************/ diff --git a/platform/firefox/vapi-common.js b/platform/firefox/vapi-common.js index 374890a6679dd..ab5c47867c700 100644 --- a/platform/firefox/vapi-common.js +++ b/platform/firefox/vapi-common.js @@ -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)); + } +}; + +/******************************************************************************/ + })(); /******************************************************************************/ diff --git a/src/js/start.js b/src/js/start.js index d33ea55e383d0..782cb1ff145d2 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -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); }; /******************************************************************************/ diff --git a/src/js/storage.js b/src/js/storage.js index 07d601276a9e0..e14986deaed1c 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -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); }; /******************************************************************************/ diff --git a/tools/make-chromium.sh b/tools/make-chromium.sh index 1290c4b3765f9..1c4e06f9f0f2f 100755 --- a/tools/make-chromium.sh +++ b/tools/make-chromium.sh @@ -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