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 ABILITY_RUNTIME_CALLER_CALLBACK_H 17 #define ABILITY_RUNTIME_CALLER_CALLBACK_H 18 19 #include "iremote_object.h" 20 21 namespace OHOS { 22 namespace AbilityRuntime { 23 const std::string ON_RELEASE = "release"; 24 const std::string ON_DIED = "died"; 25 /** 26 * @class CallerCallBack 27 * CallerCallBack the callback function of caller. 28 */ 29 class CallerCallBack : public std::enable_shared_from_this<CallerCallBack> { 30 public: 31 /* Caller's callback object */ 32 using CallBackClosure = std::function<void(const sptr<IRemoteObject> &)>; 33 using OnReleaeClosure = std::function<void(const std::string &)>; 34 35 CallerCallBack() = default; 36 virtual ~CallerCallBack() = default; 37 SetCallBack(CallBackClosure callback)38 void SetCallBack(CallBackClosure callback) 39 { 40 callback_ = callback; 41 }; SetOnRelease(OnReleaeClosure onRelease)42 void SetOnRelease(OnReleaeClosure onRelease) 43 { 44 onRelease_ = onRelease; 45 }; InvokeCallBack(const sptr<IRemoteObject> & remoteObject)46 void InvokeCallBack(const sptr<IRemoteObject> &remoteObject) 47 { 48 if (callback_) { 49 callback_(remoteObject); 50 isCallBack_ = true; 51 } 52 }; InvokeOnRelease(const std::string & key)53 void InvokeOnRelease(const std::string &key) 54 { 55 if (onRelease_) { 56 onRelease_(key); 57 } 58 }; IsCallBack()59 bool IsCallBack() 60 { 61 return isCallBack_; 62 }; 63 64 private: 65 CallBackClosure callback_ = {}; 66 OnReleaeClosure onRelease_ = {}; 67 bool isCallBack_ = false; 68 }; 69 } // namespace AbilityRuntime 70 } // namespace OHOS 71 #endif // ABILITY_RUNTIME_CALLER_CALLBACK_H