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_ITME_H
17 #define OHOS_AAFWK_CONNECTION_STATE_ITME_H
18 
19 #include <string>
20 #include <map>
21 #include <vector>
22 
23 #include "connection_record.h"
24 #include "extension_ability_info.h"
25 #include "connection_data.h"
26 #include "data_ability_record.h"
27 
28 namespace OHOS {
29 namespace AAFwk {
30 /**
31  * @class DataAbilityCaller
32  * DataAbilityCaller,This class is used to record data ability caller info.
33  */
34 struct DataAbilityCaller {
35     bool isNotHap = false;
36     int32_t callerPid = 0;
37     int32_t callerUid = 0;
38     std::string callerName;
39     sptr<IRemoteObject> callerToken = nullptr;
40 };
41 
42 /**
43  * @class ConnectionStateItem
44  * ConnectionStateItem,This class is used to record connection state of a process.
45  */
46 class ConnectedExtension;
47 class ConnectedDataAbility;
48 class ConnectionStateItem : public std::enable_shared_from_this<ConnectionStateItem> {
49 public:
50     static std::shared_ptr<ConnectionStateItem> CreateConnectionStateItem(
51         const std::shared_ptr<ConnectionRecord> &record);
52 
53     static std::shared_ptr<ConnectionStateItem> CreateConnectionStateItem(
54         const DataAbilityCaller &dataCaller);
55 
56     ConnectionStateItem(int32_t callerUid, int32_t callerPid, const std::string &callerName);
57     virtual ~ConnectionStateItem();
58 
59     /**
60      * add a connection to target extension.
61      *
62      * @param record the connection record which mark an connection.
63      * @param data output relationship data.
64      * @return Returns true if need report relationship.
65      */
66     bool AddConnection(std::shared_ptr<ConnectionRecord> record, AbilityRuntime::ConnectionData &data);
67 
68     /**
69      * remove a connection to target extension.
70      *
71      * @param record the connection record which mark an connection.
72      * @param data output relationship data.
73      * @return Returns true if need report relationship.
74      */
75     bool RemoveConnection(std::shared_ptr<ConnectionRecord> record, AbilityRuntime::ConnectionData &data);
76 
77     /**
78      * add a connection to target data ability.
79      *
80      * @param caller the caller of this data ability.
81      * @param dataAbility data ability that acquired.
82      * @param data output relationship data.
83      * @return Returns true if need report relationship.
84      */
85     bool AddDataAbilityConnection(const DataAbilityCaller &caller,
86         const std::shared_ptr<DataAbilityRecord> &dataAbility, AbilityRuntime::ConnectionData &data);
87 
88     /**
89      * remove a connection to target data ability.
90      *
91      * @param caller the caller of this data ability.
92      * @param dataAbility data ability that acquired.
93      * @param data output relationship data.
94      * @return Returns true if need report relationship.
95      */
96     bool RemoveDataAbilityConnection(const DataAbilityCaller &caller,
97         const std::shared_ptr<DataAbilityRecord> &dataAbility, AbilityRuntime::ConnectionData &data);
98 
99     /**
100      * handle died of data ability.
101      *
102      * @param token target token of data ability.
103      * @param data output relationship data.
104      * @return Returns true if need report relationship.
105      */
106     bool HandleDataAbilityDied(const sptr<IRemoteObject> &token, AbilityRuntime::ConnectionData &data);
107 
108     /**
109      * generate all relationship data of this item.
110      *
111      * @param datas output relationship data.
112      */
113     void GenerateAllConnectionData(std::vector<AbilityRuntime::ConnectionData> &datas);
114 
115     /**
116      * check if connections is empty.
117      *
118      * @return true if no connections.
119      */
120     bool IsEmpty() const;
121 
122 private:
123     DISALLOW_COPY_AND_MOVE(ConnectionStateItem);
124 
125     void GenerateConnectionData(const std::shared_ptr<ConnectedExtension> &connectedExtension,
126         AbilityRuntime::ConnectionData &data);
127 
128     void GenerateConnectionData(const std::shared_ptr<ConnectedDataAbility> &connectedDataAbility,
129         AbilityRuntime::ConnectionData &data);
130 
131     int32_t callerUid_ = 0;
132     int32_t callerPid_ = 0;
133     std::string callerName_;
134     std::map<sptr<IRemoteObject>, std::shared_ptr<ConnectedExtension>> connectionMap_; // key:targetExtension token
135     std::map<sptr<IRemoteObject>, std::shared_ptr<ConnectedDataAbility>> dataAbilityMap_; // key:targetDatability token
136 };
137 }  // namespace AAFwk
138 }  // namespace OHOS
139 #endif  // OHOS_AAFWK_CONNECTION_STATE_ITME_H
140