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
GetSystemAbility()134 sptr<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
AbilityConnectionStub()150 AbilityConnectionStub::AbilityConnectionStub()
151 {}
152
~AbilityConnectionStub()153 AbilityConnectionStub::~AbilityConnectionStub()
154 {}
155
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)156 int 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 return AVSESSION_ERROR;
177 }
178 auto resultCode = data.ReadInt32();
179 OnAbilityConnectDone(*element, remoteObject, resultCode);
180 delete element;
181 return ERR_NONE;
182 }
183 if (code == AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE) {
184 auto resultCode = data.ReadInt32();
185 OnAbilityDisconnectDone(*element, resultCode);
186 delete element;
187 return ERR_NONE;
188 }
189 delete element;
190 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
191 }
192
~AbilityConnectCallback()193 AbilityConnectCallback::~AbilityConnectCallback()
194 {}
195
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)196 void AbilityConnectCallback::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
197 const sptr<IRemoteObject>& __attribute__((unused)) remoteObject, int resultCode)
198 {
199 SLOGI("OnAbilityConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
200 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
201 }
202
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)203 void AbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element, int resultCode)
204 {
205 SLOGI("OnAbilityDisConnectDone callback, retcode:%{public}d, bundlename:%{public}s, abilityname:%{public}s",
206 resultCode, element.GetBundleName().c_str(), element.GetAbilityName().c_str());
207 }
208 } // namespace OHOS::AVSession