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