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 #include "distributed_event_died_listener.h"
28 #include "distributed_sched_types.h"
29 
30 namespace OHOS {
31 namespace DistributedSchedule {
32 using FuncContinuationCallback = std::function<void(int32_t missionId)>;
33 
34 class DSchedContinuation : public std::enable_shared_from_this<DSchedContinuation> {
35 public:
36     void Init(const FuncContinuationCallback& contCallback);
37     bool PushAbilityToken(int32_t sessionId, const sptr<IRemoteObject>& abilityToken);
38     sptr<IRemoteObject> PopAbilityToken(int32_t sessionId);
39     int32_t GenerateSessionId();
40     bool IsInContinuationProgress(int32_t missionId);
41     void SetTimeOut(int32_t missionId, int32_t timeout);
42     void RemoveTimeOut(int32_t missionId);
43     bool PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback,
44         std::string deviceId, bool isFreeInstall);
45     bool PushCallback(const sptr<IRemoteObject>& callback);
46     bool CleanupCallback(const sptr<IRemoteObject>& callback);
47     std::vector<sptr<IRemoteObject>> GetCallback();
48     sptr<IRemoteObject> PopCallback(int32_t missionId);
49     int32_t NotifyMissionCenterResult(int32_t missionId, int32_t resultCode);
50     int32_t NotifyDSchedEventResult(int32_t resultCode);
51     bool IsFreeInstall(int32_t missionId);
52     std::string GetTargetDevice(int32_t missionId);
53     bool IsCleanMission(int32_t missionId);
54     void SetCleanMissionFlag(int32_t missionId, bool isCleanMission);
55     EventNotify continueEvent_;
56     ContinueInfo continueInfo_;
57 
58 private:
59     int32_t NotifyDSchedEventForOneCB(const sptr<IRemoteObject> &cb, int32_t resultCode);
60 
61 private:
62     class ContinuationHandler : public AppExecFwk::EventHandler {
63     public:
ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::shared_ptr<DSchedContinuation> & continuationObj,const FuncContinuationCallback & contCallback)64         ContinuationHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner,
65             const std::shared_ptr<DSchedContinuation>& continuationObj,
66             const FuncContinuationCallback& contCallback)
67             : AppExecFwk::EventHandler(runner), continuationObj_(continuationObj), contCallback_(contCallback) {}
68         ~ContinuationHandler() = default;
69 
70         void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override;
71     private:
72         std::weak_ptr<DSchedContinuation> continuationObj_;
73         FuncContinuationCallback contCallback_;
74     };
75 
76     std::shared_ptr<ContinuationHandler> continuationHandler_;
77     std::mutex continuationLock_;
78     int32_t currSessionId_ = 1;
79     std::map<int32_t, sptr<IRemoteObject>> continuationMap_;
80     std::vector<sptr<IRemoteObject>> continuationCallbackArr_;
81     sptr<DistributedEventDiedListener> diedListener_;
82     std::map<int32_t, sptr<IRemoteObject>> callbackMap_;
83     std::map<int32_t, bool> freeInstall_;
84     std::map<int32_t, bool> cleanMission_;
85     std::map<int32_t, std::string> continuationDevices_;
86 };
87 } // namespace DistributedSchedule
88 } // namespace OHOS
89 #endif // OHOS_DISTRIBUTED_SCHED_CONTINUATION_H
90