19NetState parseNetCapabilities(QOhosJsState &jsState,
const QNapi::Object &netCapabilities)
24 const auto bearerTypes = QNapi::getFilteredArrayElements<std::vector<NetBearType>, QNapi::Number>(
25 netCapabilities.get<QNapi::Array>(
"bearerTypes"),
26 [&](QNapi::Number bearerType) {
27 return jsState.tryMapOhosEnumFromJs<NetBearType>(bearerType);
29 if (!bearerTypes.empty())
30 state.transport = bearerTypes.front();
32 auto networkCapArray = QNapi::getOptionalPropOrEmpty<QNapi::Array>(netCapabilities,
"networkCap");
33 if (!networkCapArray.IsEmpty()) {
34 const auto networkCap = QNapi::getFilteredArrayElements<std::vector<NetCap>, QNapi::Number>(
36 [&](QNapi::Number capability) {
37 return jsState.tryMapOhosEnumFromJs<NetCap>(capability);
39 bool internetCapable =
false;
40 bool validated =
false;
41 bool checkingConnectivity =
false;
43 for (
const auto capability : networkCap) {
44 if (capability == NetCap::NET_CAPABILITY_INTERNET)
45 internetCapable =
true;
46 else if (capability == NetCap::NET_CAPABILITY_VALIDATED)
48 else if (capability == NetCap::NET_CAPABILITY_CHECKING_CONNECTIVITY)
49 checkingConnectivity =
true;
50 else if (capability == NetCap::NET_CAPABILITY_PORTAL)
51 state.behindCaptivePortal =
true;
52 else if (capability == NetCap::NET_CAPABILITY_NOT_METERED)
53 state.metered =
false;
55 if (checkingConnectivity)
59 else if (internetCapable)
66NetState readDefaultNetState(QOhosJsState &jsState)
69 auto netHandle = jsState.eval<QNapi::Object>(
"@ohos.net.connection.getDefaultNetSync()");
70 constexpr qint32 invalidNetId = 0;
71 if (netHandle.get<QNapi::Number>(
"netId").Int32Value() == invalidNetId)
73 auto netCapabilities = jsState.eval<QNapi::Object>(
74 "@ohos.net.connection.getNetCapabilitiesSync(*)", {netHandle});
75 return parseNetCapabilities(jsState, netCapabilities);
76 }
catch (
const Napi::Error &error) {
78 "%s: failed to read default network state: %s", Q_FUNC_INFO, error.what());
83std::shared_ptr<
void> registerNetConnectionEventHandlers(
84 QNapi::Object netConnection,
85 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> eventHandlers)
87 for (
auto &[eventTypeName, eventHandler] : eventHandlers)
88 netConnection.eval(
"on(*)", {eventTypeName, std::move(eventHandler)});
93 [](
const QOhosCallbackInfo &cbInfo) {
94 if (cbInfo.Length() != 0 && cbInfo.getFirstArg<QNapi::Value>(Q_FUNC_INFO).IsObject())
95 QtOhos::logJsCallbackError(cbInfo,
"@ohos.net.connection.NetConnection.register()");
99 auto netConnectionRef = QtOhos::moveToSharedPtr(
100 QNapi::Reference<>::makePersistentFrom(netConnection));
102 return QtOhos::makeDestroyNotifier(
103 [netConnectionRef]() {
105 netConnectionRef->eval(
108 [](
const QOhosCallbackInfo &cbInfo) {
109 if (cbInfo.Length() != 0 && cbInfo.getFirstArg<QNapi::Value>(Q_FUNC_INFO).IsObject())
110 QtOhos::logJsCallbackError(cbInfo,
"@ohos.net.connection.NetConnection.unregister()");
113 }
catch (
const Napi::Error &error) {
115 "%s: net connection unregister failed (ignoring): %s",
116 Q_FUNC_INFO, error.what());
121std::shared_ptr<
void> registerNetConnectionListener(
122 QOhosJsState &jsState, QOhosConsumer<NetState> updateListener)
124 auto sharedUpdateListener = QtOhos::moveToSharedPtr(std::move(updateListener));
126 return registerNetConnectionEventHandlers(
127 jsState.eval<QNapi::Object>(
"@ohos.net.connection.createNetConnection()"),
131 [sharedUpdateListener](
const QOhosCallbackInfo &cbInfo) {
132 (*sharedUpdateListener)(readDefaultNetState(cbInfo.jsState()));
136 "netCapabilitiesChange",
137 [sharedUpdateListener](
const QOhosCallbackInfo &cbInfo) {
138 auto netCapabilityInfo = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
139 (*sharedUpdateListener)(
140 parseNetCapabilities(cbInfo.jsState(), netCapabilityInfo.get<QNapi::Object>(
"netCap")));
145 [sharedUpdateListener]() {
146 (*sharedUpdateListener)(NetState{});
151 [sharedUpdateListener]() {
152 (*sharedUpdateListener)(NetState{});
163 QOhosConsumer<NetState> stateChangedHandler;
165 std::shared_ptr<
void> listenerHandle;
168 auto context = QOhosJsThreadGateway::eval(
169 [&](QOhosJsState &jsState) {
170 auto context = std::make_shared<Context>();
171 context->stateChangedHandler = std::move(stateChangeConsumer);
172 context->currentState = readDefaultNetState(jsState);
174 auto onStateChanged = [weakContext = QtOhos::makeWeakPtr(context)](NetState state) {
175 QtOhos::invokeInQtThread(
176 [weakContext, state]() {
177 auto context = weakContext.lock();
179 context->currentState = state;
180 context->stateChangedHandler(context->currentState);
186 context->listenerHandle = QtOhos::makeProxyWithJsThreadDeleter(
187 registerNetConnectionListener(jsState, std::move(onStateChanged)));
188 }
catch (
const Napi::Error &error) {
190 "%s: failed to register @ohos.net.connection listener: %s", Q_FUNC_INFO, error.what());
197 return context->currentState;