Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring task #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/AdblockPlus.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <AdblockPlus/AppInfo.h>
#include <AdblockPlus/FilterEngine.h>
#include <AdblockPlus/Updater.h>
#include <AdblockPlus/LogSystem.h>
#include <AdblockPlus/JsEngine.h>
#include <AdblockPlus/JsValue.h>
Expand Down
47 changes: 7 additions & 40 deletions include/AdblockPlus/FilterEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ namespace AdblockPlus
* It handles:
* - Filter management and matching.
* - Subscription management and synchronization.
* - Update checks for the application.
*/
class FilterEngine
{
Expand Down Expand Up @@ -225,18 +224,6 @@ namespace AdblockPlus
*/
typedef int32_t ContentTypeMask;

/**
* Callback type invoked when an update becomes available.
* The parameter is the download URL of the update.
*/
typedef std::function<void(const std::string&)> UpdateAvailableCallback;

/**
* Callback type invoked when a manually triggered update check finishes.
* The parameter is an optional error message.
*/
typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;

/**
* Callback type invoked when the filters change.
* The first parameter is the action event code (see
Expand Down Expand Up @@ -287,6 +274,12 @@ namespace AdblockPlus
*/
typedef std::function<void(const FilterEnginePtr&)> OnCreatedCallback;

/**
* Callback type for evaluating JS expression.
* The parameter is the JS file name containing the expression.
*/
typedef std::function<void(const std::string&)> EvaluateCallback;

/**
* Asynchronously constructs FilterEngine.
* @param jsEngine `JsEngine` instance used to run JavaScript code
Expand All @@ -296,6 +289,7 @@ namespace AdblockPlus
* @param parameters optional creation parameters.
*/
static void CreateAsync(const JsEnginePtr& jsEngine,
const EvaluateCallback& evaluateCallback,
const OnCreatedCallback& onCreated,
const CreationParameters& parameters = CreationParameters());

Expand Down Expand Up @@ -476,32 +470,6 @@ namespace AdblockPlus
*/
std::string GetHostFromURL(const std::string& url) const;

/**
* Sets the callback invoked when an application update becomes available.
* @param callback Callback to invoke.
*/
void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);

/**
* Removes the callback invoked when an application update becomes
* available.
*/
void RemoveUpdateAvailableCallback();

/**
* Forces an immediate update check.
* `FilterEngine` will automatically check for updates in regular intervals,
* so applications should only call this when the user triggers an update
* check manually.
* @param callback Optional callback to invoke when the update check is
* finished. The string parameter will be empty when the update check
* succeeded, or contain an error message if it failed.
* Note that the callback will be invoked whether updates are
* available or not - to react to updates being available, use
* `FilterEngine::SetUpdateAvailableCallback()`.
*/
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());

/**
* Sets the callback invoked when the filters change.
* @param callback Callback to invoke.
Expand Down Expand Up @@ -558,7 +526,6 @@ namespace AdblockPlus
private:
JsEnginePtr jsEngine;
bool firstRun;
int updateCheckId;
static const std::map<ContentType, std::string> contentTypes;

explicit FilterEngine(const JsEnginePtr& jsEngine);
Expand Down
13 changes: 13 additions & 0 deletions include/AdblockPlus/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
#include "AppInfo.h"
#include "Scheduler.h"
#include "FilterEngine.h"
#include "Updater.h"
#include <mutex>
#include <future>
#include <set>
#include <string>
#include <functional>

namespace AdblockPlus
{
Expand Down Expand Up @@ -108,6 +112,11 @@ namespace AdblockPlus
*/
FilterEngine& GetFilterEngine();

/**
* Retrieves the Updater component instance.
*/
Updater& GetUpdater();

typedef std::function<void(ITimer&)> WithTimerCallback;
virtual void WithTimer(const WithTimerCallback&);

Expand All @@ -130,6 +139,10 @@ namespace AdblockPlus
std::mutex modulesMutex;
std::shared_ptr<JsEngine> jsEngine;
std::shared_future<FilterEnginePtr> filterEngine;
std::shared_ptr<Updater> updater;
std::set<std::string> evaluatedJsSources;
std::mutex evaluatedJsSourcesMutex;
std::function<void(const std::string&)> GetEvaluateCallback();
};

/**
Expand Down
100 changes: 100 additions & 0 deletions include/AdblockPlus/Updater.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ADBLOCK_PLUS_UPDATER_H
#define ADBLOCK_PLUS_UPDATER_H

#include <functional>
#include <string>
#include <AdblockPlus/JsEngine.h>
#include <AdblockPlus/JsValue.h>

namespace AdblockPlus
{
/**
* Component of libadblockplus responsible for Update checks for the application.
*/
class Updater
{
public:
/**
* Callback type invoked when an update becomes available.
* The parameter is the download URL of the update.
*/
typedef std::function<void(const std::string&)> UpdateAvailableCallback;

/**
* Callback type invoked when a manually triggered update check finishes.
* The parameter is an optional error message.
*/
typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;

/**
* Sets the callback invoked when an application update becomes available.
* @param callback Callback to invoke.
*/
void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);

