Skip to content

Commit 5d21081

Browse files
committed
Updates in Updater class
Added cpp methods for Prefs JS API. Removed not needed JS API. Added _isSubscriptionDownloadAllowed event callback to unblock downloads in JS code. Cleaned up UpdateCheckTest a bit removing FilterEngine because with the change above those tests don't require anymore FilterEnigne object to pass.
1 parent 540ca04 commit 5d21081

4 files changed

Lines changed: 56 additions & 24 deletions

File tree

include/AdblockPlus/Updater.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <functional>
2222
#include <string>
2323
#include <AdblockPlus/JsEngine.h>
24+
#include <AdblockPlus/JsValue.h>
2425

2526
namespace AdblockPlus
2627
{
@@ -70,6 +71,20 @@ namespace AdblockPlus
7071
*/
7172
void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());
7273

74+
/**
75+
* Retrieves a preference value.
76+
* @param pref Preference name.
77+
* @return Preference value, or `null` if it doesn't exist.
78+
*/
79+
JsValue GetPref(const std::string& pref) const;
80+
81+
/**
82+
* Sets a preference value.
83+
* @param pref Preference name.
84+
* @param value New value of the preference.
85+
*/
86+
void SetPref(const std::string& pref, const JsValue& value);
87+
7388
private:
7489
JsEnginePtr jsEngine;
7590
int updateCheckId;

lib/apiUpdater.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ let API_UPDATER = (() =>
3737
forceUpdateCheck(eventName)
3838
{
3939
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);
5040
}
5141
};
5242
})();

src/Updater.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <AdblockPlus.h>
1919
#include "Utils.h"
20+
#include <cassert>
2021

2122
using namespace AdblockPlus;
2223

@@ -31,14 +32,25 @@ namespace {
3132
"common.js",
3233
"downloader.js",
3334
"updater.js",
34-
"apiUpdater.js",
35-
"basedomain.js"
35+
"apiUpdater.js"
3636
};
3737
}
3838

3939
Updater::Updater(const JsEnginePtr& jsEngine, const JsEngine::EvaluateCallback& evaluateCallback)
4040
: jsEngine(jsEngine), updateCheckId(0)
4141
{
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+
4254
// Set empty preconfigured prefs
4355
auto preconfiguredPrefsObject = jsEngine->NewObject();
4456
jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
@@ -79,3 +91,18 @@ void Updater::ForceUpdateCheck(const Updater::UpdateCheckDoneCallback& callback)
7991
}
8092
func.Call(params);
8193
}
94+
95+
JsValue Updater::GetPref(const std::string& pref) const
96+
{
97+
JsValue func = jsEngine->Evaluate("API_UPDATER.getPref");
98+
return func.Call(jsEngine->NewValue(pref));
99+
}
100+
101+
void Updater::SetPref(const std::string& pref, const JsValue& value)
102+
{
103+
JsValue func = jsEngine->Evaluate("API_UPDATER.setPref");
104+
JsValueList params;
105+
params.push_back(jsEngine->NewValue(pref));
106+
params.push_back(value);
107+
func.Call(params);
108+
}

test/UpdateCheck.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace
4848
updateCallbackCalled = false;
4949
}
5050

51-
void CreateFilterEngine()
51+
void CreateUpdater()
5252
{
5353
LazyFileSystem* fileSystem;
5454
ThrowingPlatformCreationParameters platformParams;
@@ -64,7 +64,7 @@ namespace
6464
eventCallbackParams = std::move(params);
6565
});
6666

67-
::CreateFilterEngine(*fileSystem, *platform);
67+
platform->GetUpdater();
6868
}
6969

7070
// Returns a URL or the empty string if there is no such request.
@@ -107,7 +107,7 @@ TEST_F(UpdateCheckTest, RequestFailure)
107107
appInfo.applicationVersion = "2";
108108
appInfo.developmentBuild = false;
109109

110-
CreateFilterEngine();
110+
CreateUpdater();
111111
ForceUpdateCheck();
112112

