1 /*
2  * Copyright (c) 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_DMS_CALLBACK_TASK_H
17 #define OHOS_DMS_CALLBACK_TASK_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <map>
22 #include <mutex>
23 
24 #include "event_handler.h"
25 #include "iremote_object.h"
26 #include "refbase.h"
27 #include "want.h"
28 
29 namespace OHOS {
30 namespace DistributedSchedule {
31 using DmsCallbackTaskInitCallbackFunc = std::function<void(int64_t taskId)>;
32 
33 enum class LaunchType {
34     FREEINSTALL_START = 0,
35     FREEINSTALL_CONTINUE = 1
36 };
37 
38 struct CallbackTaskItem {
39     sptr<IRemoteObject> callback = nullptr;
40     int64_t taskId = -1;
41     LaunchType launchType = LaunchType::FREEINSTALL_START;
42     std::string deviceId = "";
43     OHOS::AAFwk::Want want;
44 };
45 
46 class DmsCallbackTask : public std::enable_shared_from_this<DmsCallbackTask> {
47 public:
48     void Init(const DmsCallbackTaskInitCallbackFunc& callback);
49     int64_t GenerateTaskId();
50     int32_t PushCallback(int64_t taskId, const sptr<IRemoteObject>& callback, const std::string& deviceId,
51         LaunchType launchType, const OHOS::AAFwk::Want& want);
52     CallbackTaskItem PopCallback(int64_t taskId);
53     void SetContinuationMissionMap(int64_t taskId, int32_t missionId);
54     LaunchType GetLaunchType(int64_t taskId);
55     void PopContinuationMissionMap(int64_t taskId);
56     int64_t GetContinuaionMissionId(int64_t taskId);
57     void NotifyDeviceOffline(const std::string& deviceId);
58 
59 private:
60     class DmsCallbackHandler : public AppExecFwk::EventHandler {
61     public:
DmsCallbackHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::shared_ptr<DmsCallbackTask> & callbackTask,const DmsCallbackTaskInitCallbackFunc & callback)62         DmsCallbackHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner,
63             const std::shared_ptr<DmsCallbackTask>& callbackTask, const DmsCallbackTaskInitCallbackFunc& callback)
64             : AppExecFwk::EventHandler(runner), callbackTask_(callbackTask), callback_(callback)
65         {
66         }
67         ~DmsCallbackHandler() = default;
68 
69         void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override;
70     private:
71         std::weak_ptr<DmsCallbackTask> callbackTask_;
72         DmsCallbackTaskInitCallbackFunc callback_;
73     };
74 
75     std::shared_ptr<DmsCallbackHandler> dmsCallbackHandler_;
76     std::mutex callbackMapMutex_;
77     std::mutex taskMutex_;
78     std::atomic<int64_t> currTaskId_ {1};
79     std::map<int64_t, CallbackTaskItem> callbackMap_;
80     std::map<int64_t, int64_t> continuationMissionMap_;
81 };
82 }  // namespace DistributedSchedule
83 }  // namespace OHOS
84 #endif  // OHOS_DMS_CALLBACK_TASK_H
85