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 OHOS_ABILITY_RUNTIME_ASSERT_FAULT_CALLBACK_DEATH_MGR_H
17 #define OHOS_ABILITY_RUNTIME_ASSERT_FAULT_CALLBACK_DEATH_MGR_H
18 #include <memory>
19 #include <mutex>
20 #include <unordered_map>
21 
22 #include "assert_fault_proxy.h"
23 #include "iremote_object.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 class AssertFaultCallbackDeathMgr : public DelayedSingleton<AssertFaultCallbackDeathMgr>,
29     public std::enable_shared_from_this<AssertFaultCallbackDeathMgr> {
30     DISALLOW_COPY_AND_MOVE(AssertFaultCallbackDeathMgr);
31 public:
32     using CallbackTask = std::function<void(const std::string &)>;
33     AssertFaultCallbackDeathMgr() = default;
34     virtual ~AssertFaultCallbackDeathMgr();
35     struct DeathItem {
36         int32_t pid_;
37         sptr<IRemoteObject> iremote_;
38         sptr<IRemoteObject::DeathRecipient> deathObj_;
39         CallbackTask callbackTask_;
40     };
41 
42     void AddAssertFaultCallback(sptr<IRemoteObject> &remote, CallbackTask callback);
43     void RemoveAssertFaultCallback(const wptr<IRemoteObject> &remote, bool isCallbackDeath = false);
44     void CallAssertFaultCallback(
45         uint64_t assertFaultSessionId, AAFwk::UserStatus status = AAFwk::UserStatus::ASSERT_TERMINATE);
46 
47 private:
48     using DeathMap = std::unordered_map<uint64_t, DeathItem>;
49     std::mutex assertFaultSessionMutex_;
50     DeathMap assertFaultSessionDialogs_;
51 };
52 }  // namespace AbilityRuntime
53 }  // namespace OHOS
54 #endif  // OHOS_ABILITY_RUNTIME_ASSERT_FAULT_CALLBACK_DEATH_MGR_H
55