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 OHOS_IPC_IPC_OBJECT_PROXY_H 17 #define OHOS_IPC_IPC_OBJECT_PROXY_H 18 19 #include <mutex> 20 #include <vector> 21 22 #include "iremote_object.h" 23 24 namespace OHOS { 25 class IPCObjectProxy : public IRemoteObject { 26 public: 27 explicit IPCObjectProxy(int handle, std::u16string descriptor = std::u16string(), 28 int proto = IRemoteObject::IF_PROT_DEFAULT); 29 ~IPCObjectProxy(); 30 31 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &optionoption) override; 32 IsProxyObject()33 bool IsProxyObject() const override 34 { 35 return true; 36 }; 37 38 bool IsObjectDead() const; 39 40 int32_t GetObjectRefCount() override; 41 42 int Dump(int fd, const std::vector<std::u16string> &args) override; 43 44 void OnFirstStrongRef(const void *objectId) override; 45 46 void OnLastStrongRef(const void *objectId) override; 47 48 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override; 49 50 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override; 51 52 void SendObituary(); 53 IsSubscribeDeathNotice()54 bool IsSubscribeDeathNotice() const 55 { 56 if (recipients_.empty()) { 57 return false; 58 } 59 return true; 60 }; 61 GetHandle()62 uint32_t GetHandle() const 63 { 64 return handle_; 65 }; 66 67 int InvokeListenThread(MessageParcel &data, MessageParcel &reply); 68 int32_t NoticeServiceDie(); 69 std::string GetPidAndUidInfo(); 70 71 std::string GetDataBusName(); 72 std::string TransDataBusName(uint32_t uid, uint32_t pid); 73 int GetProto() const; 74 void WaitForInit(); 75 std::u16string GetInterfaceDescriptor(); 76 77 private: 78 void MarkObjectDied(); 79 int SendLocalRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &optionoption); 80 int SendRequestInner(bool isLocal, uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); 81 82 #ifndef CONFIG_IPC_SINGLE 83 void SetProto(int proto); 84 85 int UpdateProto(); 86 87 void ReleaseProto(); 88 89 void IncRefToRemote(); 90 91 int GetSessionFromDBinderService(); 92 93 bool AddDbinderDeathRecipient(); 94 95 bool RemoveDbinderDeathRecipient(); 96 97 void ReleaseDatabusProto(); 98 99 void ReleaseBinderProto(); 100 101 bool UpdateDatabusClientSession(int handle, MessageParcel &reply); 102 103 bool CheckHaveSession(uint32_t &type); 104 #endif 105 106 private: 107 std::mutex initMutex_; 108 std::recursive_mutex mutex_; 109 110 std::vector<sptr<DeathRecipient>> recipients_; 111 const uint32_t handle_; 112 int proto_; 113 bool isFinishInit_; 114 bool isRemoteDead_; 115 std::u16string remoteDescriptor_; 116 }; 117 } // namespace OHOS 118 #endif // OHOS_IPC_IPC_OBJECT_PROXY_H 119