1686862fbSopenharmony_ci/* 2686862fbSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4686862fbSopenharmony_ci * you may not use this file except in compliance with the License. 5686862fbSopenharmony_ci * You may obtain a copy of the License at 6686862fbSopenharmony_ci * 7686862fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8686862fbSopenharmony_ci * 9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12686862fbSopenharmony_ci * See the License for the specific language governing permissions and 13686862fbSopenharmony_ci * limitations under the License. 14686862fbSopenharmony_ci */ 15686862fbSopenharmony_ci 16686862fbSopenharmony_ci#ifndef OHOS_DMS_CALLBACK_TASK_H 17686862fbSopenharmony_ci#define OHOS_DMS_CALLBACK_TASK_H 18686862fbSopenharmony_ci 19686862fbSopenharmony_ci#include <atomic> 20686862fbSopenharmony_ci#include <cstdint> 21686862fbSopenharmony_ci#include <map> 22686862fbSopenharmony_ci#include <mutex> 23686862fbSopenharmony_ci 24686862fbSopenharmony_ci#include "event_handler.h" 25686862fbSopenharmony_ci#include "iremote_object.h" 26686862fbSopenharmony_ci#include "refbase.h" 27686862fbSopenharmony_ci#include "want.h" 28686862fbSopenharmony_ci 29686862fbSopenharmony_cinamespace OHOS { 30686862fbSopenharmony_cinamespace DistributedSchedule { 31686862fbSopenharmony_ciusing DmsCallbackTaskInitCallbackFunc = std::function<void(int64_t taskId)>; 32686862fbSopenharmony_ci 33686862fbSopenharmony_cienum class LaunchType { 34686862fbSopenharmony_ci FREEINSTALL_START = 0, 35686862fbSopenharmony_ci FREEINSTALL_CONTINUE = 1 36686862fbSopenharmony_ci}; 37686862fbSopenharmony_ci 38686862fbSopenharmony_cistruct CallbackTaskItem { 39686862fbSopenharmony_ci sptr<IRemoteObject> callback = nullptr; 40686862fbSopenharmony_ci int64_t taskId = -1; 41686862fbSopenharmony_ci LaunchType launchType = LaunchType::FREEINSTALL_START; 42686862fbSopenharmony_ci std::string deviceId = ""; 43686862fbSopenharmony_ci OHOS::AAFwk::Want want; 44686862fbSopenharmony_ci}; 45686862fbSopenharmony_ci 46686862fbSopenharmony_ciclass DmsCallbackTask : public std::enable_shared_from_this<DmsCallbackTask> { 47686862fbSopenharmony_cipublic: 48686862fbSopenharmony_ci void Init(const DmsCallbackTaskInitCallbackFunc& callback); 49686862fbSopenharmony_ci int64_t GenerateTaskId(); 50686862fbSopenharmony_ci int32_t PushCallback(int64_t taskId, const sptr<IRemoteObject>& callback, const std::string& deviceId, 51686862fbSopenharmony_ci LaunchType launchType, const OHOS::AAFwk::Want& want); 52686862fbSopenharmony_ci CallbackTaskItem PopCallback(int64_t taskId); 53686862fbSopenharmony_ci void SetContinuationMissionMap(int64_t taskId, int32_t missionId); 54686862fbSopenharmony_ci LaunchType GetLaunchType(int64_t taskId); 55686862fbSopenharmony_ci void PopContinuationMissionMap(int64_t taskId); 56686862fbSopenharmony_ci int64_t GetContinuaionMissionId(int64_t taskId); 57686862fbSopenharmony_ci void NotifyDeviceOffline(const std::string& deviceId); 58686862fbSopenharmony_ci 59686862fbSopenharmony_ciprivate: 60686862fbSopenharmony_ci class DmsCallbackHandler : public AppExecFwk::EventHandler { 61686862fbSopenharmony_ci public: 62686862fbSopenharmony_ci DmsCallbackHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 63686862fbSopenharmony_ci const std::shared_ptr<DmsCallbackTask>& callbackTask, const DmsCallbackTaskInitCallbackFunc& callback) 64686862fbSopenharmony_ci : AppExecFwk::EventHandler(runner), callbackTask_(callbackTask), callback_(callback) 65686862fbSopenharmony_ci { 66686862fbSopenharmony_ci } 67686862fbSopenharmony_ci ~DmsCallbackHandler() = default; 68686862fbSopenharmony_ci 69686862fbSopenharmony_ci void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override; 70686862fbSopenharmony_ci private: 71686862fbSopenharmony_ci std::weak_ptr<DmsCallbackTask> callbackTask_; 72686862fbSopenharmony_ci DmsCallbackTaskInitCallbackFunc callback_; 73686862fbSopenharmony_ci }; 74686862fbSopenharmony_ci 75686862fbSopenharmony_ci std::shared_ptr<DmsCallbackHandler> dmsCallbackHandler_; 76686862fbSopenharmony_ci std::mutex callbackMapMutex_; 77686862fbSopenharmony_ci std::mutex taskMutex_; 78686862fbSopenharmony_ci std::atomic<int64_t> currTaskId_ {1}; 79686862fbSopenharmony_ci std::map<int64_t, CallbackTaskItem> callbackMap_; 80686862fbSopenharmony_ci std::map<int64_t, int64_t> continuationMissionMap_; 81686862fbSopenharmony_ci}; 82686862fbSopenharmony_ci} // namespace DistributedSchedule 83686862fbSopenharmony_ci} // namespace OHOS 84686862fbSopenharmony_ci#endif // OHOS_DMS_CALLBACK_TASK_H 85