Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
Apart from changes in new code, this also withdraws
changes in existing code not related with this feature.
  • Loading branch information
kzlomek committed Aug 4, 2018
1 parent 7d6bdda commit 49f0171
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 53 deletions.
8 changes: 7 additions & 1 deletion include/AdblockPlus/FilterEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ namespace AdblockPlus
*/
typedef std::function<void(const FilterEnginePtr&)> OnCreatedCallback;

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

/**
* Asynchronously constructs FilterEngine.
* @param jsEngine `JsEngine` instance used to run JavaScript code
Expand All @@ -283,7 +289,7 @@ namespace AdblockPlus
* @param parameters optional creation parameters.
*/
static void CreateAsync(const JsEnginePtr& jsEngine,
const JsEngine::EvaluateCallback& evaluateCallback,
const EvaluateCallback& evaluateCallback,
const OnCreatedCallback& onCreated,
const CreationParameters& parameters = CreationParameters());

Expand Down
6 changes: 0 additions & 6 deletions include/AdblockPlus/JsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ 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;

/**
* Initiates a garbage collection.
*/
Expand Down
4 changes: 2 additions & 2 deletions include/AdblockPlus/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <future>
#include <set>
#include <string>
#include <functional>

namespace AdblockPlus
{
Expand Down Expand Up @@ -133,15 +134,14 @@ namespace AdblockPlus
TimerPtr timer;
FileSystemPtr fileSystem;
WebRequestPtr webRequest;
JsEngine::EvaluateCallback evaluateCallback;
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();
std::function<void(const std::string&)> GetEvaluateCallback();
};

/**
Expand Down
10 changes: 8 additions & 2 deletions include/AdblockPlus/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ namespace AdblockPlus
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.
Expand All @@ -57,6 +55,12 @@ namespace AdblockPlus
*/
void RemoveUpdateAvailableCallback();

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

/**
* Forces an immediate update check.
* `Updater` will automatically check for updates in regular intervals,
Expand Down Expand Up @@ -85,6 +89,8 @@ namespace AdblockPlus
*/
void SetPref(const std::string& pref, const JsValue& value);

explicit Updater(const JsEnginePtr& jsEngine, const EvaluateCallback& callback);

private:
JsEnginePtr jsEngine;
int updateCheckId;
Expand Down
1 change: 0 additions & 1 deletion lib/apiUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

let API_UPDATER = (() =>
{
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
const {Prefs} = require("prefs");
const {checkForUpdates} = require("updater");

Expand Down
7 changes: 7 additions & 0 deletions lib/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ XMLHttpRequest.prototype =
for (let i = 0; i < list.length; i++)
list[i].call(this, event);
};

if (this._url.includes("update.json"))
{
window._webRequest.GET(this._url, this._requestHeaders, onGetDone);
return;
}

// HACK (#5066): the code checking whether the connection is
// allowed is temporary, the actual check should be in the core
// when we make a decision whether to update a subscription with
Expand Down
9 changes: 5 additions & 4 deletions lib/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ let Prefs = exports.Prefs = {
}
};

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

// Define defaults
for (let key in defaults)
Expand Down
20 changes: 13 additions & 7 deletions src/FilterEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
#include <thread>

#include <AdblockPlus.h>
#include "Utils.h"
#include "JsContext.h"
#include "Thread.h"
#include <mutex>
#include <condition_variable>

using namespace AdblockPlus;

namespace {
static std::string filterEngineJsFiles[] = {
namespace
{
const std::string filterEngineJsFiles[] =
{
"compat.js",
"info.js",
"io.js",
Expand Down Expand Up @@ -221,7 +222,7 @@ FilterEngine::FilterEngine(const JsEnginePtr& jsEngine)
}

void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
const JsEngine::EvaluateCallback& evaluateCallback,
const EvaluateCallback& evaluateCallback,
const FilterEngine::OnCreatedCallback& onCreated,
const FilterEngine::CreationParameters& params)
{
Expand Down Expand Up @@ -262,7 +263,7 @@ void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
isSubscriptionDownloadAllowedCallback(params[0].IsString() ? &allowedConnectionType : nullptr, callJsCallback);
});
}

jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated](JsValueList&& params)
{
filterEngine->firstRun = params.size() && params[0].AsBool();
Expand All @@ -280,15 +281,20 @@ void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
filterEngine->GetJsEngine().NotifyLowMemory();
});

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

// Load adblockplus scripts
for(size_t i = 0; i < ArraySize(filterEngineJsFiles); ++i)
evaluateCallback(filterEngineJsFiles[i]);
for (const auto& filterEngineJsFile: filterEngineJsFiles)
evaluateCallback(filterEngineJsFile);

}

Expand Down
5 changes: 1 addition & 4 deletions src/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <AdblockPlus/FilterEngine.h>
#include <AdblockPlus/DefaultLogSystem.h>
#include <AdblockPlus/AsyncExecutor.h>
#include "JsContext.h"
#include "DefaultTimer.h"
#include "DefaultWebRequest.h"
#include "DefaultFileSystem.h"
Expand Down Expand Up @@ -68,16 +67,14 @@ JsEngine& Platform::GetJsEngine()
return *jsEngine;
}

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

// Lock the JS engine while we are loading scripts
const JsContext context(*jsEngine);
for (int i = 0; !jsSources[i].empty(); i += 2)
if (jsSources[i] == filename)
{
Expand Down
35 changes: 11 additions & 24 deletions src/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
*/

#include <AdblockPlus.h>
#include "Utils.h"
#include <cassert>
#include "JsContext.h"

using namespace AdblockPlus;

namespace {
static std::string updaterJsFiles[] = {
namespace
{
const std::string updaterJsFiles[] =
{
"compat.js",
"info.js",
"prefs.js",
Expand All @@ -36,28 +37,14 @@ namespace {
};
}

Updater::Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& evaluateCallback)
Updater::Updater(const JsEnginePtr& jsEngine, const EvaluateCallback& evaluateCallback)
: jsEngine(jsEngine), updateCheckId(0)
{
// Hack to enable downloads from JS (see compat.js)
jsEngine->SetEventCallback("_isSubscriptionDownloadAllowed", [this](JsValueList&& params)
{
// param[0] - nullable string Prefs.allowed_connection_type
// param[1] - function(Boolean)
bool areArgumentsValid = params.size() == 2 && (params[0].IsNull() || params[0].IsString()) && params[1].IsFunction();
assert(areArgumentsValid && "Invalid argument: there should be two args and the second one should be a function");
if (!areArgumentsValid)
return;
params[1].Call(this->jsEngine->NewValue(true));
});

// Set empty preconfigured prefs
auto preconfiguredPrefsObject = jsEngine->NewObject();
jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);

// Load adblockplus scripts
for(size_t i = 0; i < ArraySize(updaterJsFiles); ++i)
evaluateCallback(updaterJsFiles[i]);
// Lock the JS engine while we are loading scripts, no timeouts should fire
// until we are done.
const JsContext context(*jsEngine);
for (const auto& updaterJsFile: updaterJsFiles)
evaluateCallback(updaterJsFile);
}

void Updater::SetUpdateAvailableCallback(const Updater::UpdateAvailableCallback& callback)
Expand Down
2 changes: 0 additions & 2 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

#include "JsError.h"

#define ArraySize(a) (sizeof(a) / sizeof(a[0]))

namespace AdblockPlus
{
namespace Utils
Expand Down

0 comments on commit 49f0171

Please sign in to comment.