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 NAPI_SYSTEM_TIMER_H
17 #define NAPI_SYSTEM_TIMER_H
18 
19 #include <functional>
20 
21 #include "uv.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "napi_work.h"
25 #include "time_service_client.h"
26 
27 namespace OHOS {
28 namespace MiscServices {
29 namespace Time {
30 using namespace OHOS::AppExecFwk;
31 class ITimerInfoInstance : public OHOS::MiscServices::ITimerInfo {
32 public:
33     ITimerInfoInstance();
34     virtual ~ITimerInfoInstance();
35     virtual void OnTrigger() override;
36     virtual void SetType(const int &type) override;
37     virtual void SetRepeat(bool repeat) override;
38     virtual void SetInterval(const uint64_t &interval) override;
39     virtual void SetWantAgent(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent) override;
40     void SetCallbackInfo(const napi_env &env, const napi_ref &ref);
41 
42 private:
43     struct CallbackInfo {
CallbackInfoCallbackInfo44         CallbackInfo(){};
CallbackInfoCallbackInfo45         CallbackInfo(napi_env napiEnv, napi_ref napiRef) : env(napiEnv), ref(napiRef){};
46         napi_env env = nullptr;
47         napi_ref ref = nullptr;
48     };
49 
50     void Call(napi_env env, void *data);
51     CallbackInfo callbackInfo_;
52 };
53 
54 class NapiSystemTimer {
55 public:
56     static napi_value SystemTimerInit(napi_env env, napi_value exports);
57     static void GetTimerOptions(const napi_env &env, ContextBase *context, const napi_value &value,
58                                 std::shared_ptr<ITimerInfoInstance> &iTimerInfoInstance);
59     static napi_value CreateTimer(napi_env env, napi_callback_info info);
60     static napi_value StartTimer(napi_env env, napi_callback_info info);
61     static napi_value StopTimer(napi_env env, napi_callback_info info);
62     static napi_value DestroyTimer(napi_env env, napi_callback_info info);
63 };
64 
65 }
66 }
67 }
68 #endif // NAPI_SYSTEM_TIMER_H