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