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 
FfrtDumperProc(std::string & ffrtDumperInfo)308 bool LocalAbilityManagerProxy::FfrtDumperProc(std::string& ffrtDumperInfo)
309 {
310     sptr<IRemoteObject> iro = Remote();
311     if (iro == nullptr) {
312         HILOG_ERROR(LOG_CORE, "FfrtDumperProc Remote null");
313         return false;
314     }
315 
316     MessageParcel data;
317     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
318         HILOG_WARN(LOG_CORE, "FfrtDumperProc write interface token failed");
319         return false;
320     }
321 
322     MessageParcel reply;
323     MessageOption option;
324     int32_t status = iro->SendRequest(
325         static_cast<uint32_t>(SafwkInterfaceCode::FFRT_DUMPER_TRANSACTION), data, reply, option);
326     if (status != NO_ERROR) {
327         HILOG_ERROR(LOG_CORE, "FfrtDumperProc SendRequest failed, return value : %{public}d", status);
328         return false;
329     }
330     if (!reply.ReadString(ffrtDumperInfo)) {
331         HILOG_WARN(LOG_CORE, "FfrtDumperProc read ffrtDumperInfo failed!");
332         return false;
333     }
334     return true;
335 }
336 
SystemAbilityExtProc(const std::string & extension,int32_t said,SystemAbilityExtensionPara * callback,bool isAsync)337 int32_t LocalAbilityManagerProxy::SystemAbilityExtProc(const std::string& extension, int32_t said,
338     SystemAbilityExtensionPara* callback, bool isAsync)
339 {
340     if (said <= 0) {
341         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc systemAbilityId invalid.");
342         return INVALID_DATA;
343     }
344 
345     if (extension.empty()) {
346         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc extension invalid.");
347         return INVALID_DATA;
348     }
349 
350     sptr<IRemoteObject> iro = Remote();
351     if (iro == nullptr) {
352         HILOG_ERROR(LOG_CORE, "SystemAbilityExtProc Remote return null");
353         return OBJECT_NULL;
354     }
355 
356     MessageParcel data;
357     if (!PrepareData(data, said, extension)) {
358         return INVALID_DATA;
359     }
360 
361     if (callback != nullptr && !callback->InputParaSet(data)) {
362         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc InputParaSet failed!");
363         return INVALID_DATA;
364     }
365 
366     MessageParcel reply;
367     MessageOption option;
368     if (isAsync) {
369         option.SetFlags(MessageOption::TF_ASYNC);
370     }
371 
372     int32_t status = iro->SendRequest(
373         static_cast<uint32_t>(SafwkInterfaceCode::SYSTEM_ABILITY_EXT_TRANSACTION), data, reply, option);
374     if (status != NO_ERROR) {
375         HILOG_ERROR(LOG_CORE, "SystemAbilityExtProc SendRequest failed, return value : %{public}d", status);
376         return status;
377     }
378 
379     if ((!isAsync) && callback != nullptr && !callback->OutputParaGet(reply)) {
380         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc OutputParaGet failed!");
381         return INVALID_DATA;
382     }
383     return NO_ERROR;
384 }
385 
PrepareData(MessageParcel & data,int32_t said,const std::string & extension)386 bool LocalAbilityManagerProxy::PrepareData(MessageParcel& data, int32_t said, const std::string& extension)
387 {
388     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
389         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc interface token check failed");
390         return false;
391     }
392 
393     if (!data.WriteInt32(said)) {
394         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc write said failed!");
395         return false;
396     }
397 
398     if (!data.WriteString(extension)) {
399         HILOG_WARN(LOG_CORE, "SystemAbilityExtProc write extension failed!");
400         return false;
401     }
402     return true;
403 }
404 }
405