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 #include "napi_utils.h"
18 #include "power_log.h"
19 #include "power_mgr_client.h"
20 #include "runninglock_entity.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 POWER_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 POWER_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 POWER_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
SetMode(napi_env & env,napi_value & mode)61 void AsyncCallbackInfo::AsyncData::SetMode(napi_env& env, napi_value& mode)
62 {
63 uint32_t result;
64 napi_get_value_uint32(env, mode, &result);
65 powerMode_ = static_cast<PowerMode>(result);
66 }
67
SetType(napi_env & env,napi_value & type)68 void AsyncCallbackInfo::AsyncData::SetType(napi_env& env, napi_value& type)
69 {
70 int32_t result;
71 napi_get_value_int32(env, type, &result);
72 type_ = static_cast<RunningLockType>(result);
73 }
74
SetName(napi_env & env,napi_value & name)75 void AsyncCallbackInfo::AsyncData::SetName(napi_env& env, napi_value& name)
76 {
77 name_ = NapiUtils::GetStringFromNapi(env, name);
78 }
79
CreateRunningLock()80 PowerErrors AsyncCallbackInfo::AsyncData::CreateRunningLock()
81 {
82 runningLock_ = PowerMgrClient::GetInstance().CreateRunningLock(name_, type_);
83 return PowerMgrClient::GetInstance().GetError();
84 }
85
CreateInstanceForRunningLock(napi_env & env)86 napi_value AsyncCallbackInfo::AsyncData::CreateInstanceForRunningLock(napi_env& env)
87 {
88 if (runningLock_ == nullptr || napiRunningLockIns_ == nullptr) {
89 POWER_HILOGW(FEATURE_RUNNING_LOCK, "RunningLock is nullptr");
90 return nullptr;
91 }
92 napi_value cons = nullptr;
93 napi_status status = napi_get_reference_value(env, napiRunningLockIns_, &cons);
94 if (status != napi_ok) {
95 POWER_HILOGW(FEATURE_RUNNING_LOCK, "NAPI failed to create a reference value");
96 return nullptr;
97 }
98 napi_value instance = nullptr;
99 status = napi_new_instance(env, cons, 0, nullptr, &instance);
100 if (status != napi_ok) {
101 POWER_HILOGW(FEATURE_RUNNING_LOCK, "NAPI failed to create a reference");
102 return nullptr;
103 }
104
105 RunningLockEntity* entity = nullptr;
106 status = napi_unwrap(env, instance, (void**)&entity);
107 if (status != napi_ok || entity == nullptr) {
108 POWER_HILOGW(FEATURE_RUNNING_LOCK, "Cannot unwrap entity from instance");
109 return nullptr;
110 }
111 entity->runningLock = runningLock_;
112 return instance;
113 }
114 } // namespace PowerMgr
115 } // namespace OHOS