Skip to content

Commit

Permalink
Nit: Pass connectionhealth status to Extension (#9854)
Browse files Browse the repository at this point in the history
* Pass connection health status

* oops

* fix parse error

* uh what are we reporting?

* Fix value

* add concepts to source target on gcc

* okay only add it for that file!

* okay i give up

* wtf
  • Loading branch information
strseb authored Sep 24, 2024
1 parent 0d09e7e commit ac3ccf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/webextensionadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QWindow>
#include <functional>

#include "connectionhealth.h"
#include "controller.h"
#include "feature/feature.h"
#include "leakdetector.h"
Expand Down Expand Up @@ -41,6 +42,13 @@ struct match : Ts... {
template <class... Ts>
match(Ts...) -> match<Ts...>;

template <typename T>
const char* asString(T qEnumValue) {
const QMetaObject* meta = qt_getEnumMetaObject(qEnumValue);
int index = meta->indexOfEnumerator(qt_getEnumName(qEnumValue));
return meta->enumerator(index).valueToKey(qEnumValue);
};

Logger logger("WebExtensionAdapter");
} // namespace

Expand All @@ -55,6 +63,8 @@ WebExtensionAdapter::WebExtensionAdapter(QObject* parent)
&WebExtensionAdapter::writeState);
connect(vpn->controller(), &Controller::stateChanged, this,
&WebExtensionAdapter::writeState);
connect(vpn->connectionHealth(), &ConnectionHealth::stabilityChanged, this,
&WebExtensionAdapter::writeState);

mProxyStateChanged = vpn->proxyController()->stateBindable().subscribe(
[this]() { serializeStatus(); });
Expand Down Expand Up @@ -161,25 +171,13 @@ QJsonObject WebExtensionAdapter::serializeStatus() {
{
int stateValue = vpn->state();
if (stateValue > App::StateCustom) {
MozillaVPN::CustomState state =
static_cast<MozillaVPN::CustomState>(stateValue);
const QMetaObject* meta = qt_getEnumMetaObject(state);
int index = meta->indexOfEnumerator(qt_getEnumName(state));
obj["app"] = meta->enumerator(index).valueToKey(state);
obj["app"] = asString(static_cast<MozillaVPN::CustomState>(stateValue));
} else {
App::State state = static_cast<App::State>(stateValue);
const QMetaObject* meta = qt_getEnumMetaObject(state);
int index = meta->indexOfEnumerator(qt_getEnumName(state));
obj["app"] = meta->enumerator(index).valueToKey(state);
obj["app"] = asString(static_cast<App::State>(stateValue));
}
}

{
Controller::State state = vpn->controller()->state();
const QMetaObject* meta = qt_getEnumMetaObject(state);
int index = meta->indexOfEnumerator(qt_getEnumName(state));
obj["vpn"] = meta->enumerator(index).valueToKey(state);
}
obj["vpn"] = asString(vpn->controller()->state());
obj["connectionHealth"] = asString(vpn->connectionHealth()->stability());
#if defined MZ_PROXY_ENABLED
{
auto* proxyController = vpn->proxyController();
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/testWebExtensionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('WebExtension API', function() {
sentToClient(new ExtensionMessage('status'), sock);
const msg = await statusPromise
assert(msg.status.version, `A Version is sent in msg: ${JSON.stringify(msg)}` )
assert(msg.status.connectionHealth, `The current Connection Health status is sent in msg: ${JSON.stringify(msg)}` )
sock.destroy();
});
it('A Webextension can activate the VPN', async () => {
Expand Down Expand Up @@ -104,6 +105,15 @@ describe('WebExtension API', function() {

sock.destroy();
});
it('A Webextension will be notified if the stability becomes instable ', async () => {
const sock = await connectExtension();
const messagePipe = getMessageStream(sock);
const statusPromise = readResponseOfType('status', messagePipe);
await vpn.forceConnectionStabilityStatus("unstable");
const msg = await statusPromise
assert(msg.status.connectionHealth == "Unstable", "The extension was notified of the instability: "+ msg.status.connectionHealth)
sock.destroy();
});
});

}

0 comments on commit ac3ccf6

Please sign in to comment.