113113
auto requestUrl = ProcessPendingUpdateWebRequest();
@@ -116,7 +116,7 @@ TEST_F(UpdateCheckTest, RequestFailure)
116116
ASSERT_TRUE(updateCallbackCalled);
117117
ASSERT_FALSE(updateError.empty());
118118

119-
std::string expectedUrl(platform->GetFilterEngine().GetPref("update_url_release").AsString());
119+
std::string expectedUrl(platform->GetUpdater().GetPref("update_url_release").AsString());
120120
std::string platform = GetJsEngine().Evaluate("require('info').platform").AsString();
121121
std::string platformVersion = GetJsEngine().Evaluate("require('info').platformVersion").AsString();
122122

@@ -144,7 +144,7 @@ TEST_F(UpdateCheckTest, UpdateAvailable)
144144
appInfo.applicationVersion = "2";
145145
appInfo.developmentBuild = true;
146146

147-
CreateFilterEngine();
147+
CreateUpdater();
148148
ForceUpdateCheck();
149149

150150
auto requestUrl = ProcessPendingUpdateWebRequest();
@@ -155,7 +155,7 @@ TEST_F(UpdateCheckTest, UpdateAvailable)
155155
ASSERT_TRUE(updateCallbackCalled);
156156
ASSERT_TRUE(updateError.empty());
157157

158-
std::string expectedUrl(platform->GetFilterEngine().GetPref("update_url_devbuild").AsString());
158+
std::string expectedUrl(platform->GetUpdater().GetPref("update_url_devbuild").AsString());
159159
std::string platform = GetJsEngine().Evaluate("require('info').platform").AsString();
160160
std::string platformVersion = GetJsEngine().Evaluate("require('info').platformVersion").AsString();
161161

@@ -183,7 +183,7 @@ TEST_F(UpdateCheckTest, ApplicationUpdateAvailable)
183183
appInfo.applicationVersion = "2";
184184
appInfo.developmentBuild = true;
185185

186-
CreateFilterEngine();
186+
CreateUpdater();
187187
ForceUpdateCheck();
188188

189189
ProcessPendingUpdateWebRequest();
@@ -206,7 +206,7 @@ TEST_F(UpdateCheckTest, WrongApplication)
206206
appInfo.applicationVersion = "2";
207207
appInfo.developmentBuild = true;
208208

209-
CreateFilterEngine();
209+
CreateUpdater();
210210
ForceUpdateCheck();
211211

212212
ProcessPendingUpdateWebRequest();
@@ -228,7 +228,7 @@ TEST_F(UpdateCheckTest, WrongVersion)
228228
appInfo.applicationVersion = "2";
229229
appInfo.developmentBuild = true;
230230

231-
CreateFilterEngine();
231+
CreateUpdater();
232232
ForceUpdateCheck();
233233

234234
ProcessPendingUpdateWebRequest();
@@ -250,7 +250,7 @@ TEST_F(UpdateCheckTest, WrongURL)
250250
appInfo.applicationVersion = "2";
251251
appInfo.developmentBuild = true;
252252

253-
CreateFilterEngine();
253+
CreateUpdater();
254254
ForceUpdateCheck();
255255

256256
ProcessPendingUpdateWebRequest();
@@ -274,7 +274,7 @@ TEST_F(UpdateCheckTest, SetRemoveUpdateAvailableCallback)
274274

275275
appInfo.name = "test";
276276
appInfo.version = "1.0.1";
277-
CreateFilterEngine();
277+
CreateUpdater();
278278

279279
int timesCalled = 0;
280280
platform->GetUpdater().SetUpdateAvailableCallback([&timesCalled](const std::string&)->void
@@ -288,7 +288,7 @@ TEST_F(UpdateCheckTest, SetRemoveUpdateAvailableCallback)
288288

289289
EXPECT_EQ(1, timesCalled);
290290

291-
// FilterEngine::SetUpdateAvailableCallback overriddes previously installed on JsEngine
291+
// Updater::SetUpdateAvailableCallback overriddes previously installed on JsEngine
292292
// handler for "updateAvailable" event.
293293
EXPECT_FALSE(eventCallbackCalled);
294294

0 commit comments

Comments
 (0)