Skip to content

Commit 7a6c647

Browse files
author
Krystian Zlomek
committed
Issue 1319,5450,1285 - 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. Updated unit tests. Signed-off-by: Krystian Zlomek [email protected]
1 parent 9fb9625 commit 7a6c647

File tree

14 files changed

+540
-110
lines changed

14 files changed

+540
-110
lines changed

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: 7 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
@@ -287,6 +274,12 @@ namespace AdblockPlus
287274
*/
288275
typedef std::function<void(const FilterEnginePtr&)> OnCreatedCallback;
289276

277+
/**
278+
* Callback type for evaluating JS expression.
279+
* The parameter is the JS file name containing the expression.
280+
*/
281+
typedef std::function<void(const std::string&)> EvaluateCallback;
282+
290283
/**
291284
* Asynchronously constructs FilterEngine.
292285
* @param jsEngine `JsEngine` instance used to run JavaScript code
@@ -296,6 +289,7 @@ namespace AdblockPlus
296289
* @param parameters optional creation parameters.
297290
*/
298291
static void CreateAsync(const JsEnginePtr& jsEngine,
292+
const EvaluateCallback& evaluateCallback,
299293
const OnCreatedCallback& onCreated,
300294
const CreationParameters& parameters = CreationParameters());
301295

@@ -476,32 +470,6 @@ namespace AdblockPlus
476470
*/
477471
std::string GetHostFromURL(const std::string& url) const;
478472

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-
505473
/**
506474
* Sets the callback invoked when the filters change.
507475
* @param callback Callback to invoke.
@@ -558,7 +526,6 @@ namespace AdblockPlus
558526
private:
559527
JsEnginePtr jsEngine;
560528
bool firstRun;
561-
int updateCheckId;
562529
static const std::map<ContentType, std::string> contentTypes;
563530

564531
explicit FilterEngine(const JsEnginePtr& jsEngine);

include/AdblockPlus/Platform.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
#include "AppInfo.h"
2626
#include "Scheduler.h"
2727
#include "FilterEngine.h"
28+
#include "Updater.h"
2829
#include <mutex>
2930
#include <future>
31+
#include <set>
32+
#include <string>
33+
#include <functional>
3034

3135
namespace AdblockPlus
3236
{
@@ -108,6 +112,11 @@ namespace AdblockPlus
108112
*/
109113
FilterEngine& GetFilterEngine();
110114

115+
/**
116+
* Retrieves the Updater component instance.
117+
*/
118+
Updater& GetUpdater();
119+
111120
typedef std::function<void(ITimer&)> WithTimerCallback;
112121
virtual void WithTimer(const WithTimerCallback&);
113122

@@ -130,6 +139,10 @@ namespace AdblockPlus
130139
std::mutex modulesMutex;
131140
std::shared_ptr<JsEngine> jsEngine;
132141
std::shared_future<FilterEnginePtr> filterEngine;
142+
std::shared_ptr<Updater> updater;
143+
std::set<std::string> evaluatedJsSources;
144+
std::mutex evaluatedJsSourcesMutex;
145+
std::function<void(const std::string&)> GetEvaluateCallback();
133146
};
134147

