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