14d6c458bSopenharmony_ci/*
24d6c458bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34d6c458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44d6c458bSopenharmony_ci * you may not use this file except in compliance with the License.
54d6c458bSopenharmony_ci * You may obtain a copy of the License at
64d6c458bSopenharmony_ci *
74d6c458bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84d6c458bSopenharmony_ci *
94d6c458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104d6c458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114d6c458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124d6c458bSopenharmony_ci * See the License for the specific language governing permissions and
134d6c458bSopenharmony_ci * limitations under the License.
144d6c458bSopenharmony_ci */
154d6c458bSopenharmony_ci
164d6c458bSopenharmony_ci#ifndef TIMER_TIMER_H
174d6c458bSopenharmony_ci#define TIMER_TIMER_H
184d6c458bSopenharmony_ci
194d6c458bSopenharmony_ci#include <map>
204d6c458bSopenharmony_ci#include <mutex>
214d6c458bSopenharmony_ci#include <uv.h>
224d6c458bSopenharmony_ci
234d6c458bSopenharmony_ci#include "commonlibrary/ets_utils/js_concurrent_module/common/helper/napi_helper.h"
244d6c458bSopenharmony_ci#include "commonlibrary/ets_utils/js_concurrent_module/common/helper/object_helper.h"
254d6c458bSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE
264d6c458bSopenharmony_ci#include "core/common/container_scope.h"
274d6c458bSopenharmony_ci#endif
284d6c458bSopenharmony_ci#include "napi/native_api.h"
294d6c458bSopenharmony_ci#include "napi/native_node_api.h"
304d6c458bSopenharmony_ci
314d6c458bSopenharmony_ci#ifdef WINDOWS_PLATFORM
324d6c458bSopenharmony_ci#define TIMER_PUBLIC_API __declspec(dllexport)
334d6c458bSopenharmony_ci#else
344d6c458bSopenharmony_ci#define TIMER_PUBLIC_API __attribute__((visibility ("default")))
354d6c458bSopenharmony_ci#endif
364d6c458bSopenharmony_ci
374d6c458bSopenharmony_cinamespace OHOS::JsSysModule {
384d6c458bSopenharmony_cistruct TimerCallbackInfo {
394d6c458bSopenharmony_ci    napi_env env_ {nullptr};
404d6c458bSopenharmony_ci    uint32_t tId_ {};
414d6c458bSopenharmony_ci    int32_t timeout_ {};
424d6c458bSopenharmony_ci    napi_ref callback_ {};
434d6c458bSopenharmony_ci    bool repeat_ {};
444d6c458bSopenharmony_ci    uv_timer_t* timeReq_ {nullptr};
454d6c458bSopenharmony_ci    size_t argc_ {};
464d6c458bSopenharmony_ci    napi_ref* argv_ {nullptr};
474d6c458bSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE
484d6c458bSopenharmony_ci    int32_t containerScopeId_ {-1};
494d6c458bSopenharmony_ci#endif
504d6c458bSopenharmony_ci
514d6c458bSopenharmony_ci    TimerCallbackInfo(napi_env env, uint32_t tId, int32_t timeout, napi_ref callback,
524d6c458bSopenharmony_ci                     bool repeat, size_t argc, napi_ref* argv)
534d6c458bSopenharmony_ci        : env_(env), tId_(tId), timeout_(timeout), callback_(callback),
544d6c458bSopenharmony_ci          repeat_(repeat), argc_(argc), argv_(argv)
554d6c458bSopenharmony_ci    {
564d6c458bSopenharmony_ci        uv_loop_t* loop = Commonlibrary::Concurrent::Common::Helper::NapiHelper::GetLibUV(env_);
574d6c458bSopenharmony_ci        timeReq_ = new uv_timer_t;
584d6c458bSopenharmony_ci        uv_timer_init(loop, timeReq_);
594d6c458bSopenharmony_ci        timeReq_->data = this;
604d6c458bSopenharmony_ci    }
614d6c458bSopenharmony_ci
624d6c458bSopenharmony_ci    ~TimerCallbackInfo();
634d6c458bSopenharmony_ci};
644d6c458bSopenharmony_ci
654d6c458bSopenharmony_ciclass Timer {
664d6c458bSopenharmony_cipublic:
674d6c458bSopenharmony_ci    Timer() = default;
684d6c458bSopenharmony_ci    ~Timer() = default;
694d6c458bSopenharmony_ci    TIMER_PUBLIC_API static bool RegisterTime(napi_env env);
704d6c458bSopenharmony_ci    static void ClearEnvironmentTimer(napi_env env);
714d6c458bSopenharmony_ci    static bool HasTimer(napi_env env);
724d6c458bSopenharmony_ci    friend class TimerTest;
734d6c458bSopenharmony_ci
744d6c458bSopenharmony_ciprivate:
754d6c458bSopenharmony_ci    static napi_value SetTimeout(napi_env env, napi_callback_info cbinfo);
764d6c458bSopenharmony_ci    static napi_value SetInterval(napi_env env, napi_callback_info cbinfo);
774d6c458bSopenharmony_ci    static napi_value ClearTimer(napi_env env, napi_callback_info cbinfo);
784d6c458bSopenharmony_ci    static napi_value SetTimeoutInnerCore(napi_env env, napi_value* argv, size_t argc, bool repeat);
794d6c458bSopenharmony_ci    static napi_value SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool repeat);
804d6c458bSopenharmony_ci    static void TimerCallback(uv_timer_t* handle);
814d6c458bSopenharmony_ci    static void DeleteTimer(uint32_t tId, TimerCallbackInfo* callbackInfo);
824d6c458bSopenharmony_ci
834d6c458bSopenharmony_ci    static uint32_t timeCallbackId;
844d6c458bSopenharmony_ci    static std::map<uint32_t, TimerCallbackInfo*> timerTable;
854d6c458bSopenharmony_ci    static std::mutex timeLock;
864d6c458bSopenharmony_ci};
874d6c458bSopenharmony_ci} // namespace Commonlibrary::JsSysModule
884d6c458bSopenharmony_ci#endif // TIMER_TIMER_H
89