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 class VpnEventCallback : public VpnEventCallbackStub {
37 public:
38     void OnVpnStateChanged(const bool &isConnected) override;
OnVpnMultiUserSetUp()39     void OnVpnMultiUserSetUp() override{};
40 };
41 
42 class VpnMonitor {
43 private:
44     VpnMonitor() = default;
45     ~VpnMonitor() = default;
46     VpnMonitor(const VpnMonitor &) = delete;
47     VpnMonitor &operator=(const VpnMonitor &) = delete;
48 
49 public:
50     static VpnMonitor &GetInstance();
51 
52 public:
53     napi_value On(napi_env env, napi_callback_info info);
54     napi_value Off(napi_env env, napi_callback_info info);
55     bool ShowVpnDialog(const std::string &bundleName, const std::string &abilityName, const std::string &appName);
56 
GetManager()57     EventManager inline *GetManager() const
58     {
59         return manager_;
60     }
61 
62 private:
63     sptr<VpnEventCallback> eventCallback_ = nullptr;
64     napi_value callback_ = nullptr;
65     EventManager *manager_ = nullptr;
66 
67 class VpnAbilityConn : public AAFwk::AbilityConnectionStub {
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)68     void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
69         int32_t resultCode) override
70     {
71         NETMANAGER_EXT_LOGI("connect done");
72     }
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)73     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override
74     {
75         NETMANAGER_EXT_LOGI("disconnect done");
76     }
77 };
78 
79 private:
80     bool ParseParams(napi_env env, napi_callback_info info);
81     bool UnwrapManager(napi_env env, napi_value jsObject);
82     void Register(napi_env env);
83     void Unregister(napi_env env);
84 };
85 } // namespace NetManagerStandard
86 } // namespace OHOS
87 #endif // NET_EXT_NAPI_VPN_MONITOR_H