1 /*
2  * Copyright (c) 2023-2024 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_ABILITY_RUNTIME_DISTRIBUTED_MISSION_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_DISTRIBUTED_MISSION_MANAGER_H
18 
19 #include <uv.h>
20 
21 #include "distributed_mission_manager_helper.h"
22 #include "mission_continue_interface.h"
23 #include "mission_continue_stub.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "js_runtime_utils.h"
27 #include "securec.h"
28 #include "want.h"
29 #include "remote_mission_listener_stub.h"
30 #include "remote_on_listener_stub.h"
31 
32 namespace OHOS {
33 namespace AAFwk {
34 using namespace std;
35 napi_value NAPI_StartSyncRemoteMissions(napi_env env, napi_callback_info info);
36 napi_value NAPI_StopSyncRemoteMissions(napi_env env, napi_callback_info info);
37 napi_value NAPI_RegisterMissionListener(napi_env env, napi_callback_info info);
38 napi_value NAPI_UnRegisterMissionListener(napi_env env, napi_callback_info info);
39 napi_value NAPI_NotifyToOn(napi_env env, napi_callback_info info);
40 napi_value NAPI_NotifyToOff(napi_env env, napi_callback_info info);
41 napi_value NAPI_ContinueAbility(napi_env env, napi_callback_info info);
42 napi_value WrapString(napi_env &env, const std::string &deviceId, const std::string &paramName);
43 napi_value WrapInt32(napi_env &env, int32_t num, const std::string &paramName);
44 napi_value CreateInt32(napi_env &env, int32_t num, const std::string &paramName);
45 
46 class NAPIMissionContinue : public MissionContinueStub {
47 public:
48     void OnContinueDone(int32_t result) override;
49 
SetEnv(const napi_env & env)50     void SetEnv(const napi_env &env)
51     {
52         env_ = env;
53     }
54 
SetContinueAbilityEnv(const napi_env & env)55     void SetContinueAbilityEnv(const napi_env &env)
56     {
57         env_ = env;
58     }
59 
SetContinueAbilityCBRef(const napi_ref & ref)60     void SetContinueAbilityCBRef(const napi_ref &ref)
61     {
62         onContinueDoneRef_ = ref;
63     }
64 
SetContinueAbilityHasBundleName(bool hasBundleName)65     void SetContinueAbilityHasBundleName(bool hasBundleName)
66     {
67         onContinueDoneHasBundleName_ = hasBundleName;
68     }
69 
SetContinueAbilityPromiseRef(const napi_deferred & promiseDeferred)70     void SetContinueAbilityPromiseRef(const napi_deferred &promiseDeferred)
71     {
72         promiseDeferred_ = promiseDeferred;
73     }
74 
75 private:
76     bool onContinueDoneHasBundleName_ = false;
77     napi_env env_ = nullptr;
78     napi_ref onContinueDoneRef_ = nullptr;
79     napi_deferred promiseDeferred_ = nullptr;
80 };
81 
82 class NAPIRemoteMissionListener : public AAFwk::RemoteMissionListenerStub {
83 public:
84     virtual ~NAPIRemoteMissionListener();
85 
86     void NotifyMissionsChanged(const std::string &deviceId) override;
87     void NotifySnapshot(const std::string &deviceId, int32_t missionId) override;
88     void NotifyNetDisconnect(const std::string &deviceId, int32_t state) override;
89     void SetEnv(const napi_env &env);
90     void SetNotifyMissionsChangedCBRef(const napi_ref &ref);
91     void SetNotifySnapshotCBRef(const napi_ref &ref);
92     void SetNotifyNetDisconnectCBRef(const napi_ref &ref);
93 
94 private:
95     napi_env env_ = nullptr;
96     napi_ref notifyMissionsChangedRef_ = nullptr;
97     napi_ref notifySnapshotRef_ = nullptr;
98     napi_ref notifyNetDisconnectRef_ = nullptr;
99 };
100 
101 class NAPIRemoteOnListener : public AAFwk::RemoteOnListenerStub {
102 public:
~NAPIRemoteOnListener()103     virtual ~NAPIRemoteOnListener() {};
104 
105     void OnCallback(const uint32_t continueState, const std::string &srcDeviceId,
106         const std::string &bundleName, const std::string &continueType = "",
107         const std::string &srcBundleName = "") override;
108     void SetEnv(const napi_env &env);
109     void SetOnCallbackCBRef(std::shared_ptr<NativeReference> &ref);
110     std::vector<std::shared_ptr<NativeReference>> GetOnCallbackCBRef();
111     bool DelOnCallbackCBRef(napi_env env, std::shared_ptr<NativeReference> &ref);
112 
113 private:
114     napi_env env_ = nullptr;
115     std::vector<std::shared_ptr<NativeReference>> callbacks_;
116 };
117 
118 struct CallbackInfo {
119     napi_env env;
120     napi_ref callback;
121     std::vector<std::shared_ptr<NativeReference>> vecCallbacks;
122     napi_deferred deferred;
123 };
124 
125 struct CBBase {
126     CallbackInfo cbInfo;
127     napi_async_work asyncWork = nullptr;
128     napi_deferred deferred = nullptr;
129     int errCode = 0;
130 };
131 
132 struct MissionRegistrationCB {
133     napi_env env = nullptr;
134     napi_ref callback[3] = {nullptr};
135     int resultCode = 0;
136 };
137 
138 struct RegisterMissionCB {
139     CBBase cbBase;
140     std::string deviceId;
141     sptr<NAPIRemoteMissionListener> missionRegistration;
142     MissionRegistrationCB missionRegistrationCB;
143     int result = 0;
144     int missionId = 0;
145     int state = 0;
146     napi_ref callbackRef;
147 };
148 
149 struct OnCallbackCB {
150     napi_env env = nullptr;
151     napi_ref callback = nullptr;
152     std::shared_ptr<NativeReference> napiCallback;
153     int resultCode = 0;
154 };
155 
156 struct OnCB {
157     CBBase cbBase;
158     std::string type;
159     sptr<NAPIRemoteOnListener> onRegistration;
160     int continueState = 0;
161     std::string srcDeviceId;
162     std::string bundleName;
163     std::string continueType;
164     std::string srcBundleName;
165     OnCallbackCB onCallbackCB;
166     int result = 0;
167     napi_ref callbackRef;
168 };
169 
170 struct AbilityContinuationCB {
171     napi_env env;
172     napi_ref callback[1] = {nullptr};
173 };
174 
175 struct ContinueAbilityCB {
176     CBBase cbBase;
177     std::string dstDeviceId;
178     std::string srcDeviceId;
179     sptr<NAPIMissionContinue> abilityContinuation;
180     AbilityContinuationCB abilityContinuationCB;
181     AAFwk::WantParams wantParams;
182     ErrCode result = 0;
183     int resultCode = 0;
184     int missionId = 0;
185     std::string bundleName;
186     std::string srcBundleName;
187     std::string continueType;
188     bool hasArgsWithBundleName = false;
189     napi_ref callbackRef = nullptr;
190 };
191 
192 struct SyncRemoteMissionsContext {
193     napi_env env;
194     napi_async_work work;
195 
196     std::string deviceId;
197     size_t valueLen = 0;
198     bool fixConflict = false;
199     int64_t tag = -1;
200     int result = 0;
201 
202     napi_deferred deferred;
203     napi_ref callbackRef;
204 };
205 
206 bool SetSyncRemoteMissionsContext(const napi_env &env, const napi_value &value,
207     SyncRemoteMissionsContext* context, std::string &errInfo);
208 bool ProcessSyncInput(napi_env &env, napi_callback_info info, bool isStart,
209     SyncRemoteMissionsContext* syncContext, std::string &errInfo);
210 void ReturnValueToApplication(napi_env &env, napi_value *result, RegisterMissionCB *registerMissionCB);
211 void ReturnValueToApplication(napi_env &env, napi_value *result, OnCB *onCB);
212 void CallbackReturn(napi_value *result, RegisterMissionCB *registerMissionCB);
213 napi_value GetUndefined();
214 mutex registrationLock_;
215 mutex onLock_;
216 map<std::string, sptr<NAPIRemoteMissionListener>> registration_;
217 map<std::string, sptr<NAPIRemoteOnListener>> registrationOfOn_;
218 
219 enum ErrorCode {
220     NO_ERROR = 0,
221     INVALID_PARAMETER = -1,
222     REMOTE_MISSION_NOT_FOUND = -2,
223     PERMISSION_DENY = -3,
224     REGISTRATION_NOT_FOUND = -4,
225     /**
226      * Result(201) for permission denied.
227      */
228     PERMISSION_DENIED = 201,
229     /**
230      * Result(202) for non-system-app use system-api.
231      */
232     NOT_SYSTEM_APP = 202,
233     /**
234      * Result(401) for parameter check failed.
235      */
236     PARAMETER_CHECK_FAILED = 401,
237     /**
238      * Result(16300501) for the system ability work abnormally.
239      */
240     SYSTEM_WORK_ABNORMALLY = 16300501,
241     /**
242      * Result(29360221) for failed to get the missionInfo of the specified missionId.
243      */
244     NO_MISSION_INFO_FOR_MISSION_ID = 29360221,
245     /**
246      * Result(16300503) for the application is not installed on the remote end and installation-free is
247      * not supported.
248      */
249     REMOTE_UNINSTALLED_AND_UNSUPPORT_FREEINSTALL_FOR_CONTINUE = 16300503,
250     /**
251      * Result(16300504) for the application is not installed on the remote end but installation-free is
252      * supported, try again with freeInstall flag.
253      */
254     CONTINUE_WITHOUT_FREEINSTALL_FLAG = 16300504,
255     /**
256      * Result(16300506) throw to js for the local continuation task is already in progress.
257      */
258     ERR_CONTINUE_ALREADY_IN_PROGRESS = 16300506,
259     /**
260      * Result(16300507) throw to js for Failed to get the missionInfo of the specified bundle name.
261      */
262     ERR_GET_MISSION_INFO_OF_BUNDLE_NAME = 16300507,
263     /**
264      * Result(16300508) throw to js for bind error due to the remote device hotspot enable, try again after disable
265      * the remote device hotspot.
266      */
267     ERR_BIND_REMOTE_HOTSPOT_ENABLE_STATE = 16300508,
268     /**
269      * Result(16300509) throw to js for the remote device has been linked with other devices, try again when
270      * the remote device is idle.
271      */
272     ERR_BIND_REMOTE_IN_BUSY_LINK = 16300509,
273     /**
274      * Result(29360222) for the operation device must be the device where the application to be continued
275      * is located or the target device to be continued.
276      */
277     OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET = 29360222,
278     /**
279      * Result(29360223) for the local continuation task is already in progress.
280      */
281     CONTINUE_ALREADY_IN_PROGRESS = 29360223,
282     /**
283      * Result(29360224) for the mission is dead, try again after restart mission.
284      */
285     MISSION_FOR_CONTINUING_IS_NOT_ALIVE = 29360224,
286     /*
287      * Result(29360144) for get local deviceid fail.
288      */
289     GET_LOCAL_DEVICE_ERR = 29360144,
290     /**
291      * Result(29360174) for get remote dms fail.
292      */
293     GET_REMOTE_DMS_FAIL = 29360174,
294     /*
295      * Result(29360202) for continue remote not install and support free install.
296      */
297     CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL = 29360202,
298     /*
299      * Result(29360203) for continue remote not install and not support free install.
300      */
301     CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL = 29360203,
302 };
303 }  // namespace AAFwk
304 }  // namespace OHOS
305 #endif  // OHOS_ABILITY_RUNTIME_DISTRIBUTED_MISSION_MANAGER_H
306