1 /* 2 * Copyright (c) 2023-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 OHOS_FORM_FWK_FORM_COMMAND_QUEUE_H 17 #define OHOS_FORM_FWK_FORM_COMMAND_QUEUE_H 18 19 #include <string> 20 #include <mutex> 21 #include <queue> 22 23 namespace OHOS { 24 namespace AppExecFwk { 25 enum class TaskCommandType : int64_t { 26 RENDER_FORM, 27 RECOVER_FORM, 28 RECYCLE_FORM, 29 DELETE_FORM, 30 READDFORM_FORM, 31 }; 32 33 // FormCommand Interface 34 class FormCommand { 35 public: 36 FormCommand() = default; FormCommand(int64_t formId,const std::pair<TaskCommandType,int64_t> & eventMsg,uint32_t ms,std::function<void ()> func)37 FormCommand( 38 int64_t formId, 39 const std::pair<TaskCommandType, 40 int64_t>& eventMsg, 41 uint32_t ms, 42 std::function<void()> func) 43 : formId_(formId), eventMsg_(eventMsg), ms_(ms), func_(func) {} 44 45 void PostFormCommandTask(); 46 getFormId()47 inline int64_t getFormId() const { return formId_; } setFormId(int64_t formId)48 inline void setFormId(int64_t formId) { formId_ = formId; } 49 getEventMsg()50 inline std::pair<TaskCommandType, int64_t> getEventMsg() const { return eventMsg_; } setEventMsg(const std::pair<TaskCommandType,int64_t> & eventMsg)51 inline void setEventMsg(const std::pair<TaskCommandType, int64_t>& eventMsg) { eventMsg_ = eventMsg; } 52 getMs()53 inline uint32_t getMs() const { return ms_; } setMs(uint32_t ms)54 inline void setMs(uint32_t ms) { ms_ = ms; } 55 getFunc()56 inline std::function<void()> getFunc() const { return func_; } setFunc(const std::function<void ()> & func)57 inline void setFunc(const std::function<void()>& func) { func_ = func; } 58 59 protected: 60 int64_t formId_; 61 std::pair<TaskCommandType, int64_t> eventMsg_; // <TaskCommandType, formId> 62 uint32_t ms_; // delay time 63 std::function<void()> func_; // formCommand funtion 64 }; 65 66 /** 67 * @brief This class processes the commands in the queue based on the current form state. 68 * @class FormCommandQueue 69 * @param formId The form ID associated with this command queue. 70 */ 71 class FormCommandQueue final { 72 public: 73 explicit FormCommandQueue(const int64_t &formId); 74 ~FormCommandQueue(); 75 76 /** 77 * @brief Push a task command to the command queue. 78 * @param taskType TaskCommandType 79 * @param taskCommand TaskCommand 80 * @return none. 81 */ 82 void PushFormCommand(FormCommand formCommand); 83 84 /** 85 * @brief Pop a task command form the command queue. 86 * @param taskType TaskCommandType 87 * @param taskCommand TaskCommand 88 * @return none. 89 */ 90 bool PopFormCommand(FormCommand &formCommand); 91 92 /** 93 * @brief check command queue is empty. 94 * @return bool. 95 */ 96 bool IsCommondQueueEmpty(); 97 98 private: 99 /** 100 * @brief Processes the tasks in the command queue based on the current card state. 101 * @return none. 102 */ 103 void ProcessCommandQueue(); 104 105 //FormCommand queue 106 std::mutex formCommandQueueMutex_; 107 std::queue<FormCommand> CommandQueue; 108 int64_t formId_; 109 }; 110 } 111 } 112 #endif // OHOS_FORM_FWK_FORM_COMMAND_QUEUE_H 113