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 #include "local_ability_manager_proxy.h"
17
18 #include "ipc_types.h"
19 #include "iremote_object.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 #include "nlohmann/json.hpp"
23 #include "refbase.h"
24
25 using namespace std;
26 using namespace OHOS::HiviewDFX;
27
28 namespace OHOS {
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD001800
31 #undef LOG_TAG
32 #define LOG_TAG "SA"
StartAbility(int32_t systemAbilityId,const std::string & eventStr)33 bool LocalAbilityManagerProxy::StartAbility(int32_t systemAbilityId, const std::string& eventStr)
34 {
35 HILOG_INFO(LOG_CORE, "StartAbility proxy SA:%{public}d", systemAbilityId);
36 if (systemAbilityId <= 0) {
37 HILOG_WARN(LOG_CORE, "StartAbility systemAbilityId invalid.");
38 return false;
39 }
40
41 if (eventStr.empty()) {
42 HILOG_WARN(LOG_CORE, "StartAbility eventStr invalid.");
43 return false;
44 }
45
46 sptr<IRemoteObject> iro = Remote();
47 if (iro == nullptr) {
48 HILOG_ERROR(LOG_CORE, "StartAbility Remote return null");
49 return false;
50 }
51
52 MessageParcel data;
53 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
54 HILOG_WARN(LOG_CORE, "StartAbility interface token check failed");
55 return false;
56 }
57 bool ret = data.WriteInt32(systemAbilityId);
58 if (!ret) {
59 HILOG_WARN(LOG_CORE, "StartAbility write systemAbilityId failed!");
60 return false;
61 }
62 ret = data.WriteString(eventStr);
63 if (!ret) {
64 HILOG_WARN(LOG_CORE, "StartAbility write eventStr failed!");
65 return false;
66 }
67 MessageParcel reply;
68 MessageOption option(MessageOption::TF_ASYNC);
69 int32_t status = iro->SendRequest(
70 static_cast<uint32_t>(SafwkInterfaceCode::START_ABILITY_TRANSACTION), data, reply, option);
71 if (status != NO_ERROR) {
72 HILOG_ERROR(LOG_CORE, "StartAbility SendRequest failed, return value : %{public}d", status);
73 return false;
74 }
75 HILOG_INFO(LOG_CORE, "StartAbility SendRequest suc, SA:%{public}d, pid:%{public}d", systemAbilityId, getpid());
76 return true;
77 }
78
StopAbility(int32_t systemAbilityId,const std::string & eventStr)79 bool LocalAbilityManagerProxy::StopAbility(int32_t systemAbilityId, const std::string& eventStr)
80 {
81 if (systemAbilityId <= 0) {
82 HILOG_WARN(LOG_CORE, "StopAbility systemAbilityId invalid.");
83 return false;
84 }
85
86 if (eventStr.empty()) {
87 HILOG_WARN(LOG_CORE, "StartAbility eventStr invalid.");
88 return false;
89 }
90
91 sptr<IRemoteObject> iro = Remote();
92 if (iro == nullptr) {
93 HILOG_ERROR(LOG_CORE, "StopAbility Remote return null");
94 return false;
95 }
96
97 MessageParcel data;
98 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
99 HILOG_WARN(LOG_CORE, "StopAbility interface token check failed");
100 return false;
101 }
102 bool ret = data.WriteInt32(systemAbilityId);
103 if (!ret) {
104 HILOG_WARN(LOG_CORE, "StopAbility write systemAbilityId failed!");
105 return false;
106 }
107 ret = data.WriteString(eventStr);
108 if (!ret) {
109 HILOG_WARN(LOG_CORE, "StopAbility write eventStr failed!");
110 return false;
111 }
112 MessageParcel reply;
113 MessageOption option(MessageOption::TF_ASYNC);
114 int32_t status = iro->SendRequest(
115 static_cast<uint32_t>(SafwkInterfaceCode::STOP_ABILITY_TRANSACTION), data, reply, option);
116 if (status != NO_ERROR) {
117 HILOG_ERROR(LOG_CORE, "StopAbility SendRequest failed, return value : %{public}d", status);
118 return false;
119 }
120 return true;
121 }
122
ActiveAbility(int32_t systemAbilityId,const nlohmann::json & activeReason)123 bool LocalAbilityManagerProxy::ActiveAbility(int32_t systemAbilityId,
124 const nlohmann::json& activeReason)
125 {
126 if (systemAbilityId <= 0) {
127 HILOG_WARN(LOG_CORE, "ActiveAbility systemAbilityId invalid.");
128 return false;
129 }
130
131 sptr<IRemoteObject> iro = Remote();
132 if (iro == nullptr) {
133 HILOG_ERROR(LOG_CORE, "ActiveAbility Remote return null");
134 return false;
135 }
136
137 MessageParcel data;
138 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
139 HILOG_WARN(LOG_CORE, "ActiveAbility interface token check failed");
140 return false;
141 }
142 if (!data.WriteInt32(systemAbilityId)) {
143 HILOG_WARN(LOG_CORE, "ActiveAbility write systemAbilityId failed!");
144 return false;
145 }
146 if (!data.WriteString(activeReason.dump())) {
147 HILOG_WARN(LOG_CORE, "ActiveAbility write activeReason failed!");
148 return false;
149 }
150
151 MessageParcel reply;
152 MessageOption option;
153 int32_t status = iro->SendRequest(
154 static_cast<uint32_t>(SafwkInterfaceCode::ACTIVE_ABILITY_TRANSACTION), data, reply, option);
155 if (status != NO_ERROR) {
156 HILOG_ERROR(LOG_CORE, "ActiveAbility SendRequest failed, return value : %{public}d", status);
157 return false;
158 }
159 bool result = false;
160 if (!reply.ReadBool(result)) {
161 HILOG_WARN(LOG_CORE, "ActiveAbility read result failed!");
162 return false;
163 }
164 return result;
165 }
166
IdleAbility(int32_t systemAbilityId,const nlohmann::json & idleReason,int32_t & delayTime)167 bool LocalAbilityManagerProxy::IdleAbility(int32_t systemAbilityId,
168 const nlohmann::json& idleReason, int32_t& delayTime)
169 {
170 if (systemAbilityId <= 0) {
171 HILOG_WARN(LOG_CORE, "IdleAbility systemAbilityId invalid.");
172 return false;
173 }
174
175 sptr<IRemoteObject> iro = Remote();
176 if (iro == nullptr) {
177 HILOG_ERROR(LOG_CORE, "IdleAbility Remote return null");
178 return false;
179 }
180
181 MessageParcel data;
182 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
183 HILOG_WARN(LOG_CORE, "IdleAbility interface token check failed");
184 return false;
185 }
186 if (!data.WriteInt32(systemAbilityId)) {
187 HILOG_WARN(LOG_CORE, "IdleAbility write systemAbilityId failed!");
188 return false;
189 }
190 if (!data.WriteString(idleReason.dump())) {
191 HILOG_WARN(LOG_CORE, "IdleAbility write ildeReason failed!");
192 return false;
193 }
194
195 MessageParcel reply;
196 MessageOption option;
197 int32_t status = iro->SendRequest(
198 static_cast<uint32_t>(SafwkInterfaceCode::IDLE_ABILITY_TRANSACTION), data, reply, option);
199 if (status != NO_ERROR) {
200 HILOG_ERROR(LOG_CORE, "IdleAbility SendRequest failed, return value : %{public}d", status);
201 return false;
202 }
203 bool result = false;
204 if (!reply.ReadBool(result)) {
205 HILOG_WARN(LOG_CORE, "IdleAbility read result failed!");
206 return false;
207 }
208 if (!reply.ReadInt32(delayTime)) {
209 HILOG_WARN(LOG_CORE, "IdleAbility read delayTime failed!");
210 return false;
211 }
212 return result;
213 }
214
SendStrategyToSA(int32_t type,int32_t systemAbilityId,int32_t level,std::string & action)215 bool LocalAbilityManagerProxy::SendStrategyToSA(int32_t type, int32_t systemAbilityId,
216 int32_t level, std::string& action)
217 {
218 if (systemAbilityId <= 0) {
219 HILOG_WARN(LOG_CORE, "SendStrategy systemAbilityId invalid.");
220 return false;
221 }
222
223 sptr<IRemoteObject> iro = Remote();
224 if (iro == nullptr) {
225 HILOG_ERROR(LOG_CORE, "SendStrategy Remote return null");
226 return false;
227 }
228
229 MessageParcel data;
230 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
231 HILOG_WARN(LOG_CORE, "SendStrategy interface token check failed");
232 return false;
233 }
234 bool ret = data.WriteInt32(type);
235 if (!ret) {
236 HILOG_WARN(LOG_CORE, "SendStrategy write type failed!");
237 return false;
238 }
239 ret = data.WriteInt32(systemAbilityId);
240 if (!ret) {
241 HILOG_WARN(LOG_CORE, "SendStrategy write systemAbilityId failed!");
242 return false;
243 }
244 ret = data.WriteInt32(level);
245 if (!ret) {
246 HILOG_WARN(LOG_CORE, "SendStrategy write level failed!");
247 return false;
248 }
249 ret = data.WriteString(action);
250 if (!ret) {
251 HILOG_WARN(LOG_CORE, "SendStrategy write action failed!");
252 return false;
253 }
254
255 MessageParcel reply;
256 MessageOption option(MessageOption::TF_ASYNC);
257 int32_t status = iro->SendRequest(
258 static_cast<uint32_t>(SafwkInterfaceCode::SEND_STRATEGY_TO_SA_TRANSACTION), data, reply, option);
259 if (status != NO_ERROR) {
260 HILOG_ERROR(LOG_CORE, "SendStrategy SendRequest failed, return value : %{public}d", status);
261 return false;
262 }
263 return true;
264 }
265
IpcStatCmdProc(int32_t fd,int32_t cmd)266 bool LocalAbilityManagerProxy::IpcStatCmdProc(int32_t fd, int32_t cmd)
267 {
268 sptr<IRemoteObject> iro = Remote();
269 if (iro == nullptr) {
270 HILOG_ERROR(LOG_CORE, "IpcStatCmdProc Remote null");
271 return false;
272 }
273
274 MessageParcel data;
275 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
276 HILOG_WARN(LOG_CORE, "IpcStatCmdProc interface token check failed");
277 return false;
278 }
279
280 if (!data.WriteFileDescriptor(fd)) {
281 HILOG_WARN(LOG_CORE, "IpcStatCmdProc write fd failed");
282 return false;
283 }
284
285 if (!data.WriteInt32(cmd)) {
286 HILOG_WARN(LOG_CORE, "IpcStatCmdProc write cmd faild");
287 return false;
288 }
289
290 MessageParcel reply;
291 MessageOption option;
292 int32_t status = iro->SendRequest(
293 static_cast<uint32_t>(SafwkInterfaceCode::IPC_STAT_CMD_TRANSACTION), data, reply, option);
294 if (status != NO_ERROR) {
295 HILOG_ERROR(LOG_CORE, "IpcStatCmdProc SendRequest failed, return value : %{public}d", status);
296 return false;
297 }
298
299 bool result = false;
300 if (!reply.ReadBool(result)) {
301 HILOG_WARN(LOG_CORE, "IpcStatCmdProc read bool faild");
302 return false;
303 }
304
305 return result;
306 }
307
FfrtStatCmdProc(int32_t fd,int32_t cmd)308 bool LocalAbilityManagerProxy::FfrtStatCmdProc(int32_t fd, int32_t cmd)
309 {
310 sptr<IRemoteObject> iro = Remote();
311 if (iro == nullptr) {
312 HILOG_ERROR(LOG_CORE, "FfrtStatCmdProc Remote null");
313 return false;
314 }
315
316 MessageParcel data;
317 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
318 HILOG_WARN(LOG_CORE, "FfrtStatCmdProc interface token check failed");
319 return false;
320 }
321
322 if (!data.WriteFileDescriptor(fd)) {
323 HILOG_WARN(LOG_CORE, "FfrtStatCmdProc write fd failed");
324 return false;
325 }
326
327 if (!data.WriteInt32(cmd)) {
328 HILOG_WARN(LOG_CORE, "FfrtStatCmdProc write cmd faild");
329 return false;
330 }
331
332 MessageParcel reply;
333 MessageOption option;
334 int32_t status = iro->SendRequest(
335 static_cast<uint32_t>(SafwkInterfaceCode::FFRT_STAT_CMD_TRANSACTION), data, reply, option);
336 if (status != NO_ERROR) {
337 HILOG_ERROR(LOG_CORE, "FfrtStatCmdProc SendRequest failed, return value : %{public}d", status);
338 return false;
339 }
340
341 bool result = false;
342 if (!reply.ReadBool(result)) {
343 HILOG_WARN(LOG_CORE, "FfrtStatCmdProc read bool faild");
344 return false;
345 }
346
347 return result;
348 }
349
FfrtDumperProc(std::string & ffrtDumperInfo)350 bool LocalAbilityManagerProxy::FfrtDumperProc(std::string& ffrtDumperInfo)
351 {
352 sptr<IRemoteObject> iro = Remote();
353 if (iro == nullptr) {
354 HILOG_ERROR(LOG_CORE, "FfrtDumperProc Remote null");
355 return false;
356 }
357
358 MessageParcel data;
359 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
360 HILOG_WARN(LOG_CORE, "FfrtDumperProc write interface token failed");
361 return false;
362 }
363
364 MessageParcel reply;
365 MessageOption option;
366 int32_t status = iro->SendRequest(
367 static_cast<uint32_t>(SafwkInterfaceCode::FFRT_DUMPER_TRANSACTION), data, reply, option);
368 if (status != NO_ERROR) {
369 HILOG_ERROR(LOG_CORE, "FfrtDumperProc SendRequest failed, return value : %{public}d", status);
370 return false;
371 }
372 if (!reply.ReadString(ffrtDumperInfo)) {
373 HILOG_WARN(LOG_CORE, "FfrtDumperProc read ffrtDumperInfo failed!");
374 return false;
375 }
376 return true;
377 }
378
SystemAbilityExtProc(const std::string & extension,int32_t said,SystemAbilityExtensionPara * callback,bool isAsync)379 int32_t LocalAbilityManagerProxy::SystemAbilityExtProc(const std::string& extension, int32_t said,
380 SystemAbilityExtensionPara* callback, bool isAsync)
381 {
382 if (said <= 0) {
383 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc systemAbilityId invalid.");
384 return INVALID_DATA;
385 }
386
387 if (extension.empty()) {
388 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc extension invalid.");
389 return INVALID_DATA;
390 }
391
392 sptr<IRemoteObject> iro = Remote();
393 if (iro == nullptr) {
394 HILOG_ERROR(LOG_CORE, "SystemAbilityExtProc Remote return null");
395 return OBJECT_NULL;
396 }
397
398 MessageParcel data;
399 if (!PrepareData(data, said, extension)) {
400 return INVALID_DATA;
401 }
402
403 if (callback != nullptr && !callback->InputParaSet(data)) {
404 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc InputParaSet failed!");
405 return INVALID_DATA;
406 }
407
408 MessageParcel reply;
409 MessageOption option;
410 if (isAsync) {
411 option.SetFlags(MessageOption::TF_ASYNC);
412 }
413
414 int32_t status = iro->SendRequest(
415 static_cast<uint32_t>(SafwkInterfaceCode::SYSTEM_ABILITY_EXT_TRANSACTION), data, reply, option);
416 if (status != NO_ERROR) {
417 HILOG_ERROR(LOG_CORE, "SystemAbilityExtProc SendRequest failed, return value : %{public}d", status);
418 return status;
419 }
420
421 if ((!isAsync) && callback != nullptr && !callback->OutputParaGet(reply)) {
422 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc OutputParaGet failed!");
423 return INVALID_DATA;
424 }
425 return NO_ERROR;
426 }
427
PrepareData(MessageParcel & data,int32_t said,const std::string & extension)428 bool LocalAbilityManagerProxy::PrepareData(MessageParcel& data, int32_t said, const std::string& extension)
429 {
430 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
431 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc interface token check failed");
432 return false;
433 }
434
435 if (!data.WriteInt32(said)) {
436 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc write said failed!");
437 return false;
438 }
439
440 if (!data.WriteString(extension)) {
441 HILOG_WARN(LOG_CORE, "SystemAbilityExtProc write extension failed!");
442 return false;
443 }
444 return true;
445 }
446
ServiceControlCmd(int32_t fd,int32_t systemAbilityId,const std::vector<std::u16string> & args)447 int32_t LocalAbilityManagerProxy::ServiceControlCmd(int32_t fd, int32_t systemAbilityId,
448 const std::vector<std::u16string>& args)
449 {
450 if (systemAbilityId <= 0) {
451 HILOG_WARN(LOG_CORE, "ServiceControlCmd systemAbilityId invalid.");
452 return INVALID_DATA;
453 }
454
455 sptr<IRemoteObject> iro = Remote();
456 if (iro == nullptr) {
457 HILOG_ERROR(LOG_CORE, "ServiceControlCmd remote return null");
458 return OBJECT_NULL;
459 }
460
461 MessageParcel data;
462 if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
463 HILOG_WARN(LOG_CORE, "ServiceControlCmd interface token check failed");
464 return INVALID_DATA;
465 }
466 if (!data.WriteInt32(systemAbilityId)) {
467 HILOG_WARN(LOG_CORE, "ServiceControlCmd write systemAbilityId failed");
468 return INVALID_DATA;
469 }
470 if (!data.WriteFileDescriptor(fd)) {
471 HILOG_WARN(LOG_CORE, "ServiceControlCmd write fd failed");
472 return INVALID_DATA;
473 }
474 if (!data.WriteString16Vector(args)) {
475 HILOG_WARN(LOG_CORE, "ServiceControlCmd write args failed");
476 return INVALID_DATA;
477 }
478
479 MessageParcel reply;
480 MessageOption option;
481 int32_t status = iro->SendRequest(
482 static_cast<uint32_t>(SafwkInterfaceCode::SERVICE_CONTROL_CMD_TRANSACTION), data, reply, option);
483 if (status != NO_ERROR) {
484 HILOG_ERROR(LOG_CORE, "ServiceControlCmd SendRequest failed, return value : %{public}d", status);
485 }
486 return status;
487 }
488 }
489