• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_TIMER_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_TIMER_H
18 
19 #include "ark_native_engine.h"
20 #include "ecmascript/napi/include/jsnapi_expo.h"
21 #if !defined(PREVIEW) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
22 #include <uv.h>
23 #endif
24 
25 using TimerCallbackFunc = panda::TimerCallbackFunc;
26 using EcmaVM = panda::EcmaVM;
27 using JSNApi = panda::JSNApi;
28 
29 class NativeTimerCallbackInfo {
30 public:
NativeTimerCallbackInfo(ArkNativeEngine * engine,TimerCallbackFunc cb,void * data,bool repeat)31     NativeTimerCallbackInfo(ArkNativeEngine* engine, TimerCallbackFunc cb, void* data, bool repeat)
32         : engine_(engine), cb_(cb), data_(data), repeat_(repeat) {}
33 
~NativeTimerCallbackInfo()34     ~NativeTimerCallbackInfo()
35     {
36         engine_ = nullptr;
37         cb_ = nullptr;
38         data_ = nullptr;
39         if (timerReq_ == nullptr) {
40             return;
41         }
42         timerReq_->data = nullptr;
43         uv_timer_stop(timerReq_);
44         uv_close(reinterpret_cast<uv_handle_t*>(timerReq_), [](uv_handle_t* handle) {
45             if (handle != nullptr) {
46                 delete (uv_timer_t*)handle;
47                 handle = nullptr;
48             }
49         });
50     }
51     static void* TimerTaskCallback(
52         EcmaVM* vm, void* data, TimerCallbackFunc func, uint64_t timeout, bool repeat);
53     static void CancelTimerCallback(void* timerCallbackInfo);
54 
55     bool Init(uint64_t timeout);
56     void Insert();
57     void Erase();
58     static void ReleaseTimerList(ArkNativeEngine* engine);
59 
60 private:
61     static void TimerCallback(uv_timer_t* handle);
62 
63     ArkNativeEngine* engine_ {nullptr};
64     TimerCallbackFunc cb_ {nullptr};
65     void* data_ {nullptr};
66     bool repeat_ {};
67     bool timeoutExecuting_ {};
68     uv_timer_t* timerReq_ {nullptr};
69     NativeTimerCallbackInfo* prev_ {nullptr};
70     NativeTimerCallbackInfo* next_ {nullptr};
71 };
72 #endif