Skip to content

Commit 540ca04

Browse files
committed
WIP: FilterEngine and Updater JS api separation
Moved Updater class to dedicated header and source files. Created apiUpdater.js with a separate API for Updater component. Issues to fix: 1. Pass Prefs to Updater component - Now Updater sets just an empty Prefs object 2. Review content of apiUpdater.js and updaterJsFiles[] array - Check that all what's needed is set there and nothing is redundant - Does Updater component need to take care of asynchronicity in JS files? 3. Fix UpdateCheckTests - They are now passing just because FilterEngine object is created while those tests should just rely only on Updater object.
1 parent 46488a5 commit 540ca04

10 files changed

Lines changed: 220 additions & 140 deletions

File tree

include/AdblockPlus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <AdblockPlus/AppInfo.h>
3434
#include <AdblockPlus/FilterEngine.h>
35+
#include <AdblockPlus/Updater.h>
3536
#include <AdblockPlus/LogSystem.h>
3637
#include <AdblockPlus/JsEngine.h>
3738
#include <AdblockPlus/JsValue.h>

include/AdblockPlus/FilterEngine.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -534,57 +534,6 @@ namespace AdblockPlus
534534
ContentTypeMask contentTypeMask,
535535
const std::vector<std::string>& documentUrls) const;
536536
};
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-
};
588537
}
589538

590539
#endif

include/AdblockPlus/Platform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "AppInfo.h"
2626
#include "Scheduler.h"
2727
#include "FilterEngine.h"
28+
#include "Updater.h"
2829
#include <mutex>
2930
#include <future>
3031
#include <set>

include/AdblockPlus/Updater.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This file is part of Adblock Plus <https://adblockplus.org/>,
3+
* Copyright (C) 2006-present eyeo GmbH
4+
*
5+
* Adblock Plus is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License version 3 as
7+
* published by the Free Software Foundation.
8+
*
9+
* Adblock Plus is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef ADBLOCK_PLUS_UPDATER_H
19+
#define ADBLOCK_PLUS_UPDATER_H
20+
21+
#include <functional>
22+
#include <string>
23+
#include <AdblockPlus/JsEngine.h>
24+
25+
namespace AdblockPlus
26+
{
27+
/**
28+
* Component of libadblockplus responsible for Update checks for the application.
29+
*/
30+
class Updater
31+
{
32+
public:
33+
explicit Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& callback);
34+
35+
/**
36+
* Callback type invoked when an update becomes available.
37+
* The parameter is the download URL of the update.
38+
*/
39+
typedef std::function<void(const std::string&)> UpdateAvailableCallback;
40+
41+
/**
42+
* Callback type invoked when a manually triggered update check finishes.
43+
* The parameter is an optional error message.
44+
*/
45+
typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;
46+
47+
/**
48+
* Sets the callback invoked when an application update becomes available.
49+
* @param callback Callback to invoke.
50+
*/
51+
void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);
52+
53+
/**
54+
* Removes the callback invoked when an application update becomes
55+
* available.
56+
*/
57+
void RemoveUpdateAvailableCallback();
58+
59+
/**
60+
* Forces an immediate update check.
61+
* `Updater` will automatically check for updates in regular intervals,
62+
* so applications should only call this when the user triggers an update
63+
* check manually.
64+
* @param callback Optional callback to invoke when the update check is
65+
* finished. The string parameter will be empty when the update check
66+
* succeeded, or contain an error message if it failed.
67+
* Note that the callback will be invoked whether updates are
68+
* available or not - to react to updates being available, use
69+
* `Updater::SetUpdateAvailableCallback()`.
70+
*/
71+
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());
72+
73+
private:
74+
JsEnginePtr jsEngine;
75+
int updateCheckId;
76+
};
77+
}
78+
79+
#endif

