1 /* 2 * Copyright (c) 2022 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 29 namespace OHOS { 30 namespace AAFwk { 31 using namespace std; 32 napi_value NAPI_StartSyncRemoteMissions(napi_env env, napi_callback_info info); 33 napi_value NAPI_StopSyncRemoteMissions(napi_env env, napi_callback_info info); 34 napi_value NAPI_RegisterMissionListener(napi_env env, napi_callback_info info); 35 napi_value NAPI_UnRegisterMissionListener(napi_env env, napi_callback_info info); 36 napi_value NAPI_ContinueAbility(napi_env env, napi_callback_info info); 37 napi_value WrapString(napi_env env, const std::string &deviceId, const std::string ¶mName); 38 napi_value WrapInt32(napi_env env, int32_t num, const std::string ¶mName); 39 40 class NAPIMissionContinue : public MissionContinueStub { 41 public: 42 void OnContinueDone(int32_t result) override; 43 void SetContinueEnv(const napi_env &env); 44 void SetEnv(const napi_env &env); 45 void SetContinueAbilityEnv(const napi_env &env); 46 void SetContinueAbilityCBRef(const napi_ref &ref); 47 48 private: 49 napi_env env_ = nullptr; 50 napi_ref onContinueDoneRef_ = nullptr; 51 }; 52 53 class NAPIRemoteMissionListener : public AAFwk::RemoteMissionListenerStub { 54 public: 55 virtual ~NAPIRemoteMissionListener(); 56 57 void NotifyMissionsChanged(const std::string& deviceId) override; 58 void NotifySnapshot(const std::string& deviceId, int32_t missionId) override; 59 void NotifyNetDisconnect(const std::string& deviceId, int32_t state) override; 60 void SetEnv(const napi_env &env); 61 void SetNotifyMissionsChangedCBRef(const napi_ref &ref); 62 void SetNotifySnapshotCBRef(const napi_ref &ref); 63 void SetNotifyNetDisconnectCBRef(const napi_ref &ref); 64 65 private: 66 napi_env env_ = nullptr; 67 napi_ref notifyMissionsChangedRef_ = nullptr; 68 napi_ref notifySnapshotRef_ = nullptr; 69 napi_ref notifyNetDisconnectRef_ = nullptr; 70 }; 71 72 struct CallbackInfo { 73 napi_env env; 74 napi_ref callback; 75 napi_deferred deferred; 76 }; 77 78 struct CBBase { 79 CallbackInfo cbInfo; 80 napi_async_work asyncWork; 81 napi_deferred deferred; 82 int errCode = 0; 83 }; 84 85 struct MissionRegistrationCB { 86 napi_env env = nullptr; 87 napi_ref callback[3] = {nullptr}; 88 int resultCode = 0; 89 }; 90 91 struct RegisterMissionCB { 92 CBBase cbBase; 93 std::string deviceId; 94 sptr<NAPIRemoteMissionListener> missionRegistration; 95 MissionRegistrationCB missionRegistrationCB; 96 int result = 0; 97 int missionId = 0; 98 int state = 0; 99 napi_ref callbackRef; 100 }; 101 102 struct AbilityContinuationCB { 103 napi_env env; 104 napi_ref callback[1] = {nullptr}; 105 }; 106 107 struct ContinueAbilityCB { 108 CBBase cbBase; 109 std::string dstDeviceId; 110 std::string srcDeviceId; 111 sptr<NAPIMissionContinue> abilityContinuation; 112 AbilityContinuationCB abilityContinuationCB; 113 AAFwk::WantParams wantParams; 114 ErrCode result = 0; 115 int resultCode = 0; 116 int missionId = 0; 117 napi_ref callbackRef; 118 }; 119 120 struct SyncRemoteMissionsContext { 121 napi_env env; 122 napi_async_work work; 123 124 std::string deviceId; 125 size_t valueLen = 0; 126 bool fixConflict = false; 127 int64_t tag = -1; 128 int result = 0; 129 130 napi_deferred deferred; 131 napi_ref callbackRef; 132 }; 133 134 int32_t ErrorCodeReturn(int32_t code); 135 bool SetSyncRemoteMissionsContext(const napi_env &env, const napi_value &value, 136 SyncRemoteMissionsContext* context, std::string &errInfo); 137 bool ProcessSyncInput(napi_env env, napi_callback_info info, bool isStart, 138 SyncRemoteMissionsContext* syncContext, std::string &errInfo); 139 void ReturnValueToApplication(napi_env env, napi_value *result, RegisterMissionCB *registerMissionCB); 140 void CallbackReturn(napi_value *result, RegisterMissionCB *registerMissionCB); 141 napi_value GetUndefined(); 142 mutex registrationLock_; 143 map<std::string, sptr<NAPIRemoteMissionListener>> registration_; 144 145 enum ErrorCode { 146 NO_ERROR = 0, 147 INVALID_PARAMETER = -1, 148 REMOTE_MISSION_NOT_FOUND = -2, 149 PERMISSION_DENY = -3, 150 REGISTRATION_NOT_FOUND = -4, 151 /* 152 * Result(29360203) for continue remote not install and not support free install. 153 */ 154 CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL = 29360203, 155 /* 156 * Result(29360202) for continue remote not install and support free install. 157 */ 158 CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL = 29360202, 159 /** 160 * Result(201) for permission denied. 161 */ 162 PERMISSION_DENIED = 201, 163 /** 164 * Result(401) for parameter check failed. 165 */ 166 PARAMETER_CHECK_FAILED = 401, 167 /** 168 * Result(16300501) for the system ability work abnormally. 169 */ 170 SYSTEM_WORK_ABNORMALLY = 16300501, 171 /** 172 * Result(16300502) for failed to get the missionInfo of the specified missionId. 173 */ 174 NO_MISSION_INFO_FOR_MISSION_ID = 16300502, 175 /** 176 * Result(16300503) for the application is not installed on the remote end and installation-free is 177 * not supported. 178 */ 179 REMOTE_UNINSTALLED_AND_UNSUPPORT_FREEINSTALL_FOR_CONTINUE = 16300503, 180 /** 181 * Result(16300504) for the application is not installed on the remote end but installation-free is 182 * supported, try again with freeInstall flag. 183 */ 184 CONTINUE_WITHOUT_FREEINSTALL_FLAG = 16300504, 185 /** 186 * Result(16300505) for the operation device must be the device where the application to be continued 187 * is located or the target device to be continued. 188 */ 189 OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET = 16300505, 190 /** 191 * Result(16300506) for the local continuation task is already in progress. 192 */ 193 CONTINUE_ALREADY_IN_PROGRESS = 16300506, 194 /** 195 * Result(16300507) for the mission is dead, try again after restart mission. 196 */ 197 MISSION_FOR_CONTINUING_IS_NOT_ALIVE = 16300507, 198 }; 199 } // namespace AAFwk 200 } // namespace OHOS 201 #endif // OHOS_ABILITY_RUNTIME_DISTRIBUTED_MISSION_MANAGER_H 202