• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const int ERROR_CODE_LEN = 16; // 16 is the max len of error code
26 
27 namespace OHOS {
SetMemberInt32(napi_env env,napi_value result,const char * key,int32_t value)28 napi_status SetMemberInt32(napi_env env, napi_value result, const char *key, int32_t value)
29 {
30     napi_value num;
31     GNAPI_INNER(napi_create_int32(env, value, &num));
32     GNAPI_INNER(napi_set_named_property(env, result, key, num));
33     return napi_ok;
34 }
35 
SetMemberUint32(napi_env env,napi_value result,const char * key,uint32_t value)36 napi_status SetMemberUint32(napi_env env, napi_value result, const char *key, uint32_t value)
37 {
38     napi_value num;
39     GNAPI_INNER(napi_create_uint32(env, value, &num));
40     GNAPI_INNER(napi_set_named_property(env, result, key, num));
41     return napi_ok;
42 }
43 
SetMemberUndefined(napi_env env,napi_value result,const char * key)44 napi_status SetMemberUndefined(napi_env env, napi_value result, const char *key)
45 {
46     napi_value undefined;
47     GNAPI_INNER(napi_get_undefined(env, &undefined));
48     GNAPI_INNER(napi_set_named_property(env, result, key, undefined));
49     return napi_ok;
50 }
51 
CheckCallingPermission(const std::string & permission)52 bool CheckCallingPermission(const std::string &permission)
53 {
54     WLOGFI("CheckCallingPermission, permission:%{public}s", permission.c_str());
55     if (!permission.empty() &&
56         Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission)
57         != AppExecFwk::Constants::PERMISSION_GRANTED) {
58         WLOGFE("%{public}s permission not granted.", permission.c_str());
59         return false;
60     }
61     WLOGFI("CheckCallingPermission end.");
62     return true;
63 }
64 
SetErrorInfo(napi_env env,Rosen::DmErrorCode wret,const std::string & errMessage,napi_value result[],int count)65 void SetErrorInfo(napi_env env, Rosen::DmErrorCode wret, const std::string& errMessage, napi_value result[], int count)
66 {
67     if (count != 2 || result == nullptr) { // input param number is 2
68         GNAPI_LOG("Error, input param number must be 2");
69         return;
70     }
71     napi_value code = nullptr;
72     napi_value message = nullptr;
73     char errorCode[ERROR_CODE_LEN];
74     (void)sprintf_s(errorCode, sizeof(errorCode), "%d", static_cast<int32_t>(wret));
75     napi_create_string_utf8(env, errorCode, strlen(errorCode), &code);
76     napi_create_string_utf8(env, errMessage.c_str(), strlen(errMessage.c_str()), &message);
77     napi_create_error(env, code, message, &result[0]);
78     napi_get_undefined(env, &result[1]);
79 }
80 
ProcessPromise(napi_env env,Rosen::DmErrorCode wret,napi_deferred deferred,napi_value result[],int count)81 void ProcessPromise(napi_env env, Rosen::DmErrorCode wret, napi_deferred deferred, napi_value result[], int count)
82 {
83     if (count != 2 || result == nullptr) { // input param number is 2
84         GNAPI_LOG("Error, input param number must be 2");
85         return;
86     }
87     GNAPI_LOG("AsyncProcess: Promise");
88     if (wret == Rosen::DmErrorCode::DM_OK) {
89         GNAPI_LOG("AsyncProcess: Promise resolve");
90         napi_resolve_deferred(env, deferred, result[1]);
91     } else {
92         GNAPI_LOG("AsyncProcess: Promise reject");
93         napi_reject_deferred(env, deferred, result[0]);
94     }
95 }
96 
ProcessCallback(napi_env env,napi_ref ref,napi_value result[],int count)97 void ProcessCallback(napi_env env, napi_ref ref, napi_value result[], int count)
98 {
99     if (count != 2 || result == nullptr) { // input param number is 2
100         GNAPI_LOG("Error, input param number must be 2");
101         return;
102     }
103     GNAPI_LOG("AsyncProcess Callback");
104     napi_value callback = nullptr;
105     napi_value returnVal = nullptr;
106     napi_get_reference_value(env, ref, &callback);
107     napi_call_function(env, nullptr, callback, 2, result, &returnVal); // 2: callback func input number
108     napi_delete_reference(env, ref);
109 }
110 
NAPICall(napi_env env,napi_status status)111 bool NAPICall(napi_env env, napi_status status)
112 {
113     if (status == napi_ok) {
114         return true;
115     }
116 
117     const napi_extended_error_info *errorInfo = nullptr;
118     bool isPending = false;
119     napi_get_last_error_info(env, &errorInfo);
120     napi_is_exception_pending(env, &isPending);
121     if (!isPending && errorInfo != nullptr) {
122         const char *errorMessage =
123             errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message";
124         napi_throw_error(env, nullptr, errorMessage);
125     }
126 
127     return false;
128 }
129 } // namespace OHOS
130