• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         initRes_ = -1;
51     }
52 
53 private:
54     /**
55      * @fn TimerModule::TimerModule()
56      * @brief Constructor
57      */
TimerModule()58     TimerModule() : PresetModule(nullptr),
59         timerList_(nullptr),
60         initRes_(-1)
61     {
62     }
63 
64     /**
65      * @fn TimerModule::~TimerModule()
66      *
67      */
68     ~TimerModule() = default;
69 
GetTimerList()70     TimerList *GetTimerList()
71     {
72         if (timerList_ == nullptr) {
73             timerList_ = new TimerList();
74             if (timerList_ == nullptr) {
75                 HILOG_ERROR(HILOG_MODULE_ACE, "malloc timer heap memory failed.");
76             }
77         }
78         return timerList_;
79     }
80 
81     /**
82      * @brief set the scheduled tasks, execute only once.
83      * @return the id of timer
84      */
SetTimeout(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)85     static jerry_value_t SetTimeout(const jerry_value_t func,
86                                     const jerry_value_t context,
87                                     const jerry_value_t *args,
88                                     const jerry_length_t argsNum)
89     {
90         return CreateTimer(func, context, args, argsNum, false);
91     }
92 
93     /**
94      * @brief start timer task
95      */
96     static jerry_value_t StartTask(TimerList::Arguments *arguments, jerry_value_t time, bool repeated);
97 
98     /**
99      * @brief set the repeated timer
100      * @return the timer id
101      */
SetInterval(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum)102     static jerry_value_t SetInterval(const jerry_value_t func,
103                                      const jerry_value_t context,
104                                      const jerry_value_t *args,
105                                      const jerry_length_t argsNum)
106     {
107         return CreateTimer(func, context, args, argsNum, true);
108     }
109 
110     /**
111      * @brief set the repeated timer
112      * @return the timer id
113      */
114     static jerry_value_t CreateTimer(const jerry_value_t func,
115                                      const jerry_value_t context,
116                                      const jerry_value_t *args,
117                                      const jerry_length_t argsNum,
118                                      bool repeated);
119 
120     /**
121      * @brief set the timer task
122      * @return the value of task executed
123      */
124     static void Task(void *arguments);
125 
126     /**
127      * @brief cancel the timer which is not execute
128      * @return the result of cancel timer
129      */
130     static jerry_value_t ClearTimer(const jerry_value_t func,
131                                     const jerry_value_t context,
132                                     const jerry_value_t *args,
133                                     const jerry_length_t argsNum);
134 
135 #ifndef SYNC_TIMER_CALLBACK
136     static void Execute(void *data);
137 #endif
138     TimerList *timerList_;
139     int initRes_;
140 };
141 } // namespace ACELite
142 } // namespace OHOS
143 #endif
144 namespace OHOS {
145 namespace ACELite {
146 class TimersModule final {
147 public:
148     ACE_DISALLOW_COPY_AND_MOVE(TimersModule);
149     TimersModule() = default;
150     ~TimersModule() = default;
Load()151     static void Load()
152     {
153 #if (FEATURE_TIMER_MODULE == 1)
154         TimerModule *timerModule = const_cast<TimerModule *>(TimerModule::GetInstance());
155         timerModule->Init();
156 #endif
157     }
Clear()158     static void Clear()
159     {
160 #if (FEATURE_TIMER_MODULE == 1)
161         TimerModule *timerModule = const_cast<TimerModule *>(TimerModule::GetInstance());
162         timerModule->Clear();
163 #endif
164     }
165 };
166 } // namespace ACELite
167 } // namespace OHOS
168 #endif // OHOS_ACELITE_TIMER_MODULE_H
169