1 /*
2  * Copyright (c) 2022-2023 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_LOCAL_CALL_RECORD_H
17 #define OHOS_ABILITY_RUNTIME_LOCAL_CALL_RECORD_H
18 
19 #include "caller_callback.h"
20 #include "element_name.h"
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
25 /**
26  * @class LocalCallRecord
27  * LocalCallRecord record local call info.
28  */
29 class LocalCallRecord : public std::enable_shared_from_this<LocalCallRecord> {
30 public:
31     explicit LocalCallRecord(const AppExecFwk::ElementName &elementName);
32     virtual ~LocalCallRecord();
33 
34     void ClearData();
35     void SetRemoteObject(const sptr<IRemoteObject> &call);
36     void SetRemoteObject(const sptr<IRemoteObject> &call, sptr<IRemoteObject::DeathRecipient> callRecipient);
37     void AddCaller(const std::shared_ptr<CallerCallBack> &callback);
38     bool RemoveCaller(const std::shared_ptr<CallerCallBack> &callback);
39     void OnCallStubDied(const wptr<IRemoteObject> &remote);
40     void NotifyRemoteStateChanged(int32_t abilityState);
41     sptr<IRemoteObject> GetRemoteObject() const;
42     void InvokeCallBack() const;
43     AppExecFwk::ElementName GetElementName() const;
44     bool IsExistCallBack() const;
45     int GetRecordId() const;
46     std::vector<std::shared_ptr<CallerCallBack>> GetCallers() const;
47     bool IsSameObject(const sptr<IRemoteObject> &remote) const;
48     void SetIsSingleton(bool flag);
49     bool IsSingletonRemote();
50     void SetConnection(const sptr<IRemoteObject> &connect);
51     sptr<IRemoteObject> GetConnection();
52     void SetUserId(int32_t userId);
53     int32_t GetUserId() const;
54 private:
55     static int64_t callRecordId;
56     int recordId_ = 0;
57     int32_t userId_ = -1;
58     sptr<IRemoteObject> remoteObject_ = nullptr;
59     wptr<IRemoteObject> connection_ = nullptr;
60     sptr<IRemoteObject::DeathRecipient> callRecipient_ = nullptr;
61     std::vector<std::shared_ptr<CallerCallBack>> callers_;
62     AppExecFwk::ElementName elementName_ = {};
63     bool isSingleton_ = false;
64 };
65 
66 /**
67  * @class CallRecipient
68  * CallRecipient notices IRemoteBroker died.
69  */
70 class CallRecipient : public IRemoteObject::DeathRecipient {
71 public:
72     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
73 
CallRecipient(RemoteDiedHandler handler)74     explicit CallRecipient(RemoteDiedHandler handler) : handler_(handler) {};
75     virtual ~CallRecipient() = default;
76 
OnRemoteDied(const wptr<IRemoteObject> & remote)77     void OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote) override
78     {
79         if (handler_) {
80             handler_(remote);
81         }
82     };
83 
84 private:
85     RemoteDiedHandler handler_ = nullptr;
86 };
87 } // namespace AbilityRuntime
88 } // namespace OHOS
89 #endif // OHOS_ABILITY_RUNTIME_LOCAL_CALL_RECORD_H
90