1 /*
2 * Copyright (c) 2021-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 "app_state_callback_host.h"
17
18 #include "appexecfwk_errors.h"
19 #include "configuration.h"
20 #include "hitrace_meter.h"
21 #include "hilog_tag_wrapper.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24
25 #include "app_state_callback_proxy.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 constexpr int32_t CYCLE_LIMIT = 1000;
AppStateCallbackHost()30 AppStateCallbackHost::AppStateCallbackHost() {}
31
~AppStateCallbackHost()32 AppStateCallbackHost::~AppStateCallbackHost() {}
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int AppStateCallbackHost::OnRemoteRequest(
35 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37 TAG_LOGD(AAFwkTag::APPMGR, "AppStateCallbackHost::OnReceived, code = %{public}u, flags= %{public}d.", code,
38 option.GetFlags());
39 std::u16string descriptor = AppStateCallbackHost::GetDescriptor();
40 std::u16string remoteDescriptor = data.ReadInterfaceToken();
41 if (descriptor != remoteDescriptor) {
42 TAG_LOGE(AAFwkTag::APPMGR, "local descriptor is not equal to remote");
43 return ERR_INVALID_STATE;
44 }
45
46 switch (code) {
47 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_STATE_CHANGED):
48 return HandleOnAppStateChanged(data, reply);
49 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_ABILITY_REQUEST_DONE):
50 return HandleOnAbilityRequestDone(data, reply);
51 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_CONFIG_CHANGE):
52 return HandleNotifyConfigurationChange(data, reply);
53 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS):
54 return HandleNotifyStartResidentProcess(data, reply);
55 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED):
56 return HandleOnAppRemoteDied(data, reply);
57 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_PRE_CACHE):
58 return HandleNotifyAppPreCache(data, reply);
59 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_START_PROCESS_FAILED):
60 return HandleOnStartProcessFailed(data, reply);
61 }
62
63 TAG_LOGD(AAFwkTag::APPMGR, "AppStateCallbackHost::OnRemoteRequest end");
64 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66
OnAbilityRequestDone(const sptr<IRemoteObject> &,const AbilityState)67 void AppStateCallbackHost::OnAbilityRequestDone(const sptr<IRemoteObject> &, const AbilityState)
68 {
69 TAG_LOGD(AAFwkTag::APPMGR, "called");
70 }
71
OnAppStateChanged(const AppProcessData &)72 void AppStateCallbackHost::OnAppStateChanged(const AppProcessData &)
73 {
74 TAG_LOGD(AAFwkTag::APPMGR, "called");
75 }
76
NotifyAppPreCache(int32_t pid,int32_t userId)77 void AppStateCallbackHost::NotifyAppPreCache(int32_t pid, int32_t userId)
78 {
79 TAG_LOGD(AAFwkTag::APPMGR, "called");
80 }
81
NotifyConfigurationChange(const AppExecFwk::Configuration & config,int32_t userId)82 void AppStateCallbackHost::NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId)
83 {
84 }
85
NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)86 void AppStateCallbackHost::NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
87 {
88 TAG_LOGD(AAFwkTag::APPMGR, "called");
89 }
90
OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> & abilityTokens)91 void AppStateCallbackHost::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
92 {
93 TAG_LOGD(AAFwkTag::APPMGR, "called");
94 }
95
OnStartProcessFailed(sptr<IRemoteObject> token)96 void AppStateCallbackHost::OnStartProcessFailed(sptr<IRemoteObject> token)
97 {
98 TAG_LOGD(AAFwkTag::APPMGR, "called");
99 }
100
101
HandleOnAppStateChanged(MessageParcel & data,MessageParcel & reply)102 int32_t AppStateCallbackHost::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply)
103 {
104 HITRACE_METER(HITRACE_TAG_APP);
105 std::unique_ptr<AppProcessData> processData(data.ReadParcelable<AppProcessData>());
106 if (!processData) {
107 TAG_LOGE(AAFwkTag::APPMGR, "ReadParcelable<AppProcessData> failed");
108 return ERR_APPEXECFWK_PARCEL_ERROR;
109 }
110
111 OnAppStateChanged(*processData);
112 return NO_ERROR;
113 }
114
HandleOnAbilityRequestDone(MessageParcel & data,MessageParcel & reply)115 int32_t AppStateCallbackHost::HandleOnAbilityRequestDone(MessageParcel &data, MessageParcel &reply)
116 {
117 HITRACE_METER(HITRACE_TAG_APP);
118 sptr<IRemoteObject> obj = nullptr;
119 if (data.ReadBool()) {
120 obj = data.ReadRemoteObject();
121 }
122 int32_t state = data.ReadInt32();
123 OnAbilityRequestDone(obj, static_cast<AbilityState>(state));
124 return NO_ERROR;
125 }
126
HandleNotifyConfigurationChange(MessageParcel & data,MessageParcel & reply)127 int32_t AppStateCallbackHost::HandleNotifyConfigurationChange(MessageParcel &data, MessageParcel &reply)
128 {
129 std::unique_ptr<AppExecFwk::Configuration> config(data.ReadParcelable<AppExecFwk::Configuration>());
130 if (config == nullptr) {
131 TAG_LOGE(AAFwkTag::APPMGR, "To read config failed.");
132 return ERR_DEAD_OBJECT;
133 }
134 auto userId = data.ReadInt32();
135 NotifyConfigurationChange(*config, userId);
136 return NO_ERROR;
137 }
138
HandleNotifyStartResidentProcess(MessageParcel & data,MessageParcel & reply)139 int32_t AppStateCallbackHost::HandleNotifyStartResidentProcess(MessageParcel &data, MessageParcel &reply)
140 {
141 std::vector<AppExecFwk::BundleInfo> bundleInfos;
142 int32_t infoSize = data.ReadInt32();
143 if (infoSize > CYCLE_LIMIT) {
144 TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
145 return ERR_INVALID_VALUE;
146 }
147 for (int32_t i = 0; i < infoSize; i++) {
148 std::unique_ptr<AppExecFwk::BundleInfo> bundleInfo(data.ReadParcelable<AppExecFwk::BundleInfo>());
149 if (!bundleInfo) {
150 TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable infos failed.");
151 return ERR_INVALID_VALUE;
152 }
153 bundleInfos.emplace_back(*bundleInfo);
154 }
155 NotifyStartResidentProcess(bundleInfos);
156 return NO_ERROR;
157 }
158
HandleOnAppRemoteDied(MessageParcel & data,MessageParcel & reply)159 int32_t AppStateCallbackHost::HandleOnAppRemoteDied(MessageParcel &data, MessageParcel &reply)
160 {
161 std::vector<sptr<IRemoteObject>> abilityTokens;
162 int32_t infoSize = data.ReadInt32();
163 if (infoSize > CYCLE_LIMIT) {
164 TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
165 return ERR_INVALID_VALUE;
166 }
167 for (int32_t i = 0; i < infoSize; i++) {
168 sptr<IRemoteObject> obj = data.ReadRemoteObject();
169 if (!obj) {
170 TAG_LOGE(AAFwkTag::APPMGR, "Read token failed.");
171 return ERR_INVALID_VALUE;
172 }
173 abilityTokens.emplace_back(obj);
174 }
175 OnAppRemoteDied(abilityTokens);
176 return NO_ERROR;
177 }
178
HandleOnStartProcessFailed(MessageParcel & data,MessageParcel & reply)179 int32_t AppStateCallbackHost::HandleOnStartProcessFailed(MessageParcel &data, MessageParcel &reply)
180 {
181 sptr<IRemoteObject> token = data.ReadRemoteObject();
182 if (!token) {
183 TAG_LOGE(AAFwkTag::APPMGR, "null token");
184 return ERR_INVALID_VALUE;
185 }
186 OnStartProcessFailed(token);
187 return NO_ERROR;
188 }
189
HandleNotifyAppPreCache(MessageParcel & data,MessageParcel & reply)190 int32_t AppStateCallbackHost::HandleNotifyAppPreCache(MessageParcel &data, MessageParcel &reply)
191 {
192 int32_t pid = data.ReadInt32();
193 if (pid <= 0) {
194 TAG_LOGE(AAFwkTag::APPMGR, "pid is illegal");
195 return ERR_INVALID_VALUE;
196 }
197 int32_t userId = data.ReadInt32();
198 if (userId < 0) {
199 TAG_LOGE(AAFwkTag::APPMGR, "userId is illegal");
200 return ERR_INVALID_VALUE;
201 }
202 NotifyAppPreCache(pid, userId);
203 return NO_ERROR;
204 }
205 } // namespace AppExecFwk
206 } // namespace OHOS
207