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_CONTINUATION_MANAGER_STAGE_H 17 #define OHOS_ABILITY_RUNTIME_CONTINUATION_MANAGER_STAGE_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include "ability_info.h" 23 #include "continuation_state.h" 24 #include "event_handler.h" 25 #include "iremote_object.h" 26 #include "want.h" 27 28 using OHOS::AAFwk::WantParams; 29 namespace OHOS { 30 namespace AbilityRuntime { 31 class UIAbility; 32 } 33 namespace AppExecFwk { 34 class ContinuationHandlerStage; 35 class IAbilityContinuation; 36 class ContinuationManagerStage : public std::enable_shared_from_this<ContinuationManagerStage> { 37 public: 38 /** 39 * @brief constructed function 40 */ 41 ContinuationManagerStage(); 42 virtual ~ContinuationManagerStage() = default; 43 44 /** 45 * @brief Init the ContinuationManagerStage 46 * @param ability Indicates the ability to Init 47 * @param continueToken Indicates the continueToken to Init 48 * @param abilityInfo Indicate the Ability information 49 * @param continuationHandler Indicate the continuation Handler instance 50 */ 51 bool Init(const std::shared_ptr<AbilityRuntime::UIAbility> &ability, const sptr<IRemoteObject> &continueToken, 52 const std::shared_ptr<AbilityInfo> &abilityInfo, 53 const std::shared_ptr<ContinuationHandlerStage> &continuationHandler); 54 55 /** 56 * @brief Obtains the migration state of this ability. 57 * @return Returns the migration state. 58 */ 59 ContinuationState GetContinuationState(); 60 61 /** 62 * @brief Obtains the ID of the source device from which this ability is migrated. 63 * @return Returns the source device ID. 64 */ 65 std::string GetOriginalDeviceId(); 66 67 /** 68 * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its 69 * ability slices must implement the IAbilityContinuation interface. 70 * @param deviceId Indicates the ID of the target device where this ability will be migrated to. 71 * @param versionCode Target bundle version. 72 */ 73 void ContinueAbilityWithStack(const std::string &deviceId, uint32_t versionCode); 74 75 /** 76 * @brief Migrates this ability to the given device on the same distributed network. The ability to migrate and its 77 * ability slices must implement the IAbilityContinuation interface. 78 * @param reversible Parameter of Boolean type, passed in true or false 79 * @param deviceId Indicates the ID of the target device where this ability will be migrated to. If this parameter 80 * is null, this method has the same effect as continueAbility(). 81 */ 82 void ContinueAbility(bool reversible, const std::string &deviceId); 83 84 /** 85 * @brief Reverse Continue Ability 86 * @return If the success returns true, the failure returns false. 87 */ 88 bool ReverseContinueAbility(); 89 90 /** 91 * @brief Start Continuation Ability 92 * @return If the success returns true, the failure returns false. 93 */ 94 bool StartContinuation(); 95 96 /** 97 * @brief Prepare user data of local Ability. 98 * @param wantParams Indicates the user data to be saved. 99 * @return If the ability is willing to continue and data saved successfully, it returns 0; 100 * otherwise, it returns errcode. 101 */ 102 int32_t OnContinue(WantParams &wantParams); 103 104 /** 105 * @brief OnStart And Save Data 106 * @param wantParams Indicates the user data. 107 * @return If the success code is returned successfully, otherwise the failure code is returned. 108 */ 109 int32_t OnStartAndSaveData(WantParams &wantParams); 110 111 /** 112 * @brief Determine whether to continue the continuous management phase of the page stack 113 * @param wantParams Indicates the user data. 114 * @return If you want to continue the continuous management phase of the 115 * page stack, return true, otherwise return false 116 */ 117 bool IsContinuePageStack(const WantParams &wantParams); 118 119 /** 120 * @brief Handle the continuation request and retrieve content information if needed 121 * @param wantParams Indicates the user data. 122 * @return An error code indicating the success or failure of the operation 123 */ 124 int32_t OnContinueAndGetContent(WantParams &wantParams); 125 126 /** 127 * @brief Save Data for continuation 128 * @param saveData Indicates WantParams data to be saved 129 * @return If the success returns true, the failure returns false. 130 */ 131 bool SaveData(WantParams &saveData); 132 133 /** 134 * @brief Restore data for continuation 135 * @param restoreData The WantParams containing the data 136 * @param reversible reversible A flag indicating whether the continuation is reversible 137 * @param originalDeviceId the original device ID 138 * @return If the success returns true, othrewise returns false. 139 */ 140 bool RestoreData(const WantParams &restoreData, bool reversible, const std::string &originalDeviceId); 141 142 /** 143 * @brief Notifies the completion of continuation 144 * @param originDeviceId The ID of the originating device 145 * @param sessionId The session ID associated with the continuation 146 * @param success A flag indicating the success of the continuation 147 * @param reverseScheduler A remote object for reverse scheduling 148 */ 149 void NotifyCompleteContinuation( 150 const std::string &originDeviceId, int sessionId, bool success, 151 [[maybe_unused]] const sptr<IRemoteObject> &reverseScheduler); 152 153 /** 154 * @brief complete the continuation process 155 * @param result The result of the continuation process 156 */ 157 void CompleteContinuation(int result); 158 159 /** 160 * @brief Restore from a remote continuation 161 * @param The WantParams containing the data for restoration 162 * @return true if restoration from was successful,otherwise false 163 */ 164 bool RestoreFromRemote(const WantParams &restoreData); 165 166 /** 167 * @brief Notify that remote continuation has terminated 168 * @return true indicating successful notification of remote termination,otherwise false 169 */ 170 bool NotifyRemoteTerminated(); 171 172 /** 173 * @brief Change the process state to Initial state and remove timeout task 174 */ 175 void ChangeProcessStateToInit(); 176 177 enum OnContinueResult { 178 AGREE = 0, 179 REJECT = 1, 180 MISMATCH = 2 181 }; 182 private: 183 enum ProgressState { INITIAL, WAITING_SCHEDULE, IN_PROGRESS }; 184 bool CheckContinuationIllegal(); 185 bool HandleContinueAbilityWithStack(const std::string &deviceId, uint32_t versionCode); 186 bool HandleContinueAbility(bool reversible, const std::string &deviceId); 187 ProgressState GetProcessState(); 188 void ChangeProcessState(const ProgressState &newState); 189 void RestoreStateWhenTimeout(long timeoutInMs, const ProgressState &preState); 190 void InitMainHandlerIfNeed(); 191 bool CheckAbilityToken(); 192 void CheckDmsInterfaceResult(int result, const std::string &interfaceName); 193 bool DoScheduleStartContinuation(); 194 bool DoScheduleSaveData(WantParams &saveData); 195 bool DoScheduleRestoreData(const WantParams &restoreData); 196 bool DoRestoreFromRemote(const WantParams &restoreData); 197 #ifdef SUPPORT_GRAPHICS 198 bool GetContentInfo(WantParams &wantParams); 199 #endif 200 sptr<IRemoteObject> continueToken_ = nullptr; 201 std::weak_ptr<AbilityRuntime::UIAbility> ability_; 202 std::weak_ptr<AbilityInfo> abilityInfo_; 203 ProgressState progressState_ = ProgressState::INITIAL; 204 bool reversible_ = false; 205 ContinuationState continuationState_ = ContinuationState::LOCAL_RUNNING; 206 std::string originalDeviceId_; 207 std::weak_ptr<ContinuationHandlerStage> continuationHandler_; 208 std::shared_ptr<EventHandler> mainHandler_ = nullptr; 209 std::mutex lock_; 210 }; 211 } // namespace AppExecFwk 212 } // namespace OHOS 213 #endif // OHOS_ABILITY_RUNTIME_CONTINUATION_MANAGER_STAGE_H 214