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