Skip to content

Commit 49f0171

Browse files
committed
Addressed review comments
Apart from changes in new code, this also withdraws changes in existing code not related with this feature.
1 parent 7d6bdda commit 49f0171

11 files changed

Lines changed: 54 additions & 53 deletions

File tree

include/AdblockPlus/FilterEngine.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ namespace AdblockPlus
274274
*/
275275
typedef std::function<void(const FilterEnginePtr&)> OnCreatedCallback;
276276

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+
277283
/**
278284
* Asynchronously constructs FilterEngine.
279285
* @param jsEngine `JsEngine` instance used to run JavaScript code
@@ -283,7 +289,7 @@ namespace AdblockPlus
283289
* @param parameters optional creation parameters.
284290
*/
285291
static void CreateAsync(const JsEnginePtr& jsEngine,
286-
const JsEngine::EvaluateCallback& evaluateCallback,
292+
const EvaluateCallback& evaluateCallback,
287293
const OnCreatedCallback& onCreated,
288294
const CreationParameters& parameters = CreationParameters());
289295

include/AdblockPlus/JsEngine.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,6 @@ namespace AdblockPlus
143143
JsValue Evaluate(const std::string& source,
144144
const std::string& filename = "");
145145

146-
/**
147-
* Callback type for evaluating JS expression.
148-
* The parameter is the JS file name containing the expression.
149-
*/
150-
typedef std::function<void(const std::string&)> EvaluateCallback;
151-
152146
/**
153147
* Initiates a garbage collection.
154148
*/

include/AdblockPlus/Platform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <future>
3131
#include <set>
3232
#include <string>
33+
#include <functional>
3334

3435
namespace AdblockPlus
3536
{
@@ -133,15 +134,14 @@ namespace AdblockPlus
133134
TimerPtr timer;
134135
FileSystemPtr fileSystem;
135136
WebRequestPtr webRequest;
136-
JsEngine::EvaluateCallback evaluateCallback;
137137
private:
138138
// used for creation and deletion of modules.
139139
std::mutex modulesMutex;
140140
std::shared_ptr<JsEngine> jsEngine;
141141
std::shared_future<FilterEnginePtr> filterEngine;
142142
std::shared_ptr<Updater> updater;
143143
std::set<std::string> evaluatedJsSources;
144-
JsEngine::EvaluateCallback GetEvaluateCallback();
144+
std::function<void(const std::string&)> GetEvaluateCallback();
145145
};
146146

147147
/**

include/AdblockPlus/Updater.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ namespace AdblockPlus
3131
class Updater
3232
{
3333
public:
34-
explicit Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& callback);
35-
3634
/**
3735
* Callback type invoked when an update becomes available.
3836
* The parameter is the download URL of the update.
@@ -57,6 +55,12 @@ namespace AdblockPlus
5755
*/
5856
void RemoveUpdateAvailableCallback();
5957

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+
6064
/**
6165
* Forces an immediate update check.
6266
* `Updater` will automatically check for updates in regular intervals,
@@ -85,6 +89,8 @@ namespace AdblockPlus
8589
*/
8690
void SetPref(const std::string& pref, const JsValue& value);
8791

92+
explicit Updater(const JsEnginePtr& jsEngine, const EvaluateCallback& callback);
93+
8894
private:
8995
JsEnginePtr jsEngine;
9096
int updateCheckId;

lib/apiUpdater.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
let API_UPDATER = (() =>
2121
{
22-
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
2322
const {Prefs} = require("prefs");
2423
const {checkForUpdates} = require("updater");
2524

lib/compat.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ XMLHttpRequest.prototype =
333333
for (let i = 0; i < list.length; i++)
334334
list[i].call(this, event);
335335
};
336+
337+
if (this._url.includes("update.json"))
338+
{
339+
window._webRequest.GET(this._url, this._requestHeaders, onGetDone);
340+
return;
341+
}
342+
336343
// HACK (#5066): the code checking whether the connection is
337344
// allowed is temporary, the actual check should be in the core
338345
// 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)

src/FilterEngine.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@
2323
#include <thread>
2424

2525
#include <AdblockPlus.h>
26-
#include "Utils.h"
2726
#include "JsContext.h"
2827
#include "Thread.h"
2928
#include <mutex>
3029
#include <condition_variable>
3130

3231
using namespace AdblockPlus;
3332

34-
namespace {
35-
static std::string filterEngineJsFiles[] = {
33+
namespace
34+
{
35+
const std::string filterEngineJsFiles[] =
36+
{
3637
"compat.js",
3738
"info.js",
3839
"io.js",
@@ -221,7 +222,7 @@ FilterEngine::FilterEngine(const JsEnginePtr& jsEngine)
221222
}
222223

223224
void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
224-
const JsEngine::EvaluateCallback& evaluateCallback,
225+
const EvaluateCallback& evaluateCallback,
225226
const FilterEngine::OnCreatedCallback& onCreated,
226227
const FilterEngine::CreationParameters& params)
227228
{
@@ -262,7 +263,7 @@ void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
262263
isSubscriptionDownloadAllowedCallback(params[0].IsString() ? &allowedConnectionType : nullptr, callJsCallback);
263264
});
264265
}
265-
266+
266267
jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated](JsValueList&& params)
267268
{
268269
filterEngine->firstRun = params.size() && params[0].AsBool();
@@ -280,15 +281,20 @@ void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
280281
filterEngine->GetJsEngine().NotifyLowMemory();
281282
});
282283

