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
27 namespace OHOS::AVSession {
GetInstance()28 AbilityConnectHelper& AbilityConnectHelper::GetInstance()
29 {
30 static AbilityConnectHelper abilityConnectHelper;
31 return abilityConnectHelper;
32 }
33
StartAbilityForegroundByCall(const std::string & bundleName,const std::string & abilityName)34 int32_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
StartAbilityByCall(const std::string & bundleName,const std::string & abilityName)85 int32_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
StartAVPlayback(AppExecFwk::InsightIntentExecuteParam & executeParam)134 int32_t AbilityConnectHelper::StartAVPlayback(AppExecFwk::InsightIntentExecuteParam &executeParam)
135 {
136 SLOGD("bundleName=%{public}s abilityName=%{public}s moduleName=%{public}s IntentName=%{public}s",
137 executeParam.bundleName_.c_str(), executeParam.abilityName_.c_str(),
138 executeParam.moduleName_.c_str(), executeParam.insightIntentName_.c_str());
139 sptr<IRemoteObject> remote = GetSystemAbility();
140 if (remote == nullptr) {
141 return ERR_SERVICE_NOT_EXIST;
142 }
143 auto ret = AAFwk::AbilityManagerClient::GetInstance()->ExecuteIntent((uint64_t) AVSESSION_SERVICE_ID,
144 remote, executeParam);
145 if (ret != AVSESSION_SUCCESS) {
146 SLOGE("ExecuteIntent insightIntent=%{public}s fail", executeParam.insightIntentName_.c_str());
147 return AVSESSION_ERROR;
148 }
149 return AVSESSION_SUCCESS;
150 }
151
GetSystemAbility()152 sptr<IRemoteObject> AbilityConnectHelper::GetSystemAbility()
153 {
154 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
155 if (systemManager == nullptr) {
156 SLOGE("Fail to get registry");
157 return nullptr;
158 }
159 sptr<IRemoteObject> remote = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
160 if (remote == nullptr) {
161 SLOGE("Fail to connect ability manager service");
162 return nullptr;
163 }
164 SLOGI("Connect ability manager service success");
165 return remote;
166 }
167
AbilityConnectionStub()168 AbilityConnectionStub::AbilityConnectionStub()
169 {}
170
~AbilityConnectionStub()171 AbilityConnectionStub::~AbilityConnectionStub()
172 {}
173
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)174 int AbilityConnectionStub::OnRemoteRequest(
175 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
176 {
177 auto descriptor = AbilityConnectionStub::GetDescriptor();
178 auto remoteDescriptor = data.ReadInterfaceToken();
179 if (descriptor != remoteDescriptor) {
180 SLOGE("Local descriptor is not equal to remote");
181 return AVSESSION_ERROR;
182 }
183
184 auto element = data.ReadParcelable<AppExecFwk::ElementName>();
185 if (element == nullptr) {
186 SLOGE("callback stub receive element is nullptr");
187 return AVSESSION_ERROR;
188 }
189 if (code == AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE) {
190 auto remoteObject = data.ReadRemoteObject();
191 if (remoteObject == nullptr) {
192 SLOGE("callback stub receive remoteObject is nullptr");
193 delete element;
194 return AVSESSION_ERROR;
195 }
196 auto resultCode = data.ReadInt32();
197 OnAbilityConnectDone(*element, remoteObject, resultCode);
198 delete element;
199 return ERR_NONE;
200 }
201 if (code == AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE) {
202 auto resultCode = data.ReadInt32();
203 OnAbilityDisconnectDone(*element, resultCode);
204 delete element;
205 return ERR_NONE;
206 }
207 delete element;
208 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
209 }
210
~AbilityConnectCallback()211 AbilityConnectCallback::~AbilityConnectCallback()
212 {}
213
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)214 void AbilityConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
215 const sptr<IRemoteObject>& __attribute__((unused)) remoteObject, int resultCode)
216 {
217 SLOGI("OnAbilityConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
218 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
219 }
220
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)221 void AbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode)
222 {
223 SLOGI("OnAbilityDisConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
224 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
225 }
226 } // namespace OHOS::AVSession