1 /*
2  * Copyright (c) 2023 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_APP_RUNNING_STAUS_MOUDLE_H
17 #define OHOS_ABILITY_RUNTIME_APP_RUNNING_STAUS_MOUDLE_H
18 
19 #include <map>
20 #include <shared_mutex>
21 
22 #include "app_running_status_listener_interface.h"
23 #include "iremote_object.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 class AppRunningStatusModule : public std::enable_shared_from_this<AppRunningStatusModule> {
28 public:
29     AppRunningStatusModule() = default;
30     virtual ~AppRunningStatusModule() = default;
31 
32     /**
33      * Register listener.
34      *
35      * @param listener App running status listener object.
36      * @return Returns ERR_OK on success, others on failure.
37      */
38     int32_t RegisterListener(const sptr<AppRunningStatusListenerInterface> &listener);
39 
40     /**
41      * Unregister listener.
42      *
43      * @param listener App running status listener object.
44      * @return Returns ERR_OK on success, others on failure.
45      */
46     int32_t UnregisterListener(const sptr<AppRunningStatusListenerInterface> &listener);
47 
48     /**
49      * Notify the app running status event.
50      *
51      * @param bundle Bundle name in application record.
52      * @param uid Uid of bundle.
53      * @param runningStatus Running status.
54      * @return
55      */
56     void NotifyAppRunningStatusEvent(const std::string &bundle, int32_t uid, RunningStatus runningStatus);
57 
58     /**
59      * @class ClientDeathRecipient.
60      * Notices IRemoteBroker died.
61      */
62     class ClientDeathRecipient : public IRemoteObject::DeathRecipient {
63     public:
64         explicit ClientDeathRecipient(const std::weak_ptr<AppRunningStatusModule> &weakPtr);
65         virtual ~ClientDeathRecipient() = default;
66 
67         /**
68          * Handle remote object died event.
69          *
70          * @param Remote Remote object.
71          */
72         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
73 
74     private:
75         std::weak_ptr<AppRunningStatusModule> weakPtr_;
76     };
77 
78 private:
79     void SetDeathRecipient(const sptr<AppRunningStatusListenerInterface> &listener,
80         const sptr<IRemoteObject::DeathRecipient> &deathRecipient);
81     int32_t RemoveListenerAndDeathRecipient(const wptr<IRemoteObject> &remote);
82 
83     mutable std::mutex listenerMutex_;
84     std::map<sptr<AppRunningStatusListenerInterface>, sptr<IRemoteObject::DeathRecipient>> listeners_;
85 };
86 } // namespace AbilityRuntime
87 } // namespace OHOS
88 #endif // OHOS_ABILITY_RUNTIME_APP_RUNNING_STAUS_MOUDLE_H
89