1 /* 2 * Copyright (c) 2022-2024 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_DM_IPC_SERVER_STUB_H 17 #define OHOS_DM_IPC_SERVER_STUB_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <tuple> 23 #include <unordered_set> 24 #include <vector> 25 26 #include "ipc_remote_broker.h" 27 #include "ipc_req.h" 28 #include "ipc_rsp.h" 29 #include "iremote_stub.h" 30 #include "system_ability.h" 31 32 #include "account_boot_listener.h" 33 #include "dm_single_instance.h" 34 35 namespace OHOS { 36 namespace DistributedHardware { 37 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 38 39 class AppDeathRecipient : public IRemoteObject::DeathRecipient { 40 public: 41 /** 42 * @tc.name: AppDeathRecipient::OnRemoteDied 43 * @tc.desc: OnRemoteDied function of the App DeathRecipient 44 * @tc.type: FUNC 45 */ 46 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 47 AppDeathRecipient() = default; 48 ~AppDeathRecipient() override = default; 49 }; 50 51 class IpcServerStub : public SystemAbility, public IRemoteStub<IpcRemoteBroker> { 52 DECLARE_SYSTEM_ABILITY(IpcServerStub); 53 DM_DECLARE_SINGLE_INSTANCE_BASE(IpcServerStub); 54 55 public: 56 /** 57 * @tc.name: IpcServerStub::OnStart 58 * @tc.desc: OnStart of the IpcServerStub 59 * @tc.type: FUNC 60 */ 61 void OnStart() override; 62 63 /** 64 * @tc.name: IpcServerStub::OnStop 65 * @tc.desc: OnStop of the IpcServerStub 66 * @tc.type: FUNC 67 */ 68 void OnStop() override; 69 70 /** 71 * @tc.name: IpcServerStub::OnRemoteRequest 72 * @tc.desc: On Remote Request of the IpcServerStub 73 * @tc.type: FUNC 74 */ 75 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 76 77 /** 78 * @tc.name: IpcServerStub::SendCmd 79 * @tc.desc: Send Cmd of the IpcServerStub 80 * @tc.type: FUNC 81 */ 82 int32_t SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp) override; 83 84 /** 85 * @tc.name: IpcServerStub::RegisterDeviceManagerListener 86 * @tc.desc: Register DeviceManager Listener of the IpcServerStub 87 * @tc.type: FUNC 88 */ 89 int32_t RegisterDeviceManagerListener(std::string &pkgName, sptr<IpcRemoteBroker> listener); 90 91 /** 92 * @tc.name: IpcServerStub::UnRegisterDeviceManagerListener 93 * @tc.desc: UnRegister DeviceManager Listener of the IpcServerStub 94 * @tc.type: FUNC 95 */ 96 int32_t UnRegisterDeviceManagerListener(std::string &pkgName); 97 98 /** 99 * @tc.name: IpcServerStub::QueryServiceState 100 * @tc.desc: Query Service State of the IpcServerStub 101 * @tc.type: FUNC 102 */ 103 ServiceRunningState QueryServiceState() const; 104 105 /** 106 * @tc.name: IpcServerStub::SendALL 107 * @tc.desc: SendALL of the IpcServerStub 108 * @tc.type: FUNC 109 */ 110 int32_t SendALL(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp); 111 112 /** 113 * @tc.name: IpcServerStub::GetAllPkgName 114 * @tc.desc: Get All PkgName from dmListener_ 115 * @tc.type: FUNC 116 */ 117 std::vector<std::string> GetAllPkgName(); 118 119 /** 120 * @tc.name: IpcServerStub::GetDmListener 121 * @tc.desc: Get DmListener of the IpcServerStub 122 * @tc.type: FUNC 123 */ 124 const sptr<IpcRemoteBroker> GetDmListener(std::string pkgName) const; 125 126 /** 127 * @tc.name: IpcServerStub::GetDmListenerPkgName 128 * @tc.desc: Get DmListener PkgName of the IpcServerStub 129 * @tc.type: FUNC 130 */ 131 const std::string GetDmListenerPkgName(const wptr<IRemoteObject> &remote) const; 132 133 /** 134 * @tc.name: IpcServerStub::Dump 135 * @tc.desc: Dump of the Device Manager Service 136 * @tc.type: FUNC 137 */ 138 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 139 140 /** 141 * @tc.name: IpcServerStub::OnAddSystemAbility 142 * @tc.desc: OnAddSystemAbility of the IpcServerStub 143 * @tc.type: FUNC 144 */ 145 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 146 147 /** 148 * @tc.name: IpcServerStub::OnRemoveSystemAbility 149 * @tc.desc: OnRemoveSystemAbility of the IpcServerStub 150 * @tc.type: FUNC 151 */ 152 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 153 154 private: 155 IpcServerStub(); 156 ~IpcServerStub() override = default; 157 bool Init(); 158 159 private: 160 bool registerToService_; 161 ServiceRunningState state_; 162 mutable std::mutex listenerLock_; 163 std::map<std::string, sptr<AppDeathRecipient>> appRecipient_; 164 std::map<std::string, sptr<IpcRemoteBroker>> dmListener_; 165 std::shared_ptr<AccountBootListener> accountBootListener_; 166 }; 167 } // namespace DistributedHardware 168 } // namespace OHOS 169 #endif // OHOS_DM_IPC_SERVER_STUB_H 170