1 /*
2  * Copyright (c) 2022 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 TIMER_TIMER_H
17 #define TIMER_TIMER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <uv.h>
22 
23 #include "commonlibrary/ets_utils/js_concurrent_module/common/helper/napi_helper.h"
24 #include "commonlibrary/ets_utils/js_concurrent_module/common/helper/object_helper.h"
25 #ifdef ENABLE_CONTAINER_SCOPE
26 #include "core/common/container_scope.h"
27 #endif
28 #include "napi/native_api.h"
29 #include "napi/native_node_api.h"
30 
31 namespace OHOS::JsSysModule {
32 struct TimerCallbackInfo {
33     napi_env env_ {nullptr};
34     uint32_t tId_ {};
35     int32_t timeout_ {};
36     napi_ref callback_ {};
37     bool repeat_ {};
38     uv_timer_t* timeReq_ {nullptr};
39     size_t argc_ {};
40     napi_ref* argv_ {nullptr};
41 #ifdef ENABLE_CONTAINER_SCOPE
42     int32_t containerScopeId_ {-1};
43 #endif
44 
TimerCallbackInfoTimerCallbackInfo45     TimerCallbackInfo(napi_env env, uint32_t tId, int32_t timeout, napi_ref callback,
46                      bool repeat, size_t argc, napi_ref* argv)
47         : env_(env), tId_(tId), timeout_(timeout), callback_(callback),
48           repeat_(repeat), argc_(argc), argv_(argv)
49     {
50         uv_loop_t* loop = Commonlibrary::Concurrent::Common::Helper::NapiHelper::GetLibUV(env_);
51         timeReq_ = new uv_timer_t;
52         uv_timer_init(loop, timeReq_);
53         timeReq_->data = this;
54     }
55 
56     ~TimerCallbackInfo();
57 };
58 
59 class Timer {
60 public:
61     Timer() = default;
62     ~Timer() = default;
63     static bool RegisterTime(napi_env env);
64     static void ClearEnvironmentTimer(napi_env env);
65     static bool HasTimer(napi_env env);
66     friend class TimerTest;
67 
68 private:
69     static napi_value SetTimeout(napi_env env, napi_callback_info cbinfo);
70     static napi_value SetInterval(napi_env env, napi_callback_info cbinfo);
71     static napi_value ClearTimer(napi_env env, napi_callback_info cbinfo);
72     static napi_value SetTimeoutInnerCore(napi_env env, napi_value* argv, size_t argc, bool repeat);
73     static napi_value SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool repeat);
74     static void TimerCallback(uv_timer_t* handle);
75     static void DeleteTimer(uint32_t tId, TimerCallbackInfo* callbackInfo);
76     static napi_value SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat);
77     static void CleanUpHook(void* data);
78 
79 #define DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION_WITH_DATA(name, func, data)                   \
80     {                                                                                        \
81         (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default_jsproperty, data    \
82     }
83 
84     static uint32_t timeCallbackId;
85     static std::map<uint32_t, TimerCallbackInfo*> timerTable;
86     static std::mutex timeLock;
87 };
88 } // namespace Commonlibrary::JsSysModule
89 #endif // TIMER_TIMER_H
90