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