1 /* 2 * Copyright (c) 2021-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_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 47 private: 48 class ContinuationHandler : public AppExecFwk::EventHandler { 49 public: ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::shared_ptr<DSchedContinuation> & continuationObj,const FuncContinuationCallback & contCallback)50 ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 51 const std::shared_ptr<DSchedContinuation>& continuationObj, 52 const FuncContinuationCallback& contCallback) 53 : AppExecFwk::EventHandler(runner), continuationObj_(continuationObj), contCallback_(contCallback) {} 54 ~ContinuationHandler() = default; 55 56 void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override; 57 private: 58 std::weak_ptr<DSchedContinuation> continuationObj_; 59 FuncContinuationCallback contCallback_; 60 }; 61 62 std::shared_ptr<ContinuationHandler> continuationHandler_; 63 std::mutex continuationLock_; 64 int32_t currSessionId_ = 1; 65 std::map<int32_t, sptr<IRemoteObject>> continuationMap_; 66 std::map<int32_t, sptr<IRemoteObject>> callbackMap_; 67 std::map<int32_t, bool> freeInstall_; 68 std::map<int32_t, std::string> continuationDevices_; 69 }; 70 } // namespace DistributedSchedule 71 } // namespace OHOS 72 73 #endif // OHOS_DISTRIBUTED_SCHED_CONTINUATION_H