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