1 /*
2  * Copyright (c) 2021 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 "dm_napi_common.h"
17 
18 #include <securec.h>
19 #include <string>
20 
21 #include "accesstoken_kit.h"
22 #include "bundle_constants.h"
23 #include "ipc_skeleton.h"
24 
25 namespace OHOS {
SetMemberInt32(napi_env env, napi_value result, const char *key, int32_t value)26 napi_status SetMemberInt32(napi_env env, napi_value result, const char *key, int32_t value)
27 {
28     napi_value num;
29     napi_status ret = napi_ok;
30     ret = napi_create_int32(env, value, &num);
31     if (ret != napi_ok) {
32         GNAPI_LOG("napi_create_int32 error, code is %{public}d", ret);
33         return ret;
34     }
35     ret = napi_set_named_property(env, result, key, num);
36     if (ret != napi_ok) {
37         GNAPI_LOG("napi_set_named_property error, code is %{public}d", ret);
38         return ret;
39     }
40     return ret;
41 }
42 
SetMemberUint32(napi_env env, napi_value result, const char *key, uint32_t value)43 napi_status SetMemberUint32(napi_env env, napi_value result, const char *key, uint32_t value)
44 {
45     napi_value num;
46     napi_status ret = napi_ok;
47     ret = napi_create_uint32(env, value, &num);
48     if (ret != napi_ok) {
49         GNAPI_LOG("napi_create_uint32 error, code is %{public}d", ret);
50         return ret;
51     }
52     ret = napi_set_named_property(env, result, key, num);
53     if (ret != napi_ok) {
54         GNAPI_LOG("napi_set_named_property error, code is %{public}d", ret);
55         return ret;
56     }
57     return ret;
58 }
59 
SetMemberUndefined(napi_env env, napi_value result, const char *key)60 napi_status SetMemberUndefined(napi_env env, napi_value result, const char *key)
61 {
62     napi_value undefined;
63     napi_status ret = napi_ok;
64     ret = napi_get_undefined(env, &undefined);
65     if (ret != napi_ok) {
66         GNAPI_LOG("napi_get_undefined error, code is %{public}d", ret);
67         return ret;
68     }
69     ret = napi_set_named_property(env, result, key, undefined);
70     if (ret != napi_ok) {
71         GNAPI_LOG("napi_set_named_property error, code is %{public}d", ret);
72         return ret;
73     }
74     return ret;
75 }
76 
CheckCallingPermission(const std::string &permission)77 bool CheckCallingPermission(const std::string &permission)
78 {
79     WLOGI("CheckCallingPermission, permission:%{public}s", permission.c_str());
80     if (!permission.empty() &&
81         Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission)
82         != AppExecFwk::Constants::PERMISSION_GRANTED) {
83         WLOGFE("%{public}s permission not granted.", permission.c_str());
84         return false;
85     }
86     WLOGI("CheckCallingPermission end.");
87     return true;
88 }
89 
SetErrorInfo(napi_env env, Rosen::DmErrorCode wret, const std::string& errMessage, napi_value result[], int count)90 void SetErrorInfo(napi_env env, Rosen::DmErrorCode wret, const std::string& errMessage, napi_value result[], int count)
91 {
92     if (count != 2 || result == nullptr) { // input param number is 2
93         GNAPI_LOG("Error, input param number must be 2");
94         return;
95     }
96     napi_value code = nullptr;
97     napi_value message = nullptr;
98     napi_create_int32(env, static_cast<int32_t>(wret), &code);
99     napi_create_string_utf8(env, errMessage.c_str(), strlen(errMessage.c_str()), &message);
100     napi_create_error(env, code, message, &result[0]);
101     napi_get_undefined(env, &result[1]);
102 }
103 
ProcessPromise(napi_env env, Rosen::DmErrorCode wret, napi_deferred deferred, napi_value result[], int count)104 void ProcessPromise(napi_env env, Rosen::DmErrorCode wret, napi_deferred deferred, napi_value result[], int count)
105 {
106     if (count != 2 || result == nullptr) { // input param number is 2
107         GNAPI_LOG("Error, input param number must be 2");
108         return;
109     }
110     GNAPI_LOG("AsyncProcess: Promise");
111     if (wret == Rosen::DmErrorCode::DM_OK) {
112         GNAPI_LOG("AsyncProcess: Promise resolve");
113         napi_resolve_deferred(env, deferred, result[1]);
114     } else {
115         GNAPI_LOG("AsyncProcess: Promise reject");
116         napi_reject_deferred(env, deferred, result[0]);
117     }
118 }
119 
ProcessCallback(napi_env env, napi_ref ref, napi_value result[], int count)120 void ProcessCallback(napi_env env, napi_ref ref, napi_value result[], int count)
121 {
122     if (count != 2 || result == nullptr) { // input param number is 2
123         GNAPI_LOG("Error, input param number must be 2");
124         return;
125     }
126     GNAPI_LOG("AsyncProcess Callback");
127     napi_value callback = nullptr;
128     napi_value returnVal = nullptr;
129     napi_get_reference_value(env, ref, &callback);
130     napi_call_function(env, nullptr, callback, 2, result, &returnVal); // 2: callback func input number
131     napi_delete_reference(env, ref);
132 }
133 
NAPICall(napi_env env, napi_status status)134 bool NAPICall(napi_env env, napi_status status)
135 {
136     if (status == napi_ok) {
137         return true;
138     }
139 
140     const napi_extended_error_info *errorInfo = nullptr;
141     bool isPending = false;
142     napi_get_last_error_info(env, &errorInfo);
143     napi_is_exception_pending(env, &isPending);
144     if (!isPending && errorInfo != nullptr) {
145         const char *errorMessage =
146             errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message";
147         napi_throw_error(env, nullptr, errorMessage);
148     }
149 
150     return false;
151 }
152 } // namespace OHOS
153