Skip to content

Commit

Permalink
Merge pull request #37 in EXTENSIONS/browser-extension from feature/i…
Browse files Browse the repository at this point in the history
…ssues/865 to master

* commit '1ed16bfc4515f65864252050c1b35b899e12f5ce':
  #865
  • Loading branch information
Aleksandr Tropnikov committed Sep 29, 2017
2 parents 50d52c3 + 1ed16bf commit 807e4b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
25 changes: 23 additions & 2 deletions Extension/browser/chrome/lib/utils/local-storage.js
Expand Up @@ -114,25 +114,37 @@ adguard.localStorageImpl = (function () {
* @returns {*}
*/
var getItem = function (key) {
if (!isInitialized()) {
return null;
}
if (!(key in values)) {
migrateKeyValue(key);
}
return values[key];
};

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);
write(ADGUARD_SETTINGS_PROP, values, checkError);
};

var hasItem = function (key) {
if (!isInitialized()) {
return false;
}
if (key in values) {
return true;
}
Expand All @@ -148,7 +160,7 @@ adguard.localStorageImpl = (function () {
* @param callback
*/
var init = function (callback) {
if (values !== null) {
if (isInitialized()) {
// Already initialized
callback();
return;
Expand All @@ -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
};

})();
11 changes: 10 additions & 1 deletion Extension/lib/storage.js
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions Extension/lib/utils/user-settings.js
Expand Up @@ -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)) {
Expand Down

0 comments on commit 807e4b1

Please sign in to comment.