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 #include "async_callback_info.h"
17 
18 #include "battery_stats_client.h"
19 #include "napi_utils.h"
20 #include "stats_log.h"
21 
22 namespace OHOS {
23 namespace PowerMgr {
CallFunction(napi_env & env,napi_value results)24 void AsyncCallbackInfo::CallFunction(napi_env& env, napi_value results)
25 {
26     napi_value callback = nullptr;
27     if (napi_ok != napi_get_reference_value(env, callbackRef_, &callback)) {
28         STATS_HILOGW(COMP_FWK, "Failed to get a callback reference");
29         return;
30     }
31     const int32_t maxArgc = 2;
32     napi_value argv[] = {error_.GetNapiError(env), results};
33     napi_value result;
34     if (napi_ok != napi_call_function(env, nullptr, callback, maxArgc, argv, &result)) {
35         STATS_HILOGW(COMP_FWK, "Failed to call the callback function");
36     }
37 }
38 
Release(napi_env & env)39 void AsyncCallbackInfo::Release(napi_env& env)
40 {
41     NapiUtils::ReleaseReference(env, callbackRef_);
42     if (asyncWork_ != nullptr) {
43         napi_delete_async_work(env, asyncWork_);
44         asyncWork_ = nullptr;
45     }
46     deferred_ = nullptr;
47 }
48 
CreatePromise(napi_env & env,napi_value & promise)49 void AsyncCallbackInfo::CreatePromise(napi_env& env, napi_value& promise)
50 {
51     if (napi_ok != napi_create_promise(env, &deferred_, &promise)) {
52         STATS_HILOGW(COMP_FWK, "napi_create_promise failure");
53     }
54 }
55 
CreateCallback(napi_env & env,napi_value & callback)56 void AsyncCallbackInfo::CreateCallback(napi_env& env, napi_value& callback)
57 {
58     callbackRef_ = NapiUtils::CreateReference(env, callback);
59 }
60 
GetBatteryStatsInfo()61 StatsError AsyncCallbackInfo::AsyncData::GetBatteryStatsInfo()
62 {
63     statsInfos_.clear();
64     BatteryStatsInfoList nativeStatsInfos = BatteryStatsClient::GetInstance().GetBatteryStats();
65     StatsError code = BatteryStatsClient::GetInstance().GetLastError();
66     std::transform(
67         nativeStatsInfos.begin(), nativeStatsInfos.end(), std::back_inserter(statsInfos_), [](const auto& item) {
68             StatsInfo statsInfo = {
69                 .uid_ = item->GetUid(),
70                 .type_ = item->GetConsumptionType(),
71                 .power_ = item->GetPower()};
72             return statsInfo;
73         });
74     return code;
75 }
76 
CreateArrayValue(napi_env & env,napi_value & arrRes)77 void AsyncCallbackInfo::AsyncData::CreateArrayValue(napi_env& env, napi_value& arrRes)
78 {
79     if (napi_ok != napi_create_array_with_length(env, statsInfos_.size(), &arrRes)) {
80         STATS_HILOGW(COMP_FWK, "napi creates array error");
81         return;
82     }
83     int32_t index = 0;
84     for (auto& item : statsInfos_) {
85         napi_value result = nullptr;
86         napi_create_object(env, &result);
87 
88         NapiUtils::SetIntValue(env, "uid", item.uid_, result);
89         NapiUtils::SetIntValue(env, "type", item.type_, result);
90         NapiUtils::SetDoubleValue(env, "power", item.power_, result);
91 
92         napi_set_element(env, arrRes, index, result);
93         index++;
94     }
95 }
96 } // namespace PowerMgr
97 } // namespace OHOS