• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "runninglock_napi.h"
17 
18 #include <memory>
19 
20 #include "napi_errors.h"
21 #include "napi_utils.h"
22 #include "power_common.h"
23 #include "power_log.h"
24 #include "runninglock_entity.h"
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 namespace {
29 constexpr uint32_t CREATE_PROMISE_MAX_ARGC = 2;
30 constexpr uint32_t CREATE_CALLBACK_MAX_ARGC = 3;
31 constexpr uint32_t ISSUPPORTED_MAX_ARGC = 1;
32 constexpr uint32_t HOLD_MAX_ARGC = 1;
33 constexpr int32_t INDEX_0 = 0;
34 constexpr int32_t INDEX_1 = 1;
35 constexpr int32_t INDEX_2 = 2;
36 }
37 
Create(napi_env & env,napi_callback_info & info,napi_ref & napiRunningLockIns)38 napi_value RunningLockNapi::Create(napi_env& env, napi_callback_info& info, napi_ref& napiRunningLockIns)
39 {
40     size_t argc = CREATE_CALLBACK_MAX_ARGC;
41     napi_value argv[argc];
42     NapiUtils::GetCallbackInfo(env, info, argc, argv);
43 
44     NapiErrors error;
45     if (argc != CREATE_CALLBACK_MAX_ARGC && argc != CREATE_PROMISE_MAX_ARGC) {
46         return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
47     }
48 
49     std::unique_ptr<AsyncCallbackInfo> asyncInfo = std::make_unique<AsyncCallbackInfo>();
50     RETURN_IF_WITH_RET(asyncInfo == nullptr, nullptr);
51     asyncInfo->GetData().SetRunningLockInstance(napiRunningLockIns);
52     // callback
53     if (argc == CREATE_CALLBACK_MAX_ARGC) {
54         return CreateAsyncCallback(env, argv, asyncInfo);
55     }
56 
57     // promise
58     return CreatePromise(env, argv, asyncInfo);
59 }
60 
IsSupported(napi_env env,napi_callback_info info)61 napi_value RunningLockNapi::IsSupported(napi_env env, napi_callback_info info)
62 {
63     size_t argc = ISSUPPORTED_MAX_ARGC;
64     napi_value argv[argc];
65     NapiUtils::GetCallbackInfo(env, info, argc, argv);
66 
67     NapiErrors error;
68     if (argc != ISSUPPORTED_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number)) {
69         return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
70     }
71 
72     int32_t numType;
73     napi_get_value_int32(env, argv[INDEX_0], &numType);
74     RunningLockType type = static_cast<RunningLockType>(numType);
75 
76     bool isSupported = (type == RunningLockType::RUNNINGLOCK_BACKGROUND) ||
77         (type == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
78 
79     napi_value result;
80     napi_get_boolean(env, isSupported, &result);
81     return result;
82 }
83 
Hold(napi_env env,napi_callback_info info)84 napi_value RunningLockNapi::Hold(napi_env env, napi_callback_info info)
85 {
86     size_t argc = HOLD_MAX_ARGC;
87     napi_value argv[argc];
88     napi_value thisArg = NapiUtils::GetCallbackInfo(env, info, argc, argv);
89     NapiErrors error;
90     if (argc != HOLD_MAX_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number)) {
91         return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
92     }
93 
94     uint32_t timeOut;
95     if (napi_ok != napi_get_value_uint32(env, argv[INDEX_0], &timeOut)) {
96         POWER_HILOGE(FEATURE_RUNNING_LOCK, "napi_get_value_uint32 failed");
97         return nullptr;
98     }
99     auto runningLock = UnwrapRunningLock(env, thisArg);
100     RETURN_IF_WITH_RET(runningLock == nullptr, nullptr);
101     runningLock->Lock(timeOut);
102 
103     return nullptr;
104 }
105 
IsHolding(napi_env env,napi_callback_info info)106 napi_value RunningLockNapi::IsHolding(napi_env env, napi_callback_info info)
107 {
108     size_t argc = 0;
109     napi_value thisArg = NapiUtils::GetCallbackInfo(env, info, argc, nullptr);
110     auto runningLock = UnwrapRunningLock(env, thisArg);
111     RETURN_IF_WITH_RET(runningLock == nullptr, nullptr);
112     bool isUsed = runningLock->IsUsed();
113     napi_value result;
114     napi_get_boolean(env, isUsed, &result);
115     return result;
116 }
117 
UnHold(napi_env env,napi_callback_info info)118 napi_value RunningLockNapi::UnHold(napi_env env, napi_callback_info info)
119 {
120     size_t argc = 0;
121     napi_value thisArg = NapiUtils::GetCallbackInfo(env, info, argc, nullptr);
122     auto runningLock = UnwrapRunningLock(env, thisArg);
123     RETURN_IF_WITH_RET(runningLock == nullptr, nullptr);
124     runningLock->UnLock();
125     return nullptr;
126 }
127 
CreateAsyncCallback(napi_env & env,napi_value argv[],std::unique_ptr<AsyncCallbackInfo> & asyncInfo)128 napi_value RunningLockNapi::CreateAsyncCallback(
129     napi_env& env, napi_value argv[], std::unique_ptr<AsyncCallbackInfo>& asyncInfo)
130 {
131     bool isStr = NapiUtils::CheckValueType(env, argv[INDEX_0], napi_string);
132     bool isNum = NapiUtils::CheckValueType(env, argv[INDEX_1], napi_number);
133     bool isFunc = NapiUtils::CheckValueType(env, argv[INDEX_2], napi_function);
134     if (!isStr || !isNum || !isFunc) {
135         POWER_HILOGD(
136             FEATURE_RUNNING_LOCK, "isStr: %{public}d, isNum: %{public}d, isFunc: %{public}d", isStr, isNum, isFunc);
137         return asyncInfo->GetError().ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
138     }
139     asyncInfo->GetData().SetName(env, argv[INDEX_0]);
140     asyncInfo->GetData().SetType(env, argv[INDEX_1]);
141     asyncInfo->CreateCallback(env, argv[INDEX_2]);
142 
143     AsyncWork(
144         env, asyncInfo, "CreateAsyncCallback",
145         [](napi_env env, void* data) {
146             AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
147             RETURN_IF(asyncInfo == nullptr);
148             auto error = asyncInfo->GetData().CreateRunningLock();
149             asyncInfo->GetError().Error(error);
150         },
151         [](napi_env env, napi_status status, void* data) {
152             AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
153             RETURN_IF(asyncInfo == nullptr);
154             napi_value result = asyncInfo->GetData().CreateInstanceForRunningLock(env);
155             asyncInfo->CallFunction(env, result);
156             asyncInfo->Release(env);
157             delete asyncInfo;
158         });
159     return nullptr;
160 }
161 
CreatePromise(napi_env & env,napi_value argv[],std::unique_ptr<AsyncCallbackInfo> & asyncInfo)162 napi_value RunningLockNapi::CreatePromise(
163     napi_env& env, napi_value argv[], std::unique_ptr<AsyncCallbackInfo>& asyncInfo)
164 {
165     bool isStr = NapiUtils::CheckValueType(env, argv[INDEX_0], napi_string);
166     bool isNum = NapiUtils::CheckValueType(env, argv[INDEX_1], napi_number);
167     if (!isStr || !isNum) {
168         POWER_HILOGW(FEATURE_RUNNING_LOCK, "isStr: %{public}d, isNum: %{public}d", isStr, isNum);
169         return asyncInfo->GetError().ThrowError(env, PowerErrors::ERR_PARAM_INVALID);
170     }
171 
172     napi_value promise;
173     asyncInfo->CreatePromise(env, promise);
174     RETURN_IF_WITH_RET(promise == nullptr, nullptr);
175     asyncInfo->GetData().SetName(env, argv[INDEX_0]);
176     asyncInfo->GetData().SetType(env, argv[INDEX_1]);
177 
178     AsyncWork(
179         env, asyncInfo, "CreatePromise",
180         [](napi_env env, void* data) {
181             AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
182             RETURN_IF(asyncInfo == nullptr);
183             auto error = asyncInfo->GetData().CreateRunningLock();
184             asyncInfo->GetError().Error(error);
185         },
186         [](napi_env env, napi_status status, void* data) {
187             AsyncCallbackInfo* asyncInfo = (AsyncCallbackInfo*)data;
188             RETURN_IF(asyncInfo == nullptr);
189             if (asyncInfo->GetError().IsError()) {
190                 napi_reject_deferred(env, asyncInfo->GetDeferred(), asyncInfo->GetError().GetNapiError(env));
191             } else {
192                 napi_value result = asyncInfo->GetData().CreateInstanceForRunningLock(env);
193                 napi_resolve_deferred(env, asyncInfo->GetDeferred(), result);
194             }
195             asyncInfo->Release(env);
196             delete asyncInfo;
197         });
198     return promise;
199 }
200 
AsyncWork(napi_env & env,std::unique_ptr<AsyncCallbackInfo> & asyncInfo,const std::string resourceName,napi_async_execute_callback execute,napi_async_complete_callback complete)201 void RunningLockNapi::AsyncWork(napi_env& env, std::unique_ptr<AsyncCallbackInfo>& asyncInfo,
202     const std::string resourceName, napi_async_execute_callback execute, napi_async_complete_callback complete)
203 {
204     napi_value resource = nullptr;
205     napi_create_string_utf8(env, resourceName.c_str(), NAPI_AUTO_LENGTH, &resource);
206     napi_create_async_work(
207         env, nullptr, resource, execute, complete, (void*)asyncInfo.get(), &asyncInfo->GetAsyncWork());
208     NAPI_CALL_RETURN_VOID(env, napi_queue_async_work(env, asyncInfo->GetAsyncWork()));
209     asyncInfo.release();
210 }
211 
UnwrapRunningLock(napi_env & env,napi_value & thisArg)212 std::shared_ptr<RunningLock> RunningLockNapi::UnwrapRunningLock(napi_env& env, napi_value& thisArg)
213 {
214     RunningLockEntity* entity = nullptr;
215     if (napi_ok != napi_unwrap(env, thisArg, (void**)&entity)) {
216         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Cannot unwrap for pointer");
217         return nullptr;
218     }
219     if (entity == nullptr || entity->runningLock == nullptr) {
220         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Entity runningLock is nullptr");
221         return nullptr;
222     }
223     return entity->runningLock;
224 }
225 } // namespace PowerMgr
226 } // namespace OHOS
227