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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_REQUEST_SUSPEND_DELAY_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_REQUEST_SUSPEND_DELAY_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "common.h"
23 #include "delay_suspend_info.h"
24 #include "expired_callback.h"
25 
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
28 class CallbackInstance : public ExpiredCallback {
29 public:
30     CallbackInstance();
31 
32     ~CallbackInstance() override;
33 
34     void OnExpired() override;
35 
36     void SetCallbackInfo(const napi_env &env, const napi_ref &ref);
37 
38 private:
39     void DeleteNapiRef();
40     struct CallbackInfo {
41         napi_env env = nullptr;
42         napi_ref ref = nullptr;
43     };
44 
45     CallbackInfo expiredCallbackInfo_;
46 };
47 
48 extern std::map<int32_t, std::shared_ptr<ExpiredCallback>> callbackInstances_;
49 extern std::mutex callbackLock_;
50 
51 napi_value RequestSuspendDelay(napi_env env, napi_callback_info info);
52 
53 napi_value RequestSuspendDelayThrow(napi_env env, napi_callback_info info);
54 }  // namespace BackgroundTaskMgr
55 }  // namespace OHOS
56 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_INTERFACES_KITS_NAPI_INCLUDE_REQUEST_SUSPEND_DELAY_H