From 1ed16bfc4515f65864252050c1b35b899e12f5ce Mon Sep 17 00:00:00 2001 From: "a.tropnikov" Date: Fri, 29 Sep 2017 13:23:40 +0300 Subject: [PATCH] https://github.com/AdguardTeam/AdguardBrowserExtension/issues/865 --- .../browser/chrome/lib/utils/local-storage.js | 25 +++++++++++++++++-- Extension/lib/storage.js | 11 +++++++- Extension/lib/utils/user-settings.js | 7 ++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Extension/browser/chrome/lib/utils/local-storage.js b/Extension/browser/chrome/lib/utils/local-storage.js index fa941c908c..9c887270fe 100644 --- a/Extension/browser/chrome/lib/utils/local-storage.js +++ b/Extension/browser/chrome/lib/utils/local-storage.js @@ -114,6 +114,9 @@ adguard.localStorageImpl = (function () { * @returns {*} */ var getItem = function (key) { + if (!isInitialized()) { + return null; + } if (!(key in values)) { migrateKeyValue(key); } @@ -121,11 +124,17 @@ adguard.localStorageImpl = (function () { }; var setItem = function (key, value) { + if (!isInitialized()) { + return; + } values[key] = value; write(ADGUARD_SETTINGS_PROP, values, checkError); }; var removeItem = function (key) { + if (!isInitialized()) { + return; + } delete values[key]; // Remove from localStorage too, as a part of migration process localStorage.removeItem(key); @@ -133,6 +142,9 @@ adguard.localStorageImpl = (function () { }; var hasItem = function (key) { + if (!isInitialized()) { + return false; + } if (key in values) { return true; } @@ -148,7 +160,7 @@ adguard.localStorageImpl = (function () { * @param callback */ var init = function (callback) { - if (values !== null) { + if (isInitialized()) { // Already initialized callback(); return; @@ -162,12 +174,21 @@ adguard.localStorageImpl = (function () { }); }; + /** + * Due to async initialization of storage, we have to check it before accessing values object + * @returns {boolean} + */ + var isInitialized = function () { + return values !== null; + }; + return { getItem: getItem, setItem: setItem, removeItem: removeItem, hasItem: hasItem, - init: init + init: init, + isInitialized: isInitialized }; })(); \ No newline at end of file diff --git a/Extension/lib/storage.js b/Extension/lib/storage.js index b5168c5131..51f2d9a2de 100644 --- a/Extension/lib/storage.js +++ b/Extension/lib/storage.js @@ -65,12 +65,21 @@ adguard.localStorage = (function (adguard, impl) { } }; + var isInitialized = function () { + // WebExtension storage has async initialization + if (typeof impl.isInitialized === 'function') { + return impl.isInitialized(); + } + return true; + }; + return { getItem: getItem, setItem: setItem, removeItem: removeItem, hasItem: hasItem, - init: init + init: init, + isInitialized: isInitialized }; })(adguard, adguard.localStorageImpl); diff --git a/Extension/lib/utils/user-settings.js b/Extension/lib/utils/user-settings.js index 95e0212d61..95833b444b 100644 --- a/Extension/lib/utils/user-settings.js +++ b/Extension/lib/utils/user-settings.js @@ -70,6 +70,13 @@ adguard.settings = (function (adguard) { return properties[propertyName]; } + /** + * Don't cache values in case of uninitialized storage + */ + if (!adguard.localStorage.isInitialized()) { + return defaultProperties.defaults[propertyName]; + } + var propertyValue = null; if (adguard.localStorage.hasItem(propertyName)) {