• 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 
39 protected:
40     bool RegisterClientDeathRecipient(const sptr<IRemoteObject> &binderClientSrv);
41     bool AddClientPid(const sptr<IRemoteObject> &binderClientSrv, int32_t pid);
42     void RemoveClientPid(int32_t pid);
43     int32_t FindClientPid(const sptr<IRemoteObject> &binderClientSrv);
44     void NotifyDeath(int32_t pid);
45 
46 private:
47     void OnDeath(const wptr<IRemoteObject> &remoteObj);
48     std::mutex mutexPidMap_;
49     std::map<int32_t, sptr<IRemoteObject>> clientPidMap_;
50     std::mutex mutexDeathRecipient_;
51     sptr<InputBinderClientDeathRecipient> deathRecipient_ = nullptr;
52     std::mutex mutexCallbacks_;
53     std::map<CallBackType, ClientDeathCallback> deathCallbacks_;
54 };
55 } // namespace MMI
56 } // namespace OHOS
57 #endif // CLIENT_DEATH_HANDLER_H
58