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_CALLER_CALLBACK_H
17 #define OHOS_ABILITY_RUNTIME_CALLER_CALLBACK_H
18 
19 #include "iremote_object.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 constexpr const char* ON_RELEASE = "release";
24 constexpr const char* ON_DIED = "died";
25 class LocalCallRecord;
26 /**
27  * @class CallerCallBack
28  * CallerCallBack the callback function of caller.
29  */
30 class CallerCallBack : public std::enable_shared_from_this<CallerCallBack> {
31 public:
32     /* Caller's callback object */
33     using CallBackClosure = std::function<void(const sptr<IRemoteObject> &)>;
34     using OnReleaseClosure = std::function<void(const std::string &)>;
35     using OnRemoteStateChangedClosure = std::function<void(const std::string &)>;
36 
37     CallerCallBack() = default;
38     virtual ~CallerCallBack() = default;
39 
SetCallBack(CallBackClosure callback)40     void SetCallBack(CallBackClosure callback)
41     {
42         callback_ = callback;
43     };
SetOnRelease(OnReleaseClosure onRelease)44     void SetOnRelease(OnReleaseClosure onRelease)
45     {
46         onRelease_ = onRelease;
47     };
SetOnRemoteStateChanged(OnRemoteStateChangedClosure onRemoteStateChanged)48     void SetOnRemoteStateChanged(OnRemoteStateChangedClosure onRemoteStateChanged)
49     {
50         onRemoteStateChanged_ = onRemoteStateChanged;
51     };
InvokeCallBack(const sptr<IRemoteObject> & remoteObject)52     void InvokeCallBack(const sptr<IRemoteObject> &remoteObject)
53     {
54         if (callback_) {
55             callback_(remoteObject);
56             isCallBack_ = true;
57         }
58     };
InvokeOnRelease(const std::string & key)59     void InvokeOnRelease(const std::string &key)
60     {
61         if (onRelease_) {
62             onRelease_(key);
63         }
64     };
InvokeOnNotify(const std::string & state)65     void InvokeOnNotify(const std::string &state)
66     {
67         if (onRemoteStateChanged_) {
68             onRemoteStateChanged_(state);
69         }
70     };
IsCallBack()71     bool IsCallBack() const
72     {
73         return isCallBack_;
74     };
75 
SetRecord(const std::weak_ptr<LocalCallRecord> & localCallRecord)76     void SetRecord(const std::weak_ptr<LocalCallRecord> &localCallRecord)
77     {
78         localCallRecord_ = localCallRecord;
79     }
80 
GetRecord()81     std::shared_ptr<LocalCallRecord> GetRecord()
82     {
83         return localCallRecord_.lock();
84     }
85 
86 private:
87     CallBackClosure callback_ = {};
88     OnReleaseClosure onRelease_ = {};
89     OnRemoteStateChangedClosure onRemoteStateChanged_ = {};
90     bool isCallBack_ = false;
91     std::weak_ptr<LocalCallRecord> localCallRecord_;
92 };
93 } // namespace AbilityRuntime
94 } // namespace OHOS
95 #endif // OHOS_ABILITY_RUNTIME_CALLER_CALLBACK_H
96