1 /*
2  * Copyright (c) 2023-2024 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 "child_scheduler_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
ChildSchedulerProxy(const sptr<IRemoteObject> & impl)23 ChildSchedulerProxy::ChildSchedulerProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IChildScheduler>(impl)
24 {}
25 
WriteInterfaceToken(MessageParcel & data)26 bool ChildSchedulerProxy::WriteInterfaceToken(MessageParcel &data)
27 {
28     if (!data.WriteInterfaceToken(ChildSchedulerProxy::GetDescriptor())) {
29         TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed");
30         return false;
31     }
32     return true;
33 }
34 
ScheduleLoadChild()35 bool ChildSchedulerProxy::ScheduleLoadChild()
36 {
37     TAG_LOGD(AAFwkTag::APPMGR, "ScheduleLoadChild start");
38     MessageParcel data;
39     MessageParcel reply;
40     MessageOption option(MessageOption::TF_ASYNC);
41     if (!WriteInterfaceToken(data)) {
42         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
43         return false;
44     }
45 
46     sptr<IRemoteObject> remote = Remote();
47     if (remote == nullptr) {
48         TAG_LOGE(AAFwkTag::APPMGR, "Remote() is NULL");
49         return false;
50     }
51     int32_t ret = remote->SendRequest(
52         static_cast<uint32_t>(IChildScheduler::Message::SCHEDULE_LOAD_JS), data, reply, option);
53     if (ret != NO_ERROR) {
54         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
55         return false;
56     }
57     TAG_LOGD(AAFwkTag::APPMGR, "ScheduleLoadChild end");
58     return true;
59 }
60 
ScheduleExitProcessSafely()61 bool ChildSchedulerProxy::ScheduleExitProcessSafely()
62 {
63     TAG_LOGD(AAFwkTag::APPMGR, "ScheduleExitProcessSafely start.");
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option(MessageOption::TF_ASYNC);
67     if (!WriteInterfaceToken(data)) {
68         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
69         return false;
70     }
71 
72     sptr<IRemoteObject> remote = Remote();
73     if (remote == nullptr) {
74         TAG_LOGE(AAFwkTag::APPMGR, "Remote() is NULL.");
75         return false;
76     }
77     int32_t ret = remote->SendRequest(
78         static_cast<uint32_t>(IChildScheduler::Message::SCHEDULE_EXIT_PROCESS_SAFELY), data, reply, option);
79     if (ret != NO_ERROR) {
80         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d.", ret);
81         return false;
82     }
83     TAG_LOGD(AAFwkTag::APPMGR, "ScheduleExitProcessSafely end.");
84     return true;
85 }
86 
ScheduleRunNativeProc(const sptr<IRemoteObject> & mainProcessCb)87 bool ChildSchedulerProxy::ScheduleRunNativeProc(const sptr<IRemoteObject> &mainProcessCb)
88 {
89     TAG_LOGD(AAFwkTag::APPMGR, "ScheduleRunNativeProc start.");
90     MessageParcel data;
91     MessageParcel reply;
92     MessageOption option(MessageOption::TF_ASYNC);
93     if (!WriteInterfaceToken(data)) {
94         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
95         return false;
96     }
97 
98     if (!data.WriteRemoteObject(mainProcessCb)) {
99         TAG_LOGE(AAFwkTag::APPMGR, "Write main process callback ipc object failed.");
100         return false;
101     }
102 
103     sptr<IRemoteObject> remote = Remote();
104     if (remote == nullptr) {
105         TAG_LOGE(AAFwkTag::APPMGR, "Remote() is null.");
106         return false;
107     }
108 
109     int32_t ret = remote->SendRequest(
110         static_cast<uint32_t>(IChildScheduler::Message::SCHEDULE_RUN_NATIVE_PROC), data, reply, option);
111     if (ret != NO_ERROR) {
112         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d.", ret);
113         return false;
114     }
115 
116     TAG_LOGD(AAFwkTag::APPMGR, "ScheduleRunNativeProc end.");
117     return true;
118 }
119 
120 }  // namespace AppExecFwk
121 }  // namespace OHOS
122