• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-2024 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 FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_ACTIONS_ACTION_WAIT_H
17 #define FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_ACTIONS_ACTION_WAIT_H
18 
19 #include <cstdint>
20 #include <functional>
21 
22 #include "action.h"
23 #include "task_manager.h"
24 #include "tasks/task_inner.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 
29 class ActionWait : public Action {
30 public:
31     using callback_t = std::function<void()>;
32 
ActionWait(uint64_t completeId,uint32_t timeoutMs)33     ActionWait(uint64_t completeId, uint32_t timeoutMs)
34         : timeoutMs_(timeoutMs), completeId_(completeId), timeoutId_(Task::GetNextSeqId())
35     {
36     }
37 
ActionWait(uint64_t completeId,uint32_t timeoutMs,callback_t onComplete,callback_t onTimeout)38     ActionWait(uint64_t completeId, uint32_t timeoutMs, callback_t onComplete, callback_t onTimeout)
39         : timeoutMs_(timeoutMs), completeId_(completeId), timeoutId_(Task::GetNextSeqId()), onComplete_(onComplete),
40           onTimeout_(onTimeout)
41     {
42     }
43 
44     ~ActionWait() = default;
45 
Execute()46     RunningState Execute() override
47     {
48         state_ = RUNNING_STATE_PAUSED;
49         // trigger timeout with delay
50         auto task = std::make_shared<TaskResume>(timeoutId_);
51         TaskManager::GetInstance().PostTask(task, timeoutMs_);
52         return state_;
53     }
54 
Resume(uint64_t seqId)55     RunningState Resume(uint64_t seqId) override
56     {
57         if (state_ != RUNNING_STATE_PAUSED) {
58             return RUNNING_STATE_ERROR;
59         }
60 
61         if (seqId == completeId_) {
62             if (onComplete_) {
63                 onComplete_();
64             }
65             state_ = RUNNING_STATE_COMPLETED;
66             return state_;
67         }
68         if (seqId == timeoutId_) {
69             if (onTimeout_) {
70                 onTimeout_();
71             }
72             state_ = RUNNING_STATE_COMPLETED;
73             return state_;
74         }
75 
76         return state_;
77     }
78 
79 private:
80     const uint32_t timeoutMs_;
81     const uint64_t completeId_;
82     const uint64_t timeoutId_;
83     std::function<void()> onComplete_;
84     std::function<void()> onTimeout_;
85 };
86 } // namespace MiscServices
87 } // namespace OHOS
88 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_ACTIONS_ACTION_WAIT_H