• 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(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(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     /**
159      * Get exist connection data including Extension and Data connection.
160      *
161      * @param infos output connection result.
162      */
163     void GetConnectionData(std::vector<AbilityRuntime::ConnectionData> &connectionData);
164 
165 private:
166     bool CheckDataAbilityConnectionParams(const DataAbilityCaller &caller,
167         const std::shared_ptr<DataAbilityRecord> &record) const;
168 
169 private:
170     class InnerAppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
171     public:
172         using ProcessDiedHandler = std::function<void(int32_t)>;
InnerAppStateObserver(const ProcessDiedHandler handler)173         explicit InnerAppStateObserver(const ProcessDiedHandler handler) : handler_(handler) {}
174         ~InnerAppStateObserver() = default;
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)175         void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) {}
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)176         void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) {}
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)177         void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) {}
OnProcessCreated(const AppExecFwk::ProcessData & processData)178         void OnProcessCreated(const AppExecFwk::ProcessData &processData) {}
OnProcessStateChanged(const AppExecFwk::ProcessData & processData)179         void OnProcessStateChanged(const AppExecFwk::ProcessData &processData) {}
OnApplicationStateChanged(const AppExecFwk::AppStateData & appStateData)180         void OnApplicationStateChanged(const AppExecFwk::AppStateData &appStateData) {}
OnProcessDied(const AppExecFwk::ProcessData & processData)181         void OnProcessDied(const AppExecFwk::ProcessData &processData)
182         {
183             if (handler_) {
184                 handler_(processData.pid);
185             }
186         }
187 
188     private:
189         ProcessDiedHandler handler_;
190     };
191 
192     bool AddConnectionInner(std::shared_ptr<ConnectionRecord> connectionRecord,
193         AbilityRuntime::ConnectionData &data);
194     bool RemoveConnectionInner(std::shared_ptr<ConnectionRecord> connectionRecord,
195         AbilityRuntime::ConnectionData &data);
196     bool AddDataAbilityConnectionInner(const DataAbilityCaller &caller,
197         const std::shared_ptr<DataAbilityRecord> &record, AbilityRuntime::ConnectionData &data);
198     bool RemoveDataAbilityConnectionInner(const DataAbilityCaller &caller,
199         const std::shared_ptr<DataAbilityRecord> &record, AbilityRuntime::ConnectionData &data);
200     void HandleCallerDied(int32_t callerPid);
201     std::shared_ptr<ConnectionStateItem> RemoveDiedCaller(int32_t callerPid);
202     void HandleDataAbilityDiedInner(const sptr<IRemoteObject> &abilityToken,
203         std::vector<AbilityRuntime::ConnectionData> &allData);
204     bool HandleDlpAbilityInner(const std::shared_ptr<AbilityRecord> &dlpAbility,
205         bool isAdd, AbilityRuntime::DlpStateData &dlpData);
206     void InitAppStateObserver();
207 
208 private:
209     std::shared_ptr<ConnectionObserverController> observerController_;
210 
211     ffrt::mutex stateLock_;
212     std::unordered_map<int32_t, std::shared_ptr<ConnectionStateItem>> connectionStates_;
213 
214     ffrt::mutex dlpLock_;
215     std::unordered_map<int32_t, std::shared_ptr<DlpStateItem>> dlpItems_;
216 
217     sptr<InnerAppStateObserver> appStateObserver_;
218     std::shared_ptr<TaskHandlerWrap> handler_;
219 
220     int32_t retry_ = 0;
221 };
222 }  // namespace AAFwk
223 }  // namespace OHOS
224 #endif  // OHOS_AAFWK_CONNECTION_STATE_MANAGER_H
225