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 BATTERY_STATISTICS_ASYNC_CALLBACK_INFO_H
17 #define BATTERY_STATISTICS_ASYNC_CALLBACK_INFO_H
18 
19 #include <vector>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23 #include "napi/native_node_api.h"
24 
25 #include "battery_stats_info.h"
26 #include "napi_error.h"
27 #include "stats_utils.h"
28 
29 namespace OHOS {
30 namespace PowerMgr {
31 class AsyncCallbackInfo {
32 public:
33     void CallFunction(napi_env& env, napi_value results);
34     void CreatePromise(napi_env& env, napi_value& promise);
35     void Release(napi_env& env);
36     void CreateCallback(napi_env& env, napi_value& callback);
GetAsyncWork()37     napi_async_work& GetAsyncWork()
38     {
39         return asyncWork_;
40     }
GetDeferred()41     napi_deferred& GetDeferred()
42     {
43         return deferred_;
44     }
GetError()45     inline NapiError& GetError()
46     {
47         return error_;
48     }
49 
50     class AsyncData {
51     public:
52         struct StatsInfo {
53             int32_t uid_ = StatsUtils::INVALID_VALUE;
54             int32_t type_ = BatteryStatsInfo::CONSUMPTION_TYPE_INVALID;
55             double power_ = StatsUtils::DEFAULT_VALUE;
56         };
57         StatsError GetBatteryStatsInfo();
58         void CreateArrayValue(napi_env& env, napi_value& arrRes);
59 
60     private:
61         std::vector<StatsInfo> statsInfos_;
62     };
GetData()63     inline AsyncData& GetData()
64     {
65         return data_;
66     }
67 
68 private:
69     napi_ref callbackRef_ = nullptr;
70     napi_async_work asyncWork_ = nullptr;
71     napi_deferred deferred_ = nullptr;
72     NapiError error_;
73     AsyncData data_;
74 };
75 } // namespace PowerMgr
76 } // namespace OHOS
77 
78 #endif // BATTERY_STATISTICS_ASYNC_CALLBACK_INFO_H