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 POWERMGR_POWER_ASYNC_CALLBACK_INFO_H
17 #define POWERMGR_POWER_ASYNC_CALLBACK_INFO_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_common.h"
21 #include "napi/native_node_api.h"
22 
23 #include "napi_errors.h"
24 #include "power_mode_info.h"
25 #include "running_lock.h"
26 #include "running_lock_info.h"
27 
28 namespace OHOS {
29 namespace PowerMgr {
30 class AsyncCallbackInfo {
31 public:
32     void CallFunction(napi_env& env, napi_value results);
33     void Release(napi_env& env);
34     void CreatePromise(napi_env& env, napi_value& promise);
35     void CreateCallback(napi_env& env, napi_value& callback);
GetAsyncWork()36     napi_async_work& GetAsyncWork()
37     {
38         return asyncWork_;
39     }
GetDeferred()40     napi_deferred& GetDeferred()
41     {
42         return deferred_;
43     }
GetError()44     inline NapiErrors& GetError()
45     {
46         return error_;
47     }
48     class AsyncData {
49     public:
50         void SetMode(napi_env& env, napi_value& mode);
GetMode()51         inline PowerMode GetMode() const
52         {
53             return powerMode_;
54         }
55         // runninglock
56         void SetType(napi_env& env, napi_value& type);
57         void SetName(napi_env& env, napi_value& name);
58         PowerErrors CreateRunningLock();
59         napi_value CreateInstanceForRunningLock(napi_env& env);
SetRunningLockInstance(const napi_ref & instance)60         inline void SetRunningLockInstance(const napi_ref& instance)
61         {
62             napiRunningLockIns_ = instance;
63         }
64 
65     private:
66         // power
67         PowerMode powerMode_ = PowerMode::NORMAL_MODE;
68         // runninglock
69         RunningLockType type_ = RunningLockType::RUNNINGLOCK_BUTT;
70         std::string name_;
71         std::shared_ptr<RunningLock> runningLock_ = nullptr;
72         napi_ref napiRunningLockIns_ = nullptr;
73     };
GetData()74     inline AsyncData& GetData()
75     {
76         return data_;
77     }
78 
79 private:
80     napi_ref callbackRef_ = nullptr;
81     napi_async_work asyncWork_ = nullptr;
82     napi_deferred deferred_ = nullptr;
83     NapiErrors error_;
84     AsyncData data_;
85 };
86 } // namespace PowerMgr
87 } // namespace OHOS
88 
89 #endif // POWERMGR_POWER_ASYNC_CALLBACK_INFO_H