• 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_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 
ConnectionInfoConnectionInfo38     ConnectionInfo(const sptr<IRemoteObject> &connectCaller, const AAFwk::Operation &connectReceiver,
39         const sptr<AbilityConnection> &abilityConnection) : connectCaller(connectCaller),
40         connectReceiver(connectReceiver), abilityConnection(abilityConnection)
41     {
42     }
43 
44     inline bool operator < (const ConnectionInfo &that) const
45     {
46         if (connectCaller < that.connectCaller) {
47             return true;
48         }
49         if (connectCaller == that.connectCaller &&
50             connectReceiver.GetBundleName() < that.connectReceiver.GetBundleName()) {
51             return true;
52         }
53         if (connectCaller == that.connectCaller &&
54             connectReceiver.GetBundleName() == that.connectReceiver.GetBundleName() &&
55             connectReceiver.GetModuleName() < that.connectReceiver.GetModuleName()) {
56             return true;
57         }
58         if (connectCaller == that.connectCaller &&
59             connectReceiver.GetBundleName() == that.connectReceiver.GetBundleName() &&
60             connectReceiver.GetModuleName() == that.connectReceiver.GetModuleName() &&
61             connectReceiver.GetAbilityName() < that.connectReceiver.GetAbilityName()) {
62             return true;
63         }
64         if (connectCaller == that.connectCaller &&
65             connectReceiver.GetBundleName() == that.connectReceiver.GetBundleName() &&
66             connectReceiver.GetModuleName() == that.connectReceiver.GetModuleName() &&
67             connectReceiver.GetAbilityName() == that.connectReceiver.GetAbilityName() &&
68             !(connectReceiver == that.connectReceiver)) {
69             return true;
70         }
71         return false;
72     }
73 };
74 
75 class ConnectionManager {
76 public:
77     /**
78      * @brief Destructor.
79      *
80      */
81     ~ConnectionManager() = default;
82 
83     ConnectionManager(const ConnectionManager&)=delete;
84 
85     ConnectionManager& operator=(const ConnectionManager&)=delete;
86 
87     static ConnectionManager& GetInstance();
88 
89     /**
90      * @brief connect ability connection.
91      *
92      * @param connectCaller The connection caller.
93      * @param connectReceiver The connection receiver.
94      * @param connectCallback The connection callback.
95      * @return Returns the result of connecting ability connection.
96      */
97     ErrCode ConnectAbility(const sptr<IRemoteObject> &connectCaller,
98         const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback);
99 
100     /**
101      * @brief connect ability connection by user.
102      *
103      * @param connectCaller The connection caller.
104      * @param connectReceiver The connection receiver.
105      * @param accountId caller user.
106      * @param connectCallback The connection callback.
107      * @return Returns the result of connecting ability connection.
108      */
109     ErrCode ConnectAbilityWithAccount(const sptr<IRemoteObject> &connectCaller,
110         const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback);
111 
112     /**
113      * @brief disconnect ability connection.
114      *
115      * @param connectCaller The connection caller.
116      * @param connectReceiver The connection receiver.
117      * @param connectCallback The connection callback.
118      * @return Returns the result of disconnecting ability connection.
119      */
120     ErrCode DisconnectAbility(const sptr<IRemoteObject> &connectCaller,
121         const AppExecFwk::ElementName &connectReceiver, const sptr<AbilityConnectCallback> &connectCallback);
122 
123     /**
124      * @brief check the ability connection of caller is disconnect.
125      *
126      * @param connectCaller The connection caller.
127      * @return Returns whether the ability connection of caller is disconnect.
128      */
129     bool DisconnectCaller(const sptr<IRemoteObject> &connectCaller);
130 
131     /**
132      * @brief check the ability connection of receiver is disconnect.
133      *
134      * @param connectReceiver The connection receiver.
135      * @return Returns whether the ability connection of receiver is disconnect.
136      */
137     bool DisconnectReceiver(const AppExecFwk::ElementName &connectReceiver);
138 
139     /**
140      * @brief Report the ability connection leak event.
141      *
142      * @param pid The process id.
143      * @param tid The thread id.
144      */
145     void ReportConnectionLeakEvent(const int pid, const int tid);
146 private:
147     ConnectionManager() = default;
148     bool IsConnectCallerEqual(const sptr<IRemoteObject> &connectCaller, const sptr<IRemoteObject> &connectCallerOther);
149     bool IsConnectReceiverEqual(AAFwk::Operation &connectReceiver,
150         const AppExecFwk::ElementName &connectReceiverOther);
151     bool MatchConnection(
152         const sptr<IRemoteObject>& connectCaller, const AAFwk::Want& connectReceiver,
153         const std::map<ConnectionInfo, std::vector<sptr<AbilityConnectCallback>>>::value_type& connection);
154     std::recursive_mutex connectionsLock_;
155     std::map<ConnectionInfo, std::vector<sptr<AbilityConnectCallback>>> abilityConnections_;
156     ErrCode ConnectAbilityInner(const sptr<IRemoteObject> &connectCaller,
157         const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback);
158     ErrCode CreateConnection(const sptr<IRemoteObject> &connectCaller,
159         const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback);
160 };
161 } // namespace AbilityRuntime
162 } // namespace OHOS
163 #endif // OHOS_ABILITY_RUNTIME_CONNECTION_MANAGER_H
164