Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #include "XLNetworkController.h" 24 : 25 : #if LINUX 26 : #include "linux/XLPlatformLinuxDbus.h" 27 : #endif 28 : 29 : #if ANDROID 30 : #include "android/XLPlatformAndroidActivity.h" 31 : #endif 32 : 33 : #if WIN32 34 : #include "win32/XLPlatformWin32Library.h" 35 : #endif 36 : 37 : namespace STAPPLER_VERSIONIZED stappler::xenolith::network { 38 : 39 : #if LINUX 40 : 41 21 : SPUNUSED static void registerNetworkCallback(Application *, void *key, Function<void(NetworkCapabilities)> &&cb) { 42 21 : auto lib = stappler::xenolith::platform::DBusLibrary::get(); 43 21 : lib.addNetworkConnectionCallback(key, [cb = move(cb)] (const stappler::xenolith::platform::NetworkState &state) { 44 21 : NetworkCapabilities defaultFlags = NetworkCapabilities::NotRoaming | NetworkCapabilities::NotCongested | NetworkCapabilities::NotVpn; 45 21 : NetworkCapabilities caps = NetworkCapabilities::None; 46 : 47 21 : switch (state.connectivity) { 48 0 : case platform::NM_CONNECTIVITY_UNKNOWN: 49 : case platform::NM_CONNECTIVITY_NONE: 50 0 : break; 51 0 : case platform::NM_CONNECTIVITY_PORTAL: 52 0 : caps |= NetworkCapabilities::Internet | NetworkCapabilities::CaptivePortal | defaultFlags; 53 0 : break; 54 21 : case platform::NM_CONNECTIVITY_LIMITED: 55 21 : caps |= NetworkCapabilities::Internet | defaultFlags; 56 21 : break; 57 0 : case platform::NM_CONNECTIVITY_FULL: 58 0 : caps |= NetworkCapabilities::Internet | NetworkCapabilities::Validated | NetworkCapabilities::NotRestricted | defaultFlags; 59 0 : break; 60 : } 61 : 62 21 : switch (state.state) { 63 0 : case platform::NM_STATE_UNKNOWN: 64 0 : break; 65 0 : case platform::NM_STATE_ASLEEP: 66 0 : break; 67 0 : case platform::NM_STATE_DISCONNECTED: 68 0 : break; 69 0 : case platform::NM_STATE_DISCONNECTING: 70 0 : break; 71 0 : case platform::NM_STATE_CONNECTING: 72 0 : break; 73 0 : case platform::NM_STATE_CONNECTED_LOCAL: 74 0 : caps |= NetworkCapabilities::NotSuspended; 75 0 : break; 76 0 : case platform::NM_STATE_CONNECTED_SITE: 77 0 : caps |= NetworkCapabilities::NotSuspended; 78 0 : break; 79 21 : case platform::NM_STATE_CONNECTED_GLOBAL: 80 21 : caps |= NetworkCapabilities::NotRestricted | NetworkCapabilities::NotSuspended; 81 21 : break; 82 : } 83 : 84 21 : switch (state.metered) { 85 0 : case platform::NM_METERED_UNKNOWN: 86 0 : break; 87 0 : case platform::NM_METERED_YES: 88 : case platform::NM_METERED_GUESS_YES: 89 0 : break; 90 21 : case platform::NM_METERED_NO: 91 : case platform::NM_METERED_GUESS_NO: 92 21 : caps |= NetworkCapabilities::NotMetered; 93 21 : break; 94 : } 95 : 96 21 : cb(caps); 97 21 : }); 98 21 : } 99 : 100 0 : SPUNUSED static void unregisterNetworkCallback(Application *, void *key) { 101 0 : auto lib = stappler::xenolith::platform::DBusLibrary::get(); 102 0 : lib.removeNetworkConnectionCallback(key); 103 0 : } 104 : 105 : #endif 106 : 107 : #if ANDROID 108 : 109 : SPUNUSED static void registerNetworkCallback(Application *app, void *key, Function<void(NetworkCapabilities)> &&cb) { 110 : auto activity = reinterpret_cast<platform::Activity *>(app->getInfo().nativeHandle); 111 : cb(NetworkCapabilities(activity->getNetworkCapabilities())); 112 : activity->addNetworkCallback(key, [key, cb = move(cb)] (platform::NetworkCapabilities caps) { 113 : cb(NetworkCapabilities(caps)); 114 : }); 115 : 116 : } 117 : 118 : SPUNUSED static void unregisterNetworkCallback(Application *app, void *key) { 119 : auto activity = reinterpret_cast<platform::Activity *>(app->getInfo().nativeHandle); 120 : activity->removeNetworkCallback(key); 121 : } 122 : 123 : #endif 124 : 125 : #if WIN32 126 : 127 : SPUNUSED static void registerNetworkCallback(Application *, void *key, Function<void(NetworkCapabilities)> &&cb) { 128 : auto lib = stappler::xenolith::platform::Win32Library::getInstance(); 129 : lib->addNetworkConnectionCallback(key, move(cb)); 130 : } 131 : 132 : SPUNUSED static void unregisterNetworkCallback(Application *app, void *key) { 133 : auto lib = stappler::xenolith::platform::Win32Library::getInstance(); 134 : lib->removeNetworkConnectionCallback(key); 135 : 136 : } 137 : 138 : #endif 139 : 140 : }