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 OHOS_NETMANAGER_EXT_JS_VPN_EXTENSION_H
17 #define OHOS_NETMANAGER_EXT_JS_VPN_EXTENSION_H
18 
19 #include "configuration.h"
20 #ifdef SUPPORT_GRAPHICS
21 #include "system_ability_status_change_stub.h"
22 #endif
23 #include "display_manager.h"
24 #include "vpn_extension.h"
25 #include "runtime.h"
26 #include "js_runtime.h"
27 #include "js_runtime_utils.h"
28 #include "napi/native_api.h"
29 #include "napi/native_common.h"
30 #include "napi/native_node_api.h"
31 
32 class NativeReference;
33 
34 namespace OHOS {
35 namespace NetManagerStandard {
36 using namespace AbilityRuntime;
37 class VpnExtension;
38 /**
39  * @brief Basic vpn components.
40  */
41 class JsVpnExtension : public VpnExtension {
42 public:
43     explicit JsVpnExtension(JsRuntime& jsRuntime);
44     virtual ~JsVpnExtension() override;
45 
46     /**
47      * @brief Create JsVpnExtension.
48      *
49      * @param runtime The runtime.
50      * @return The JsVpnExtension instance.
51      */
52     static JsVpnExtension* Create(const std::unique_ptr<Runtime>& runtime);
53 
54     /**
55      * @brief Init the extension.
56      *
57      * @param record the extension record.
58      * @param application the application info.
59      * @param handler the extension handler.
60      * @param token the remote token.
61      */
62     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
63         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
64         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
65         const sptr<IRemoteObject> &token) override;
66 
67     /**
68      * @brief Called when this extension is started. You must override this function if you want to perform some
69      *        initialization operations during extension startup.
70      *
71      * This function can be called only once in the entire lifecycle of an extension.
72      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
73      */
74     virtual void OnStart(const AAFwk::Want &want) override;
75 
76     /**
77      * @brief Called when this Vpn extension is connected for the first time.
78      *
79      * You can override this function to implement your own processing logic.
80      *
81      * @param want Indicates the {@link Want} structure containing connection information about the Vpn extension.
82      * @return Returns a pointer to the <b>sid</b> of the connected Vpn extension.
83      */
84     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
85 
86     /**
87      * @brief Called when this Vpn extension is connected for the first time.
88      *
89      * You can override this function to implement your own processing logic.
90      *
91      * @param want Indicates the {@link Want} structure containing connection information about the Vpn extension.
92      * @param callbackInfo Indicates the lifecycle transaction callback information
93      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
94      * @return Returns a pointer to the <b>sid</b> of the connected Vpn extension.
95      */
96     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want,
97         AppExecFwk::AbilityTransactionCallbackInfo<sptr<IRemoteObject>> *callbackInfo, bool &isAsyncCallback) override;
98 
99     /**
100      * @brief Called when all abilities connected to this Vpn extension are disconnected.
101      *
102      * You can override this function to implement your own processing logic.
103      *
104      */
105     virtual void OnDisconnect(const AAFwk::Want &want) override;
106 
107     /**
108      * @brief Called when all abilities connected to this Vpn extension are disconnected.
109      *
110      * You can override this function to implement your own processing logic.
111      * @param callbackInfo Indicates the lifecycle transaction callback information
112      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
113      */
114     void OnDisconnect(const AAFwk::Want &want, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo,
115         bool &isAsyncCallback) override;
116 
117     /**
118      * @param want Indicates the want of Vpn to start.
119      * @param restart Indicates the startup mode. The value true indicates that Vpn is restarted after being
120      * destroyed, and the value false indicates a normal startup.
121      * @param startId Indicates the number of times the Vpn extension has been started. The startId is incremented
122      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
123      * value of startId is 6.
124      */
125     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
126 
127     /**
128      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
129      *
130      * The extension in the <b>STATE_STOP</b> is being destroyed.
131      * You can override this function to implement your own processing logic.
132      */
133     virtual void OnStop() override;
134 
135     /**
136      * @brief Called when the system configuration is updated.
137      *
138      * @param configuration Indicates the updated configuration information.
139      */
140     void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
141 
142     /**
143      * @brief Called when configuration changed, including system configuration and window configuration.
144      *
145      */
146     void ConfigurationUpdated();
147 
148     /**
149      * @brief Called when extension need dump info.
150      *
151      * @param params The params from vpn.
152      * @param info The dump info to show.
153      */
154     virtual void Dump(const std::vector<std::string> &params, std::vector<std::string> &info) override;
155 
156 private:
157     napi_value CallObjectMethod(const char* name, napi_value const *argv = nullptr, size_t argc = 0);
158 
159     void BindContext(napi_env env, napi_value obj);
160 
161     void GetSrcPath(std::string &srcPath);
162 
163     napi_value CallOnConnect(const AAFwk::Want &want);
164 
165     napi_value CallOnDisconnect(const AAFwk::Want &want, bool withResult = false);
166 
167     bool CheckPromise(napi_value result);
168 
169     bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo);
170 
171     void ListenWMS();
172 
173     JsRuntime& jsRuntime_;
174     std::unique_ptr<NativeReference> jsObj_;
175     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
176     std::shared_ptr<AbilityHandler> handler_ = nullptr;
177 
178 #ifdef SUPPORT_GRAPHICS
179 protected:
180     class JsVpnExtensionDisplayListener : public Rosen::DisplayManager::IDisplayListener {
181     public:
JsVpnExtensionDisplayListener(const std::weak_ptr<JsVpnExtension> & jsVpnExtension)182         explicit JsVpnExtensionDisplayListener(const std::weak_ptr<JsVpnExtension>& jsVpnExtension)
183         {
184             jsVpnExtension_ = jsVpnExtension;
185         }
186 
OnCreate(Rosen::DisplayId displayId)187         void OnCreate(Rosen::DisplayId displayId) override
188         {
189             auto sptr = jsVpnExtension_.lock();
190             if (sptr != nullptr) {
191                 sptr->OnCreate(displayId);
192             }
193         }
194 
OnDestroy(Rosen::DisplayId displayId)195         void OnDestroy(Rosen::DisplayId displayId) override
196         {
197             auto sptr = jsVpnExtension_.lock();
198             if (sptr != nullptr) {
199                 sptr->OnDestroy(displayId);
200             }
201         }
202 
OnChange(Rosen::DisplayId displayId)203         void OnChange(Rosen::DisplayId displayId) override
204         {
205             auto sptr = jsVpnExtension_.lock();
206             if (sptr != nullptr) {
207                 sptr->OnChange(displayId);
208             }
209         }
210 
211     private:
212         std::weak_ptr<JsVpnExtension> jsVpnExtension_;
213     };
214 
215     void OnCreate(Rosen::DisplayId displayId);
216     void OnDestroy(Rosen::DisplayId displayId);
217     void OnChange(Rosen::DisplayId displayId);
218 
219 private:
220     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
221     public:
SystemAbilityStatusChangeListener(sptr<JsVpnExtensionDisplayListener> displayListener)222         SystemAbilityStatusChangeListener(sptr<JsVpnExtensionDisplayListener> displayListener)
223             : tmpDisplayListener_(displayListener) {};
224         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)225         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {}
226 
227     private:
228         sptr<JsVpnExtensionDisplayListener> tmpDisplayListener_ = nullptr;
229     };
230 
231     sptr<JsVpnExtensionDisplayListener> displayListener_ = nullptr;
232 #endif
233 };
234 }  // namespace NetManagerStandard
235 }  // namespace OHOS
236 #endif  // OHOS_NETMANAGER_EXT_JS_VPN_EXTENSION_H
237