1 /* 2 * Copyright (c) 2021 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_H 17 #define OHOS_ABILITY_RUNTIME_CONTINUATION_MANAGER_H 18 19 #include <memory> 20 #include <mutex> 21 #include "continuation_state.h" 22 #include "ability_info.h" 23 #include "event_handler.h" 24 #include "iremote_object.h" 25 #include "want.h" 26 27 using OHOS::AAFwk::WantParams; 28 namespace OHOS { 29 namespace AppExecFwk { 30 class Ability; 31 class ContinuationHandler; 32 class IAbilityContinuation; 33 class ContinuationManager : public std::enable_shared_from_this<ContinuationManager> { 34 public: 35 ContinuationManager(); 36 virtual ~ContinuationManager() = default; 37 38 bool Init(const std::shared_ptr<Ability> &ability, const sptr<IRemoteObject> &continueToken, 39 const std::shared_ptr<AbilityInfo> &abilityInfo, 40 const std::shared_ptr<ContinuationHandler> &continuationHandler); 41 42 ContinuationState GetContinuationState(); 43 44 std::string GetOriginalDeviceId(); 45 46 void ContinueAbilityWithStack(const std::string &deviceId, uint32_t versionCode); 47 48 void ContinueAbility(bool reversible, const std::string &deviceId); 49 50 bool ReverseContinueAbility(); 51 52 bool StartContinuation(); 53 54 int32_t OnContinue(WantParams &wantParams); 55 56 int32_t OnStartAndSaveData(WantParams &wantParams); 57 58 bool IsContinuePageStack(const WantParams &wantParams); 59 60 int32_t OnContinueAndGetContent(WantParams &wantParams); 61 62 bool SaveData(WantParams &saveData); 63 64 bool RestoreData(const WantParams &restoreData, bool reversible, const std::string &originalDeviceId); 65 66 void NotifyCompleteContinuation( 67 const std::string &originDeviceId, int sessionId, bool success, const sptr<IRemoteObject> &reverseScheduler); 68 69 void CompleteContinuation(int result); 70 71 bool RestoreFromRemote(const WantParams &restoreData); 72 73 bool NotifyRemoteTerminated(); 74 75 void ChangeProcessStateToInit(); 76 77 enum OnContinueResult { 78 AGREE = 0, 79 REJECT = 1, 80 MISMATCH = 2 81 }; 82 private: 83 enum ProgressState { INITIAL, WAITING_SCHEDULE, IN_PROGRESS }; 84 85 bool CheckContinuationIllegal(); 86 87 bool HandleContinueAbilityWithStack(const std::string &deviceId, uint32_t versionCode); 88 89 bool HandleContinueAbility(bool reversible, const std::string &deviceId); 90 91 ProgressState GetProcessState(); 92 93 void ChangeProcessState(const ProgressState &newState); 94 95 void RestoreStateWhenTimeout(long timeoutInMs, const ProgressState &preState); 96 97 void InitMainHandlerIfNeed(); 98 99 bool CheckAbilityToken(); 100 101 void CheckDmsInterfaceResult(int result, const std::string &interfaceName); 102 103 bool DoScheduleStartContinuation(); 104 105 bool DoScheduleSaveData(WantParams &saveData); 106 107 bool DoScheduleRestoreData(const WantParams &restoreData); 108 109 bool DoRestoreFromRemote(const WantParams &restoreData); 110 #ifdef SUPPORT_GRAPHICS 111 bool GetContentInfo(WantParams &wantParams); 112 #endif 113 sptr<IRemoteObject> continueToken_ = nullptr; 114 std::weak_ptr<Ability> ability_; 115 std::weak_ptr<AbilityInfo> abilityInfo_; 116 ProgressState progressState_ = ProgressState::INITIAL; 117 bool reversible_ = false; 118 ContinuationState continuationState_ = ContinuationState::LOCAL_RUNNING; 119 std::string originalDeviceId_; 120 std::weak_ptr<ContinuationHandler> continuationHandler_; 121 std::shared_ptr<EventHandler> mainHandler_ = nullptr; 122 std::mutex lock_; 123 124 static const int TIMEOUT_MS_WAIT_DMS_SCHEDULE_START_CONTINUATION; 125 static const int TIMEOUT_MS_WAIT_DMS_NOTIFY_CONTINUATION_COMPLETE; 126 static const int TIMEOUT_MS_WAIT_REMOTE_NOTIFY_BACK; 127 }; 128 } // namespace AppExecFwk 129 } // namespace OHOS 130 #endif // OHOS_ABILITY_RUNTIME_CONTINUATION_MANAGER_H 131