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