1/* 2 * Copyright (c) 2022 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#include "ability_connect_helper.h" 17 18#include "ability_manager_interface.h" 19#include "ability_manager_client.h" 20#include "avsession_errors.h" 21#include "avsession_log.h" 22#include "iservice_registry.h" 23#include "ipc_skeleton.h" 24#include "message_parcel.h" 25#include "system_ability_definition.h" 26 27namespace OHOS::AVSession { 28AbilityConnectHelper& AbilityConnectHelper::GetInstance() 29{ 30 static AbilityConnectHelper abilityConnectHelper; 31 return abilityConnectHelper; 32} 33 34int32_t AbilityConnectHelper::StartAbilityForegroundByCall(const std::string& bundleName, 35 const std::string& abilityName) 36{ 37 SLOGI("StartAbilityForegroundByCall bundleName=%{public}s abilityName=%{public}s", 38 bundleName.c_str(), abilityName.c_str()); 39 MessageParcel data; 40 MessageParcel reply; 41 MessageOption option; 42 if (!data.WriteInterfaceToken(ABILITY_MANAGER_INTERFACE_TOKEN)) { 43 SLOGE("write interface token failed"); 44 return ERR_MARSHALLING; 45 } 46 AAFwk::Want want; 47 AppExecFwk::ElementName element("", bundleName, abilityName); 48 want.SetElement(element); 49 want.SetParam("ohos.aafwk.param.callAbilityToForeground", true); 50 51 if (!data.WriteParcelable(&want)) { 52 SLOGE("want write failed"); 53 return ERR_INVALID_PARAM; 54 } 55 sptr<AAFwk::IAbilityConnection> connect = new(std::nothrow) AbilityConnectCallback(); 56 if (connect == nullptr) { 57 SLOGE("connect is nullptr"); 58 return ERR_NO_MEMORY; 59 } 60 if (!data.WriteRemoteObject(connect->AsObject())) { 61 SLOGE("resolve write failed"); 62 return ERR_MARSHALLING; 63 } 64 if (!data.WriteBool(false)) { 65 SLOGE("Failed to write flag"); 66 return ERR_MARSHALLING; 67 } 68 if (!data.WriteInt32(-1)) { // -1 is default connect id of ability manager 69 SLOGE("Failed to write connect id"); 70 return ERR_MARSHALLING; 71 } 72 73 sptr<IRemoteObject> remote = GetSystemAbility(); 74 if (remote == nullptr) { 75 return ERR_SERVICE_NOT_EXIST; 76 } 77 if (remote->SendRequest(static_cast<uint32_t>(AAFwk::AbilityManagerInterfaceCode::START_CALL_ABILITY), 78 data, reply, option) != 0) { 79 SLOGE("Send request error"); 80 return ERR_IPC_SEND_REQUEST; 81 } 82 return reply.ReadInt32() == ERR_OK ? AVSESSION_SUCCESS : ERR_ABILITY_NOT_AVAILABLE; 83} 84 85int32_t AbilityConnectHelper::StartAbilityByCall(const std::string& bundleName, const std::string& abilityName) 86{ 87 SLOGI("bundleName=%{public}s abilityName=%{public}s", bundleName.c_str(), abilityName.c_str()); 88 MessageParcel data; 89 MessageParcel reply; 90 MessageOption option; 91 if (!data.WriteInterfaceToken(ABILITY_MANAGER_INTERFACE_TOKEN)) { 92 SLOGE("write interface token failed"); 93 return ERR_MARSHALLING; 94 } 95 96 AAFwk::Want want; 97 AppExecFwk::ElementName element("", bundleName, abilityName); 98 want.SetElement(element); 99 if (!data.WriteParcelable(&want)) { 100 SLOGE("want write failed"); 101 return ERR_INVALID_PARAM; 102 } 103 104 sptr<AAFwk::IAbilityConnection> connect = new(std::nothrow) AbilityConnectCallback(); 105 if (connect == nullptr) { 106 SLOGE("connect is nullptr"); 107 return ERR_NO_MEMORY; 108 } 109 if (!data.WriteRemoteObject(connect->AsObject())) { 110 SLOGE("resolve write failed"); 111 return ERR_MARSHALLING; 112 } 113 if (!data.WriteBool(false)) { 114 SLOGE("Failed to write flag"); 115 return ERR_MARSHALLING; 116 } 117 if (!data.WriteInt32(-1)) { // -1 is default connect id of ability manager 118 SLOGE("Failed to write connect id"); 119 return ERR_MARSHALLING; 120 } 121 122 sptr<IRemoteObject> remote = GetSystemAbility(); 123 if (remote == nullptr) { 124 return ERR_SERVICE_NOT_EXIST; 125 } 126 if (remote->SendRequest(static_cast<uint32_t>(AAFwk::AbilityManagerInterfaceCode::START_CALL_ABILITY), 127 data, reply, option) != 0) { 128 SLOGE("Send request error"); 129 return ERR_IPC_SEND_REQUEST; 130 } 131 return reply.ReadInt32() == ERR_OK ? AVSESSION_SUCCESS : ERR_ABILITY_NOT_AVAILABLE; 132} 133 134sptr<IRemoteObject> AbilityConnectHelper::GetSystemAbility() 135{ 136 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 137 if (systemManager == nullptr) { 138 SLOGE("Fail to get registry"); 139 return nullptr; 140 } 141 sptr<IRemoteObject> remote = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); 142 if (remote == nullptr) { 143 SLOGE("Fail to connect ability manager service"); 144 return nullptr; 145 } 146 SLOGI("Connect ability manager service success"); 147 return remote; 148} 149 150AbilityConnectionStub::AbilityConnectionStub() 151{} 152 153AbilityConnectionStub::~AbilityConnectionStub() 154{} 155 156int AbilityConnectionStub::OnRemoteRequest( 157 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) 158{ 159 auto descriptor = AbilityConnectionStub::GetDescriptor(); 160 auto remoteDescriptor = data.ReadInterfaceToken(); 161 if (descriptor != remoteDescriptor) { 162 SLOGE("Local descriptor is not equal to remote"); 163 return AVSESSION_ERROR; 164 } 165 166 auto element = data.ReadParcelable<AppExecFwk::ElementName>(); 167 if (element == nullptr) { 168 SLOGE("callback stub receive element is nullptr"); 169 return AVSESSION_ERROR; 170 } 171 if (code == AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE) { 172 auto remoteObject = data.ReadRemoteObject(); 173 if (remoteObject == nullptr) { 174 SLOGE("callback stub receive remoteObject is nullptr"); 175 delete element; 176 element = nullptr; 177 return AVSESSION_ERROR; 178 } 179 auto resultCode = data.ReadInt32(); 180 OnAbilityConnectDone(*element, remoteObject, resultCode); 181 delete element; 182 element = nullptr; 183 return ERR_NONE; 184 } 185 if (code == AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE) { 186 auto resultCode = data.ReadInt32(); 187 OnAbilityDisconnectDone(*element, resultCode); 188 delete element; 189 element = nullptr; 190 return ERR_NONE; 191 } 192 delete element; 193 element = nullptr; 194 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 195} 196 197AbilityConnectCallback::~AbilityConnectCallback() 198{} 199 200void AbilityConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName& element, 201 const sptr<IRemoteObject>& __attribute__((unused)) remoteObject, int resultCode) 202{ 203 SLOGI("OnAbilityConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s", 204 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str()); 205} 206 207void AbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode) 208{ 209 SLOGI("OnAbilityDisConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s", 210 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str()); 211} 212} // namespace OHOS::AVSession