From 497d8b1c201ad62d4dee8e86ad6622da38805163 Mon Sep 17 00:00:00 2001 From: mizzick Date: Tue, 24 May 2016 20:51:25 +0530 Subject: [PATCH] #253 file storage --- Extension/lib/filter/update-service.js | 93 +++++++++++++------------- Extension/lib/utils/browser-utils.js | 6 +- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/Extension/lib/filter/update-service.js b/Extension/lib/filter/update-service.js index 6c26239b15..405fa2d726 100644 --- a/Extension/lib/filter/update-service.js +++ b/Extension/lib/filter/update-service.js @@ -84,6 +84,9 @@ exports.ApplicationUpdateService = { if (Utils.isGreaterVersion("2.1.2", runInfo.prevVersion) && Utils.isFirefoxBrowser()) { methods.push(this._onUpdateFirefoxStorage); } + if (Utils.isGreaterVersion("2.3.5", runInfo.prevVersion) && Utils.isChromium() && !Utils.isSafariBrowser()) { + methods.push(this._onUpdateChromiumStorage); + } var dfd = this._executeMethods(methods); dfd.then(callback); @@ -281,6 +284,50 @@ exports.ApplicationUpdateService = { return dfd; }, + /** + * Update chromium file storage by switching to local storage + * + * Version 2.3.5 + * @returns {exports.Promise} + * @private + */ + _onUpdateChromiumStorage: function () { + Log.info('Call update to version 2.3.5'); + + var dfd = new Promise(); + + var adguardFilters = JSON.parse(LS.getItem('adguard-filters')) || Object.create(null); + + for (var filterId in adguardFilters) { + if (filterId == AntiBannerFiltersId.WHITE_LIST_FILTER_ID) { + continue; + } + + var filePath = FilterStorage._getFilePath(filterId); + FileStorage.readFromFile(filePath, function (e, rules) { + if (e) { + Log.error("Error while reading rules from file {0} cause: {1}", filePath, e); + continue; + } + + var onTransferCompleted = function () { + Log.info("Rules have been transferred to local storage for filter {0}", filterId); + + FileStorage.removeFile(filePath, function () { + Log.info("File removed for filter {0}", filterId); + }, function () { + Log.error("File remove error for filter {0}", filterId); + }); + }; + + FilterStorage.saveFilterRules(filterId, rules, onTransferCompleted.bind(this)); + }.bind(this)); + } + + dfd.resolve(); + return dfd; + }, + /** * Mark 'adguard-filters' as installed and loaded on extension version update * @private @@ -334,7 +381,7 @@ exports.ApplicationUpdateService = { /** * File storage adapter - * @Deprecated Used now only to upgrade from versions older than v2.3 + * @Deprecated Used now only to upgrade from versions older than v2.3.5 */ var FileStorage = exports.FileStorage = { @@ -372,50 +419,6 @@ var FileStorage = exports.FileStorage = { this._getFile(path, true, successCallback, callback); }, - writeToFile: function (path, data, callback) { - - var successCallback = function (fs, fileEntry) { - - fileEntry.createWriter(function (fileWriter) { - - var writeOperation = function (operation, nextOperation) { - - fileWriter.onwriteend = function () { - if (fileWriter.error) { - callback(fileWriter.error); - } else { - nextOperation(); - } - }; - - fileWriter.onerror = function (e) { - callback(e); - }; - - operation(); - }; - - var nextOperation = function () { - var blob; - try { - blob = new Blob([data.join(FS.LINE_BREAK)], {type: "text/plain"}); - } catch (ex) { - var builder = new (window.BlobBuilder || window.WebKitBlobBuilder)(); - builder.append(data.join(FS.LINE_BREAK)); - blob = builder.getBlob("text/plain"); - } - - writeOperation(fileWriter.write.bind(fileWriter, blob), callback); - }; - - writeOperation(fileWriter.truncate.bind(fileWriter, 0), nextOperation); - - }, callback); - }; - - this._getFile(path, true, successCallback, callback); - }, - _getFile: function (path, create, successCallback, errorCallback) { path = path.replace(/^.*[\/\\]/, ""); diff --git a/Extension/lib/utils/browser-utils.js b/Extension/lib/utils/browser-utils.js index f0de8ca0a3..422a45e467 100644 --- a/Extension/lib/utils/browser-utils.js +++ b/Extension/lib/utils/browser-utils.js @@ -100,6 +100,10 @@ var Utils = exports.Utils = { return Prefs.getBrowser() == "Chrome"; }, + isChromium: function () { + return Prefs.platform == 'chromium'; + }, + isWindowsOs: function () { return this.navigator.userAgent.toLowerCase().indexOf("win") >= 0; }, @@ -118,7 +122,7 @@ var Utils = exports.Utils = { isShadowDomSupported: function() { // Shadow DOM is supported by all modern chromium browsers - return Prefs.platform == 'chromium'; + return this.isChromium(); }, /**