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 #ifndef FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_TASK_MANAGER_H 16 #define FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_TASK_MANAGER_H 17 18 #include <list> 19 #include <memory> 20 21 #include "actions/action.h" 22 #include "event_handler.h" 23 #include "tasks/task.h" 24 25 namespace OHOS { 26 namespace MiscServices { 27 28 using task_ptr_t = std::shared_ptr<Task>; 29 using action_ptr_t = std::unique_ptr<Action>; 30 31 class TaskManager final { 32 private: 33 TaskManager(); 34 35 public: 36 ~TaskManager() = default; 37 38 TaskManager(const TaskManager &) = delete; 39 TaskManager(TaskManager &&) = delete; 40 TaskManager &operator=(const TaskManager &) = delete; 41 TaskManager &operator=(TaskManager &&) = delete; 42 43 static TaskManager &GetInstance(); 44 45 // Post a task to work thread 46 uint64_t PostTask(task_ptr_t task, uint32_t delayMs = 0); 47 48 // Trigger task process async 49 void ProcessAsync(); 50 51 // Resume paused task with seqId 52 void Complete(uint64_t seqId); 53 54 // Pend an action to current task during executing 55 int32_t Pend(action_ptr_t action); 56 int32_t Pend(std::function<void()>); 57 58 // Wait for task and execute 59 int32_t WaitExec(uint64_t seqId, uint32_t timeoutMs, std::function<void()>); 60 61 private: 62 friend class InputMethodAbility; 63 friend class TaskAmsInit; 64 void SetInited(bool flag); 65 66 private: 67 void OnNewTask(task_ptr_t task); // Accept a new task 68 void Process(); // Process next task 69 void ProcessNextInnerTask(); // Process next inner task 70 void ProcessNextAmsTask(); // Process next AMS task 71 void ProcessNextImaTask(); // process next IMA task 72 void ProcessNextImsaTask(); // process next IMSA task 73 void ExecuteCurrentTask(); // Execute current task 74 75 void Reset(); 76 77 private: 78 bool inited_ { false }; 79 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr }; 80 81 task_ptr_t curTask_ = { nullptr }; 82 std::list<task_ptr_t> amsTasks_; 83 std::list<task_ptr_t> imaTasks_; 84 std::list<task_ptr_t> imsaTasks_; 85 std::list<task_ptr_t> innerTasks_; 86 }; 87 } // namespace MiscServices 88 } // namespace OHOS 89 90 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_TASK_MANAGER_H