lib/api.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ let API = (() =>
2828
const {ElemHide} = require("elemHide");
2929
const {Synchronizer} = require("synchronizer");
3030
const {Prefs} = require("prefs");
31-
const {checkForUpdates} = require("updater");
3231
const {Notification} = require("notification");
3332

3433
return {
@@ -188,6 +187,7 @@ let API = (() =>
188187
{
189188
Notification.markAsShown(id);
190189
},
190+
191191
checkFilterMatch(url, contentTypeMask, documentUrl)
192192
{
193193
let requestHost = extractHostFromURL(url);
@@ -213,11 +213,6 @@ let API = (() =>
213213
Prefs[pref] = value;
214214
},
215215

216-
forceUpdateCheck(eventName)
217-
{
218-
checkForUpdates(eventName ? _triggerEvent.bind(null, eventName) : null);
219-
},
220-
221216
getHostFromUrl(url)
222217
{
223218
return extractHostFromURL(url);

lib/apiUpdater.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of Adblock Plus <https://adblockplus.org/>,
3+
* Copyright (C) 2006-present eyeo GmbH
4+
*
5+
* Adblock Plus is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License version 3 as
7+
* published by the Free Software Foundation.
8+
*
9+
* Adblock Plus is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
"use strict";
19+
20+
let API_UPDATER = (() =>
21+
{
22+
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
23+
const {Prefs} = require("prefs");
24+
const {checkForUpdates} = require("updater");
25+
26+
return {
27+
getPref(pref)
28+
{
29+
return Prefs[pref];
30+
},
31+
32+
setPref(pref, value)
33+
{
34+
Prefs[pref] = value;
35+
},
36+
37+
forceUpdateCheck(eventName)
38+
{
39+
checkForUpdates(eventName ? _triggerEvent.bind(null, eventName) : null);
40+
},
41+
42+
getHostFromUrl(url)
43+
{
44+
return extractHostFromURL(url);
45+
},
46+
47+
compareVersions(v1, v2)
48+
{
49+
return Services.vc.compare(v1, v2);
50+
}
51+
};
52+
})();

libadblockplus.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'src/DefaultWebRequest.cpp',
4545
'src/FileSystemJsObject.cpp',
4646
'src/FilterEngine.cpp',
47+
'src/Updater.cpp',
4748
'src/GlobalJsObject.cpp',
4849
'src/JsContext.cpp',
4950
'src/JsEngine.cpp',
@@ -159,6 +160,7 @@
159160
],
160161
'load_after_files': [
161162
'lib/api.js',
163+
'lib/apiUpdater.js',
162164
'lib/publicSuffixList.js',
163165
'lib/punycode.js',
164166
'lib/basedomain.js',

src/FilterEngine.cpp

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,15 @@
2323
#include <thread>
2424

2525
#include <AdblockPlus.h>
26+
#include "Utils.h"
2627
#include "JsContext.h"
2728
#include "Thread.h"
2829
#include <mutex>
2930
#include <condition_variable>
3031

31-
#define ArraySize(a) (sizeof(a) / sizeof(a[0]))
32-
3332
using namespace AdblockPlus;
3433

3534
namespace {
36-
37-
/*
38-
* TODO: Clarify JS dependencies for FilterEngine and Updater
39-
* so both are using only required JS files.
40-
* Now both tables are the same with full list of JS files.
41-
*/
42-
4335
static std::string filterEngineJsFiles[] = {
4436
"compat.js",
4537
"info.js",
@@ -65,39 +57,6 @@ namespace {
6557
"synchronizer.js",
6658
"filterUpdateRegistration.js",
6759
"subscriptions.xml",
68-
"updater.js",
69-
"api.js",
70-
"publicSuffixList.js",
71-
"punycode.js",
72-
"basedomain.js"
73-
};
74-
75-
static std::string updaterJsFiles[] = {
76-
"compat.js",
77-
"info.js",
78-
"io.js",
79-
"prefs.js",
80-
"utils.js",
81-
"elemHideHitRegistration.js",
82-
"events.js",
83-
"coreUtils.js",
84-
"filterNotifier.js",
85-
"init.js",
86-
"common.js",
87-
"filterClasses.js",
88-
"subscriptionClasses.js",
89-
"filterStorage.js",
90-
"elemHide.js",
91-
"elemHideEmulation.js",
92-
"matcher.js",
93-
"filterListener.js",
94-
"downloader.js",
95-
"notification.js",
96-
"notificationShowRegistration.js",
97-
"synchronizer.js",
98-
"filterUpdateRegistration.js",
99-
"subscriptions.xml",
100-
"updater.js",
10160
"api.js",
10261
"publicSuffixList.js",
10362
"punycode.js",
@@ -640,44 +599,3 @@ FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
640599
while (urlIterator != documentUrls.end());
641600
return FilterPtr();
642601
}
643-
644-
645-
Updater::Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& evaluateCallback)
646-
: jsEngine(jsEngine), updateCheckId(0)
647-
{
648-
// Load adblockplus scripts
649-
for(size_t i = 0; i < ArraySize(updaterJsFiles); ++i)
650-
evaluateCallback(updaterJsFiles[i]);
651-
}
652-
653-
void Updater::SetUpdateAvailableCallback(const Updater::UpdateAvailableCallback& callback)
654-
{
655-
jsEngine->SetEventCallback("updateAvailable", [this, callback](JsValueList&& params)
656-
{
657-
if (params.size() >= 1 && !params[0].IsNull())
658-
callback(params[0].AsString());
659-
});
660-
}
661-
662-
void Updater::RemoveUpdateAvailableCallback()
663-
{
664-
jsEngine->RemoveEventCallback("updateAvailable");
665-
}
666-
667-
void Updater::ForceUpdateCheck(const Updater::UpdateCheckDoneCallback& callback)
668-
{
669-
JsValue func = jsEngine->Evaluate("API.forceUpdateCheck");
670-
JsValueList params;
671-
if (callback)
672-
{
673-
std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId);
674-
jsEngine->SetEventCallback(eventName, [this, eventName, callback](JsValueList&& params)
675-
{
676-
std::string error(params.size() >= 1 && !params[0].IsNull() ? params[0].AsString() : "");
677-
callback(error);
678-
jsEngine->RemoveEventCallback(eventName);
679-
});
680-
params.push_back(jsEngine->NewValue(eventName));
681-
}
682-
func.Call(params);
683-
}

0 commit comments

Comments
 (0)