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