1 /*
2  * Copyright (c) 2023 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 "napi_process_skeleton.h"
17 
18 #include "iremote_invoker.h"
19 
20 namespace OHOS {
NAPI_getCallingPid(napi_env env,napi_callback_info info)21 napi_value NAPI_getCallingPid(napi_env env, napi_callback_info info)
22 {
23     napi_value global = nullptr;
24     napi_get_global(env, &global);
25     napi_value napiActiveStatus = nullptr;
26     napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
27     if (napiActiveStatus != nullptr) {
28         int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
29         napi_get_value_int32(env, napiActiveStatus, &activeStatus);
30         if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
31             napi_value callingPid = nullptr;
32             napi_get_named_property(env, global, "callingPid_", &callingPid);
33             return callingPid;
34         }
35     }
36     pid_t pid = getpid();
37     napi_value result = nullptr;
38     napi_create_int32(env, static_cast<int32_t>(pid), &result);
39     return result;
40 }
41 
NAPI_getCallingUid(napi_env env,napi_callback_info info)42 napi_value NAPI_getCallingUid(napi_env env, napi_callback_info info)
43 {
44     napi_value global = nullptr;
45     napi_get_global(env, &global);
46     napi_value napiActiveStatus = nullptr;
47     napi_get_named_property(env, global, "activeStatus_", &napiActiveStatus);
48     if (napiActiveStatus != nullptr) {
49         int32_t activeStatus = IRemoteInvoker::IDLE_INVOKER;
50         napi_get_value_int32(env, napiActiveStatus, &activeStatus);
51         if (activeStatus == IRemoteInvoker::ACTIVE_INVOKER) {
52             napi_value callingUid = nullptr;
53             napi_get_named_property(env, global, "callingUid_", &callingUid);
54             return callingUid;
55         }
56     }
57     uint32_t uid = getuid();
58     napi_value result = nullptr;
59     napi_create_int32(env, static_cast<int32_t>(uid), &result);
60     return result;
61 }
62 } // namespace OHOS