• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NET_EXT_NAPI_VPN_MONITOR_H
17 #define NET_EXT_NAPI_VPN_MONITOR_H
18 
19 #include <napi/native_api.h>
20 #include <refbase.h>
21 
22 #include "event_manager.h"
23 #include "vpn_event_callback_stub.h"
24 #include "ability_connect_callback_stub.h"
25 #include "ability_manager_client.h"
26 #include "netmanager_ext_log.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 /*
31  * There is several functionalities of this string:
32  * 1: to identify the application which called StartVpnExtensionAbility is the VPN APP or VPNDialog APP.
33  * 2: to identify VPNDialog APP call UpdateVpnAuthorize in order to update vpnExtMode to 1 or 0.
34  */
35 static constexpr const char *VPN_DIALOG_POSTFIX = "**vpndialog**";
36 static constexpr const char *VPN_DIALOG_BUNDLENAME = "com.ohos.vpndialog";
37 class VpnEventCallback : public VpnEventCallbackStub {
38 public:
39     int32_t OnVpnStateChanged(bool isConnected) override;
OnMultiVpnStateChanged(bool isConnected,const std::string & bundleName,const std::string & vpnId)40     int32_t OnMultiVpnStateChanged(bool isConnected, const std::string &bundleName,
41         const std::string &vpnId) override{ return ERR_OK; };
OnVpnMultiUserSetUp()42     int32_t OnVpnMultiUserSetUp() override{ return ERR_OK; };
43 };
44 
45 class VpnMonitor {
46 private:
47     VpnMonitor() = default;
48     ~VpnMonitor() = default;
49     VpnMonitor(const VpnMonitor &) = delete;
50     VpnMonitor &operator=(const VpnMonitor &) = delete;
51 
52 public:
53     static VpnMonitor &GetInstance();
54 
55 public:
56     napi_value On(napi_env env, napi_callback_info info);
57     napi_value Off(napi_env env, napi_callback_info info);
58     bool ShowVpnDialog(const std::string &bundleName, const std::string &abilityName, const std::string &appName);
59 
GetManager()60     inline std::shared_ptr<EventManager> GetManager() const
61     {
62         return manager_;
63     }
64 
65 private:
66     sptr<VpnEventCallback> eventCallback_ = nullptr;
67     napi_value callback_ = nullptr;
68     std::shared_ptr<EventManager> manager_ = nullptr;
69 
70 class VpnAbilityConn : public AAFwk::AbilityConnectionStub {
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)71     void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
72         int32_t resultCode) override
73     {
74         NETMANAGER_EXT_LOGI("connect done");
75     }
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)76     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override
77     {
78         NETMANAGER_EXT_LOGI("disconnect done");
79     }
80 };
81 
82 private:
83     bool ParseParams(napi_env env, napi_callback_info info);
84     bool UnwrapManager(napi_env env, napi_value jsObject);
85     void Register(napi_env env);
86     void Unregister(napi_env env);
87 };
88 } // namespace NetManagerStandard
89 } // namespace OHOS
90 #endif // NET_EXT_NAPI_VPN_MONITOR_H