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_DISTRIBUTED_MISSION_MANAGER_H 17 #define OHOS_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 void NotifyMissionsChanged(const std::string& deviceId) override; 56 void NotifySnapshot(const std::string& deviceId, int32_t missionId) override; 57 void NotifyNetDisconnect(const std::string& deviceId, int32_t state) override; 58 void SetEnv(const napi_env &env); 59 void SetNotifyMissionsChangedCBRef(const napi_ref &ref); 60 void SetNotifySnapshotCBRef(const napi_ref &ref); 61 void SetNotifyNetDisconnectCBRef(const napi_ref &ref); 62 63 private: 64 napi_env env_ = nullptr; 65 napi_ref notifyMissionsChangedRef_ = nullptr; 66 napi_ref notifySnapshotRef_ = nullptr; 67 napi_ref notifyNetDisconnectRef_ = nullptr; 68 }; 69 70 struct CallbackInfo { 71 napi_env env; 72 napi_ref callback; 73 napi_deferred deferred; 74 }; 75 76 struct CBBase { 77 CallbackInfo cbInfo; 78 napi_async_work asyncWork; 79 napi_deferred deferred; 80 int errCode = 0; 81 }; 82 83 struct MissionRegistrationCB { 84 napi_env env = nullptr; 85 napi_ref callback[3] = {0}; 86 int resultCode = 0; 87 }; 88 89 struct RegisterMissonCB { 90 CBBase cbBase; 91 std::string deviceId; 92 sptr<NAPIRemoteMissionListener> missionRegistration; 93 MissionRegistrationCB missionRegistrationCB; 94 int result = 0; 95 int missionId = 0; 96 int state = 0; 97 napi_ref callbackRef; 98 }; 99 100 struct AbilityContinuationCB { 101 napi_env env; 102 napi_ref callback[1] = {0}; 103 }; 104 105 struct ContinueAbilityCB { 106 CBBase cbBase; 107 std::string dstDeviceId; 108 std::string srcDeviceId; 109 sptr<NAPIMissionContinue> abilityContinuation; 110 AbilityContinuationCB abilityContinuationCB; 111 AAFwk::WantParams wantParams; 112 ErrCode result = 0; 113 int resultCode = 0; 114 int missionId = 0; 115 napi_ref callbackRef; 116 }; 117 118 struct SyncRemoteMissionsContext { 119 napi_env env; 120 napi_async_work work; 121 122 std::string deviceId; 123 size_t valueLen = 0; 124 bool fixConflict = false; 125 int64_t tag = -1; 126 int result = 0; 127 128 napi_deferred deferred; 129 napi_ref callbackRef; 130 }; 131 132 bool SetSyncRemoteMissionsContext( 133 const napi_env &env, const napi_value &value, SyncRemoteMissionsContext* context); 134 bool ProcessSyncInput(napi_env env, napi_callback_info info, bool isStart, 135 SyncRemoteMissionsContext* syncContext); 136 mutex registrationLock_; 137 map<std::string, sptr<NAPIRemoteMissionListener>> registration_; 138 139 enum ErrorCode { 140 NO_ERROR = 0, 141 INVALID_PARAMETER = -1, 142 REMOTE_MISSION_NOT_FOUND = -2, 143 PERMISSION_DENY = -3, 144 REGISTRATION_NOT_FOUND = -4, 145 }; 146 } // namespace AAFwk 147 } // namespace OHOS 148 #endif // OHOS_DISTRIBUTED_MISSION_MANAGER_H 149