Skip to content

Commit 46488a5

Browse files
committed
FilterEngine refactoring
Created Updater class for handling application updates and moved respective API from FilterEngine to Updater component. For now Updater class does not care about asynchronicity (if any) in JS files which are used for update checks logic. TODO: Move Updater class to dedicated .h and .cpp files. Abstracted jsSources from FilterEngine. Now every component (like FilterEngine and Updater) created by Platfrom gets EvaluateCallback and evaluates own JS files needed for internal logic. EvaluateCallback keeps track of evaluated files to avoid duplicated evaluations.
1 parent 007faaf commit 46488a5

6 files changed

Lines changed: 242 additions & 93 deletions

File tree

include/AdblockPlus/FilterEngine.h

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ namespace AdblockPlus
187187
* It handles:
188188
* - Filter management and matching.
189189
* - Subscription management and synchronization.
190-
* - Update checks for the application.
191190
*/
192191
class FilterEngine
193192
{
@@ -225,18 +224,6 @@ namespace AdblockPlus
225224
*/
226225
typedef int32_t ContentTypeMask;
227226

228-
/**
229-
* Callback type invoked when an update becomes available.
230-
* The parameter is the download URL of the update.
231-
*/
232-
typedef std::function<void(const std::string&)> UpdateAvailableCallback;
233-
234-
/**
235-
* Callback type invoked when a manually triggered update check finishes.
236-
* The parameter is an optional error message.
237-
*/
238-
typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;
239-
240227
/**
241228
* Callback type invoked when the filters change.
242229
* The first parameter is the action event code (see
@@ -296,6 +283,7 @@ namespace AdblockPlus
296283
* @param parameters optional creation parameters.
297284
*/
298285
static void CreateAsync(const JsEnginePtr& jsEngine,
286+
const JsEngine::EvaluateCallback& evaluateCallback,
299287
const OnCreatedCallback& onCreated,
300288
const CreationParameters& parameters = CreationParameters());
301289

@@ -476,32 +464,6 @@ namespace AdblockPlus
476464
*/
477465
std::string GetHostFromURL(const std::string& url) const;
478466

479-
/**
480-
* Sets the callback invoked when an application update becomes available.
481-
* @param callback Callback to invoke.
482-
*/
483-
void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);
484-
485-
/**
486-
* Removes the callback invoked when an application update becomes
487-
* available.
488-
*/
489-
void RemoveUpdateAvailableCallback();
490-
491-
/**
492-
* Forces an immediate update check.
493-
* `FilterEngine` will automatically check for updates in regular intervals,
494-
* so applications should only call this when the user triggers an update
495-
* check manually.
496-
* @param callback Optional callback to invoke when the update check is
497-
* finished. The string parameter will be empty when the update check
498-
* succeeded, or contain an error message if it failed.
499-
* Note that the callback will be invoked whether updates are
500-
* available or not - to react to updates being available, use
501-
* `FilterEngine::SetUpdateAvailableCallback()`.
502-
*/
503-
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());
504-
505467
/**
506468
* Sets the callback invoked when the filters change.
507469
* @param callback Callback to invoke.
@@ -558,7 +520,6 @@ namespace AdblockPlus
558520
private:
559521
JsEnginePtr jsEngine;
560522
bool firstRun;
561-
int updateCheckId;
562523
static const std::map<ContentType, std::string> contentTypes;
563524

564525
explicit FilterEngine(const JsEnginePtr& jsEngine);
@@ -573,6 +534,57 @@ namespace AdblockPlus
573534
ContentTypeMask contentTypeMask,
574535
const std::vector<std::string>& documentUrls) const;
575536
};
537+
538+
/**
539+
* Component of libadblockplus responsible for Update checks for the application.
540+
*/
541+
class Updater
542+
{
543+
public:
544+
explicit Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& callback);
545+
546+
/**
547+
* Callback type invoked when an update becomes available.
548+
* The parameter is the download URL of the update.
549+
*/
550+
typedef std::function<void(const std::string&)> UpdateAvailableCallback;
551+
552+
/**
553+
* Callback type invoked when a manually triggered update check finishes.
554+
* The parameter is an optional error message.
555+
*/
556+
typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;
557+
558+
/**
559+
* Sets the callback invoked when an application update becomes available.
560+
* @param callback Callback to invoke.
561+
*/
562+
void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);
563+
564+
/**
565+
* Removes the callback invoked when an application update becomes
566+
* available.
567+
*/
568+
void RemoveUpdateAvailableCallback();
569+
570+
/**
571+
* Forces an immediate update check.
572+
* `Updater` will automatically check for updates in regular intervals,
573+
* so applications should only call this when the user triggers an update
574+
* check manually.
575+
* @param callback Optional callback to invoke when the update check is
576+
* finished. The string parameter will be empty when the update check
577+
* succeeded, or contain an error message if it failed.
578+
* Note that the callback will be invoked whether updates are
579+
* available or not - to react to updates being available, use
580+
* `Updater::SetUpdateAvailableCallback()`.
581+
*/
582+
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());
583+
584+
private:
585+
JsEnginePtr jsEngine;
586+
int updateCheckId;
587+
};
576588
}
577589

578590
#endif

include/AdblockPlus/JsEngine.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <functional>
2222
#include <map>
2323
#include <list>
24+
#include <set>
2425
#include <stdexcept>
2526
#include <stdint.h>
2627
#include <string>
@@ -143,6 +144,12 @@ namespace AdblockPlus
143144
JsValue Evaluate(const std::string& source,
144145
const std::string& filename = "");
145146

147+
/**
148+
* Callback type for evaluating JS expression.
149+
* The parameter is the JS file name containing the expression.
150+
*/
151+
typedef std::function<void(const std::string&)> EvaluateCallback;
152+
146153
/**
147154
* Initiates a garbage collection.
148155
*/
@@ -293,6 +300,7 @@ namespace AdblockPlus
293300
std::mutex eventCallbacksMutex;
294301
JsWeakValuesLists jsWeakValuesLists;
295302
std::mutex jsWeakValuesListsMutex;
303+
std::set<std::string> evaluatedJsSources;
296304
};
297305
}
298306

include/AdblockPlus/Platform.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "FilterEngine.h"
2828
#include <mutex>
2929
#include <future>
30+
#include <set>
31+
#include <string>
3032

3133
namespace AdblockPlus
3234
{
@@ -108,6 +110,11 @@ namespace AdblockPlus
108110
*/
109111
FilterEngine& GetFilterEngine();
110112

113+
/**
114+
* Retrieves the Updater component instance.
115+
*/
116+
Updater& GetUpdater();
117+
111118
typedef std::function<void(ITimer&)> WithTimerCallback;
112119
virtual void WithTimer(const WithTimerCallback&);
113120

@@ -125,11 +132,15 @@ namespace AdblockPlus
125132
TimerPtr timer;
126133
FileSystemPtr fileSystem;
127134
WebRequestPtr webRequest;
135+
JsEngine::EvaluateCallback evaluateCallback;
128136
private:
129137
// used for creation and deletion of modules.
130138
std::mutex modulesMutex;
131139
std::shared_ptr<JsEngine> jsEngine;
132140
std::shared_future<FilterEnginePtr> filterEngine;
141+
std::shared_ptr<Updater> updater;
142+
std::set<std::string> evaluatedJsSources;
143+
JsEngine::EvaluateCallback GetEvaluateCallback();
133144
};
134145

135146
/**

0 commit comments

Comments
 (0)