1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2021-2023 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_DISTRIBUTED_SCHED_CONTINUATION_H
17686862fbSopenharmony_ci#define OHOS_DISTRIBUTED_SCHED_CONTINUATION_H
18686862fbSopenharmony_ci
19686862fbSopenharmony_ci#include <cstdint>
20686862fbSopenharmony_ci#include <map>
21686862fbSopenharmony_ci#include <mutex>
22686862fbSopenharmony_ci
23686862fbSopenharmony_ci#include "event_handler.h"
24686862fbSopenharmony_ci#include "iremote_object.h"
25686862fbSopenharmony_ci#include "refbase.h"
26686862fbSopenharmony_ci
27686862fbSopenharmony_ci#include "distributed_event_died_listener.h"
28686862fbSopenharmony_ci#include "distributed_sched_types.h"
29686862fbSopenharmony_ci
30686862fbSopenharmony_cinamespace OHOS {
31686862fbSopenharmony_cinamespace DistributedSchedule {
32686862fbSopenharmony_ciusing FuncContinuationCallback = std::function<void(int32_t missionId)>;
33686862fbSopenharmony_ci
34686862fbSopenharmony_ciclass DSchedContinuation : public std::enable_shared_from_this<DSchedContinuation> {
35686862fbSopenharmony_cipublic:
36686862fbSopenharmony_ci    void Init(const FuncContinuationCallback& contCallback);
37686862fbSopenharmony_ci    bool PushAbilityToken(int32_t sessionId, const sptr<IRemoteObject>& abilityToken);
38686862fbSopenharmony_ci    sptr<IRemoteObject> PopAbilityToken(int32_t sessionId);
39686862fbSopenharmony_ci    int32_t GenerateSessionId();
40686862fbSopenharmony_ci    bool IsInContinuationProgress(int32_t missionId);
41686862fbSopenharmony_ci    void SetTimeOut(int32_t missionId, int32_t timeout);
42686862fbSopenharmony_ci    void RemoveTimeOut(int32_t missionId);
43686862fbSopenharmony_ci    bool PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback,
44686862fbSopenharmony_ci        std::string deviceId, bool isFreeInstall);
45686862fbSopenharmony_ci    bool PushCallback(const sptr<IRemoteObject>& callback);
46686862fbSopenharmony_ci    bool CleanupCallback(const sptr<IRemoteObject>& callback);
47686862fbSopenharmony_ci    std::vector<sptr<IRemoteObject>> GetCallback();
48686862fbSopenharmony_ci    sptr<IRemoteObject> PopCallback(int32_t missionId);
49686862fbSopenharmony_ci    int32_t NotifyMissionCenterResult(int32_t missionId, int32_t resultCode);
50686862fbSopenharmony_ci    int32_t NotifyDSchedEventResult(int32_t resultCode);
51686862fbSopenharmony_ci    bool IsFreeInstall(int32_t missionId);
52686862fbSopenharmony_ci    std::string GetTargetDevice(int32_t missionId);
53686862fbSopenharmony_ci    bool IsCleanMission(int32_t missionId);
54686862fbSopenharmony_ci    void SetCleanMissionFlag(int32_t missionId, bool isCleanMission);
55686862fbSopenharmony_ci    EventNotify continueEvent_;
56686862fbSopenharmony_ci    ContinueInfo continueInfo_;
57686862fbSopenharmony_ci
58686862fbSopenharmony_ciprivate:
59686862fbSopenharmony_ci    int32_t NotifyDSchedEventForOneCB(const sptr<IRemoteObject> &cb, int32_t resultCode);
60686862fbSopenharmony_ci
61686862fbSopenharmony_ciprivate:
62686862fbSopenharmony_ci    class ContinuationHandler : public AppExecFwk::EventHandler {
63686862fbSopenharmony_ci    public:
64686862fbSopenharmony_ci        ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner,
65686862fbSopenharmony_ci            const std::shared_ptr<DSchedContinuation>& continuationObj,
66686862fbSopenharmony_ci            const FuncContinuationCallback& contCallback)
67686862fbSopenharmony_ci            : AppExecFwk::EventHandler(runner), continuationObj_(continuationObj), contCallback_(contCallback) {}
68686862fbSopenharmony_ci        ~ContinuationHandler() = default;
69686862fbSopenharmony_ci
70686862fbSopenharmony_ci        void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override;
71686862fbSopenharmony_ci    private:
72686862fbSopenharmony_ci        std::weak_ptr<DSchedContinuation> continuationObj_;
73686862fbSopenharmony_ci        FuncContinuationCallback contCallback_;
74686862fbSopenharmony_ci    };
75686862fbSopenharmony_ci
76686862fbSopenharmony_ci    std::shared_ptr<ContinuationHandler> continuationHandler_;
77686862fbSopenharmony_ci    std::mutex continuationLock_;
78686862fbSopenharmony_ci    int32_t currSessionId_ = 1;
79686862fbSopenharmony_ci    std::map<int32_t, sptr<IRemoteObject>> continuationMap_;
80686862fbSopenharmony_ci    std::vector<sptr<IRemoteObject>> continuationCallbackArr_;
81686862fbSopenharmony_ci    sptr<DistributedEventDiedListener> diedListener_;
82686862fbSopenharmony_ci    std::map<int32_t, sptr<IRemoteObject>> callbackMap_;
83686862fbSopenharmony_ci    std::map<int32_t, bool> freeInstall_;
84686862fbSopenharmony_ci    std::map<int32_t, bool> cleanMission_;
85686862fbSopenharmony_ci    std::map<int32_t, std::string> continuationDevices_;
86686862fbSopenharmony_ci};
87686862fbSopenharmony_ci} // namespace DistributedSchedule
88686862fbSopenharmony_ci} // namespace OHOS
89686862fbSopenharmony_ci#endif // OHOS_DISTRIBUTED_SCHED_CONTINUATION_H
90