284+
// Lock the JS engine while we are loading scripts, no timeouts should fire
285+
// until we are done.
286+
const JsContext context(*jsEngine);
283287
// Set the preconfigured prefs
284288
auto preconfiguredPrefsObject = jsEngine->NewObject();
285289
for (const auto& pref : params.preconfiguredPrefs)
290+
{
286291
preconfiguredPrefsObject.SetProperty(pref.first, pref.second);
292+
}
287293
jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
288294

289295
// Load adblockplus scripts
290-
for(size_t i = 0; i < ArraySize(filterEngineJsFiles); ++i)
291-
evaluateCallback(filterEngineJsFiles[i]);
296+
for (const auto& filterEngineJsFile: filterEngineJsFiles)
297+
evaluateCallback(filterEngineJsFile);
292298

293299
}
294300

src/Platform.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <AdblockPlus/FilterEngine.h>
2020
#include <AdblockPlus/DefaultLogSystem.h>
2121
#include <AdblockPlus/AsyncExecutor.h>
22-
#include "JsContext.h"
2322
#include "DefaultTimer.h"
2423
#include "DefaultWebRequest.h"
2524
#include "DefaultFileSystem.h"
@@ -68,16 +67,14 @@ JsEngine& Platform::GetJsEngine()
6867
return *jsEngine;
6968
}
7069

71-
JsEngine::EvaluateCallback Platform::GetEvaluateCallback()
70+
std::function<void(const std::string&)> Platform::GetEvaluateCallback()
7271
{
7372
// GetEvaluateCallback() method assumes that jsEngine is already created
7473
return [this](const std::string& filename)
7574
{
7675
if (evaluatedJsSources.find(filename) != evaluatedJsSources.end())
7776
return; //NO-OP, file was already evaluated
7877

79-
// Lock the JS engine while we are loading scripts
80-
const JsContext context(*jsEngine);
8178
for (int i = 0; !jsSources[i].empty(); i += 2)
8279
if (jsSources[i] == filename)
8380
{

src/Updater.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
*/
1717

1818
#include <AdblockPlus.h>
19-
#include "Utils.h"
20-
#include <cassert>
19+
#include "JsContext.h"
2120

2221
using namespace AdblockPlus;
2322

24-
namespace {
25-
static std::string updaterJsFiles[] = {
23+
namespace
24+
{
25+
const std::string updaterJsFiles[] =
26+
{
2627
"compat.js",
2728
"info.js",
2829
"prefs.js",
@@ -36,28 +37,14 @@ namespace {
3637
};
3738
}
3839

39-
Updater::Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& evaluateCallback)
40+
Updater::Updater(const JsEnginePtr& jsEngine, const EvaluateCallback& evaluateCallback)
4041
: jsEngine(jsEngine), updateCheckId(0)
4142
{
42-
// Hack to enable downloads from JS (see compat.js)
43-
jsEngine->SetEventCallback("_isSubscriptionDownloadAllowed", [this](JsValueList&& params)
44-
{
45-
// param[0] - nullable string Prefs.allowed_connection_type
46-
// param[1] - function(Boolean)
47-
bool areArgumentsValid = params.size() == 2 && (params[0].IsNull() || params[0].IsString()) && params[1].IsFunction();
48-
assert(areArgumentsValid && "Invalid argument: there should be two args and the second one should be a function");
49-
if (!areArgumentsValid)
50-
return;
51-
params[1].Call(this->jsEngine->NewValue(true));
52-
});
53-
54-
// Set empty preconfigured prefs
55-
auto preconfiguredPrefsObject = jsEngine->NewObject();
56-
jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
57-
58-
// Load adblockplus scripts
59-
for(size_t i = 0; i < ArraySize(updaterJsFiles); ++i)
60-
evaluateCallback(updaterJsFiles[i]);
43+
// Lock the JS engine while we are loading scripts, no timeouts should fire
44+
// until we are done.
45+
const JsContext context(*jsEngine);
46+
for (const auto& updaterJsFile: updaterJsFiles)
47+
evaluateCallback(updaterJsFile);
6148
}
6249

6350
void Updater::SetUpdateAvailableCallback(const Updater::UpdateAvailableCallback& callback)

0 commit comments

Comments
 (0)