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_CALL_RECORD_H
17 #define OHOS_ABILITY_RUNTIME_CALL_RECORD_H
18 
19 #include "ability_connect_callback_interface.h"
20 #include "nocopyable.h"
21 
22 namespace OHOS {
23 namespace AAFwk {
24 class AbilityRecord;
25 /**
26  * @class AbilityCallRecipient
27  * AbilityCallRecipient notices IRemoteBroker died.
28  */
29 class AbilityCallRecipient : public IRemoteObject::DeathRecipient {
30 public:
31     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
32 
AbilityCallRecipient(RemoteDiedHandler handler)33     explicit AbilityCallRecipient(RemoteDiedHandler handler) : handler_(handler) {};
34     virtual ~AbilityCallRecipient() = default;
35 
OnRemoteDied(const wptr<IRemoteObject> & remote)36     void OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote) override
37     {
38         if (handler_) {
39             handler_(remote);
40         }
41     };
42 
43 private:
44     RemoteDiedHandler handler_;
45 };
46 
47 /**
48  * @enum CallState
49  * CallState defines the state of calling ability.
50  */
51 enum class CallState { INIT, REQUESTING, REQUESTED };
52 /**
53  * @class CallRecord
54  * CallRecord,This class is used to record information about a connection of calling ability.
55  */
56 class CallRecord : public std::enable_shared_from_this<CallRecord> {
57 public:
58     CallRecord(const int32_t callerUid, const std::shared_ptr<AbilityRecord> &targetService,
59         const sptr<IAbilityConnection> &connCallback, const sptr<IRemoteObject> &callToken);
60     virtual ~CallRecord();
61 
62     static std::shared_ptr<CallRecord> CreateCallRecord(const int32_t callerUid,
63         const std::shared_ptr<AbilityRecord> &targetService, const sptr<IAbilityConnection> &connCallback,
64         const sptr<IRemoteObject> &callToken);
65 
66     void SetCallStub(const sptr<IRemoteObject> &call);
67     sptr<IRemoteObject> GetCallStub();
68     void SetConCallBack(const sptr<IAbilityConnection> &connCallback);
69     sptr<IAbilityConnection> GetConCallBack() const;
70 
71     void Dump(std::vector<std::string> &info) const;
72     bool SchedulerConnectDone();
73     bool SchedulerDisconnectDone();
74     void OnCallStubDied(const wptr<IRemoteObject> &remote);
75 
76     int32_t GetCallerUid() const;
77     bool IsCallState(const CallState &state) const;
78     void SetCallState(const CallState &state);
79     int32_t GetCallRecordId() const;
80     AppExecFwk::ElementName GetTargetServiceName() const;
81     sptr<IRemoteObject> GetCallerToken() const;
82 
83 private:
84     static int64_t callRecordId;
85     int recordId_;                                  // record id
86     int32_t callerUid_;                             // caller uid
87     CallState state_;                               // call state
88     sptr<IRemoteObject> callRemoteObject_ = nullptr;
89     sptr<IRemoteObject::DeathRecipient> callDeathRecipient_ = nullptr;
90     std::weak_ptr<AbilityRecord> service_;             // target:service need to be called
91     sptr<IAbilityConnection> connCallback_ = nullptr;  // service connect callback
92     sptr<IRemoteObject> callerToken_ = nullptr;     // caller token
93     int64_t startTime_ = 0;                         // records first time of ability start
94 
95     DISALLOW_COPY_AND_MOVE(CallRecord);
96 };
97 }  // namespace AAFwk
98 }  // namespace OHOS
99 #endif  // OHOS_ABILITY_RUNTIME_CALL_RECORD_H
100