Skip to content

Commit

Permalink
Merge pull request #203 in EXTENSIONS/safari-app-extension from featu…
Browse files Browse the repository at this point in the history
…re/233 to master

* commit '1e365fc42cbab2d5e2eea49a8760f1783628ffd3':
  #233 electron remote fork
  #233 rules storage cache
  #233 converter improvements - electron remote
  #233 converter improvements - electron remote
  #233 converter improvements
  #233 rules storage cache
  #233 performance
  • Loading branch information
Mizzick committed Oct 3, 2019
2 parents bb31e04 + 1e365fc commit 707fe25
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 79 deletions.
5 changes: 5 additions & 0 deletions AdGuard/build-electron-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ else
yarn upgrade --force -P safari-ext || exit 1
fi

# Compile electron-remote
cd "node_modules/electron-remote"
yarn install
cd ../..

# Rebuild safari-ext and other node packages
yarn electron-rebuild

Expand Down
3 changes: 2 additions & 1 deletion ElectronMainApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"electron-debug": "^2.0.0",
"electron-log": "^3.0.6",
"electron-reload": "^1.3.0",
"electron-remote": "git+https://github.com/AdguardTeam/electron-remote.git#18c5b1d",
"electron-simple-updater": "^1.5.0",
"electron-store": "^2.0.0",
"electron-store": "^5.0.0",
"electron-updater": "^4.0.6",
"filters-downloader": "git+https://github.com/AdguardTeam/FiltersDownloader.git#a7d2177",
"i18n": "^0.8.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const listeners = require('../../notifier');
const events = require('../../events');
const settings = require('../settings-manager');
const antibanner = require('../antibanner');
const {jsonFromFilters} = require('../libs/JSConverter');
const whitelist = require('../whitelist');
const log = require('../utils/log');
const concurrent = require('../utils/concurrent');
const {groupRules, rulesGroupsBundles, filterGroupsBundles} = require('./rule-groups');
const {requireTaskPool} = require('electron-remote');

/**
* Safari Content Blocker Adapter
Expand Down Expand Up @@ -35,15 +35,16 @@ module.exports = (function () {
*/
const updateContentBlocker = () => {

loadRules(rules => {
loadRules(async rules => {

const grouped = groupRules(rules);
let overlimit = false;

for (let group of grouped) {
let json = emptyBlockerJSON;

const result = jsonFromFilters(group.rules.map(x => x.ruleText), RULES_LIMIT, false, false);
const rulesTexts = group.rules.map(x => x.ruleText);
const result = await jsonFromRules(rulesTexts, false);
if (result && result.converted) {
json = JSON.parse(result.converted);
if (result.overLimit) {
Expand All @@ -62,7 +63,7 @@ module.exports = (function () {
setSafariContentBlocker(rulesGroupsBundles[group.key], json, info);
}

const advancedBlocking = setAdvancedBlocking(rules.map(x => x.ruleText));
const advancedBlocking = await setAdvancedBlocking(rules.map(x => x.ruleText));

listeners.notifyListeners(events.CONTENT_BLOCKER_UPDATED, {
rulesCount: rules.length,
Expand All @@ -73,14 +74,27 @@ module.exports = (function () {
});
};

/**
* Runs converter method for rules
*
* @param rules array of rules
* @param advancedBlocking if we need advanced blocking content
*/
const jsonFromRules = async (rules, advancedBlocking) => {
const converterModule = requireTaskPool(require.resolve('../libs/JSConverter'));

const result = await converterModule.jsonFromFilters(rules, RULES_LIMIT, false, advancedBlocking);
return result;
};

/**
* Activates advanced blocking json
*
* @param rules
* @return {Array}
*/
const setAdvancedBlocking = (rules) => {
const result = jsonFromFilters(rules, RULES_LIMIT, false, true);
const setAdvancedBlocking = async (rules) => {
const result = await jsonFromRules(rules, true);
const advancedBlocking = result ? JSON.parse(result.advancedBlocking) : [];

setSafariContentBlocker(rulesGroupsBundles["advancedBlocking"], advancedBlocking);
Expand Down
2 changes: 1 addition & 1 deletion ElectronMainApp/src/main/app/filters/filter-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = (() => {
* Saves updated filter rules to the storage.
*
* @param filterId Filter id
* @param events Events (what has changed?)
* @param eventsToProcess Events (what has changed?)
* @private
*/
const processSaveFilterRulesToStorageEvents = (filterId, eventsToProcess) => {
Expand Down
15 changes: 14 additions & 1 deletion ElectronMainApp/src/main/app/storage/rules-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,38 @@ const store = new Store();

/**
* Filter rules storage implementation
* TODO: Look for faster and better implementation
*/
module.exports = (() => {

const cache = Object.create(null);

const getKey = (path) => {
return 'filter_' + path;
};

const read = (path, callback) => {
const cached = cache[path];
if (cached) {
callback(cached);
return;
}

const lines = store.get(getKey(path));
cache[path] = lines;

callback(lines);
};

const write = (path, data, callback) => {
cache[path] = data;

store.set(getKey(path), data);
callback();
};

const remove = (path, successCallback) => {
delete cache[path];

store.delete(getKey(path));
successCallback();
};
Expand Down
2 changes: 1 addition & 1 deletion ElectronMainApp/src/main/app/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const logImpl = require('electron-log');
module.exports = (() => {

// Redefine if you need it
const CURRENT_LEVEL = "DEBUG";
const CURRENT_LEVEL = "INFO";

const LEVELS = {
ERROR: 1,
Expand Down
Loading

0 comments on commit 707fe25

Please sign in to comment.