• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 CLIENT_DEATH_HANDLER_H
17 #define CLIENT_DEATH_HANDLER_H
18 
19 #include <map>
20 
21 #include "input_binder_client_death_recipient.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 enum class CallBackType : int32_t {
26     CALLBACK_TYPE_AUTHORIZE_HELPER,
27 };
28 using ClientDeathCallback = std::function<void(int32_t)>;
29 class ClientDeathHandler final : RefBase {
30 public:
31     ClientDeathHandler();
32     ~ClientDeathHandler();
33     DISALLOW_COPY_AND_MOVE(ClientDeathHandler);
34     bool RegisterClientDeathRecipient(const sptr<IRemoteObject> &binderClientSrv, int32_t pid);
35     bool AddClientDeathCallback(CallBackType type, ClientDeathCallback callback);
36     bool UnregisterClientDeathRecipient(const wptr<IRemoteObject> &remoteObj);
37     void RemoveClientDeathCallback(CallBackType type);
38     sptr<IRemoteObject> GetClientProxy(int32_t pid);
39 
40 protected:
41     bool RegisterClientDeathRecipient(const sptr<IRemoteObject> &binderClientSrv);
42     bool AddClientPid(const sptr<IRemoteObject> &binderClientSrv, int32_t pid);
43     void RemoveClientPid(int32_t pid);
44     int32_t FindClientPid(const sptr<IRemoteObject> &binderClientSrv);
45     void NotifyDeath(int32_t pid);
46 
47 private:
48     void OnDeath(const wptr<IRemoteObject> &remoteObj);
49     std::mutex mutexPidMap_;
50     std::map<int32_t, sptr<IRemoteObject>> clientPidMap_;
51     std::mutex mutexDeathRecipient_;
52     sptr<InputBinderClientDeathRecipient> deathRecipient_ = nullptr;
53     std::mutex mutexCallbacks_;
54     std::map<CallBackType, ClientDeathCallback> deathCallbacks_;
55 };
56 } // namespace MMI
57 } // namespace OHOS
58 #endif // CLIENT_DEATH_HANDLER_H
59