1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef CLIENT_DEATH_HANDLER_H
17c29fa5a6Sopenharmony_ci#define CLIENT_DEATH_HANDLER_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include <functional>
20c29fa5a6Sopenharmony_ci#include <map>
21c29fa5a6Sopenharmony_ci#include <mutex>
22c29fa5a6Sopenharmony_ci#include <vector>
23c29fa5a6Sopenharmony_ci
24c29fa5a6Sopenharmony_ci#include "iremote_object.h"
25c29fa5a6Sopenharmony_ci#include "refbase.h"
26c29fa5a6Sopenharmony_ci#include "nocopyable.h"
27c29fa5a6Sopenharmony_ci
28c29fa5a6Sopenharmony_ci#include "input_binder_client_death_recipient.h"
29c29fa5a6Sopenharmony_ci
30c29fa5a6Sopenharmony_cinamespace OHOS {
31c29fa5a6Sopenharmony_cinamespace MMI {
32c29fa5a6Sopenharmony_cienum class CallBackType : int32_t {
33c29fa5a6Sopenharmony_ci    CALLBACK_TYPE_AUTHORIZE_HELPER,
34c29fa5a6Sopenharmony_ci};
35c29fa5a6Sopenharmony_ciusing ClientDeathCallback = std::function<void(int32_t)>;
36c29fa5a6Sopenharmony_ciclass ClientDeathHandler final : RefBase {
37c29fa5a6Sopenharmony_cipublic:
38c29fa5a6Sopenharmony_ci    ClientDeathHandler();
39c29fa5a6Sopenharmony_ci    ~ClientDeathHandler();
40c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(ClientDeathHandler);
41c29fa5a6Sopenharmony_ci    bool RegisterClientDeathRecipient(const sptr<IRemoteObject> &binderClientSrv, int32_t pid);
42c29fa5a6Sopenharmony_ci    bool AddClientDeathCallback(CallBackType type, ClientDeathCallback callback);
43c29fa5a6Sopenharmony_ci    bool UnregisterClientDeathRecipient(const wptr<IRemoteObject> &remoteObj);
44c29fa5a6Sopenharmony_ci    void RemoveClientDeathCallback(CallBackType type);
45c29fa5a6Sopenharmony_ci
46c29fa5a6Sopenharmony_ciprotected:
47c29fa5a6Sopenharmony_ci    bool RegisterClientDeathRecipient(const sptr<IRemoteObject> &binderClientSrv);
48c29fa5a6Sopenharmony_ci    bool AddClientPid(const sptr<IRemoteObject> &binderClientSrv, int32_t pid);
49c29fa5a6Sopenharmony_ci    void RemoveClientPid(int32_t pid);
50c29fa5a6Sopenharmony_ci    int32_t FindClientPid(const sptr<IRemoteObject> &binderClientSrv);
51c29fa5a6Sopenharmony_ci    void NotifyDeath(int32_t pid);
52c29fa5a6Sopenharmony_ci
53c29fa5a6Sopenharmony_ciprivate:
54c29fa5a6Sopenharmony_ci    void OnDeath(const wptr<IRemoteObject> &remoteObj);
55c29fa5a6Sopenharmony_ci    std::mutex mutexPidMap_;
56c29fa5a6Sopenharmony_ci    std::map<int32_t, sptr<IRemoteObject>> clientPidMap_;
57c29fa5a6Sopenharmony_ci    std::mutex mutexDeathRecipient_;
58c29fa5a6Sopenharmony_ci    sptr<InputBinderClientDeathRecipient> deathRecipient_ = nullptr;
59c29fa5a6Sopenharmony_ci    std::mutex mutexCallbacks_;
60c29fa5a6Sopenharmony_ci    std::map<CallBackType, ClientDeathCallback> deathCallbacks_;
61c29fa5a6Sopenharmony_ci};
62c29fa5a6Sopenharmony_ci} // namespace MMI
63c29fa5a6Sopenharmony_ci} // namespace OHOS
64c29fa5a6Sopenharmony_ci#endif // CLIENT_DEATH_HANDLER_H
65