• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "app_scheduler_proxy.h"
17 
18 #include "hilog_wrapper.h"
19 #include "hitrace_meter.h"
20 #include "ipc_types.h"
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
AppSchedulerProxy(const sptr<IRemoteObject> & impl)25 AppSchedulerProxy::AppSchedulerProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppScheduler>(impl)
26 {}
27 
WriteInterfaceToken(MessageParcel & data)28 bool AppSchedulerProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30     if (!data.WriteInterfaceToken(AppSchedulerProxy::GetDescriptor())) {
31         HILOG_ERROR("write interface token failed");
32         return false;
33     }
34     return true;
35 }
36 
ScheduleForegroundApplication()37 void AppSchedulerProxy::ScheduleForegroundApplication()
38 {
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option(MessageOption::TF_ASYNC);
42     if (!WriteInterfaceToken(data)) {
43         return;
44     }
45     sptr<IRemoteObject> remote = Remote();
46     if (remote == nullptr) {
47         HILOG_ERROR("Remote() is NULL");
48         return;
49     }
50     int32_t ret =
51         remote->SendRequest(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION),
52             data,
53             reply,
54             option);
55     if (ret != NO_ERROR) {
56         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
57     }
58 }
59 
ScheduleBackgroundApplication()60 void AppSchedulerProxy::ScheduleBackgroundApplication()
61 {
62     MessageParcel data;
63     MessageParcel reply;
64     MessageOption option(MessageOption::TF_ASYNC);
65     if (!WriteInterfaceToken(data)) {
66         return;
67     }
68     sptr<IRemoteObject> remote = Remote();
69     if (remote == nullptr) {
70         HILOG_ERROR("Remote() is NULL");
71         return;
72     }
73     int32_t ret =
74         remote->SendRequest(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_BACKGROUND_APPLICATION_TRANSACTION),
75             data,
76             reply,
77             option);
78     if (ret != NO_ERROR) {
79         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
80     }
81 }
82 
ScheduleTerminateApplication()83 void AppSchedulerProxy::ScheduleTerminateApplication()
84 {
85     MessageParcel data;
86     MessageParcel reply;
87     MessageOption option(MessageOption::TF_ASYNC);
88     if (!WriteInterfaceToken(data)) {
89         return;
90     }
91     sptr<IRemoteObject> remote = Remote();
92     if (remote == nullptr) {
93         HILOG_ERROR("Remote() is NULL");
94         return;
95     }
96     int32_t ret = remote->SendRequest(
97         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_TERMINATE_APPLICATION_TRANSACTION), data, reply, option);
98     if (ret != NO_ERROR) {
99         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
100     }
101 }
102 
ScheduleLowMemory()103 void AppSchedulerProxy::ScheduleLowMemory()
104 {
105     MessageParcel data;
106     MessageParcel reply;
107     MessageOption option(MessageOption::TF_ASYNC);
108     if (!WriteInterfaceToken(data)) {
109         return;
110     }
111     sptr<IRemoteObject> remote = Remote();
112     if (remote == nullptr) {
113         HILOG_ERROR("Remote() is NULL");
114         return;
115     }
116     int32_t ret = remote->SendRequest(
117         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_LOWMEMORY_APPLICATION_TRANSACTION), data, reply, option);
118     if (ret != NO_ERROR) {
119         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
120     }
121 }
122 
ScheduleMemoryLevel(int32_t level)123 void AppSchedulerProxy::ScheduleMemoryLevel(int32_t level)
124 {
125     uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_MEMORYLEVEL_APPLICATION_TRANSACTION);
126     ScheduleMemoryCommon(level, operation);
127 }
128 
ScheduleShrinkMemory(const int32_t level)129 void AppSchedulerProxy::ScheduleShrinkMemory(const int32_t level)
130 {
131     uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_SHRINK_MEMORY_APPLICATION_TRANSACTION);
132     ScheduleMemoryCommon(level, operation);
133 }
134 
ScheduleMemoryCommon(const int32_t level,const uint32_t operation)135 void AppSchedulerProxy::ScheduleMemoryCommon(const int32_t level, const uint32_t operation)
136 {
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option(MessageOption::TF_ASYNC);
140     if (!WriteInterfaceToken(data)) {
141         return;
142     }
143     data.WriteInt32(level);
144     sptr<IRemoteObject> remote = Remote();
145     if (remote == nullptr) {
146         HILOG_ERROR("Remote() is NULL");
147         return;
148     }
149     int32_t ret = remote->SendRequest(operation, data, reply, option);
150     if (ret != NO_ERROR) {
151         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
152     }
153 }
154 
ScheduleLaunchAbility(const AbilityInfo & info,const sptr<IRemoteObject> & token,const std::shared_ptr<AAFwk::Want> & want)155 void AppSchedulerProxy::ScheduleLaunchAbility(const AbilityInfo &info, const sptr<IRemoteObject> &token,
156     const std::shared_ptr<AAFwk::Want> &want)
157 {
158     MessageParcel data;
159     MessageParcel reply;
160     MessageOption option(MessageOption::TF_ASYNC);
161     if (!WriteInterfaceToken(data)) {
162         return;
163     }
164     data.WriteParcelable(&info);
165 
166     if (token) {
167         if (!data.WriteBool(true) || !data.WriteRemoteObject(token.GetRefPtr())) {
168             HILOG_ERROR("Failed to write flag and token");
169             return;
170         }
171     } else {
172         if (!data.WriteBool(false)) {
173             HILOG_ERROR("Failed to write flag");
174             return;
175         }
176     }
177 
178     if (!data.WriteParcelable(want.get())) {
179         HILOG_ERROR("write want fail.");
180         return;
181     }
182     sptr<IRemoteObject> remote = Remote();
183     if (remote == nullptr) {
184         HILOG_ERROR("Remote() is NULL");
185         return;
186     }
187     int32_t ret = remote->SendRequest(
188         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_LAUNCH_ABILITY_TRANSACTION), data, reply, option);
189     if (ret != NO_ERROR) {
190         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
191     }
192 }
193 
ScheduleCleanAbility(const sptr<IRemoteObject> & token)194 void AppSchedulerProxy::ScheduleCleanAbility(const sptr<IRemoteObject> &token)
195 {
196     MessageParcel data;
197     MessageParcel reply;
198     MessageOption option(MessageOption::TF_ASYNC);
199     if (!WriteInterfaceToken(data)) {
200         return;
201     }
202     if (!data.WriteRemoteObject(token.GetRefPtr())) {
203         HILOG_ERROR("Failed to write token");
204         return;
205     }
206     sptr<IRemoteObject> remote = Remote();
207     if (remote == nullptr) {
208         HILOG_ERROR("Remote() is NULL");
209         return;
210     }
211     int32_t ret = remote->SendRequest(
212         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_CLEAN_ABILITY_TRANSACTION), data, reply, option);
213     if (ret != NO_ERROR) {
214         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
215     }
216 }
217 
ScheduleLaunchApplication(const AppLaunchData & launchData,const Configuration & config)218 void AppSchedulerProxy::ScheduleLaunchApplication(const AppLaunchData &launchData, const Configuration &config)
219 {
220     HILOG_INFO("AppSchedulerProxy ScheduleLaunchApplication start");
221     MessageParcel data;
222     MessageParcel reply;
223     MessageOption option(MessageOption::TF_ASYNC);
224     if (!WriteInterfaceToken(data)) {
225         return;
226     }
227 
228     if (!data.WriteParcelable(&launchData)) {
229         HILOG_ERROR("WriteParcelable launchData failed");
230         return ;
231     }
232 
233     if (!data.WriteParcelable(&config)) {
234         HILOG_ERROR("WriteParcelable config failed");
235         return ;
236     }
237 
238     sptr<IRemoteObject> remote = Remote();
239     if (remote == nullptr) {
240         HILOG_ERROR("Remote() is NULL");
241         return;
242     }
243     int32_t ret = remote->SendRequest(
244         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_LAUNCH_APPLICATION_TRANSACTION), data, reply, option);
245     if (ret != NO_ERROR) {
246         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
247     }
248 
249     HILOG_INFO("AppSchedulerProxy ScheduleLaunchApplication end");
250 }
251 
ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo & appInfo)252 void AppSchedulerProxy::ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo)
253 {
254     HILOG_INFO("AppSchedulerProxy ScheduleUpdateApplicationInfoInstalled start");
255     MessageParcel data;
256     if (!WriteInterfaceToken(data)) {
257         return;
258     }
259     if (!data.WriteParcelable(&appInfo)) {
260         return ;
261     }
262     sptr<IRemoteObject> remote = Remote();
263     if (remote == nullptr) {
264         HILOG_ERROR("Remote() is NULL");
265         return;
266     }
267     MessageParcel reply;
268     MessageOption option(MessageOption::TF_ASYNC);
269     int32_t ret = remote->SendRequest(
270         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_UPDATE_APPLICATION_INFO_INSTALLED), data, reply, option);
271     if (ret != NO_ERROR) {
272         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
273     }
274     HILOG_INFO("AppSchedulerProxy ScheduleUpdateApplicationInfoInstalled end");
275 }
276 
ScheduleAbilityStage(const HapModuleInfo & abilityStage)277 void AppSchedulerProxy::ScheduleAbilityStage(const HapModuleInfo &abilityStage)
278 {
279     HILOG_INFO("AppSchedulerProxy ScheduleAbilityStage start");
280     MessageParcel data;
281     constexpr int32_t max = 10000;
282     constexpr int32_t large = 60;
283     constexpr int32_t mid = 20;
284     auto abilityInfoSize = static_cast<int32_t>(abilityStage.abilityInfos.size());
285     auto extensionInfoSize = static_cast<int32_t>(abilityStage.extensionInfos.size());
286     if (abilityInfoSize > max || extensionInfoSize > max) {
287         HILOG_ERROR("size exceeds max");
288         return;
289     }
290     auto componentSize = abilityInfoSize + extensionInfoSize;
291     if (componentSize > large) {
292         constexpr int32_t size = 2 * 1024 * 1024; // 1.6 M
293         data.SetDataCapacity(size);
294     } else if (componentSize > mid) {
295         constexpr int32_t size = 800 * 1024; // 800 kb
296         data.SetDataCapacity(size);
297     }
298     if (!WriteInterfaceToken(data)) {
299         return;
300     }
301 
302     if (!data.WriteParcelable(&abilityStage)) {
303         return ;
304     }
305 
306     sptr<IRemoteObject> remote = Remote();
307     if (remote == nullptr) {
308         HILOG_ERROR("Remote() is NULL");
309         return;
310     }
311 
312     MessageParcel reply;
313     MessageOption option(MessageOption::TF_ASYNC);
314     int32_t ret = remote->SendRequest(
315         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_ABILITY_STAGE_INFO), data, reply, option);
316     if (ret != NO_ERROR) {
317         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
318     }
319     HILOG_INFO("AppSchedulerProxy ScheduleAbilityStage end");
320 }
321 
ScheduleProfileChanged(const Profile & profile)322 void AppSchedulerProxy::ScheduleProfileChanged(const Profile &profile)
323 {
324     MessageParcel data;
325     MessageParcel reply;
326     MessageOption option(MessageOption::TF_ASYNC);
327     if (!WriteInterfaceToken(data)) {
328         return;
329     }
330     data.WriteParcelable(&profile);
331     sptr<IRemoteObject> remote = Remote();
332     if (remote == nullptr) {
333         HILOG_ERROR("Remote() is NULL");
334         return;
335     }
336     int32_t ret = remote->SendRequest(
337         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_PROFILE_CHANGED_TRANSACTION), data, reply, option);
338     if (ret != NO_ERROR) {
339         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
340     }
341 }
342 
ScheduleConfigurationUpdated(const Configuration & config)343 void AppSchedulerProxy::ScheduleConfigurationUpdated(const Configuration &config)
344 {
345     MessageParcel data;
346     MessageParcel reply;
347     MessageOption option(MessageOption::TF_ASYNC);
348     if (!WriteInterfaceToken(data)) {
349         return;
350     }
351     data.WriteParcelable(&config);
352     sptr<IRemoteObject> remote = Remote();
353     if (remote == nullptr) {
354         HILOG_ERROR("Remote() is NULL");
355         return;
356     }
357     int32_t ret = remote->SendRequest(
358         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_CONFIGURATION_UPDATED), data, reply, option);
359     if (ret != NO_ERROR) {
360         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
361     }
362 }
363 
ScheduleProcessSecurityExit()364 void AppSchedulerProxy::ScheduleProcessSecurityExit()
365 {
366     MessageParcel data;
367     MessageParcel reply;
368     MessageOption option(MessageOption::TF_ASYNC);
369     if (!WriteInterfaceToken(data)) {
370         return;
371     }
372     sptr<IRemoteObject> remote = Remote();
373     if (remote == nullptr) {
374         HILOG_ERROR("Remote() is NULL");
375         return;
376     }
377     int32_t ret = remote->SendRequest(
378         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_PROCESS_SECURITY_EXIT_TRANSACTION), data, reply, option);
379     if (ret != NO_ERROR) {
380         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
381     }
382 }
383 
ScheduleAcceptWant(const AAFwk::Want & want,const std::string & moduleName)384 void AppSchedulerProxy::ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName)
385 {
386     MessageParcel data;
387     MessageParcel reply;
388     MessageOption option(MessageOption::TF_ASYNC);
389     if (!WriteInterfaceToken(data)) {
390         return;
391     }
392 
393     if (!data.WriteParcelable(&want) || !data.WriteString(moduleName)) {
394         return;
395     }
396     sptr<IRemoteObject> remote = Remote();
397     if (remote == nullptr) {
398         HILOG_ERROR("Remote() is NULL");
399         return;
400     }
401     int32_t ret = remote->SendRequest(
402         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_ACCEPT_WANT), data, reply, option);
403     if (ret != NO_ERROR) {
404         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
405     }
406 }
407 
ScheduleNotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback,const int32_t recordId)408 int32_t AppSchedulerProxy::ScheduleNotifyLoadRepairPatch(const std::string &bundleName,
409     const sptr<IQuickFixCallback> &callback, const int32_t recordId)
410 {
411     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
412     MessageParcel data;
413     if (!WriteInterfaceToken(data)) {
414         HILOG_ERROR("ScheduleNotifyLoadRepairPatch, Write interface token failed.");
415         return ERR_INVALID_DATA;
416     }
417 
418     if (!data.WriteString(bundleName)) {
419         HILOG_ERROR("ScheduleNotifyLoadRepairPatch, Write bundle name failed.");
420         return ERR_INVALID_DATA;
421     }
422 
423     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
424         HILOG_ERROR("Write callback failed.");
425         return ERR_INVALID_DATA;
426     }
427 
428     if (!data.WriteInt32(recordId)) {
429         HILOG_ERROR("Write record id failed.");
430         return ERR_INVALID_DATA;
431     }
432 
433     sptr<IRemoteObject> remote = Remote();
434     if (remote == nullptr) {
435         HILOG_ERROR("ScheduleNotifyLoadRepairPatch, Remote is nullptr");
436         return ERR_NULL_OBJECT;
437     }
438 
439     MessageParcel reply;
440     MessageOption option;
441     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_LOAD_REPAIR_PATCH),
442         data, reply, option);
443     if (ret != 0) {
444         HILOG_ERROR("ScheduleNotifyLoadRepairPatch, Send request failed with errno %{public}d.", ret);
445         return ret;
446     }
447 
448     return reply.ReadInt32();
449 }
450 
ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> & callback,const int32_t recordId)451 int32_t AppSchedulerProxy::ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId)
452 {
453     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
454     MessageParcel data;
455     if (!WriteInterfaceToken(data)) {
456         HILOG_ERROR("Write interface token failed.");
457         return ERR_INVALID_DATA;
458     }
459 
460     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
461         HILOG_ERROR("Write callback failed.");
462         return ERR_INVALID_DATA;
463     }
464 
465     if (!data.WriteInt32(recordId)) {
466         HILOG_ERROR("Write record id failed.");
467         return ERR_INVALID_DATA;
468     }
469 
470     sptr<IRemoteObject> remote = Remote();
471     if (remote == nullptr) {
472         HILOG_ERROR("Remote is nullptr");
473         return ERR_NULL_OBJECT;
474     }
475 
476     MessageParcel reply;
477     MessageOption option;
478     auto ret = remote->SendRequest(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_HOT_RELOAD_PAGE),
479         data, reply, option);
480     if (ret != 0) {
481         HILOG_ERROR("Send request failed with errno %{public}d.", ret);
482         return ret;
483     }
484 
485     return reply.ReadInt32();
486 }
487 
ScheduleNotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback,const int32_t recordId)488 int32_t AppSchedulerProxy::ScheduleNotifyUnLoadRepairPatch(const std::string &bundleName,
489     const sptr<IQuickFixCallback> &callback, const int32_t recordId)
490 {
491     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
492     MessageParcel data;
493     if (!WriteInterfaceToken(data)) {
494         HILOG_ERROR("Schedule notify unload patch, Write interface token failed.");
495         return ERR_INVALID_DATA;
496     }
497 
498     if (!data.WriteString(bundleName)) {
499         HILOG_ERROR("Schedule notify unload patch, Write bundle name failed.");
500         return ERR_INVALID_DATA;
501     }
502 
503     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
504         HILOG_ERROR("Write callback failed.");
505         return ERR_INVALID_DATA;
506     }
507 
508     if (!data.WriteInt32(recordId)) {
509         HILOG_ERROR("Write record id failed.");
510         return ERR_INVALID_DATA;
511     }
512 
513     sptr<IRemoteObject> remote = Remote();
514     if (remote == nullptr) {
515         HILOG_ERROR("Schedule notify unload patch, Remote is nullptr");
516         return ERR_NULL_OBJECT;
517     }
518 
519     MessageParcel reply;
520     MessageOption option;
521     int32_t ret = remote->SendRequest(
522         static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_UNLOAD_REPAIR_PATCH), data, reply, option);
523     if (ret != 0) {
524         HILOG_ERROR("Schedule notify unload patch, Send request failed with errno %{public}d.", ret);
525         return ret;
526     }
527 
528     return reply.ReadInt32();
529 }
530 }  // namespace AppExecFwk
531 }  // namespace OHOS
532