Skip to content

Commit

Permalink
#253 file storage refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed May 24, 2016
1 parent 497d8b1 commit 6db2c08
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Extension/browser/chrome/chrome_background.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<script type="text/javascript" src="lib/utils/promises.js"></script>
<script type="text/javascript" src="lib/utils/punycode.js"></script>
<script type="text/javascript" src="lib/utils/local-storage.js"></script>
<script type="text/javascript" src="lib/utils/file-storage.js"></script>
<script type="text/javascript" src="lib/utils/rules-storage.js"></script>
<script type="text/javascript" src="lib/utils/local-script-rules.js"></script>
<script type="text/javascript" src="lib/utils/workaround.js"></script>
<script type="text/javascript" src="lib/utils/common.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
/**
* This file is part of Adguard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension).
*
* Adguard Browser Extension is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Adguard Browser Extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Adguard Browser Extension. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* File storage adapter
*/
var FS = exports.FS = {

LINE_BREAK: '\n',

readFromFile: function (path, callback) {
try {
var value = LS.getItem(path);
var lines = [];
if (value) {
lines = value.split(/[\r\n]+/);
}
callback(null, lines);
} catch (ex) {
callback(ex);
}
},

writeToFile: function (path, data, callback) {
var value = data.join(FS.LINE_BREAK);
try {
LS.setItem(path, value);
callback();
} catch (ex) {
callback(ex);
}
},

removeFile: function (path, successCallback) {
LS.removeItem(path);
successCallback();
}
/**
* This file is part of Adguard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension).
*
* Adguard Browser Extension is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Adguard Browser Extension is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Adguard Browser Extension. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Filter rules storage adapter
*/
var RulesStorage = exports.RulesStorage = {

LINE_BREAK: '\n',

read: function (path, callback) {
try {
var value = LS.getItem(path);
var lines = [];
if (value) {
lines = value.split(/[\r\n]+/);
}
callback(null, lines);
} catch (ex) {
callback(ex);
}
},

write: function (path, data, callback) {
var value = data.join(RulesStorage.LINE_BREAK);
try {
LS.setItem(path, value);
callback();
} catch (ex) {
callback(ex);
}
},

remove: function (path, successCallback) {
LS.removeItem(path);
successCallback();
}
};
4 changes: 2 additions & 2 deletions Extension/browser/firefox/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ exports.main = function (options, callbacks) {
try {

var {Log} = loadAdguardModule('./utils/log');
var {FS} = loadAdguardModule('./utils/file-storage');
var {RulesStorage} = loadAdguardModule('./utils/rules-storage');
var {LS} = loadAdguardModule('./utils/local-storage');
if (options.loadReason == 'install' || options.loadReason == 'downgrade') {
LS.clean();
FS.removeAdguardDir();
RulesStorage.removeAdguardDir();
}

// In case of firefox browser we move application data from simple-storage to prefs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOServi
var Log = require('../../lib/utils/log').Log;

/**
* File storage adapter
* Filter rules storage adapter.
* For FF we store rules in files
*/
var FS = exports.FS = {
var RulesStorage = exports.RulesStorage = {

PROFILE_DIR: 'ProfD',
ADGUARD_DIR: 'Adguard',
LINE_BREAK: '\n',

readFromFile: function (filename, callback) {
read: function (filename, callback) {

try {
var filePath = FileUtils.getFile(this.PROFILE_DIR, [this.ADGUARD_DIR, filename]);
Expand Down Expand Up @@ -81,11 +82,11 @@ var FS = exports.FS = {
}
},

writeToFile: function (filename, data, callback) {
write: function (filename, data, callback) {
try {
this._createDir();
var filePath = sdkFile.join(sdkPathFor(this.PROFILE_DIR), this.ADGUARD_DIR, filename);
var content = data.join(FS.LINE_BREAK);
var content = data.join(RulesStorage.LINE_BREAK);

var textWriter = sdkFile.open(filePath, 'w');//utf-8 charset by default
textWriter.writeAsync(content, function (error) {
Expand Down Expand Up @@ -179,7 +180,7 @@ var FS = exports.FS = {
return ioService.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
},

removeFile: function (path, successCallback) {
remove: function (path, successCallback) {
var file = FileUtils.getFile(this.PROFILE_DIR, [this.ADGUARD_DIR, path]);
if (!file.exists() || file.fileSize === 0) {
successCallback();
Expand Down
2 changes: 1 addition & 1 deletion Extension/browser/safari/safari_background.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script type="text/javascript" src="lib/utils/promises.js"></script>
<script type="text/javascript" src="lib/utils/punycode.js"></script>
<script type="text/javascript" src="lib/utils/local-storage.js"></script>
<script type="text/javascript" src="lib/utils/file-storage.js"></script>
<script type="text/javascript" src="lib/utils/rules-storage.js"></script>
<script type="text/javascript" src="lib/utils/local-script-rules.js"></script>
<script type="text/javascript" src="lib/utils/workaround.js"></script>
<script type="text/javascript" src="lib/utils/common.js"></script>
Expand Down
22 changes: 11 additions & 11 deletions Extension/lib/filter/antibanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ AntiBannerService.prototype = {
},

/**
* Loads filter from FS (if in extension package) or from backend
* Loads filter from storage (if in extension package) or from backend
*
* @param filterId Filter identifier
* @param callback Called when operation is finished
Expand Down Expand Up @@ -686,7 +686,7 @@ AntiBannerService.prototype = {
}

if (FilterUtils.isAdguardFilter(filter)) {
this._loadFilterFromFS(filterId, onFilterLoaded);
this._loadFilterFromRulesStorage(filterId, onFilterLoaded);
} else {
this._loadFilterFromBackend(filterId, onFilterLoaded);
}
Expand Down Expand Up @@ -1288,14 +1288,14 @@ AntiBannerService.prototype = {

var dfds = [];
var filterFunction = function (el) {
return SAVE_FILTER_RULES_TO_FS_EVENTS.indexOf(el.event) >= 0;
return SAVE_FILTER_RULES_TO_STORAGE_EVENTS.indexOf(el.event) >= 0;
};
for (var filterId in eventsByFilter) {
var needSaveRulesToFS = eventsByFilter[filterId].some(filterFunction);
if (!needSaveRulesToFS) {
var needSaveRulesToStorage = eventsByFilter[filterId].some(filterFunction);
if (!needSaveRulesToStorage) {
continue;
}
var dfd = this._processSaveFilterRulesToFSEvents(filterId, eventsByFilter[filterId]);
var dfd = this._processSaveFilterRulesToStorageEvents(filterId, eventsByFilter[filterId]);
dfds.push(dfd);
}

Expand Down Expand Up @@ -1332,7 +1332,7 @@ AntiBannerService.prototype = {
* @param events Events (what has changed?)
* @private
*/
_processSaveFilterRulesToFSEvents: function (filterId, events) {
_processSaveFilterRulesToStorageEvents: function (filterId, events) {

var dfd = new Promise();

Expand Down Expand Up @@ -1563,12 +1563,12 @@ AntiBannerService.prototype = {
},

/**
* Load filter rules from file system
* Load filter rules from rules storage
* @param filterId
* @param callback
* @private
*/
_loadFilterFromFS: function (filterId, callback) {
_loadFilterFromRulesStorage: function (filterId, callback) {

var filter = this._getFilterById(filterId);

Expand Down Expand Up @@ -1724,10 +1724,10 @@ var FilterLSUtils = exports.FilterLSUtils = {
var UPDATE_REQUEST_FILTER_EVENTS = [EventNotifierTypes.UPDATE_FILTER_RULES, EventNotifierTypes.ENABLE_FILTER, EventNotifierTypes.DISABLE_FILTER];

/**
* List of events which cause saving filter rules to the file storage
* List of events which cause saving filter rules to the rules storage
* @type {Array}
*/
var SAVE_FILTER_RULES_TO_FS_EVENTS = [EventNotifierTypes.UPDATE_FILTER_RULES, EventNotifierTypes.ADD_RULE, EventNotifierTypes.ADD_RULES, EventNotifierTypes.REMOVE_RULE];
var SAVE_FILTER_RULES_TO_STORAGE_EVENTS = [EventNotifierTypes.UPDATE_FILTER_RULES, EventNotifierTypes.ADD_RULE, EventNotifierTypes.ADD_RULES, EventNotifierTypes.REMOVE_RULE];

// Events
// TODO: move to UI.js.
Expand Down
10 changes: 5 additions & 5 deletions Extension/lib/filter/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with Adguard Browser Extension. If not, see <http://www.gnu.org/licenses/>.
*/
var Log = require('../../lib/utils/log').Log;
var FS = require('../../lib/utils/file-storage').FS;
var RulesStorage = require('../../lib/utils/rules-storage').RulesStorage;
var FilterRuleBuilder = require('../../lib/filter/rules/filter-rule-builder').FilterRuleBuilder;

/**
Expand All @@ -39,7 +39,7 @@ var FilterStorage = exports.FilterStorage = {
*/
saveFilterRules: function (filterId, filterRules, callback) {
var filePath = FilterStorage._getFilePath(filterId);
FS.writeToFile(filePath, filterRules, function (e) {
RulesStorage.write(filePath, filterRules, function (e) {
if (e) {
Log.error("Error write filters to file {0} cause: {1}", filePath, e);
}
Expand All @@ -57,7 +57,7 @@ var FilterStorage = exports.FilterStorage = {
*/
loadFilterRules: function (filterId, callback) {
var filePath = FilterStorage._getFilePath(filterId);
FS.readFromFile(filePath, function (e, rules) {
RulesStorage.read(filePath, function (e, rules) {
if (e) {
Log.error("Error while reading rules from file {0} cause: {1}", filePath, e);
}
Expand All @@ -84,7 +84,7 @@ var FilterStorage = exports.FilterStorage = {

var filePath = FilterStorage.CSS_FILE_PATH;

FS.writeToFile(filePath, cssRules, function (e) {
RulesStorage.write(filePath, cssRules, function (e) {
if (e && e.error) {
Log.error("Error write css styleSheet to file {0} cause: {1}", filePath, e);
return;
Expand All @@ -108,7 +108,7 @@ var FilterStorage = exports.FilterStorage = {
*/
getInjectCssFileURI: function () {
if (!this.injectCssUrl) {
this.injectCssUrl = FS.getFileInAdguardDirUri(this.CSS_FILE_PATH);
this.injectCssUrl = RulesStorage.getFileInAdguardDirUri(this.CSS_FILE_PATH);
}
return this.injectCssUrl;
},
Expand Down
4 changes: 2 additions & 2 deletions Extension/lib/filter/update-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var Utils = require('../../lib/utils/browser-utils').Utils;
var LS = require('../../lib/utils/local-storage').LS;
var Prefs = require('../../lib/prefs').Prefs;
var AntiBannerFiltersId = require('../../lib/utils/common').AntiBannerFiltersId;
var FS = require('../../lib/utils/file-storage').FS;
var RulesStorage = require('../../lib/utils/rules-storage').RulesStorage;
var FilterStorage = require('../../lib/filter/storage').FilterStorage;
var CollectionUtils = require('../../lib/utils/common').CollectionUtils;
var Promise = require('../../lib/utils/promises').Promise;
Expand Down Expand Up @@ -142,7 +142,7 @@ exports.ApplicationUpdateService = {
var removeCallback = function () {
// Ignore
};
FS.removeFile(FilterStorage.FILE_PATH, removeCallback, removeCallback);
RulesStorage.remove(FilterStorage.FILE_PATH, removeCallback, removeCallback);
updateDfd.resolve();
} else {
var filter = filters.shift();
Expand Down

0 comments on commit 6db2c08

Please sign in to comment.