• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_AAFWK_CONNECTION_STATE_MANAGER_H
17 #define OHOS_AAFWK_CONNECTION_STATE_MANAGER_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 
22 #include "application_state_observer_stub.h"
23 #include "connection_state_item.h"
24 #include "connection_observer_controller.h"
25 #include "dlp_connection_info.h"
26 #include "dlp_state_item.h"
27 
28 namespace OHOS {
29 namespace AAFwk {
30 /**
31  * @class ConnectionStateManager
32  * ConnectionStateManager manage connection states.
33  */
34 class ConnectionStateManager : public std::enable_shared_from_this<ConnectionStateManager> {
35     DECLARE_DELAYED_SINGLETON(ConnectionStateManager)
36 public:
37     /**
38      * Get process name of a pid.
39      *
40      * @param pid target pid.
41      * @return process name of target pid.
42      */
43     static std::string GetProcessNameByPid(int32_t pid);
44 
45     /**
46      * init manager.
47      *
48      */
49     void Init();
50 
51     /**
52      * register connection state observer.
53      *
54      * @param observer callback of client.
55      * @return Returns ERR_OK if success.
56      */
57     int RegisterObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer);
58 
59     /**
60      * unregister connection state observer.
61      *
62      * @param observer callback of client.
63      * @return Returns ERR_OK if success.
64      */
65     int UnregisterObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer);
66 
67     /**
68      * add an connection to manager.
69      *
70      * @param connectionRecord connection record info.
71      */
72     void AddConnection(const std::shared_ptr<ConnectionRecord> &connectionRecord);
73 
74     /**
75      * remove an connection.
76      *
77      * @param connectionRecord connection record info.
78      * @param isCallerDied whether caller was died.
79      */
80     void RemoveConnection(const std::shared_ptr<ConnectionRecord> &connectionRecord, bool isCallerDied);
81 
82     /**
83      * add a data ability acquired information to manager.
84      *
85      * @param caller caller of data ability.
86      * @param record target data ability.
87      */
88     void AddDataAbilityConnection(const DataAbilityCaller &caller,
89         const std::shared_ptr<DataAbilityRecord> &record);
90 
91     /**
92      * remove a data ability acquired information from manager.
93      *
94      * @param caller caller of data ability.
95      * @param record target data ability.
96      */
97     void RemoveDataAbilityConnection(const DataAbilityCaller &caller,
98         const std::shared_ptr<DataAbilityRecord> &record);
99 
100     /**
101      * handle when data ability was died.
102      *
103      * @param record target data ability.
104      */
105     void HandleDataAbilityDied(const std::shared_ptr<DataAbilityRecord> &record);
106 
107     /**
108      * handle when data ability caller was died.
109      *
110      * @param callerPid caller pid of data ability.
111      */
112     void HandleDataAbilityCallerDied(int32_t callerPid);
113 
114     /**
115      * add dlp manager to manager.
116      *
117      * @param dlpManger dlp manager record.
118      */
119     void AddDlpManager(const std::shared_ptr<AbilityRecord> &dlpManger);
120 
121     /**
122      * remove dlp manager from manager.
123      *
124      * @param dlpManger dlp manager record.
125      */
126     void RemoveDlpManager(const std::shared_ptr<AbilityRecord> &dlpManger);
127 
128     /**
129      * a dlp ability was started.
130      *
131      * @param dlpAbility dlp manager record.
132      */
133     void AddDlpAbility(const std::shared_ptr<AbilityRecord> &dlpAbility);
134 
135     /**
136      * a dlp ability was terminated.
137      *
138      * @param dlpAbility dlp manager record.
139      */
140     void RemoveDlpAbility(const std::shared_ptr<AbilityRecord> &dlpAbility);
141 
142     /**
143      * handle app process died.
144      *
145      * @param pid app process pid.
146      */
147     void HandleAppDied(int32_t pid);
148 
149     /**
150      * get exist dlp connection infos.
151      *
152      * @param infos output dlp connection result.
153      */
154     void GetDlpConnectionInfos(std::vector<AbilityRuntime::DlpConnectionInfo> &infos);
155 
156 private:
157     bool CheckDataAbilityConnectionParams(const DataAbilityCaller &caller,
158         const std::shared_ptr<DataAbilityRecord> &record) const;
159 
160 private:
161     class InnerAppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
162     public:
163         using ProcessDiedHandler = std::function<void(int32_t)>;
InnerAppStateObserver(const ProcessDiedHandler handler)164         explicit InnerAppStateObserver(const ProcessDiedHandler handler) : handler_(handler) {}
165         ~InnerAppStateObserver() = default;
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)166         void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) {}
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)167         void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) {}
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)168         void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) {}
OnProcessCreated(const AppExecFwk::ProcessData & processData)169         void OnProcessCreated(const AppExecFwk::ProcessData &processData) {}
OnProcessStateChanged(const AppExecFwk::ProcessData & processData)170         void OnProcessStateChanged(const AppExecFwk::ProcessData &processData) {}
OnApplicationStateChanged(const AppExecFwk::AppStateData & appStateData)171         void OnApplicationStateChanged(const AppExecFwk::AppStateData &appStateData) {}
OnProcessDied(const AppExecFwk::ProcessData & processData)172         void OnProcessDied(const AppExecFwk::ProcessData &processData)
173         {
174             if (handler_) {
175                 handler_(processData.pid);
176             }
177         }
178 
179     private:
180         ProcessDiedHandler handler_;
181     };
182 
183     bool AddConnectionInner(const std::shared_ptr<ConnectionRecord> &connectionRecord,
184         AbilityRuntime::ConnectionData &data);
185     bool RemoveConnectionInner(const std::shared_ptr<ConnectionRecord> &connectionRecord,
186         AbilityRuntime::ConnectionData &data);
187     bool AddDataAbilityConnectionInner(const DataAbilityCaller &caller,
188         const std::shared_ptr<DataAbilityRecord> &record, AbilityRuntime::ConnectionData &data);
189     bool RemoveDataAbilityConnectionInner(const DataAbilityCaller &caller,
190         const std::shared_ptr<DataAbilityRecord> &record, AbilityRuntime::ConnectionData &data);
191     void HandleCallerDied(int32_t callerPid);
192     std::shared_ptr<ConnectionStateItem> RemoveDiedCaller(int32_t callerPid);
193     void HandleDataAbilityDiedInner(const sptr<IRemoteObject> &abilityToken,
194         std::vector<AbilityRuntime::ConnectionData> &allData);
195     bool HandleDlpAbilityInner(const std::shared_ptr<AbilityRecord> &dlpAbility,
196         bool isAdd, AbilityRuntime::DlpStateData &dlpData);
197     void InitAppStateObserver();
198 
199 private:
200     std::shared_ptr<ConnectionObserverController> observerController_;
201 
202     std::recursive_mutex stateLock_;
203     std::unordered_map<int32_t, std::shared_ptr<ConnectionStateItem>> connectionStates_;
204 
205     std::recursive_mutex dlpLock_;
206     std::unordered_map<int32_t, std::shared_ptr<DlpStateItem>> dlpItems_;
207 
208     sptr<InnerAppStateObserver> appStateObserver_;
209 };
210 }  // namespace AAFwk
211 }  // namespace OHOS
212 #endif  // OHOS_AAFWK_CONNECTION_STATE_MANAGER_H
213