Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
41 changes: 1 addition & 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 @@ -296,6 +283,7 @@ namespace AdblockPlus
* @param parameters optional creation parameters.
*/
static void CreateAsync(const JsEnginePtr& jsEngine,
const JsEngine::EvaluateCallback& evaluateCallback,
const OnCreatedCallback& onCreated,
const CreationParameters& parameters = CreationParameters());

Expand Down Expand Up @@ -476,32 +464,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 +520,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
6 changes: 6 additions & 0 deletions include/AdblockPlus/JsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ namespace AdblockPlus
JsValue Evaluate(const std::string& source,
const std::string& filename = "");

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually unrelated to JsEngine, it's rather an interface of FilterEngine and Updater, therefore IMO it would be better to define it there, thus two separate typedefs in both FilterEngine and Updater.


/**
* Initiates a garbage collection.
*/
Expand Down
12 changes: 12 additions & 0 deletions include/AdblockPlus/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
#include "AppInfo.h"
#include "Scheduler.h"
#include "FilterEngine.h"
#include "Updater.h"
#include <mutex>
#include <future>
#include <set>
#include <string>

namespace AdblockPlus
{
Expand Down Expand Up @@ -108,6 +111,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 @@ -125,11 +133,15 @@ namespace AdblockPlus
TimerPtr timer;
FileSystemPtr fileSystem;
WebRequestPtr webRequest;
JsEngine::EvaluateCallback evaluateCallback;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unused, could you please remove it?

private:
// used for creation and deletion of modules.
std::mutex modulesMutex;
std::shared_ptr<JsEngine> jsEngine;
std::shared_future<FilterEnginePtr> filterEngine;
std::shared_ptr<Updater> updater;
std::set<std::string> evaluatedJsSources;
JsEngine::EvaluateCallback GetEvaluateCallback();
};

/**
Expand Down
94 changes: 94 additions & 0 deletions include/AdblockPlus/Updater.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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:
explicit Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& callback);

/**
* 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();

/**
* 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);

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
42 changes: 42 additions & 0 deletions lib/apiUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems Services is not used here, could you please remove it?

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);
}
};
})();
2 changes: 2 additions & 0 deletions 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
Loading