Skip to content

Commit

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

* commit '560b6b6f88d008c4f063345eaff9a020d0541d4d':
  #221 safari extensions notifications
  disable outline on buttons
  #223 safari extensions state screen
  #223 safari extensions state screen
  #223 safari extensions state screen
  #223 safari extensions state screen
  #223 safari extensions state screen
  #221 safari extensions notifications
  #223 safari extensions state screen
  #221 safari extensions notifications
  #223 safari extensions state screen
  add static
  • Loading branch information
Mizzick committed Sep 5, 2019
2 parents 58535b4 + 560b6b6 commit fe74912
Show file tree
Hide file tree
Showing 13 changed files with 660 additions and 175 deletions.
10 changes: 10 additions & 0 deletions ElectronMainApp/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
"LANGUAGE_SPECIFIC_ID": 7,
"CUSTOM_FILTERS_GROUP_ID": 0
},
"SafariExtensionBundles": {
"GENERAL": "com.adguard.safari.AdGuard.BlockerExtension",
"PRIVACY": "com.adguard.safari.AdGuard.BlockerPrivacy",
"SOCIAL_WIDGETS_AND_ANNOYANCES": "com.adguard.safari.AdGuard.BlockerSocial",
"SECURITY": "com.adguard.safari.AdGuard.BlockerSecurity",
"OTHER": "com.adguard.safari.AdGuard.BlockerOther",
"CUSTOM": "com.adguard.safari.AdGuard.BlockerCustom",
"ADVANCED_BLOCKING": "com.adguard.safari.AdGuard.AdvancedBlocking",
"ICON": "com.adguard.safari.AdGuard.Extension"
},
"backendUrl": "https://chrome.adtidy.org",
"backendApiKey": "4DDBE80A3DA94D819A00523252FB6380",
"filtersUrl": "https://filters.adtidy.org/extension/safari",
Expand Down
55 changes: 47 additions & 8 deletions ElectronMainApp/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@
"options_about": {
"message": "About"
},
"options_content_blockers": {
"message": "Content blockers"
},
"options_compare_btn": {
"message": "How they compare"
},
Expand All @@ -303,7 +306,7 @@
"message": "Github"
},
"onboarding_safari_desc": {
"message": "Check the box next to \"AdGuard Safari Icon\", \"AdGuard Advanced Blocking\" and \"AdGuard\" in Safari preferences"
"message": "Check the boxes next to AdGuard extensions in Safari preferences"
},
"onboarding_safari_btn": {
"message": "Open preferences"
Expand Down Expand Up @@ -344,16 +347,52 @@
"options_verbose_logging_desc": {
"message": "If Verbose logging is enabled, AdGuard Safari extensions will print additional information to the browser console"
},
"settings_notification_title": {
"message": "Attention! AdGuard extensions are disabled"
},
"settings_notification_text": {
"message": "You can use these extensions to manage AdGuard for Safari. For instance, pause blocking on a website, or block an ad manually. You can enable AdGuard Icon and Advanced Blocking <a href='#' id='notificationEnableIconLink'>here<\/a>."
},
"settings_notification_protection_title": {
"message": "Attention! Protection was disabled"
},
"settings_notification_protection_text": {
"message": "AdGuard protection is paused. <a href='#' id='notificationEnableProtectionLink'>Enable AdGuard<\/a>."
"message": "AdGuard protection is paused."
},
"settings_notification_protection_button_text": {
"message": "Fix it"
},
"settings_notification_extensions_button_text": {
"message": "Fix it"
},
"settings_notification_extensions_title": {
"message": "Some AdGuard extensions are disabled!"
},
"settings_notification_extensions_text": {
"message": "You should enable all AdGuard extensions for correctly ad blocking in Safari"
},
"settings_notification_cb_extensions_title": {
"message": "Some AdGuard content blocker extensions are disabled!"
},
"settings_notification_cb_extensions_text": {
"message": "You should enable all AdGuard content blocker extensions for correctly ad blocking in Safari"
},
"options_cb_general_title": {
"message": "AdGuard General"
},
"options_cb_privacy_title": {
"message": "AdGuard Privacy"
},
"options_cb_social_title": {
"message": "AdGuard Social"
},
"options_cb_security_title": {
"message": "AdGuard Security"
},
"options_cb_other_title": {
"message": "AdGuard Other"
},
"options_cb_custom_title": {
"message": "AdGuard Custom"
},
"options_cb_rules_info": {
"message": "%s rules used"
},
"options_cb_disabled_warning": {
"message": "Disabled in Safari"
}
}
41 changes: 15 additions & 26 deletions ElectronMainApp/safari-ext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const addon = require('bindings')('safari_ext_addon');
*/
module.exports = (() => {

const ADVANCED_BLOCKING_BUNDLE_ID = "com.adguard.safari.AdGuard.AdvancedBlocking";
const ICON_EXTENSION_BUNDLE_ID = "com.adguard.safari.AdGuard.Extension";

/**
* Initializes toolbar
*
Expand Down Expand Up @@ -134,30 +137,18 @@ module.exports = (() => {
};

/**
* Getting state of the content blocker extension.
* Returns true in callback if extension enabled, else returns false.
* @param callback = (enabled as bool) => {}
*/
const extensionContentBlockerState = (callback) => {
addon.extensionContentBlockerState(callback);
};

/**
* Getting state of the icon of Safari app extension.
* Returns true in callback if extension enabled, else returns false.
* @param callback = (enabled as bool) => {}
*/
const extensionSafariIconState = (callback) => {
addon.extensionSafariIconState(callback);
};

/**
* Getting state of Advanced Blocking Safari extension.
* Returns true in callback if extension enabled, else returns false.
* @param callback = (enabled as bool) => {}
* Get safari extensions states info
* @param bundleId extension bundle identifier
* @param callback {*}
*/
const extensionAdvancedBlockingState = (callback) => {
addon.extensionAdvancedBlockingState(callback);
const getExtensionState = (bundleId, callback) => {
if (bundleId === ADVANCED_BLOCKING_BUNDLE_ID) {
addon.extensionAdvancedBlockingState(callback);
} else if (bundleId === ICON_EXTENSION_BUNDLE_ID) {
addon.extensionSafariIconState(callback);
} else {
addon.getExtensionContentBlockerState(bundleId, callback);
}
};

/**
Expand Down Expand Up @@ -192,9 +183,7 @@ module.exports = (() => {
whitelistDomains: whitelistDomains,
setUserFilter: setUserFilter,
userFilter: userFilter,
extensionContentBlockerState: extensionContentBlockerState,
extensionSafariIconState: extensionSafariIconState,
extensionAdvancedBlockingState: extensionAdvancedBlockingState,
getExtensionState: getExtensionState,
openExtensionsPreferenses: openExtensionsPreferenses,
debugLog: debugLog,
setVerboseLogging: setVerboseLogging
Expand Down
29 changes: 16 additions & 13 deletions ElectronMainApp/safari-ext/src.mm
Original file line number Diff line number Diff line change
Expand Up @@ -399,19 +399,22 @@ static void AsyncSendHandler(uv_async_t *handle) {
}];
}

NAN_METHOD(extensionContentBlockerState){
NAN_METHOD(getExtensionContentBlockerState){

if (info.Length() < 1) {
ThrowTypeError("Wrong number of arguments");
return;
}
if (info.Length() < 2) {
ThrowTypeError("Wrong number of arguments");
return;
}

if (!info[0]->IsFunction()) {
ThrowTypeError("Wrong arguments");
return;
}
if (!info[0]->IsString() || !info[1]->IsFunction()) {
ThrowTypeError("Wrong arguments");
return;
}

Nan::Callback *cb = new Nan::Callback(info[0].As<Function>());
Nan::Utf8String message (info[0]);
NSString *bundleId = [NSString stringWithCString:*message encoding:NSUTF8StringEncoding];

Nan::Callback *cb = new Nan::Callback(info[1].As<Function>());

void (^resultBlock)(BOOL result) = ^void(BOOL result) {

Expand All @@ -425,7 +428,7 @@ static void AsyncSendHandler(uv_async_t *handle) {
});
};

[SFContentBlockerManager getStateOfContentBlockerWithIdentifier:AESharedResources.blockerBundleId
[SFContentBlockerManager getStateOfContentBlockerWithIdentifier:bundleId
completionHandler:^(SFContentBlockerState * _Nullable state, NSError * _Nullable error) {
resultBlock(error == nil && state.enabled);
}];
Expand Down Expand Up @@ -732,8 +735,8 @@ static void AsyncSendHandler(uv_async_t *handle) {
Nan::Set(target, New<String>("whitelistDomains").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(whitelistDomains)).ToLocalChecked());

Nan::Set(target, New<String>("extensionContentBlockerState").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(extensionContentBlockerState)).ToLocalChecked());
Nan::Set(target, New<String>("getExtensionContentBlockerState").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(getExtensionContentBlockerState)).ToLocalChecked());

Nan::Set(target, New<String>("extensionSafariIconState").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(extensionSafariIconState)).ToLocalChecked());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const config = require('config');
const listeners = require('../../notifier');
const events = require('../../events');
const settings = require('../settings-manager');
Expand All @@ -6,7 +7,7 @@ const {jsonFromFilters} = require('../libs/JSConverter');
const whitelist = require('../whitelist');
const log = require('../utils/log');
const concurrent = require('../utils/concurrent');
const {groupRules} = require('./rule-groups');
const {groupRules, rulesGroupsBundles} = require('./rule-groups');

/**
* Safari Content Blocker Adapter
Expand All @@ -29,19 +30,6 @@ module.exports = (function () {
}
];

/**
* Rules groups to extension bundle identifiers dictionary
*/
const rulesGroupsBundles = {
general: "com.adguard.safari.AdGuard.BlockerExtension",
privacy: "com.adguard.safari.AdGuard.BlockerPrivacy",
socialWidgetsAndAnnoyances: "com.adguard.safari.AdGuard.BlockerSocial",
security: "com.adguard.safari.AdGuard.BlockerSecurity",
other: "com.adguard.safari.AdGuard.BlockerOther",
custom: "com.adguard.safari.AdGuard.BlockerCustom",
advancedBlocking: "com.adguard.safari.AdGuard.AdvancedBlocking"
};

/**
* Load content blocker
*/
Expand All @@ -64,6 +52,13 @@ module.exports = (function () {
}

setSafariContentBlocker(rulesGroupsBundles[group.key], json);

listeners.notifyListeners(events.CONTENT_BLOCKER_EXTENSION_UPDATED, {
rulesCount: group.rules.length,
bundleId: rulesGroupsBundles[group.key],
overlimit: result && result.overLimit,
filterGroups: group.filterGroups
});
}

const advancedBlocking = setAdvancedBlocking(rules.map(x => x.ruleText));
Expand Down
44 changes: 42 additions & 2 deletions ElectronMainApp/src/main/app/content-blocker/rule-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = (function () {
const AntiBannerFilterGroupsId = config.get('AntiBannerFilterGroupsId');
const AntiBannerFiltersId = config.get('AntiBannerFiltersId');

const SafariExtensionBundles = config.get('SafariExtensionBundles');

/**
* Rules groups
*
Expand Down Expand Up @@ -62,6 +64,19 @@ module.exports = (function () {
all: [groups.general, groups.privacy, groups.security, groups.socialWidgetsAndAnnoyances, groups.other, groups.custom]
};

/**
* Rules groups to extension bundle identifiers dictionary
*/
const rulesGroupsBundles = {
general: SafariExtensionBundles.GENERAL,
privacy: SafariExtensionBundles.PRIVACY,
socialWidgetsAndAnnoyances: SafariExtensionBundles.SOCIAL_WIDGETS_AND_ANNOYANCES,
security: SafariExtensionBundles.SECURITY,
other: SafariExtensionBundles.OTHER,
custom: SafariExtensionBundles.CUSTOM,
advancedBlocking: SafariExtensionBundles.ADVANCED_BLOCKING
};

/**
* Groups provided rules
*
Expand Down Expand Up @@ -103,14 +118,16 @@ module.exports = (function () {
const result = [];
for (let groupName in groups) {
const key = groups[groupName].key;
const filterGroups = groups[groupName].filterGroups;

if (rulesByAffinityBlocks[key]) {
rulesByGroup[key] = rulesByGroup[key].concat(rulesByAffinityBlocks[key]);
}

result.push({
key: key,
rules: rulesByGroup[key]
rules: rulesByGroup[key],
filterGroups: filterGroups
});

log.info(`Affinity block ${key}: rules length: ${rulesByGroup[key].length}`);
Expand Down Expand Up @@ -180,9 +197,32 @@ module.exports = (function () {
return result;
};

let bundleGroups;
const filterGroupsBundles = () => {
if (bundleGroups) {
return bundleGroups;
}

const result = [];

for (let key in rulesGroupsBundles) {
if (groups[key]) {
result.push({
bundleId: rulesGroupsBundles[key],
groupIds: groups[key].filterGroups
});
}
}

bundleGroups = result;
return result;
};

return {
groupRules,
groups
groups,
rulesGroupsBundles,
filterGroupsBundles
};

})();
1 change: 1 addition & 0 deletions ElectronMainApp/src/main/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const EventNotifierTypes = module.exports = {
NOTIFY_UPDATE_USER_FILTER_RULES: "event.notify.update.user.filter.rules",
UPDATE_WHITELIST_FILTER_RULES: "event.update.whitelist.filter.rules",
CONTENT_BLOCKER_UPDATED: "event.content.blocker.updated",
CONTENT_BLOCKER_EXTENSION_UPDATED: "event.content.blocker.extension.updated",
CONTENT_BLOCKER_UPDATE_REQUIRED: "event.content.blocker.update.required",
SHOW_OPTIONS_FILTERS_TAB: "event.show.options.filters",
SHOW_OPTIONS_USER_FILTER_TAB: "event.show.options.user.filter",
Expand Down
7 changes: 4 additions & 3 deletions ElectronMainApp/src/main/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const log = require('./app/utils/log');
const contentBlockerListener = require('./app/content-blocker/content-blocker-listener');
const notificationController = require('./notification-controller');
const safariToolbar = require('safari-ext');
const toolbarController = require('./toolbar-controller');

/**
* Application startup
Expand Down Expand Up @@ -45,9 +46,9 @@ module.exports = (() => {
safariToolbar.sendReady();

// Check safari extensions
safariToolbar.extensionContentBlockerState((result) => {
if (!result) {
log.warn('Safari extension content blocker is turned off!');
toolbarController.getExtensionsState((result) => {
if (!result || !result.contentBlockersEnabled) {
log.warn('Safari content blockers are turned off!');

showWindow();
}
Expand Down
Loading

0 comments on commit fe74912

Please sign in to comment.