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 FOUNDATION_APPEXECFWK_OHOS_CONTINUATION_MANAGER_H 17 #define FOUNDATION_APPEXECFWK_OHOS_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); 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 int32_t OnContinueAndGetContent(WantParams &wantParams); 59 60 bool SaveData(WantParams &saveData); 61 62 bool RestoreData(const WantParams &restoreData, bool reversible, const std::string &originalDeviceId); 63 64 void NotifyCompleteContinuation( 65 const std::string &originDeviceId, int sessionId, bool success, const sptr<IRemoteObject> &reverseScheduler); 66 67 void CompleteContinuation(int result); 68 69 bool RestoreFromRemote(const WantParams &restoreData); 70 71 bool NotifyRemoteTerminated(); 72 73 void ChangeProcessStateToInit(); 74 75 enum OnContinueResult { 76 AGREE = 0, 77 Reject = 1, 78 MISMATCH = 2 79 }; 80 private: 81 enum ProgressState { INITIAL, WAITING_SCHEDULE, IN_PROGRESS }; 82 83 bool CheckContinuationIllegal(); 84 85 bool HandleContinueAbilityWithStack(const std::string &deviceId); 86 87 bool HandleContinueAbility(bool reversible, const std::string &deviceId); 88 89 ProgressState GetProcessState(); 90 91 void ChangeProcessState(const ProgressState &newState); 92 93 void RestoreStateWhenTimeout(long timeoutInMs, const ProgressState &preState); 94 95 void InitMainHandlerIfNeed(); 96 97 bool CheckAbilityToken(); 98 99 void CheckDmsInterfaceResult(int result, const std::string &interfaceName); 100 101 bool DoScheduleStartContinuation(); 102 103 bool DoScheduleSaveData(WantParams &saveData); 104 105 bool DoScheduleRestoreData(const WantParams &restoreData); 106 107 bool DoRestoreFromRemote(const WantParams &restoreData); 108 #ifdef SUPPORT_GRAPHICS 109 bool GetContentInfo(WantParams &wantParams); 110 #endif 111 sptr<IRemoteObject> continueToken_ = nullptr; 112 std::weak_ptr<Ability> ability_; 113 std::weak_ptr<AbilityInfo> abilityInfo_; 114 ProgressState progressState_ = ProgressState::INITIAL; 115 bool reversible_ = false; 116 ContinuationState continuationState_ = ContinuationState::LOCAL_RUNNING; 117 std::string originalDeviceId_; 118 std::weak_ptr<ContinuationHandler> continuationHandler_; 119 std::shared_ptr<EventHandler> mainHandler_ = nullptr; 120 std::mutex lock_; 121 122 static const int TIMEOUT_MS_WAIT_DMS_SCHEDULE_START_CONTINUATION; 123 static const int TIMEOUT_MS_WAIT_DMS_NOTIFY_CONTINUATION_COMPLETE; 124 static const int TIMEOUT_MS_WAIT_REMOTE_NOTIFY_BACK; 125 }; 126 } // namespace AppExecFwk 127 } // namespace OHOS 128 #endif // FOUNDATION_APPEXECFWK_OHOS_CONTINUATION_MANAGER_H