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