1 /* 2 * Copyright (c) 2021-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_DISTRIBUTED_SCHED_CONTINUATION_H 17 #define OHOS_DISTRIBUTED_SCHED_CONTINUATION_H 18 19 #include <cstdint> 20 #include <map> 21 #include <mutex> 22 23 #include "event_handler.h" 24 #include "iremote_object.h" 25 #include "refbase.h" 26 27 namespace OHOS { 28 namespace DistributedSchedule { 29 using FuncContinuationCallback = std::function<void(int32_t missionId)>; 30 31 class DSchedContinuation : public std::enable_shared_from_this<DSchedContinuation> { 32 public: 33 void Init(const FuncContinuationCallback& contCallback); 34 bool PushAbilityToken(int32_t sessionId, const sptr<IRemoteObject>& abilityToken); 35 sptr<IRemoteObject> PopAbilityToken(int32_t sessionId); 36 int32_t GenerateSessionId(); 37 bool IsInContinuationProgress(int32_t missionId); 38 void SetTimeOut(int32_t missionId, int32_t timeout); 39 void RemoveTimeOut(int32_t missionId); 40 bool PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback, 41 std::string deviceId, bool isFreeInstall); 42 sptr<IRemoteObject> PopCallback(int32_t missionId); 43 int32_t NotifyMissionCenterResult(int32_t missionId, int32_t resultCode); 44 bool IsFreeInstall(int32_t missionId); 45 std::string GetTargetDevice(int32_t missionId); 46 bool IsCleanMission(int32_t missionId); 47 void SetCleanMissionFlag(int32_t missionId, bool isCleanMission); 48 49 private: 50 class ContinuationHandler : public AppExecFwk::EventHandler { 51 public: ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::shared_ptr<DSchedContinuation> & continuationObj,const FuncContinuationCallback & contCallback)52 ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 53 const std::shared_ptr<DSchedContinuation>& continuationObj, 54 const FuncContinuationCallback& contCallback) 55 : AppExecFwk::EventHandler(runner), continuationObj_(continuationObj), contCallback_(contCallback) {} 56 ~ContinuationHandler() = default; 57 58 void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override; 59 private: 60 std::weak_ptr<DSchedContinuation> continuationObj_; 61 FuncContinuationCallback contCallback_; 62 }; 63 64 std::shared_ptr<ContinuationHandler> continuationHandler_; 65 std::mutex continuationLock_; 66 int32_t currSessionId_ = 1; 67 std::map<int32_t, sptr<IRemoteObject>> continuationMap_; 68 std::map<int32_t, sptr<IRemoteObject>> callbackMap_; 69 std::map<int32_t, bool> freeInstall_; 70 std::map<int32_t, bool> cleanMission_; 71 std::map<int32_t, std::string> continuationDevices_; 72 }; 73 } // namespace DistributedSchedule 74 } // namespace OHOS 75 76 #endif // OHOS_DISTRIBUTED_SCHED_CONTINUATION_H