/**
* Removes the callback invoked when an application update becomes
* available.
*/
void RemoveUpdateAvailableCallback();

/**
* Callback type for evaluating JS expression.
* The parameter is the JS file name containing the expression.
*/
typedef std::function<void(const std::string&)> EvaluateCallback;

/**
* Forces an immediate update check.
* `Updater` will automatically check for updates in regular intervals,
* so applications should only call this when the user triggers an update
* check manually.
* @param callback Optional callback to invoke when the update check is
* finished. The string parameter will be empty when the update check
* succeeded, or contain an error message if it failed.
* Note that the callback will be invoked whether updates are
* available or not - to react to updates being available, use
* `Updater::SetUpdateAvailableCallback()`.
*/
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());

/**
* Retrieves a preference value.
* @param pref Preference name.
* @return Preference value, or `null` if it doesn't exist.
*/
JsValue GetPref(const std::string& pref) const;

/**
* Sets a preference value.
* @param pref Preference name.
* @param value New value of the preference.
*/
void SetPref(const std::string& pref, const JsValue& value);

explicit Updater(const JsEnginePtr& jsEngine, const EvaluateCallback& callback);

private:
JsEnginePtr jsEngine;
int updateCheckId;
};
}

#endif
7 changes: 1 addition & 6 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ let API = (() =>
const {ElemHide} = require("elemHide");
const {Synchronizer} = require("synchronizer");
const {Prefs} = require("prefs");
const {checkForUpdates} = require("updater");
const {Notification} = require("notification");

return {
Expand Down Expand Up @@ -188,6 +187,7 @@ let API = (() =>
{
Notification.markAsShown(id);
},

checkFilterMatch(url, contentTypeMask, documentUrl)
{
let requestHost = extractHostFromURL(url);
Expand All @@ -213,11 +213,6 @@ let API = (() =>
Prefs[pref] = value;
},

forceUpdateCheck(eventName)
{
checkForUpdates(eventName ? _triggerEvent.bind(null, eventName) : null);
},

getHostFromUrl(url)
{
return extractHostFromURL(url);
Expand Down
41 changes: 41 additions & 0 deletions lib/apiUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/

"use strict";

let API_UPDATER = (() =>
{
const {Prefs} = require("prefs");
const {checkForUpdates} = require("updater");

return {
getPref(pref)
{
return Prefs[pref];
},

setPref(pref, value)
{
Prefs[pref] = value;
},

forceUpdateCheck(eventName)
{
checkForUpdates(eventName ? _triggerEvent.bind(null, eventName) : null);
}
};
})();
7 changes: 7 additions & 0 deletions lib/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ XMLHttpRequest.prototype =
for (let i = 0; i < list.length; i++)
list[i].call(this, event);
};

if (this._url.includes("update.json"))
{
window._webRequest.GET(this._url, this._requestHeaders, onGetDone);
return;
}

// HACK (#5066): the code checking whether the connection is
// allowed is temporary, the actual check should be in the core
// when we make a decision whether to update a subscription with
Expand Down
9 changes: 5 additions & 4 deletions lib/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ let Prefs = exports.Prefs = {
}
};

// Update the default prefs with what was preconfigured
for (let key in _preconfiguredPrefs)
if (preconfigurable.indexOf(key) != -1)
defaults[key] = _preconfiguredPrefs[key];
if (typeof _preconfiguredPrefs !== "undefined")
// Update the default prefs with what was preconfigured
for (let key in _preconfiguredPrefs)
if (preconfigurable.indexOf(key) != -1)
defaults[key] = _preconfiguredPrefs[key];

// Define defaults
for (let key in defaults)
Expand Down
5 changes: 4 additions & 1 deletion libadblockplus.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'src/DefaultWebRequest.cpp',
'src/FileSystemJsObject.cpp',
'src/FilterEngine.cpp',
'src/Updater.cpp',
'src/GlobalJsObject.cpp',
'src/JsContext.cpp',
'src/JsEngine.cpp',
Expand Down Expand Up @@ -159,6 +160,7 @@
],
'load_after_files': [
'lib/api.js',
'lib/apiUpdater.js',
'lib/publicSuffixList.js',
'lib/punycode.js',
'lib/basedomain.js',
Expand Down Expand Up @@ -207,7 +209,8 @@
'test/Prefs.cpp',
'test/ReferrerMapping.cpp',
'test/UpdateCheck.cpp',
'test/WebRequest.cpp'
'test/WebRequest.cpp',
'test/UpdaterAndFilterEngineCreation.cpp'
],
'msvs_settings': {
'VCLinkerTool': {
Expand Down
Loading