• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_RECORD_H
17 #define OHOS_ABILITY_RUNTIME_CONNECTION_RECORD_H
18 
19 #include <mutex>
20 #include "ability_connect_callback_interface.h"
21 #include "ability_record.h"
22 #include "extension_config.h"
23 #include "nocopyable.h"
24 
25 namespace OHOS {
26 namespace AAFwk {
27 /**
28  * @enum ConnectionState
29  * ConnectionState defines the state of connect ability.
30  */
31 enum class ConnectionState { INIT, CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED };
32 /**
33  * @class ConnectionRecord
34  * ConnectionRecord,This class is used to record information about a connection.
35  */
36 class ConnectionRecord : public std::enable_shared_from_this<ConnectionRecord> {
37 public:
38     ConnectionRecord(const sptr<IRemoteObject> &callerToken, const std::shared_ptr<AbilityRecord> &targetService,
39         const sptr<IAbilityConnection> &connCallback);
40     virtual ~ConnectionRecord();
41 
42     /**
43      * create a connection record by caller token , service ability and call back ipc object.
44      *
45      * @param callerToken, the token of caller ability.
46      * @param targetService, target service ability.
47      * @param callback, call back (ipc object).
48      * @return Return the connect record.
49      */
50     static std::shared_ptr<ConnectionRecord> CreateConnectionRecord(const sptr<IRemoteObject> &callerToken,
51         const std::shared_ptr<AbilityRecord> &targetService, const sptr<IAbilityConnection> &connCallback);
52 
53     /**
54      * set the connect state.
55      *
56      * @param state, target connection state.
57      */
58     void SetConnectState(const ConnectionState &state);
59 
60     /**
61      * get the connect state.
62      *
63      * @return state, target connection state.
64      */
65     ConnectionState GetConnectState() const;
66 
67     /**
68      * get the token of the ability.
69      *
70      * @return token.
71      */
72     sptr<IRemoteObject> GetToken() const;
73 
74     /**
75      * get the ability record from connection record.
76      *
77      * @return AbilityRecord.
78      */
79     std::shared_ptr<AbilityRecord> GetAbilityRecord() const;
80 
81     sptr<IAbilityConnection> GetAbilityConnectCallback() const;
82 
83     /**
84      * disconnect the service ability.
85      *
86      * @return Returns ERR_OK on success, others on failure.
87      */
88     int DisconnectAbility();
89 
90     /**
91      * force to disconnect time out event.
92      *
93      */
94     void DisconnectTimeout();
95 
96     /**
97      * complete connect ability and invoke callback.
98      *
99      */
100     void CompleteConnect(int resultCode);
101 
102     /**
103      * complete disconnect ability and invoke callback.
104      *
105      */
106     void CompleteDisconnect(int resultCode, bool isCallerDied, bool isTargetDied = false);
107 
108     /**
109      * scheduler target service disconnect done.
110      *
111      */
112     void ScheduleDisconnectAbilityDone();
113 
114     /**
115      * scheduler target service Connect done.
116      *
117      */
118     void ScheduleConnectAbilityDone();
119 
120     /**
121      * get connection record id.
122      *
123      */
GetRecordId()124     inline int GetRecordId() const
125     {
126         return recordId_;
127     }
128 
129     void ClearConnCallBack();
130 
131     std::string ConvertConnectionState(const ConnectionState &state) const;
132 
133     void Dump(std::vector<std::string> &info) const;
134 
135     void AttachCallerInfo();
136     int32_t GetCallerUid() const;
137     int32_t GetCallerPid() const;
138     uint32_t GetCallerTokenId() const;
139     std::string GetCallerName() const;
140     sptr<IRemoteObject> GetTargetToken() const;
141     sptr<IRemoteObject> GetConnection() const;
142 
143 private:
144     static int64_t connectRecordId;
145     int recordId_ = 0;                                  // record id
146     ConnectionState state_;                         // service connection state
147     sptr<IRemoteObject> callerToken_ = nullptr;               // from:caller token
148     std::shared_ptr<AbilityRecord> targetService_ = nullptr;  // target:service need to be connected
149 
150     mutable std::mutex callbackMutex_;
151     sptr<IAbilityConnection> connCallback_ = nullptr;         // service connect callback
152     int32_t callerUid_ = 0;                             // caller uid
153     int32_t callerPid_ = 0;                             // caller pid
154     uint32_t callerTokenId_ = 0;                        // caller pid
155     std::string callerName_;                        // caller bundleName or processName
156 
157     DISALLOW_COPY_AND_MOVE(ConnectionRecord);
158 };
159 }  // namespace AAFwk
160 }  // namespace OHOS
161 #endif  // OHOS_ABILITY_RUNTIME_CONNECTION_RECORD_H
162