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_NOTIFY_START_KEEP_ALIVE_PROCESS):
60 return HandleNotifyStartKeepAliveProcess(data, reply);
61 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_START_PROCESS_FAILED):
62 return HandleOnStartProcessFailed(data, reply);
63 case static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_CACHE_EXIT_INFO):
64 return HandleOnCacheExitInfo(data, reply);
65 }
66
67 TAG_LOGD(AAFwkTag::APPMGR, "AppStateCallbackHost::OnRemoteRequest end");
68 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
69 }
70
OnAbilityRequestDone(const sptr<IRemoteObject> &,const AbilityState)71 void AppStateCallbackHost::OnAbilityRequestDone(const sptr<IRemoteObject> &, const AbilityState)
72 {
73 TAG_LOGD(AAFwkTag::APPMGR, "called");
74 }
75
OnAppStateChanged(const AppProcessData &)76 void AppStateCallbackHost::OnAppStateChanged(const AppProcessData &)
77 {
78 TAG_LOGD(AAFwkTag::APPMGR, "called");
79 }
80
NotifyAppPreCache(int32_t pid,int32_t userId)81 void AppStateCallbackHost::NotifyAppPreCache(int32_t pid, int32_t userId)
82 {
83 TAG_LOGD(AAFwkTag::APPMGR, "called");
84 }
85
NotifyConfigurationChange(const AppExecFwk::Configuration & config,int32_t userId)86 void AppStateCallbackHost::NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId)
87 {
88 }
89
NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)90 void AppStateCallbackHost::NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
91 {
92 TAG_LOGD(AAFwkTag::APPMGR, "called");
93 }
94
NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> & bundleInfos)95 void AppStateCallbackHost::NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
96 {
97 TAG_LOGD(AAFwkTag::APPMGR, "called");
98 }
99
OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> & abilityTokens)100 void AppStateCallbackHost::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
101 {
102 TAG_LOGD(AAFwkTag::APPMGR, "called");
103 }
104
OnStartProcessFailed(sptr<IRemoteObject> token)105 void AppStateCallbackHost::OnStartProcessFailed(sptr<IRemoteObject> token)
106 {
107 TAG_LOGD(AAFwkTag::APPMGR, "called");
108 }
109
OnCacheExitInfo(uint32_t accessTokenId,const AAFwk::LastExitDetailInfo & exitInfo,const std::string & bundleName,const std::vector<std::string> & abilityNames,const std::vector<std::string> & uiExtensionNames)110 void AppStateCallbackHost::OnCacheExitInfo(uint32_t accessTokenId, const AAFwk::LastExitDetailInfo &exitInfo,
111 const std::string &bundleName, const std::vector<std::string> &abilityNames,
112 const std::vector<std::string> &uiExtensionNames)
113 {
114 TAG_LOGD(AAFwkTag::APPMGR, "called");
115 }
116
HandleOnAppStateChanged(MessageParcel & data,MessageParcel & reply)117 int32_t AppStateCallbackHost::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply)
118 {
119 HITRACE_METER(HITRACE_TAG_APP);
120 std::unique_ptr<AppProcessData> processData(data.ReadParcelable<AppProcessData>());
121 if (!processData) {
122 TAG_LOGE(AAFwkTag::APPMGR, "ReadParcelable<AppProcessData> failed");
123 return ERR_APPEXECFWK_PARCEL_ERROR;
124 }
125
126 OnAppStateChanged(*processData);
127 return NO_ERROR;
128 }
129
HandleOnAbilityRequestDone(MessageParcel & data,MessageParcel & reply)130 int32_t AppStateCallbackHost::HandleOnAbilityRequestDone(MessageParcel &data, MessageParcel &reply)
131 {
132 HITRACE_METER(HITRACE_TAG_APP);
133 sptr<IRemoteObject> obj = nullptr;
134 if (data.ReadBool()) {
135 obj = data.ReadRemoteObject();
136 }
137 int32_t state = data.ReadInt32();
138 OnAbilityRequestDone(obj, static_cast<AbilityState>(state));
139 return NO_ERROR;
140 }
141
HandleNotifyConfigurationChange(MessageParcel & data,MessageParcel & reply)142 int32_t AppStateCallbackHost::HandleNotifyConfigurationChange(MessageParcel &data, MessageParcel &reply)
143 {
144 std::unique_ptr<AppExecFwk::Configuration> config(data.ReadParcelable<AppExecFwk::Configuration>());
145 if (config == nullptr) {
146 TAG_LOGE(AAFwkTag::APPMGR, "To read config failed.");
147 return ERR_DEAD_OBJECT;
148 }
149 auto userId = data.ReadInt32();
150 NotifyConfigurationChange(*config, userId);
151 return NO_ERROR;
152 }
153
HandleNotifyStartResidentProcess(MessageParcel & data,MessageParcel & reply)154 int32_t AppStateCallbackHost::HandleNotifyStartResidentProcess(MessageParcel &data, MessageParcel &reply)
155 {
156 std::vector<AppExecFwk::BundleInfo> bundleInfos;
157 int32_t infoSize = data.ReadInt32();
158 if (infoSize > CYCLE_LIMIT) {
159 TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
160 return ERR_INVALID_VALUE;
161 }
162 for (int32_t i = 0; i < infoSize; i++) {
163 std::unique_ptr<AppExecFwk::BundleInfo> bundleInfo(data.ReadParcelable<AppExecFwk::BundleInfo>());
164 if (!bundleInfo) {
165 TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable infos failed.");
166 return ERR_INVALID_VALUE;
167 }
168 bundleInfos.emplace_back(*bundleInfo);
169 }
170 NotifyStartResidentProcess(bundleInfos);
171 return NO_ERROR;
172 }
173
HandleNotifyStartKeepAliveProcess(MessageParcel & data,MessageParcel & reply)174 int32_t AppStateCallbackHost::HandleNotifyStartKeepAliveProcess(MessageParcel &data, MessageParcel &reply)
175 {
176 std::vector<AppExecFwk::BundleInfo> bundleInfos;
177 int32_t infoSize = data.ReadInt32();
178 if (infoSize > CYCLE_LIMIT) {
179 TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
180 return ERR_INVALID_VALUE;
181 }
182 for (int32_t i = 0; i < infoSize; i++) {
183 std::unique_ptr<AppExecFwk::BundleInfo> bundleInfo(data.ReadParcelable<AppExecFwk::BundleInfo>());
184 if (!bundleInfo) {
185 TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable infos failed.");
186 return ERR_INVALID_VALUE;
187 }
188 bundleInfos.emplace_back(*bundleInfo);
189 }
190 NotifyStartKeepAliveProcess(bundleInfos);
191 return NO_ERROR;
192 }
193
HandleOnAppRemoteDied(MessageParcel & data,MessageParcel & reply)194 int32_t AppStateCallbackHost::HandleOnAppRemoteDied(MessageParcel &data, MessageParcel &reply)
195 {
196 std::vector<sptr<IRemoteObject>> abilityTokens;
197 int32_t infoSize = data.ReadInt32();
198 if (infoSize > CYCLE_LIMIT) {
199 TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
200 return ERR_INVALID_VALUE;
201 }
202 for (int32_t i = 0; i < infoSize; i++) {
203 sptr<IRemoteObject> obj = data.ReadRemoteObject();
204 if (!obj) {
205 TAG_LOGE(AAFwkTag::APPMGR, "Read token failed.");
206 return ERR_INVALID_VALUE;
207 }
208 abilityTokens.emplace_back(obj);
209 }
210 OnAppRemoteDied(abilityTokens);
211 return NO_ERROR;
212 }
213
HandleOnStartProcessFailed(MessageParcel & data,MessageParcel & reply)214 int32_t AppStateCallbackHost::HandleOnStartProcessFailed(MessageParcel &data, MessageParcel &reply)
215 {
216 sptr<IRemoteObject> token = data.ReadRemoteObject();
217 if (!token) {
218 TAG_LOGE(AAFwkTag::APPMGR, "null token");
219 return ERR_INVALID_VALUE;
220 }
221 OnStartProcessFailed(token);
222 return NO_ERROR;
223 }
224
HandleNotifyAppPreCache(MessageParcel & data,MessageParcel & reply)225 int32_t AppStateCallbackHost::HandleNotifyAppPreCache(MessageParcel &data, MessageParcel &reply)
226 {
227 int32_t pid = data.ReadInt32();
228 if (pid <= 0) {
229 TAG_LOGE(AAFwkTag::APPMGR, "pid is illegal");
230 return ERR_INVALID_VALUE;
231 }
232 int32_t userId = data.ReadInt32();
233 if (userId < 0) {
234 TAG_LOGE(AAFwkTag::APPMGR, "userId is illegal");
235 return ERR_INVALID_VALUE;
236 }
237 NotifyAppPreCache(pid, userId);
238 return NO_ERROR;
239 }
240
HandleOnCacheExitInfo(MessageParcel & data,MessageParcel & reply)241 int32_t AppStateCallbackHost::HandleOnCacheExitInfo(MessageParcel &data, MessageParcel &reply)
242 {
243 uint32_t accessTokenId = data.ReadUint32();
244 std::unique_ptr<AAFwk::LastExitDetailInfo> exitInfo(data.ReadParcelable<AAFwk::LastExitDetailInfo>());
245 if (exitInfo == nullptr) {
246 TAG_LOGE(AAFwkTag::APPMGR, "To read exitInfo failed");
247 return ERR_DEAD_OBJECT;
248 }
249 std::string bundleName = data.ReadString();
250 std::vector<std::string> abilityNames;
251 data.ReadStringVector(&abilityNames);
252 std::vector<std::string> uiExtensionNames;
253 data.ReadStringVector(&uiExtensionNames);
254 OnCacheExitInfo(accessTokenId, *exitInfo, bundleName, abilityNames, uiExtensionNames);
255
256 return NO_ERROR;
257 }
258 } // namespace AppExecFwk
259 } // namespace OHOS
260