135148
/**

include/AdblockPlus/Updater.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
#include <AdblockPlus/JsValue.h>
25+
26+
namespace AdblockPlus
27+
{
28+
/**
29+
* Component of libadblockplus responsible for Update checks for the application.
30+
*/
31+
class Updater
32+
{
33+
public:
34+
/**
35+
* Callback type invoked when an update becomes available.
36+
* The parameter is the download URL of the update.
37+
*/
38+
typedef std::function<void(const std::string&)> UpdateAvailableCallback;
39+
40+
/**
41+
* Callback type invoked when a manually triggered update check finishes.
42+
* The parameter is an optional error message.
43+
*/
44+
typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;
45+
46+
/**
47+
* Sets the callback invoked when an application update becomes available.
48+
* @param callback Callback to invoke.
49+
*/
50+
void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);
51+
52+
/**
53+
* Removes the callback invoked when an application update becomes
54+
* available.
55+
*/
56+
void RemoveUpdateAvailableCallback();
57+
58+
/**
59+
* Callback type for evaluating JS expression.
60+
* The parameter is the JS file name containing the expression.
61+
*/
62+
typedef std::function<void(const std::string&)> EvaluateCallback;
63+
64+
/**
65+
* Forces an immediate update check.
66+
* `Updater` will automatically check for updates in regular intervals,
67+
* so applications should only call this when the user triggers an update
68+
* check manually.
69+
* @param callback Optional callback to invoke when the update check is
70+
* finished. The string parameter will be empty when the update check
71+
* succeeded, or contain an error message if it failed.
72+
* Note that the callback will be invoked whether updates are
73+
* available or not - to react to updates being available, use
74+
* `Updater::SetUpdateAvailableCallback()`.
75+
*/
76+
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());
77+
78+
/**
79+
* Retrieves a preference value.
80+
* @param pref Preference name.
81+
* @return Preference value, or `null` if it doesn't exist.
82+
*/
83+
JsValue GetPref(const std::string& pref) const;
84+
85+
/**
86+
* Sets a preference value.
87+
* @param pref Preference name.
88+
* @param value New value of the preference.
89+
*/
90+
void SetPref(const std::string& pref, const JsValue& value);
91+
92+
explicit Updater(const JsEnginePtr& jsEngine, const EvaluateCallback& callback);
93+
94+
private:
95+
JsEnginePtr jsEngine;
96+
int updateCheckId;
97+
};
98+
}
99+
100+
#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 {
@@ -186,6 +185,7 @@ let API = (() =>
186185
{
187186
Notification.markAsShown(id);
188187
},
188+
189189
checkFilterMatch(url, contentTypeMask, documentUrl)
190190
{
191191
let requestHost = extractHostFromURL(url);
@@ -211,11 +211,6 @@ let API = (() =>
211211
Prefs[pref] = value;
212212
},
213213

214-
forceUpdateCheck(eventName)
215-
{
216-
checkForUpdates(eventName ? _triggerEvent.bind(null, eventName) : null);
217-
},
218-
219214
getHostFromUrl(url)
220215
{
221216
return extractHostFromURL(url);

lib/apiUpdater.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 {Prefs} = require("prefs");
23+
const {checkForUpdates} = require("updater");
24+
25+
return {
26+
getPref(pref)
27+
{
28+
return Prefs[pref];
29+
},
30+
31+
setPref(pref, value)
32+
{
33+
Prefs[pref] = value;
34+
},
35+
36+
forceUpdateCheck(eventName)
37+
{
38+
checkForUpdates(eventName ? _triggerEvent.bind(null, eventName) : null);
39+
}
40+
};
41+
})();

lib/compat.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ XMLHttpRequest.prototype =
337337
for (let i = 0; i < list.length; i++)
338338
list[i].call(this, event);
339339
};
340+
341+
// #1319: Now FilterEngine and Updater are separated, so we want to
342+
// allow update requests no matter if subscriptions download requests
343+
// are allowed or not.
344+
if (this._url.includes("update.json"))
345+
{
346+
window._webRequest.GET(this._url, this._requestHeaders, onGetDone);
347+
return;
348+
}
349+
340350
// HACK (#5066): the code checking whether the connection is
341351
// allowed is temporary, the actual check should be in the core
342352
// when we make a decision whether to update a subscription with

lib/prefs.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ let Prefs = exports.Prefs = {
146146
}
147147
};
148148

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

154155
// Define defaults
155156
for (let key in defaults)

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',
@@ -160,6 +161,7 @@
160161
],
161162
'load_after_files': [
162163
'lib/api.js',
164+
'lib/apiUpdater.js',
163165
'lib/publicSuffixList.js',
164166
'lib/punycode.js',
165167
'lib/basedomain.js',

0 commit comments

Comments
 (0)