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_ABILITY_RUNTIME_CONNECTION_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_CONNECTION_MANAGER_H 18 19 #include <map> 20 #include <vector> 21 #include "ability_connect_callback.h" 22 #include "ability_connection.h" 23 #include "element_name.h" 24 #include "errors.h" 25 #include "operation.h" 26 #include "want.h" 27 28 namespace OHOS { 29 namespace AbilityRuntime { 30 struct ConnectionInfo { 31 // connection caller 32 sptr<IRemoteObject> connectCaller; 33 // connection receiver 34 AAFwk::Operation connectReceiver; 35 // connection 36 sptr<AbilityConnection> abilityConnection; 37 38 int32_t userid; 39 void* uiServiceExtProxy = nullptr; 40 41 ConnectionInfo(const sptr<IRemoteObject> &connectCaller, const AAFwk::Operation &connectReceiver, 42 const sptr<AbilityConnection> &abilityConnection, int32_t accountId = -1) : connectCaller(connectCaller), 43 connectReceiver(connectReceiver), abilityConnection(abilityConnection), userid(accountId) 44 { 45 } 46 SetUIServiceExtProxyPtrConnectionInfo47 void SetUIServiceExtProxyPtr(void* proxyPtr) 48 { 49 uiServiceExtProxy = proxyPtr; 50 } 51 52 inline bool operator < (const ConnectionInfo &that) const 53 { 54 if (userid < that.userid) { 55 return true; 56 } 57 if (uiServiceExtProxy < that.uiServiceExtProxy) { 58 return true; 59 } 60 if (connectCaller < that.connectCaller) { 61 return true; 62 } 63 if (connectCaller == that.connectCaller && 64 connectReceiver.GetBundleName() < that.connectReceiver.GetBundleName()) { 65 return true; 66 } 67 if (connectCaller == that.connectCaller && 68 connectReceiver.GetBundleName() == that.connectReceiver.GetBundleName() && 69 connectReceiver.GetModuleName() < that.connectReceiver.GetModuleName()) { 70 return true; 71 } 72 if (connectCaller == that.connectCaller && 73 connectReceiver.GetBundleName() == that.connectReceiver.GetBundleName() && 74 connectReceiver.GetModuleName() == that.connectReceiver.GetModuleName() && 75 connectReceiver.GetAbilityName() < that.connectReceiver.GetAbilityName()) { 76 return true; 77 } 78 if (connectCaller == that.connectCaller && 79 connectReceiver.GetBundleName() == that.connectReceiver.GetBundleName() && 80 connectReceiver.GetModuleName() == that.connectReceiver.GetModuleName() && 81 connectReceiver.GetAbilityName() == that.connectReceiver.GetAbilityName() && 82 !(connectReceiver == that.connectReceiver)) { 83 return true; 84 } 85 return false; 86 } 87 }; 88 89 class ConnectionManager { 90 public: 91 /** 92 * @brief Destructor. 93 * 94 */ 95 ~ConnectionManager() = default; 96 97 ConnectionManager(const ConnectionManager&)=delete; 98 99 ConnectionManager& operator=(const ConnectionManager&)=delete; 100 101 static ConnectionManager& GetInstance(); 102 103 /** 104 * @brief connect ability connection. 105 * 106 * @param connectCaller The connection caller. 107 * @param connectReceiver The connection receiver. 108 * @param connectCallback The connection callback. 109 * @return Returns the result of connecting ability connection. 110 */ 111 ErrCode ConnectAbility(const sptr<IRemoteObject> &connectCaller, 112 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback); 113 114 /** 115 * @brief connect ability connection by user. 116 * 117 * @param connectCaller The connection caller. 118 * @param connectReceiver The connection receiver. 119 * @param accountId caller user. 120 * @param connectCallback The connection callback. 121 * @return Returns the result of connecting ability connection. 122 */ 123 ErrCode ConnectAbilityWithAccount(const sptr<IRemoteObject> &connectCaller, 124 const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback); 125 126 /** 127 * @brief connect uiService ability connection. 128 * 129 * @param connectCaller The connection caller. 130 * @param connectReceiver The connection receiver. 131 * @param connectCallback The connection callback. 132 * @return Returns the result of connecting uiService ability connection. 133 */ 134 ErrCode ConnectUIServiceExtensionAbility(const sptr<IRemoteObject>& connectCaller, 135 const AAFwk::Want& want, const sptr<AbilityConnectCallback>& connectCallback); 136 137 /** 138 * @brief disconnect ability connection. 139 * 140 * @param connectCaller The connection caller. 141 * @param connectReceiver The connection receiver. 142 * @param connectCallback The connection callback. 143 * @return Returns the result of disconnecting ability connection. 144 */ 145 ErrCode DisconnectAbility(const sptr<IRemoteObject> &connectCaller, 146 const AppExecFwk::ElementName &connectReceiver, const sptr<AbilityConnectCallback> &connectCallback); 147 148 /** 149 * @brief disconnect ability connection. 150 * 151 * @param connectCaller The connection caller. 152 * @param connectReceiver The connection receiver. 153 * @param connectCallback The connection callback. 154 * @return Returns the result of disconnecting ability connection. 155 */ 156 ErrCode DisconnectAbility(const sptr<IRemoteObject> &connectCaller, const AAFwk::Want &connectReceiver, 157 const sptr<AbilityConnectCallback> &connectCallback, int32_t accountId = -1); 158 159 /** 160 * @brief check the ability connection of caller is disconnect. 161 * 162 * @param connectCaller The connection caller. 163 * @return Returns whether the ability connection of caller is disconnect. 164 */ 165 bool DisconnectCaller(const sptr<IRemoteObject> &connectCaller); 166 167 /** 168 * @brief When service is dead, remove the connection 169 * 170 * @param connection The connection. 171 * @return Returns whether the connection is removed. 172 */ 173 bool RemoveConnection(const sptr<AbilityConnection> connection); 174 175 /** 176 * @brief Report the ability connection leak event. 177 * 178 * @param pid The process id. 179 * @param tid The thread id. 180 */ 181 void ReportConnectionLeakEvent(const int pid, const int tid); 182 183 bool DisconnectNonexistentService(const AppExecFwk::ElementName& element, 184 const sptr<AbilityConnection> connection); 185 private: 186 ConnectionManager() = default; 187 bool IsConnectCallerEqual(const sptr<IRemoteObject> &connectCaller, const sptr<IRemoteObject> &connectCallerOther); 188 bool IsConnectReceiverEqual(AAFwk::Operation &connectReceiver, 189 const AppExecFwk::ElementName &connectReceiverOther); 190 void* GetUIServiceExtProxyPtr(const AAFwk::Want& want); 191 bool MatchConnection( 192 const sptr<IRemoteObject>& connectCaller, const AAFwk::Want& connectReceiver, int32_t accountId, 193 const std::map<ConnectionInfo, std::vector<sptr<AbilityConnectCallback>>>::value_type& connection); 194 std::recursive_mutex connectionsLock_; 195 std::map<ConnectionInfo, std::vector<sptr<AbilityConnectCallback>>> abilityConnections_; 196 ErrCode ConnectAbilityInner(const sptr<IRemoteObject> &connectCaller, 197 const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback, 198 bool isUIService = false); 199 ErrCode CreateConnection(const sptr<IRemoteObject> &connectCaller, 200 const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback, bool isUIService); 201 }; 202 } // namespace AbilityRuntime 203 } // namespace OHOS 204 #endif // OHOS_ABILITY_RUNTIME_CONNECTION_MANAGER_H 205