• 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_LOCAL_CALL_RECORD_H
17 #define 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     LocalCallRecord(const AppExecFwk::ElementName &elementName);
32     virtual ~LocalCallRecord();
33 
34     void SetRemoteObject(const sptr<IRemoteObject> &call);
35     void SetRemoteObject(const sptr<IRemoteObject> &call, sptr<IRemoteObject::DeathRecipient> callRecipient);
36     void AddCaller(const std::shared_ptr<CallerCallBack> &callback);
37     bool RemoveCaller(const std::shared_ptr<CallerCallBack> &callback);
38     void OnCallStubDied(const wptr<IRemoteObject> &remote);
39     sptr<IRemoteObject> GetRemoteObject() const;
40     void InvokeCallBack() const;
41     AppExecFwk::ElementName GetElementName() const;
42     bool IsExistCallBack() const;
43     int GetRecordId();
44     std::vector<std::shared_ptr<CallerCallBack>> GetCallers();
45     bool IsSameObject(const sptr<IRemoteObject> &remote);
46 
47 private:
48     static int64_t callRecordId;
49     int recordId_ = 0;                                     // record id
50     sptr<IRemoteObject> remoteObject_ = nullptr;
51     sptr<IRemoteObject::DeathRecipient> callRecipient_ = nullptr;
52     std::vector<std::shared_ptr<CallerCallBack>> callers_;
53     AppExecFwk::ElementName elementName_ = {};
54 };
55 
56 /**
57  * @class CallRecipient
58  * CallRecipient notices IRemoteBroker died.
59  */
60 class CallRecipient : public IRemoteObject::DeathRecipient {
61 public:
62     using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
63 
CallRecipient(RemoteDiedHandler handler)64     CallRecipient(RemoteDiedHandler handler) : handler_(handler) {};
65     virtual ~CallRecipient() = default;
66 
OnRemoteDied(const wptr<IRemoteObject> & remote)67     void OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote) override
68     {
69         if (handler_) {
70             handler_(remote);
71         }
72     };
73 
74 private:
75     RemoteDiedHandler handler_;
76 };
77 }  // namespace AbilityRuntime
78 }  // namespace OHOS
79 #endif  // ABILITY_RUNTIME_LOCAL_CALL_RECORD_H
80