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 #ifdef WINDOWS_PLATFORM
32 #define TIMER_PUBLIC_API __declspec(dllexport)
33 #else
34 #define TIMER_PUBLIC_API __attribute__((visibility ("default")))
35 #endif
36 
37 namespace OHOS::JsSysModule {
38 struct TimerCallbackInfo {
39     napi_env env_ {nullptr};
40     uint32_t tId_ {};
41     int32_t timeout_ {};
42     napi_ref callback_ {};
43     bool repeat_ {};
44     uv_timer_t* timeReq_ {nullptr};
45     size_t argc_ {};
46     napi_ref* argv_ {nullptr};
47 #ifdef ENABLE_CONTAINER_SCOPE
48     int32_t containerScopeId_ {-1};
49 #endif
50 
TimerCallbackInfoOHOS::JsSysModule::TimerCallbackInfo51     TimerCallbackInfo(napi_env env, uint32_t tId, int32_t timeout, napi_ref callback,
52                      bool repeat, size_t argc, napi_ref* argv)
53         : env_(env), tId_(tId), timeout_(timeout), callback_(callback),
54           repeat_(repeat), argc_(argc), argv_(argv)
55     {
56         uv_loop_t* loop = Commonlibrary::Concurrent::Common::Helper::NapiHelper::GetLibUV(env_);
57         timeReq_ = new uv_timer_t;
58         uv_timer_init(loop, timeReq_);
59         timeReq_->data = this;
60     }
61 
62     ~TimerCallbackInfo();
63 };
64 
65 class Timer {
66 public:
67     Timer() = default;
68     ~Timer() = default;
69     TIMER_PUBLIC_API static bool RegisterTime(napi_env env);
70     static void ClearEnvironmentTimer(napi_env env);
71     static bool HasTimer(napi_env env);
72     friend class TimerTest;
73 
74 private:
75     static napi_value SetTimeout(napi_env env, napi_callback_info cbinfo);
76     static napi_value SetInterval(napi_env env, napi_callback_info cbinfo);
77     static napi_value ClearTimer(napi_env env, napi_callback_info cbinfo);
78     static napi_value SetTimeoutInnerCore(napi_env env, napi_value* argv, size_t argc, bool repeat);
79     static napi_value SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool repeat);
80     static void TimerCallback(uv_timer_t* handle);
81     static void DeleteTimer(uint32_t tId, TimerCallbackInfo* callbackInfo);
82 
83     static uint32_t timeCallbackId;
84     static std::map<uint32_t, TimerCallbackInfo*> timerTable;
85     static std::mutex timeLock;
86 };
87 } // namespace Commonlibrary::JsSysModule
88 #endif // TIMER_TIMER_H
89