1 /* 2 * Copyright (c) 2020-2021 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 OHOS_ACELITE_TIMER_MODULE_H 16 #define OHOS_ACELITE_TIMER_MODULE_H 17 18 #include "acelite_config.h" 19 #include "non_copyable.h" 20 #if (FEATURE_TIMER_MODULE == 1) 21 #include "ace_log.h" 22 #include "js_timer_list.h" 23 #include "presets/preset_module.h" 24 25 namespace OHOS { 26 namespace ACELite { 27 class TimerModule final : PresetModule { 28 public: 29 ACE_DISALLOW_COPY_AND_MOVE(TimerModule); GetInstance()30 static TimerModule *GetInstance() 31 { 32 static TimerModule instance; 33 return &instance; 34 } 35 36 void Init() override; 37 GetInitState()38 int GetInitState() 39 { 40 return initRes_; 41 } 42 Clear()43 void Clear() 44 { 45 if (timerList_ != nullptr) { 46 timerList_->ClearTimerList(); 47 delete (timerList_); 48 timerList_ = nullptr; 49 } 50 } 51 52 private: 53 /** 54 * @fn TimerModule::TimerModule() 55 * @brief Constructor 56 */ TimerModule()57 TimerModule() : PresetModule(nullptr), 58 timerList_(nullptr), 59 initRes_(-1) 60 { 61 } 62 63 /** 64 * @fn TimerModule::~TimerModule() 65 * 66 */ 67 ~TimerModule() = default; 68 GetTimerList()69 TimerList *GetTimerList() 70 { 71 if (timerList_ == nullptr) { 72 timerList_ = new TimerList(); 73 if (timerList_ == nullptr) { 74 HILOG_ERROR(HILOG_MODULE_ACE, "malloc timer heap memory failed."); 75 } 76 } 77 return timerList_; 78 } 79 80 /** 81 * @brief set the scheduled tasks, execute only once. 82 * @return the id of timer 83 */ SetTimeout(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)84 static jerry_value_t SetTimeout(const jerry_value_t func, 85 const jerry_value_t context, 86 const jerry_value_t *args, 87 const jerry_length_t argsNum) 88 { 89 return CreateTimer(func, context, args, argsNum, false); 90 } 91 92 /** 93 * @brief start timer task 94 */ 95 static jerry_value_t StartTask(TimerList::Arguments *arguments, jerry_value_t time, bool repeated); 96 97 /** 98 * @brief set the repeated timer 99 * @return the timer id 100 */ SetInterval(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)101 static jerry_value_t SetInterval(const jerry_value_t func, 102 const jerry_value_t context, 103 const jerry_value_t *args, 104 const jerry_length_t argsNum) 105 { 106 return CreateTimer(func, context, args, argsNum, true); 107 } 108 109 /** 110 * @brief set the repeated timer 111 * @return the timer id 112 */ 113 static jerry_value_t CreateTimer(const jerry_value_t func, 114 const jerry_value_t context, 115 const jerry_value_t *args, 116 const jerry_length_t argsNum, 117 bool repeated); 118 119 /** 120 * @brief set the timer task 121 * @return the value of task executed 122 */ 123 static void Task(void *arguments); 124 125 /** 126 * @brief cancel the timer which is not execute 127 * @return the result of cancel timer 128 */ 129 static jerry_value_t ClearTimer(const jerry_value_t func, 130 const jerry_value_t context, 131 const jerry_value_t *args, 132 const jerry_length_t argsNum); 133 134 #ifndef SYNC_TIMER_CALLBACK 135 static void Execute(void *data); 136 #endif 137 TimerList *timerList_; 138 int initRes_; 139 }; 140 } // namespace ACELite 141 } // namespace OHOS 142 #endif 143 namespace OHOS { 144 namespace ACELite { 145 class TimersModule final { 146 public: 147 ACE_DISALLOW_COPY_AND_MOVE(TimersModule); 148 TimersModule() = default; 149 ~TimersModule() = default; Load()150 static void Load() 151 { 152 #if (FEATURE_TIMER_MODULE == 1) 153 TimerModule *timerModule = const_cast<TimerModule *>(TimerModule::GetInstance()); 154 timerModule->Init(); 155 #endif 156 } Clear()157 static void Clear() 158 { 159 #if (FEATURE_TIMER_MODULE == 1) 160 TimerModule *timerModule = const_cast<TimerModule *>(TimerModule::GetInstance()); 161 timerModule->Clear(); 162 #endif 163 } 164 }; 165 } // namespace ACELite 166 } // namespace OHOS 167 #endif // OHOS_ACELITE_TIMER_MODULE_H 168