• 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 #ifndef IPC_COMMON_H
17 #define IPC_COMMON_H
18 
19 #include <cinttypes>
20 #include <iremote_stub.h>
21 #include <optional>
22 #include <string>
23 
24 #include "nocopyable.h"
25 
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 enum Permission {
30     MANAGE_USER_IDM_PERMISSION,
31     USE_USER_IDM_PERMISSION,
32     ACCESS_USER_AUTH_INTERNAL_PERMISSION,
33     ACCESS_BIOMETRIC_PERMISSION,
34     ACCESS_AUTH_RESPOOL,
35     ENFORCE_USER_IDM,
36 };
37 
38 class IpcCommon final : public NoCopyable {
39 public:
40     using Recipient = std::function<void()>;
41     static int32_t GetCallingUserId(IPCObjectStub &stub, int32_t &userId);
42     static int32_t GetActiveUserId(std::optional<int32_t> &userId);
43     static bool CheckPermission(IPCObjectStub &stub, Permission permission);
44     static uint32_t GetAccessTokenId(IPCObjectStub &stub);
45     class PeerDeathRecipient final : public IPCObjectProxy::DeathRecipient {
46     public:
PeerDeathRecipient(Recipient && recipient)47         explicit PeerDeathRecipient(Recipient &&recipient) : recipient_(std::forward<Recipient>(recipient))
48         {
49         }
50         ~PeerDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & object)51         void OnRemoteDied(const wptr<IRemoteObject> &object) override
52         {
53             if (auto remote = object.promote(); !remote) {
54                 return;
55             }
56             if (recipient_) {
57                 recipient_();
58             }
59         };
60 
61     private:
62         Recipient recipient_;
63     };
64 
65 private:
66     static bool CheckNativeCallingProcessWhiteList(IPCObjectStub &stub);
67     static bool CheckDirectCallerAndFirstCallerIfSet(IPCObjectStub &stub, const std::string &permission);
68     static bool CheckDirectCaller(IPCObjectStub &stub, const std::string &permission);
69 };
70 } // namespace UserAuth
71 } // namespace UserIam
72 } // namespace OHOS
73 #endif // IPC_COMMON_H