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 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 void NotifyRemoteStateChanged(int32_t abilityState); 40 sptr<IRemoteObject> GetRemoteObject() const; 41 void InvokeCallBack() const; 42 AppExecFwk::ElementName GetElementName() const; 43 bool IsExistCallBack() const; 44 int GetRecordId() const; 45 std::vector<std::shared_ptr<CallerCallBack>> GetCallers() const; 46 bool IsSameObject(const sptr<IRemoteObject> &remote) const; 47 void SetIsSingleton(bool flag); 48 bool IsSingletonRemote(); 49 void SetConnection(const sptr<IRemoteObject> &connect); 50 sptr<IRemoteObject> GetConnection(); 51 void SetUserId(int32_t userId); 52 int32_t GetUserId() const; 53 private: 54 static int64_t callRecordId; 55 int recordId_ = 0; 56 int32_t userId_ = -1; 57 sptr<IRemoteObject> remoteObject_ = nullptr; 58 wptr<IRemoteObject> connection_ = nullptr; 59 sptr<IRemoteObject::DeathRecipient> callRecipient_ = nullptr; 60 std::vector<std::shared_ptr<CallerCallBack>> callers_; 61 AppExecFwk::ElementName elementName_ = {}; 62 bool isSingleton_ = false; 63 }; 64 65 /** 66 * @class CallRecipient 67 * CallRecipient notices IRemoteBroker died. 68 */ 69 class CallRecipient : public IRemoteObject::DeathRecipient { 70 public: 71 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 72 CallRecipient(RemoteDiedHandler handler)73 explicit CallRecipient(RemoteDiedHandler handler) : handler_(handler) {}; 74 virtual ~CallRecipient() = default; 75 OnRemoteDied(const wptr<IRemoteObject> & remote)76 void OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote) override 77 { 78 if (handler_) { 79 handler_(remote); 80 } 81 }; 82 83 private: 84 RemoteDiedHandler handler_ = nullptr; 85 }; 86 } // namespace AbilityRuntime 87 } // namespace OHOS 88 #endif // OHOS_ABILITY_RUNTIME_LOCAL_CALL_RECORD_H 89